PHP Standards¶
- All PHP code MUST adhere to the PSR-2 Coding Standard (which MUST follow the PSR-1 Coding Standard).
- Short array syntax (
[]instead ofarray()) MUST be used. - Associative arrays SHOULD be formatted without spaces as follows:
in comparison to with spaces as below
$translations = [ 'and' => 'und', 'Biotin' => 'Biotine', 'Calcium' => 'Calcium', 'Carbohydrate' => 'Koolhydraten' ];$translations = [ 'and' => 'und', 'Biotin' => 'Biotine', 'Calcium' => 'Calcium', 'Carbohydrate' => 'Koolhydraten' ]; - All PHP code MUST adhere to the PSR-4 Autoloading Standard.
- Class names MUST be declared in UpperCamelCase.
- Acronyms MUST be treated as normal words.
HttpException, notHTTPException. - Variable names MUST be declared in camelCase.
- Acronyms MUST be treated as normal words.
xmlHttpRequestnotXMLHTTPRequest. - Scripts MUST be named in camelCase describing their primary function.
- Non-static functions MUST NOT be called statically.
- Static functions MUST NOT be called non-statically.
- Comments SHOULD be used to provide explanation for “why” rather than “how” and SHOULD be used when there isn’t a way to make the code simpler or self-documenting.
- Nested ternary operators MUST NOT be used.
- PHP file encoding must be UTF-8.
-
ifstatements which span multiple lines should be split as follows:if ($longVariableNameNumber1 && $longVariableNameNumber2 && $longVariableNameNumber3 && $longVariableNameNumber4 || $longVariableNameNumber5) { // body of control structure } -
There MUST NOT be any assignment or modification of variables in the expressions of
ifstatements.if ($response = $request->getResponse()) { // not allowed } -
Abstract class names MUST begin with
Abstract.abstract class AbstractGenerator { } -
Interface names MUST end with
Interface.interface GeneratorInterface { } -
Class usages SHOULD be in alphabetical order.
use AbstractClass; use ExampleInterface; use Model; class SomeClassName {} -
'Not' logical operators MUST NOT have whitespace between them and the subject being negated.
$true = !false;
PHP DocBlock¶
- DocBlock for functions / methods MUST exist where the function / method has arguments or a return value and MUST use
the appropriate tags (
@param,@return) to denote that. Developers making modifications to a function / method are tasked with ensuring that the DocBlock is up-to-date. - Functions / methods / classes that are sufficiently complex (i.e. not self-documenting) SHOULD have a DocBlock explaining how to use the function / method / class.
- DocBlock presenting the type MUST be present for class member variables.
-
When a description is necessary for class member variables, the DocBlock MUST be multiline:
class Foo { /** @var int */ protected $id; /** * The Bar used to fight the foo * * @var Bar */ private $bar; } -
DocBlock types for scalar values MUST be one of:
bool(not boolean),int(not integer),string,float(not double). - DocBlock types for parameters/return values that are arrays of a single type MUST be written as:
Type[]. If the variable is a multi-dimensional array, it MUST be represented with one set of[]per depth (int[][]for an array of array of ints) - If the parameter/return value is an array of several types, it MUST be described in the DocBlock as
mixed[]. - If the parameter/return value is an array that is sometimes empty, it MUST still be written as in 6)
(NOT
Type[]|[])
example of a method's DocBlock¶
/**
* Foos the bars.
*
* @param int $barId Some number
* @param Bar[] $allTheBars Collection of Bar objects
*
* @return string[] List of messages
*/
public function barFooer($barId, array $allTheBars) {}
Open Source¶
- Example skeleton project
- League standards
- The project MUST at least depend on the lowest supported PHP version and SHOULD depend on the latest fully supported PHP version (see versions).
- The vendor name MUST be
graze. The vendor namespace MUST beGraze. - The project MUST contain a composer.json file.
- It MUST contain:
- The name of the project, this MUST match the name of the repository.
- The name of the maintainer in the
authorssection, along with the genericGraze Developers <developers@graze.com>author.
- It MUST NOT contain:
- The version of the project.
- All library code MUST be in a directory named
src/. - All tests MUST be a in a directory named
tests/. - The project SHOULD use Scrutinizer to check and fix standards and flag code quality issues.
- The project MUST be listed on Packagist.