# PHP_CodeSniffer Coding Standard ## Array Indent The opening brace of a multi-line array must be indented at least to the same level as the start of the statement.
Valid: Opening brace of a multi-line array indented to the same level as the start of the statement. | Invalid: Opening brace of a multi-line array not indented to the same level as the start of the statement. |
---|---|
$b = [ 1, 2, ]; if ($condition) { $a = [ 1, 2, ]; } | if ($condition) { $a = [ 1, 2, ]; } |
Valid: Each array element is indented by exactly four spaces. | Invalid: Array elements not indented by four spaces. |
---|---|
$a = array( 1, 2, 3, ); | $a = array( 1, 2, 3, ); |
Valid: Array closing brace on its own line. | Invalid: Array closing brace not on its own line. |
---|---|
$a = [ 1, 2, ]; | $a = [ 1, 2,]; |
Valid: Closing brace aligned with the start of the statement containing the array opener. | Invalid: Closing brace not aligned with the start of the statement containing the array opener. |
---|---|
$a = array( 1, 2, ); | $a = array( 1, 2, ); |
Valid: Short form of array. | Invalid: Long form of array. |
---|---|
$arr = [ 'foo' => 'bar', ]; | $arr = array( 'foo' => 'bar', ); |
Valid: Long form of array. | Invalid: Short form of array. |
---|---|
$arr = array( 'foo' => 'bar', ); | $arr = [ 'foo' => 'bar', ]; |
Valid: A unique class name. | Invalid: A class duplicated (including across multiple files). |
---|---|
class Foo { } | class Foo { } class Foo { } |
Valid: Opening brace on the same line. | Invalid: Opening brace on the next line. |
---|---|
class Foo { } | class Foo { } |
Valid: Opening brace is the last thing on the line. | Invalid: Opening brace not last thing on the line. |
---|---|
class Foo { } | class Foo { // Start of class. } |
Valid: A variable comparison being executed within a condition. | Invalid: A variable assignment being made within a condition. |
---|---|
if ($test === 'abc') { // Code. } | if ($test = 'abc') { // Code. } |
Valid: There is at least one statement inside the PHP tag pair. | Invalid: There is no statement inside the PHP tag pair. |
---|---|
= 'Hello World'; ?> | = ?> |
Valid: There is no superfluous semicolon after a PHP statement. | Invalid: There are one or more superfluous semicolons after a PHP statement. |
---|---|
function_call(); if (true) { echo 'Hello World'; } | function_call();;; if (true) { echo 'Hello World'; }; |
Valid: There is a statement inside the control structure. | Invalid: The control structure has no statements. |
---|---|
if ($test) { $var = 1; } | if ($test) { // do nothing } |
Valid: A for loop is used with all three expressions. | Invalid: A for loop is used without a first or third expression. |
---|---|
for ($i = 0; $i < 10; $i++) { echo "{$i}\n"; } | for (;$test;) { $test = doSomething(); } |
Valid: A for loop that determines its end condition before the loop starts. | Invalid: A for loop that unnecessarily computes the same value on every iteration. |
---|---|
$end = count($foo); for ($i = 0; $i < $end; $i++) { echo $foo[$i]."\n"; } | for ($i = 0; $i < count($foo); $i++) { echo $foo[$i]."\n"; } |
Valid: Two different variables being used to increment. | Invalid: Inner incrementer is the same variable name as the outer one. |
---|---|
for ($i = 0; $i < 10; $i++) { for ($j = 0; $j < 10; $j++) { } } | for ($i = 0; $i < 10; $i++) { for ($j = 0; $j < 10; $i++) { } } |
Valid: Making precedence clear with parentheses. | Invalid: Not using parentheses. |
---|---|
$one = false; $two = false; $three = true; $result = ($one && $two) || $three; $result2 = $one && ($two || $three); $result3 = ($one && !$two) xor $three; $result4 = $one && (!$two xor $three); if ( ($result && !$result3) || (!$result && $result3) ) {} | $one = false; $two = false; $three = true; $result = $one && $two || $three; $result3 = $one && !$two xor $three; if ( $result && !$result3 || !$result && $result3 ) {} |
Valid: An if statement that only executes conditionally. | Invalid: An if statement that is always performed. |
---|---|
if ($test) { $var = 1; } | if (true) { $var = 1; } |
Valid: An if statement that only executes conditionally. | Invalid: An if statement that is never performed. |
---|---|
if ($test) { $var = 1; } | if (false) { $var = 1; } |
Valid: A method in a final class is not marked final. | Invalid: A method in a final class is also marked final. |
---|---|
final class Foo { public function bar() { } } | final class Foo { public final function bar() { } } |
Valid: All the parameters are used. | Invalid: One of the parameters is not being used. |
---|---|
function addThree($a, $b, $c) { return $a + $b + $c; } | function addThree($a, $b, $c) { return $a + $b; } |
Valid: A method that extends functionality of a parent method. | Invalid: An overriding method that only calls the parent method. |
---|---|
final class Foo extends Baz { public function bar() { parent::bar(); $this->doSomethingElse(); } } | final class Foo extends Baz { public function bar() { parent::bar(); } } |
Valid: DocBlock with some content. | Invalid: Empty DocBlock. |
---|---|
/** * Some content. */ | /** * */ |
Valid: The opening and closing DocBlock tags have to be on a line by themselves. | Invalid: The opening and closing DocBlock tags are not on a line by themselves. |
---|---|
/** * Short description. */ | /** Short description. */ |
Valid: DocBlock with a short description on the first line. | Invalid: DocBlock without a short description or short description not on the first line. |
---|---|
/** * Short description. */ | /** * @return int */ /** * * Short description. */ |
Valid: Both the short and long description start with a capital letter. | Invalid: Neither short nor long description starts with a capital letter. |
---|---|
/** * Short description. * * Long description. */ | /** * short description. * * long description. */ |
Valid: One blank line separating the short description, the long description and tag groups. | Invalid: More than one or no blank line separating the short description, the long description and tag groups. |
---|---|
/** * Short description. * * Long description. * * @param int $foo */ | /** * Short description. * * * Long description. * @param int $foo */ |
Valid: Parameter tags grouped together. | Invalid: Parameter tags not grouped together. |
---|---|
/** * Short description. * * @param int $foo * @param string $bar */ | /** * Short description. * * @param int $foo * * @param string $bar */ |
Valid: Parameter tags are not grouped together with other tags. | Invalid: Parameter tags grouped together with other tags. |
---|---|
/** * Short description. * * @param int $foo * * @since 3.4.8 * @deprecated 6.0.0 */ | /** * Short description. * * @param int $foo * @since 3.4.8 * @deprecated 6.0.0 */ |
Valid: Tag values for different tags in the same tag group are aligned with each other. | Invalid: Tag values for different tags in the same tag group are not aligned with each other. |
---|---|
/** * Short description. * * @since 0.5.0 * @deprecated 1.0.0 */ | /** * Short description. * * @since 0.5.0 * @deprecated 1.0.0 */ |
Valid: Parameter tags are defined first. | Invalid: Parameter tags are not defined first. |
---|---|
/** * Short description. * * @param string $foo * * @return void */ | /** * Short description. * * @return void * * @param string $bar */ |
Valid: No additional blank lines before the closing DocBlock tag. | Invalid: Additional blank lines before the closing DocBlock tag. |
---|---|
/** * Short description. */ | /** * Short description. * */ |
Valid: A comment without a fixme. | Invalid: A fixme comment. |
---|---|
// Handle strange case if ($test) { $var = 1; } | // FIXME: This needs to be fixed! if ($test) { $var = 1; } |
Valid: A comment without a todo. | Invalid: A todo comment. |
---|---|
// Handle strange case if ($test) { $var = 1; } | // TODO: This needs to be fixed! if ($test) { $var = 1; } |
Valid: Value to be asserted must go on the right side of the comparison. | Invalid: Value to be asserted must not be on the left. |
---|---|
if ($test === null) { $var = 1; } | if (null === $test) { $var = 1; } |
Valid: Braces are used around the control structure. | Invalid: No braces are used for the control structure.. |
---|---|
if ($test) { $var = 1; } | if ($test) $var = 1; |
Valid: Valid JS Syntax is used. | Invalid: Trailing comma in a javascript array. |
---|---|
var foo = [1, 2]; | var foo = [1, 2,]; |
Valid: Valid CSS Syntax is used. | Invalid: The CSS has a typo in it. |
---|---|
.foo: { width: 100%; } | .foo: { width: 100 %; } |
Valid: Valid JS Syntax is used. | Invalid: The Javascript is using an undefined variable. |
---|---|
var foo = 5; | foo = 5; |
Valid: A PHP file with only PHP code in it. | Invalid: A PHP file with html in it outside of the PHP tags. |
---|---|
some string here |
Valid: Only one class in the file. | Invalid: Multiple classes defined in one file. |
---|---|
Valid: Only one interface in the file. | Invalid: Multiple interfaces defined in one file. |
---|---|
Valid: Only one object structure in the file. | Invalid: Multiple object structures defined in one file. |
---|---|
Valid: Only one trait in the file. | Invalid: Multiple traits defined in one file. |
---|---|
Valid: Two statements are spread out on two separate lines. | Invalid: Two statements are combined onto one line. |
---|---|
$foo = 1; $bar = 2; | $foo = 1; $bar = 2; |
Valid: Equals signs aligned. | Invalid: Not aligned; harder to read. |
---|---|
$shortVar = (1 + 2); $veryLongVarName = 'string'; $var = foo($bar, $baz); | $shortVar = (1 + 2); $veryLongVarName = 'string'; $var = foo($bar, $baz); |
Valid: Equals signs aligned; only one space after longest var name. | Invalid: Two spaces after longest var name. |
---|---|
$shortVar += 1; $veryLongVarName = 1; | $shortVar += 1; $veryLongVarName = 1; |
Valid: Equals signs aligned. | Invalid: Equals signs not aligned. |
---|---|
$shortVar = 1; $veryLongVarName -= 1; | $shortVar = 1; $veryLongVarName -= 1; |
Valid: A cast operator is immediately before its value. | Invalid: A cast operator is followed by whitespace. |
---|---|
$foo = (string)1; | $foo = (string) 1; |
Valid: A cast operator is followed by one space. | Invalid: A cast operator is not followed by whitespace. |
---|---|
$foo = (string) 1; | $foo = (string)1; |
Valid: A NOT operator followed by one space. | Invalid: A NOT operator not followed by whitespace or followed by too much whitespace. |
---|---|
if (! $someVar || ! $x instanceOf stdClass) {}; | if (!$someVar || !$x instanceOf stdClass) {}; if (! $someVar || ! $x instanceOf stdClass) {}; |
Valid: Single space before a cast operator. | Invalid: No space or multiple spaces before a cast operator. |
---|---|
$integer = (int) $string; $c = $a . (string) $b; | $integer =(int) $string; $c = $a . (string) $b; |
Valid: Pass-by-reference is specified in the function definition. | Invalid: Pass-by-reference is done in the call to a function. |
---|---|
function foo(&$bar) { $bar++; } $baz = 1; foo($baz); | function foo($bar) { $bar++; } $baz = 1; foo(&$baz); |
Valid: No space before and exactly one space after a comma. | Invalid: A space before and no space after a comma. |
---|---|
foo($bar, $baz); | foo($bar ,$baz); |
Valid: Opening brace on the next line. | Invalid: Opening brace on the same line. |
---|---|
function fooFunction($arg1, $arg2 = '') { // Do something } | function fooFunction($arg1, $arg2 = '') { // Do something } |
Valid: Opening brace on the same line. | Invalid: Opening brace on the next line. |
---|---|
function fooFunction($arg1, $arg2 = '') { // Do something. } | function fooFunction($arg1, $arg2 = '') { // Do something. } |
Valid: Class name starts with 'Abstract'. | Invalid: Class name does not start with 'Abstract'. |
---|---|
abstract class AbstractBar { } | abstract class Bar { } |
Valid: A function in camelCaps format. | Invalid: A function in snake_case format. |
---|---|
function doSomething() { } | function do_something() { } |
Valid: The constructor is named __construct. | Invalid: The old style class name constructor is used. |
---|---|
class Foo { function __construct() { } } | class Foo { function Foo() { } } |
Valid: Interface name ends on 'Interface'. | Invalid: Interface name does not end on 'Interface'. |
---|---|
interface BarInterface { } | interface Bar { } |
Valid: Trait name ends on 'Trait'. | Invalid: Trait name does not end on 'Trait'. |
---|---|
trait BarTrait { } | trait Bar { } |
Valid: All uppercase constant name. | Invalid: Mixed case or lowercase constant name. |
---|---|
define('FOO_CONSTANT', 'foo'); class FooClass { const FOO_CONSTANT = 'foo'; } | define('Foo_Constant', 'foo'); class FooClass { const foo_constant = 'foo'; } |
Valid: A file starting with an opening PHP tag. | Invalid: A file with content before the opening PHP tag. |
---|---|
Beginning content |
Valid: A closing tag paired with its opening tag. | Invalid: No closing tag paired with the opening tag. |
---|---|
Valid: A non-deprecated function is used. | Invalid: A deprecated function is used. |
---|---|
$foo = explode('a', $bar); | $foo = split('a', $bar); |
Valid: count() is used in place of sizeof(). | Invalid: sizeof() is used. |
---|---|
$foo = count($bar); | $foo = sizeof($bar); |
Valid: Lowercase constants. | Invalid: Uppercase constants. |
---|---|
if ($var === false || $var === null) { $var = true; } | if ($var === FALSE || $var === NULL) { $var = TRUE; } |
Valid: Lowercase array keyword used. | Invalid: Non-lowercase array keyword used. |
---|---|
$foo = array(); | $foo = Array(); |
Valid: Lowercase type declarations used. | Invalid: Non-lowercase type declarations used. |
---|---|
function myFunction(int $foo) : string { } | function myFunction(Int $foo) : STRING { } |
Valid: Lowercase type used. | Invalid: Non-lowercase type used. |
---|---|
$foo = (bool) $isValid; | $foo = (BOOL) $isValid; |
Valid: isset() is used to verify that a variable exists before trying to use it. | Invalid: Errors are suppressed. |
---|---|
if (isset($foo) && $foo) { echo "Hello\n"; } | if (@$foo) { echo "Hello\n"; } |
Valid: `strict_types` declaration is present. | Invalid: Missing `strict_types` declaration. |
---|---|
declare(strict_types=1); declare(encoding='UTF-8', strict_types=0); | declare(encoding='ISO-8859-1'); |
Valid: `strict_types` declaration is enabled. | Invalid: `strict_types` declaration is disabled. |
---|---|
declare(strict_types=1); | declare(strict_types=0); |
Valid: PHP_SAPI is used. | Invalid: Function call to php_sapi_name() is used. |
---|---|
if (PHP_SAPI === 'cli') { echo "Hello, CLI user."; } | if (php_sapi_name() === 'cli') { echo "Hello, CLI user."; } |
Valid: No PHP syntax errors. | Invalid: Code contains PHP syntax errors. |
---|---|
echo "Hello!"; $array = [1, 2, 3]; | echo "Hello!" // Missing semicolon. $array = [1, 2, 3; // Missing closing bracket. |
Valid: Uppercase constants. | Invalid: Lowercase constants. |
---|---|
if ($var === FALSE || $var === NULL) { $var = TRUE; } | if ($var === false || $var === null) { $var = true; } |
Valid: Using nowdoc syntax for a text string without any interpolation or expressions. | Invalid: Using heredoc syntax for a text string without any interpolation or expressions. |
---|---|
$nowdoc = <<<'EOD' some text EOD; |
$heredoc = << |
Valid: Using heredoc syntax for a text string containing interpolation or expressions. | Invalid: Using heredoc syntax for a text string without any interpolation or expressions. |
---|---|
$heredoc = <<<"EOD" some $text EOD; | $heredoc = <<<"EOD" some text EOD; |
Valid: A string can be concatenated with an expression. | Invalid: Strings should not be concatenated together. |
---|---|
echo '5 + 2 = ' . (5 + 2); | echo 'Hello' . ' ' . 'World'; |
Valid: No spaces on the inside of a set of arbitrary parentheses. | Invalid: Spaces or new lines on the inside of a set of arbitrary parentheses. |
---|---|
$a = (null !== $extra); | $a = ( null !== $extra ); $a = ( null !== $extra ); |
Valid: No space between the <<< and the identifier string. | Invalid: Whitespace between the <<< and the identifier string. |
---|---|
$heredoc = <<
$heredoc = <<< END
some text
END;
|
|
Valid: No whitespace between variables and increment/decrement operators. | Invalid: Whitespace between variables and increment/decrement operators. |
---|---|
++$i; --$i['key']['id']; ClassName::$prop++; $obj->prop--; | ++ $i; -- $i['key']['id']; ClassName::$prop ++; $obj->prop --; |
Valid: Single space after language construct. | Invalid: No space, more than one space or newline after language construct. |
---|---|
echo 'Hello, World!'; throw new Exception(); return $newLine; | echo'Hello, World!'; throw new Exception(); return $newLine; |
Valid: Single space between yield and from. | Invalid: More than one space or newline between yield and from. |
---|---|
function myGenerator() { yield from [1, 2, 3]; } | function myGenerator() { yield from [1, 2, 3]; yield from [1, 2, 3]; } |
Valid: 4 spaces are used to indent a control structure. | Invalid: 8 spaces are used to indent a control structure. |
---|---|
if ($test) { $var = 1; } | if ($test) { $var = 1; } |
Valid: No space between the spread operator and the variable/function call it applies to. | Invalid: Space found between the spread operator and the variable/function call it applies to. |
---|---|
function foo(&...$spread) { bar(...$spread); bar( [...$foo], ...array_values($keyedArray) ); } | function bar(... $spread) { bar(... $spread ); bar( [... $foo ],.../*@*/array_values($keyed) ); } |
Valid: Argument with default value at end of declaration. | Invalid: Argument with default value at start of declaration. |
---|---|
function connect($dsn, $persistent = false) { ... } | function connect($persistent = false, $dsn) { ... } |
Valid: One class in a file. | Invalid: Multiple classes in a single file. |
---|---|
Valid: A vendor-level namespace is used. | Invalid: No namespace used in file. |
---|---|
Valid: A class defined in a file by itself. | Invalid: A class defined in a file with other code. |
---|---|
Valid: Method name in camelCase. | Invalid: Method name not in camelCase. |
---|---|
class Foo { private function doBar() { } } | class Foo { private function do_bar() { } } |
Valid: Correct spacing around class keyword. | Invalid: 2 spaces used around class keyword. |
---|---|
abstract class Foo { } | abstract class Foo { } |
Valid: Correct property naming. | Invalid: An underscore prefix used to indicate visibility. |
---|---|
class Foo { private $bar; } | class Foo { private $_bar; } |
Valid: Visibility of property declared. | Invalid: Var keyword used to declare property. |
---|---|
class Foo { private $bar; } | class Foo { var $bar; } |
Valid: One property declared per statement. | Invalid: Multiple properties declared in one statement. |
---|---|
class Foo { private $bar; private $baz; } | class Foo { private $bar, $baz; } |
Valid: If declared as static, the static declaration must come after the visibility declaration. | Invalid: Static declaration before the visibility declaration. |
---|---|
class Foo { public static $bar; private $baz; } | class Foo { static protected $bar; } |
Valid: Correct spacing. | Invalid: Whitespace used inside the parentheses. |
---|---|
if ($foo) { $var = 1; } | if ( $foo ) { $var = 1; } |
Valid: Single word elseif keyword used. | Invalid: Separate else and if keywords used. |
---|---|
if ($foo) { $var = 1; } elseif ($bar) { $var = 2; } | if ($foo) { $var = 1; } else if ($bar) { $var = 2; } |
Valid: Keywords in lowercase. | Invalid: Keywords not in lowercase. |
---|---|
switch ($foo) { case 'bar': break; default: break; } | switch ($foo) { CASE 'bar': break; Default: break; } |
Valid: Case statement followed by one space. | Invalid: Case statement not followed by one space. |
---|---|
switch ($foo) { case 'bar': break; } | switch ($foo) { case'bar': break; } |
Valid: Colons not preceded by whitespace. | Invalid: Colons preceded by whitespace. |
---|---|
switch ($foo) { case 'bar': break; default: break; } | switch ($foo) { case 'bar' : break; default : break; } |
Valid: Body starts on the next line. | Invalid: Body on the same line as the case statement. |
---|---|
switch ($foo) { case 'bar': break; } | switch ($foo) { case 'bar': break; } |
Valid: Terminating statement on its own line. | Invalid: Terminating statement not on its own line. |
---|---|
switch ($foo) { case 'bar': echo $foo; return; } | switch ($foo) { case 'bar': echo $foo; return; } |
Valid: Break statement indented correctly. | Invalid: Break statement not indented four spaces. |
---|---|
switch ($foo) { case 'bar': break; } | switch ($foo) { case 'bar': break; } |
Valid: Using a colon for case and default statements. | Invalid: Using a semi-colon or colon followed by braces. |
---|---|
switch ($foo) { case 'bar': break; default: break; } | switch ($foo) { case 'bar'; break; default: { break; } } |
Valid: Comment marking intentional fall-through in a non-empty case body. | Invalid: No comment marking intentional fall-through in a non-empty case body. |
---|---|
switch ($foo) { case 'bar': echo $foo; // no break default: break; } | switch ($foo) { case 'bar': echo $foo; default: break; } |
Valid: Closing tag not used. | Invalid: Closing tag used. |
---|---|
Valid: Correct spacing is used around parentheses. | Invalid: Incorrect spacing used, too much space around the parentheses. |
---|---|
foo($bar, $baz); | foo ( $bar, $baz ); |
Valid: Correct number of spaces used for indent in a multi-line function call. | Invalid: Incorrect number of spaces used for indent in a multi-line function call. |
---|---|
foo( $bar, $baz ); | foo( $bar, $baz ); |
Valid: Closing parenthesis for a multi-line function call is on a new line after the last parameter. | Invalid: Closing parenthesis for a multi-line function call is not on a new line after the last parameter. |
---|---|
foo( $bar, $baz ); | foo( $bar, $baz); |
Valid: The first argument of a multi-line function call is on a new line. | Invalid: The first argument of a multi-line function call is not on a new line. |
---|---|
foo( $bar, $baz ); | foo($bar, $baz ); |
Valid: Only one argument per line in a multi-line function call. | Invalid: Two or more arguments per line in a multi-line function call. |
---|---|
foo( $bar, $baz ); | foo( $bar, $baz ); |
Valid: No blank lines in a multi-line function call. | Invalid: Blank line in multi-line function call. |
---|---|
foo( $bar, $baz ); | foo( $bar, $baz ); |
Valid: Closing brace directly follows the function body. | Invalid: Blank line between the function body and the closing brace. |
---|---|
function foo() { echo 'foo'; } | function foo() { echo 'foo'; } |
Valid: Correct method naming. | Invalid: An underscore prefix used to indicate visibility. |
---|---|
class Foo { private function bar() { } } | class Foo { private function _bar() { } } |
Valid: Correct ordering of method prefixes. | Invalid: `static` keyword used before visibility and final used after. |
---|---|
class Foo { final public static function bar() { } } | class Foo { static public final function bar() { } } |
Valid: One blank line after the namespace declaration. | Invalid: No blank line after the namespace declaration. |
---|---|
namespace Foo\Bar; use \Baz; | namespace Foo\Bar; use \Baz; |
Valid: One use declaration per namespace. | Invalid: Multiple namespaces in a use declaration. |
---|---|
use \Foo; use \Bar; | use \Foo, \Bar; |
Valid: Use statements come after first namespace. | Invalid: Namespace declared after use. |
---|---|
namespace \Foo; use \Bar; | use \Bar; namespace \Foo; |
Valid: A single blank line after the final use statement. | Invalid: No blank line after the final use statement. |
---|---|
use \Foo; use \Bar; class Baz { } | use \Foo; use \Bar; class Baz { } |
Valid: Parenthesis used. | Invalid: Parenthesis not used. |
---|---|
new Foo(); | new Foo; |
Valid: Closing brace is the last content on the line. | Invalid: Comment or statement following the closing brace on the same line. |
---|---|
class Foo { // Class content. } function bar() { // Function content. } | interface Foo2 { // Interface content. } echo 'Hello!'; function bar() { // Function content. } //end bar() |
Valid: No blank lines after opening brace. | Invalid: Blank line after opening brace. |
---|---|
class Foo { public function bar() { // Method content. } } | class Foo { public function bar() { // Method content. } } |
Valid: Boolean operator between conditions consistently at the beginning of the line. | Invalid: Mix of boolean operators at the beginning and the end of the line. |
---|---|
if ( $expr1 && $expr2 && ($expr3 || $expr4) && $expr5 ) { // if body. } | if ( $expr1 && ($expr2 || $expr3) && $expr4 ) { // if body. } |
Valid: Boolean operator between conditions consistently at the end of the line. | Invalid: Mix of boolean operators at the beginning and the end of the line. |
---|---|
if ( $expr1 || ($expr2 || $expr3) && $expr4 ) { // if body. } | match ( $expr1 && $expr2 || $expr3 ) { // structure body. }; |
Valid: No space after the opening parenthesis in a single-line condition. | Invalid: Space after the opening parenthesis in a single-line condition. |
---|---|
if ($expr) { } | if ( $expr) { } |
Valid: No space before the closing parenthesis in a single-line condition. | Invalid: Space before the closing parenthesis in a single-line condition. |
---|---|
if ($expr) { } | if ($expr ) { } |
Valid: First expression of a multi-line control structure condition block is on the line after the opening parenthesis. | Invalid: First expression of a multi-line control structure condition block is on the same line as the opening parenthesis. |
---|---|
while ( $expr1 && $expr2 ) { } | while ($expr1 && $expr2 ) { } |
Valid: Each line in a multi-line control structure condition block indented at least once. Default indentation is 4 spaces. | Invalid: Some lines in a multi-line control structure condition block not indented correctly. |
---|---|
while ( $expr1 && $expr2 ) { } | while ( $expr1 && $expr2 && $expr3 ) { } |
Valid: The closing parenthesis of a multi-line control structure condition block is on the line after the last expression. | Invalid: The closing parenthesis of a multi-line control structure condition block is on the same line as the last expression. |
---|---|
while ( $expr1 && $expr2 ) { } | while ( $expr1 && $expr2) { } |
Valid: The closing parenthesis of a multi-line control structure condition block is indented to the same level as start of the control structure. | Invalid: The closing parenthesis of a multi-line control structure condition block is not indented to the same level as start of the control structure. |
---|---|
while ( $expr1 && $expr2 ) { } | while ( $expr1 && $expr2 ) { } |
Valid: Import statement doesn't begin with a leading backslash. | Invalid: Import statement begins with a leading backslash. |
---|---|
Valid: Opening PHP tag on a line by itself. | Invalid: Opening PHP tag not on a line by itself. |
---|---|
Valid: Opening PHP tag not on a line by itself, but has markup outside the closing PHP tag. | Invalid: Opening PHP tag not on a line by itself without any markup in the file. |
---|---|
Valid: No whitespace used. | Invalid: Superfluous whitespace used. |
---|---|
public function functionName( ?string $arg1, ?int $arg2 ): ?string { } | public function functionName( ? string $arg1, ? int $arg2 ): ? string { } |
Valid: No unexpected characters. | Invalid: Unexpected characters used. |
---|---|
public function foo(?int $arg): ?string { } | public function bar(? /* comment */ int $arg): ? // nullable for a reason string { } |
Valid: A single space between the colon and type in a return type declaration. | Invalid: No space between the colon and the type in a return type declaration. |
---|---|
$closure = function ( $arg ): string { // Closure body. }; | $closure = function ( $arg ):string { // Closure body. }; |
Valid: No space before the colon in a return type declaration. | Invalid: One or more spaces before the colon in a return type declaration. |
---|---|
function someFunction( $arg ): string { // Function body. }; | function someFunction( $arg ) : string { // Function body. }; |
Valid: Short form type used. | Invalid: Long form type type used. |
---|---|
$foo = (bool) $isValid; | $foo = (boolean) $isValid; |
Valid: Max depth of 2. | Invalid: Max depth of 3. |
---|---|
use Vendor\Package\SomeNamespace\{ SubnamespaceOne\ClassA, SubnamespaceOne\ClassB, SubnamespaceTwo\ClassY, ClassZ, }; | use Vendor\Package\SomeNamespace\{ SubnamespaceOne\AnotherNamespace\ClassA, SubnamespaceOne\ClassB, ClassZ, }; |
Valid: At least 1 space used. | Invalid: No spacing used. |
---|---|
if ($a === $b) { $foo = $bar ?? $a ?? $b; } elseif ($a > $b) { $variable = $foo ? 'foo' : 'bar'; } | if ($a===$b) { $foo=$bar??$a??$b; } elseif ($a>$b) { $variable=$foo?'foo':'bar'; } |
Valid: Constant visibility declared. | Invalid: Constant visibility not declared. |
---|---|
class Foo { private const BAR = 'bar'; } | class Foo { const BAR = 'bar'; } |
Valid: Class name starts with a capital letter. | Invalid: Class name does not start with a capital letter. |
---|---|
class PascalCaseStandard { } | class notPascalCaseStandard { } |
Valid: Class name contains only letters and numbers. | Invalid: Class name contains underscores. |
---|---|
class PSR7Response { } | class PSR7_Response { } |
Valid: Correct spacing used. | Invalid: Invalid spacing used. |
---|---|
foreach ($foo as $bar => $baz) { echo $baz; } | foreach ( $foo as $bar=>$baz ) { echo $baz; } |
Valid: Lowercase as keyword. | Invalid: Uppercase as keyword. |
---|---|
foreach ($foo as $bar => $baz) { echo $baz; } | foreach ($foo AS $bar => $baz) { echo $baz; } |
Valid: Correct spacing used. | Invalid: Invalid spacing used inside brackets. |
---|---|
for ($i = 0; $i < 10; $i++) { echo $i; } | for ( $i = 0; $i < 10; $i++ ) { echo $i; } |
Valid: Correct spacing used. | Invalid: Invalid spacing used before semicolons. |
---|---|
for ($i = 0; $i < 10; $i++) { echo $i; } | for ($i = 0 ; $i < 10 ; $i++) { echo $i; } |
Valid: Correct spacing used. | Invalid: Invalid spacing used after semicolons. |
---|---|
for ($i = 0; $i < 10; $i++) { echo $i; } | for ($i = 0;$i < 10;$i++) { echo $i; } |
Valid: Lowercase if keyword. | Invalid: Uppercase if keyword. |
---|---|
if ($foo) { $bar = true; } | IF ($foo) { $bar = true; } |
Valid: Lowercase function keyword. | Invalid: Uppercase function keyword. |
---|---|
function foo() { return true; } | FUNCTION foo() { return true; } |
Valid: No spaces. | Invalid: Whitespace used inside parentheses. |
---|---|
$foo = (int)'42'; | $foo = ( int )'42'; |
Valid: Closing brace aligned with line containing opening brace. | Invalid: Closing brace misaligned with line containing opening brace. |
---|---|
function foo() { } if (!class_exists('Foo')) { class Foo { } } some output | function foo() { } if (!class_exists('Foo')) { class Foo { } } some output |
Valid: Close brace on its own line. | Invalid: Close brace on a line containing other code. |
---|---|
enum Foo { } | enum Foo {} |
Valid: A single space following the keywords. | Invalid: Multiple spaces following the keywords. |
---|---|
public static function foo() { } | public static function foo() { } |
Valid: No whitespace preceding first content in file. | Invalid: Whitespace used before content in file. |
---|---|
Valid: No whitespace found at end of line. | Invalid: Whitespace found at end of line. |
---|---|
echo 'semicolon followed by new line char'; | echo 'trailing spaces after semicolon'; |
Valid: Functions do not contain multiple empty lines in a row. | Invalid: Functions contain multiple empty lines in a row. |
---|---|
function myFunction() { echo 'code here'; echo 'code here'; } | function myFunction() { echo 'code here'; echo 'code here'; } |
Valid: A single new line appears after the last content in the file. | Invalid: Multiple new lines appear after the last content in the file. |
---|---|
function myFunction() { echo 'Closing PHP tag, then'; echo 'Single new line char, then EOF'; } ?> | function myFunction() { echo 'Closing PHP tag, then'; echo 'Multiple new line chars, then EOF'; } ?> |