I know that common coding style is helpful for cooperation. But unfortunately, I’m working alone so I didn’t understand it’s importance.
When I decide refactoring my legacy source code, I found the QUITE DIRTY CODE. So I make up my mind studying about PHP coding style.
PSR-1: Basic Coding Standard
PSR-1: Basic Coding Standard — PHP-FIG
This section of the standard comprises what should be considered the standard coding elements that are required to…
PHP Basic coding standard are described in RFC 2119.
- PHP code MUST use only
- PHP code MUST use only UTF-8 without BOM.
- A file SHOULD either declare new symbols (classes, functions, constants, etc.) or cause side effects, but SHOULD NOT do both.
- Namespaces and classes MUST follow PSR-0: Autoloading Standard & PSR-4: Autoloader.
- Classes MUST be declared in
- Constants of classes MUST be declared in
All Caps Snake Case(ALL_CAPS_SNAKE_CASE).
- Methods MUST be declared in
PSR-12: Extended Coding Style
PSR-12: Extended Coding Style — PHP-FIG
The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and…
It’s the expansion of PSR-2, so it replaces PSR-2. Extended coding style are also described in RFC 2119.
So many rules are defined in PSR-12, I just write most common (or things that I will use).
- All PHP files MUST use only
Unix LFline ending and MUST terminate with
- The closing tag(
?>) MUST be omitted if files contain only PHP.
- Lines SHOULD NOT be longer than
- There MUST NOT be
trailing white spaceat the end of lines.
- For improve readability,
blank linesMAY be added except where explicitly forbidden.
- There MUST NOT be
more then one statement per line.
- Indent MUST be
- Indent MUST NOT be tabs for indenting.
Keywords and Types
- All reserved keywords and types MUST be in lower case.
- Short form of type keywords MUST be used.
- Declare statements MUST contains no spaces.
※ Declare like this →
- The comment or statement MUST NOT be allowed after the closing brace.
- When instantiating new class, parentheses MUST always be present.
implementsMUST be located on the same line as the class name.
Regards of Classes
- When using traits, the
usekeyword MUST be located on the next line after the opening brace and MUST be declared one per line(Multiple declaration on one line MUST NOT be allowed).
- When using traits, one blank line MUST be displayed after the
- Visibility MUST be declared on all properties and constants, methods if PHP supports.
- The underscore MUST NOT be used to indicate
- In the argument list, there MUST NOT be spaces before comma, MUST be one space after comma.
- When declare return type, there MUST be one space after the colon. There MUST NOT be space between the question mark and type if return type is nullable.
finalkeyword MUST be located before the visibility declaration.
staticdeclaration MUST be located after the visibility.
Parentheses of the Control Structures
- Before & After of parentheses, there MUST be one space.
- There MUST NOT be any space within ther parentheses.
PHP is very huge programing language, and so many people are attending various projects to make libraries, frameworks, and so on. At that time we need to use coding standards for more efficiency.
Surely, there are too many rules, and it’s hard to remember. So we can fix our code using PHP CS(code sniffer).