-
Notifications
You must be signed in to change notification settings - Fork 37
Formatting
zenmiu edited this page Aug 4, 2011
·
3 revisions
Required:
- For files containing PHP code only, the closing tag ("?>") is not entered [REQ.PHP.2.1.1].
- The opening tag must be complete [REQ.PHP.2.1.2].
Required:
Line length cannot exceed 120 characters, except: [REQ.PHP.2.2.1].
- Declarations of class properties.
- Individual test lines.
- Test blocks (so-called heredoc and nowdoc).
- In class declarations if the implements block is placed on a new line and still exceeds the length restriction.
Examples of exclusions:
class A {
protected $z = ‘111....150 symbols...111’;
}
$a = ‘111....150 symbols...111’;
func(
‘111....150 symbols...111’
);
$a = array(
‘111....150 symbols...111’,
);
$a = <<<DOC
111....150 symbols...111
DOC;
Required:
- New line character - line feed character (LF or \n or 10 or 0x0a) [REQ.PHP.2.3.1].
- In all the cases when the text with the new line is not expected to go beyond the current server, use the PHP_EOL constant for the line feed character.
- In the cases when the text with the new line is expected to be transferred to other computer, use the Windows-based line feed character (\n\r or 10+13 or 0x0a + 0x0d).
##Indents/tabs/spaces
Required:
Indent for each new level - 4 spaces [REQ.PHP.2.4.1].
Recommendations:
Place as many indents as necessary but not more than you actually need. If you are indenting further than the 7th level, think about taking the code into a separate block [WRN.PHP.2.4.1].
Justification:
- When people use different tab values, the code often appears unreadable and unprintable; therefore, spaces are more preferrable.
- Nobody will ever come to an agreement on the most suitable number of spaces. Just be consistent. 4 spaces are the most common.
- The majority of PHP applications use 4 spaces.
- The majority of text editors use 4 spaces.
Example:
function func() {
if (something bad) {
if (another thing bad) {
while (more input) {
}
}
}
}
Required:
- In control structures, there must be a space between the key word and the opening bracket [REQ.PHP.2.5.1].
- The opening bracket is to be placed on the same line with the respective operator [REQ.PHP.2.5.2].
- The closing bracket is to be placed on the same indent with the operator [REQ.PHP.2.5.3].
- The closing curly bracket is to be placed on the next (following the conventional code) line [REQ.PHP.2.5.4].
- The entire content between the brackets is to be indented [REQ.PHP.2.5.5].
- The code in the body is always framed by curly brackets [REQ.PHP.2.5.6].
- Values cannot be assigned to variables within control structures, except within for [REQ.PHP.2.5.7].
- If a code block within curly brackets is large (a body is large when it has 4 or more lines of code or 1 line of comments to the body), it must be preceded by a blank line [REQ.PHP.2.5.10] [!].
Required:
- The elseif operator is placed on a single line [REQ.PHP.2.5.8].
- A block of code within curly brackets is to be followed by a blank line if it is followed by else or elseif [REQ.PHP.2.5.9] [!].
- Use elseif instead of the else if combination [REQ.PHP.2.5.11].
Example:
if ((condition1) || (condition2)) {
action1;
} elseif ((condition3) && (condition4)) {
bigaction2;
} else {
defaultbigaction;
}
Required:
- The case block, passing the control further down, must have a comment [REQ.PHP.2.5.12].
- The default block is always present [REQ.PHP.2.5.13].
- Each case block must be followed by a blank line, separating the block from a neighbor [REQ.PHP.2.5.14].
- The content of each case expression must be indented [REQ.PHP.2.5.15].
- One blank line must be placed between switch, before the first case expression.
Example:
switch (...) {
case 1:
// break intentionally omitted
case 2: {
$v = get_week_number();
...
}
break;
default:
...
}
Recommended:
- Enclose the condition expression in brackets, thus distinguishing it from the rest of the code [WRN.PHP.2.5.1].
- Where possible, the actions to be performed on the condition should be simple functions.
- If the entire branch block, located on a single line, is difficult to read, place the variant1 and variant2 blocks on a separate line each.
Example:
(condition) ? funct1() : func2();
or
(condition)
? long statement
: another long statement;
Recommended:
Minimize the use of continue and break [WRN.PHP.2.5.4].