|
| 1 | +# PSR2 Standard |
| 2 | +## From PSR1 |
| 3 | + |
| 4 | +- PHP code MUST use the long <?php ?> tags or the short-echo <?= ?> tags; |
| 5 | + it MUST NOT use the other tag variations. |
| 6 | + |
| 7 | +``` |
| 8 | +<rule ref="Generic.PHP.DisallowShortOpenTag.EchoFound"> |
| 9 | + <severity>0</severity> |
| 10 | +</rule> |
| 11 | +``` |
| 12 | + |
| 13 | +- PHP code MUST use only UTF-8 without BOM. |
| 14 | + |
| 15 | +``` |
| 16 | +<rule ref="Generic.Files.ByteOrderMark"/> |
| 17 | +``` |
| 18 | + |
| 19 | +- A file SHOULD declare new symbols and cause no other side effects, |
| 20 | + or it SHOULD execute logic with side effects, but SHOULD NOT do both. |
| 21 | + |
| 22 | +``` |
| 23 | +<rule ref="PSR1.Files.SideEffects"/> |
| 24 | +``` |
| 25 | + |
| 26 | +- Namespaces and classes MUST follow PSR-0. This means each class is in a file by itself, |
| 27 | + and is in a namespace of at least one level: a top-level vendor name. |
| 28 | + |
| 29 | +``` |
| 30 | +<rule ref="PSR1.Classes.ClassDeclaration"/> |
| 31 | +``` |
| 32 | + |
| 33 | +- Class names MUST be declared in StudlyCaps. |
| 34 | + |
| 35 | +``` |
| 36 | +<rule ref="Squiz.Classes.ValidClassName"/> |
| 37 | +``` |
| 38 | + |
| 39 | +- Class constants MUST be declared in all upper case with underscore separators. |
| 40 | + |
| 41 | +``` |
| 42 | +<rule ref="Generic.NamingConventions.UpperCaseConstantName"/> |
| 43 | +``` |
| 44 | + |
| 45 | +- Method names MUST be declared in camelCase(). |
| 46 | +``` |
| 47 | +<rule ref="PSR1.Methods.CamelCapsMethodName"/> |
| 48 | +``` |
| 49 | + |
| 50 | +## From PSR2 |
| 51 | + |
| 52 | +- All PHP files MUST use the Unix LF (linefeed) line ending. |
| 53 | + |
| 54 | +``` |
| 55 | +<rule ref="Generic.Files.LineEndings"> |
| 56 | + <properties> |
| 57 | + <property name="eolChar" value="\n"/> |
| 58 | + </properties> |
| 59 | +</rule> |
| 60 | +``` |
| 61 | + |
| 62 | +- All PHP files MUST end with a single blank line. |
| 63 | + |
| 64 | +``` |
| 65 | +<rule ref="PSR2.Files.EndFileNewline"/> |
| 66 | +``` |
| 67 | + |
| 68 | +- The closing ?> tag MUST be omitted from files containing only PHP. |
| 69 | + |
| 70 | +``` |
| 71 | +<rule ref="PSR2.Files.ClosingTag"/> |
| 72 | +``` |
| 73 | + |
| 74 | +- The soft limit on line length MUST be 120 characters; |
| 75 | + automated style checkers MUST warn but MUST NOT error at the soft limit. |
| 76 | + |
| 77 | +``` |
| 78 | +<rule ref="Generic.Files.LineLength"> |
| 79 | + <properties> |
| 80 | + <property name="lineLimit" value="120"/> |
| 81 | + <property name="absoluteLineLimit" value="0"/> |
| 82 | + </properties> |
| 83 | +</rule> |
| 84 | +``` |
| 85 | + |
| 86 | +- There MUST NOT be trailing whitespace at the end of non-blank lines. |
| 87 | + |
| 88 | +``` |
| 89 | +<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace"> |
| 90 | + <properties> |
| 91 | + <property name="ignoreBlankLines" value="true"/> |
| 92 | + </properties> |
| 93 | +</rule> |
| 94 | +<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.StartFile"> |
| 95 | + <severity>0</severity> |
| 96 | +</rule> |
| 97 | +<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EndFile"> |
| 98 | + <severity>0</severity> |
| 99 | +</rule> |
| 100 | +<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines"> |
| 101 | + <severity>0</severity> |
| 102 | +</rule> |
| 103 | +``` |
| 104 | + |
| 105 | +- There MUST NOT be more than one statement per line. |
| 106 | + |
| 107 | +``` |
| 108 | +<rule ref="Generic.Formatting.DisallowMultipleStatements"/> |
| 109 | +``` |
| 110 | + |
| 111 | +- Code MUST use an indent of 4 spaces, and MUST NOT use tabs for indenting. |
| 112 | + |
| 113 | +``` |
| 114 | +<rule ref="Generic.WhiteSpace.ScopeIndent"> |
| 115 | + <properties> |
| 116 | + <property name="ignoreIndentationTokens" type="array" value="T_COMMENT,T_DOC_COMMENT_OPEN_TAG"/> |
| 117 | + </properties> |
| 118 | +</rule> |
| 119 | +<rule ref="Generic.WhiteSpace.DisallowTabIndent"/> |
| 120 | +``` |
| 121 | + |
| 122 | +- PHP keywords MUST be in lower case. |
| 123 | + |
| 124 | +``` |
| 125 | +<rule ref="Generic.PHP.LowerCaseKeyword"/> |
| 126 | +``` |
| 127 | + |
| 128 | +- The PHP constants true, false, and null MUST be in lower case. |
| 129 | + |
| 130 | +``` |
| 131 | +<rule ref="Generic.PHP.LowerCaseConstant"/> |
| 132 | +``` |
| 133 | + |
| 134 | +- When present, there MUST be one blank line after the namespace declaration. |
| 135 | + |
| 136 | +``` |
| 137 | +<rule ref="PSR2.Namespaces.NamespaceDeclaration"/> |
| 138 | +``` |
| 139 | + |
| 140 | +- When present, all use declarations MUST go after the namespace declaration. |
| 141 | + There MUST be one use keyword per declaration. |
| 142 | + There MUST be one blank line after the use block. |
| 143 | + |
| 144 | +``` |
| 145 | +<rule ref="PSR2.Namespaces.UseDeclaration"/> |
| 146 | +``` |
| 147 | + |
| 148 | +- The extends and implements keywords MUST be declared on the same line as the class name. |
| 149 | + The opening brace for the class go MUST go on its own line; |
| 150 | + the closing brace for the class MUST go on the next line after the body. |
| 151 | + Lists of implements MAY be split across multiple lines, where each subsequent line is indented once. |
| 152 | + When doing so, the first item in the list MUST be on the next line, and there MUST be only one interface per line. |
| 153 | + |
| 154 | +``` |
| 155 | +<rule ref="PSR2.Classes.ClassDeclaration"/> |
| 156 | +``` |
| 157 | + |
| 158 | +- Visibility MUST be declared on all properties. |
| 159 | + The var keyword MUST NOT be used to declare a property. |
| 160 | + There MUST NOT be more than one property declared per statement. |
| 161 | + Property names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility. |
| 162 | + |
| 163 | +``` |
| 164 | +<rule ref="PSR2.Classes.PropertyDeclaration"/> |
| 165 | +``` |
| 166 | + |
| 167 | +- Visibility MUST be declared on all methods. |
| 168 | + |
| 169 | +``` |
| 170 | +<rule ref="Squiz.Scope.MethodScope"/> |
| 171 | +<rule ref="Squiz.WhiteSpace.ScopeKeywordSpacing"/> |
| 172 | +``` |
| 173 | + |
| 174 | +- Method names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility. |
| 175 | + |
| 176 | +``` |
| 177 | +<rule ref="PSR2.Methods.MethodDeclaration"/> |
| 178 | +``` |
| 179 | + |
| 180 | +- Method names MUST NOT be declared with a space after the method name. |
| 181 | + The opening brace MUST go on its own line, |
| 182 | + and the closing brace MUST go on the next line following the body. |
| 183 | + There MUST NOT be a space after the opening parenthesis, |
| 184 | + and there MUST NOT be a space before the closing parenthesis. |
| 185 | + |
| 186 | +``` |
| 187 | +<rule ref="PSR2.Methods.FunctionClosingBrace"/> |
| 188 | +<rule ref="Squiz.Functions.FunctionDeclaration"/> |
| 189 | +<rule ref="Squiz.Functions.LowercaseFunctionKeywords"/> |
| 190 | +``` |
| 191 | + |
| 192 | +- In the argument list, there MUST NOT be a space before each comma, |
| 193 | + and there MUST be one space after each comma. |
| 194 | + |
| 195 | +``` |
| 196 | +<rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing"> |
| 197 | + <properties> |
| 198 | + <property name="equalsSpacing" value="1"/> |
| 199 | + </properties> |
| 200 | +</rule> |
| 201 | +<rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterHint"> |
| 202 | + <severity>0</severity> |
| 203 | +</rule> |
| 204 | +``` |
| 205 | + |
| 206 | +- Method arguments with default values MUST go at the end of the argument list. |
| 207 | + |
| 208 | +``` |
| 209 | +<rule ref="PEAR.Functions.ValidDefaultValue"/> |
| 210 | +``` |
| 211 | + |
| 212 | +- Argument lists MAY be split across multiple lines, where each subsequent line is indented once. |
| 213 | + When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per line. |
| 214 | + When the argument list is split across multiple lines, |
| 215 | + the closing parenthesis and opening brace MUST be placed together on their own line with one space between them. |
| 216 | + |
| 217 | +``` |
| 218 | +<rule ref="Squiz.Functions.MultiLineFunctionDeclaration"/> |
| 219 | +``` |
| 220 | + |
| 221 | +- When present, the abstract and final declarations MUST precede the visibility declaration. |
| 222 | + When present, the static declaration MUST come after the visibility declaration. |
| 223 | + |
| 224 | +``` |
| 225 | +<rule ref="PSR2.Methods.MethodDeclaration"/> |
| 226 | +``` |
| 227 | + |
| 228 | +- When making a method or function call, there MUST NOT be a space between the method or function name |
| 229 | + and the opening parenthesis, there MUST NOT be a space after the opening parenthesis, |
| 230 | + and there MUST NOT be a space before the closing parenthesis. |
| 231 | + In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma. |
| 232 | + Argument lists MAY be split across multiple lines, where each subsequent line is indented once. |
| 233 | + When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per line. |
| 234 | + |
| 235 | +``` |
| 236 | +<rule ref="Generic.Functions.FunctionCallArgumentSpacing"/> |
| 237 | +<rule ref="PSR2.Methods.FunctionCallSignature.SpaceAfterCloseBracket"> |
| 238 | + <severity>0</severity> |
| 239 | +</rule> |
| 240 | +``` |
| 241 | + |
| 242 | +- There MUST be one space after the control structure keyword. |
| 243 | + There MUST NOT be a space after the opening parenthesis. |
| 244 | + There MUST NOT be a space before the closing parenthesis. |
| 245 | + There MUST be one space between the closing parenthesis and the opening brace. |
| 246 | + The structure body MUST be indented once. |
| 247 | + The closing brace MUST be on the next line after the body. |
| 248 | + |
| 249 | +``` |
| 250 | +<rule ref="Squiz.ControlStructures.ControlSignature"/> |
| 251 | +<rule ref="Squiz.WhiteSpace.ControlStructureSpacing" /> |
| 252 | +<rule ref="Squiz.WhiteSpace.ControlStructureSpacing.SpacingAfterOpenBrace"> |
| 253 | + <severity>0</severity> |
| 254 | +</rule> |
| 255 | +<rule ref="Squiz.WhiteSpace.ControlStructureSpacing.SpaceBeforeCloseBrace"> |
| 256 | + <severity>0</severity> |
| 257 | +</rule> |
| 258 | +<rule ref="Squiz.WhiteSpace.ControlStructureSpacing.LineAfterClose"> |
| 259 | + <severity>0</severity> |
| 260 | +</rule> |
| 261 | +<rule ref="Squiz.WhiteSpace.ControlStructureSpacing.NoLineAfterClose"> |
| 262 | + <severity>0</severity> |
| 263 | +</rule> |
| 264 | +<rule ref="Squiz.WhiteSpace.ScopeClosingBrace"/> |
| 265 | +<rule ref="Squiz.ControlStructures.ForEachLoopDeclaration"/> |
| 266 | +<rule ref="Squiz.ControlStructures.ForLoopDeclaration"/> |
| 267 | +<rule ref="Squiz.ControlStructures.LowercaseDeclaration"/> |
| 268 | +<rule ref="PSR2.ControlStructures.ControlStructureSpacing"/> |
| 269 | +``` |
| 270 | + |
| 271 | +- The body of each structure MUST be enclosed by braces. |
| 272 | + |
| 273 | +``` |
| 274 | +<rule ref="Generic.ControlStructures.InlineControlStructure"/> |
| 275 | +``` |
| 276 | + |
| 277 | +- The keyword elseif SHOULD be used instead of else if so that all control keywords look like single words. |
| 278 | + |
| 279 | +``` |
| 280 | +<rule ref="PSR2.ControlStructures/=.ElseIfDeclaration/> |
| 281 | +``` |
| 282 | + |
| 283 | +- The case statement MUST be indented once from switch, |
| 284 | + and the break keyword (or other terminating keyword) MUST be indented at the same level as the case body. |
| 285 | + There MUST be a comment such as // no break when fall-through is intentional in a non-empty case body. |
| 286 | + |
| 287 | +``` |
| 288 | +<rule ref="PSR2.ControlStructures.SwitchDeclaration/> |
| 289 | +``` |
| 290 | + |
| 291 | +- Closures MUST be declared with a space after the function keyword, and a space before and after the use keyword. |
| 292 | + The opening brace MUST go on the same line, and the closing brace MUST go on the next line following the body. |
| 293 | + There MUST NOT be a space after the opening parenthesis of the argument list or variable list, |
| 294 | + and there MUST NOT be a space before the closing parenthesis of the argument list or variable list. |
| 295 | + In the argument list and variable list, there MUST NOT be a space before each comma, |
| 296 | + and there MUST be one space after each comma. |
| 297 | + Closure arguments with default values MUST go at the end of the argument list. |
| 298 | + Argument lists and variable lists MAY be split across multiple lines, where each subsequent line is indented once. |
| 299 | + When doing so, the first item in the list MUST be on the next line, |
| 300 | + and there MUST be only one argument or variable per line. |
| 301 | + When the ending list (whether or arguments or variables) is split across multiple lines, |
| 302 | + the closing parenthesis and opening brace MUST be placed together on their own line with one space between them. |
| 303 | + |
| 304 | +``` |
| 305 | +<rule ref="Squiz.Functions.MultiLineFunctionDeclaration"/> |
| 306 | +``` |
0 commit comments