Skip to content

Latest commit

 

History

History
231 lines (196 loc) · 4.1 KB

RULES.md

File metadata and controls

231 lines (196 loc) · 4.1 KB

Complete rules

  1. All PSR-12 rules except for declare(strict_types=1) should be on the same line as PHP open tag.
  2. declare(strict_types=1) is required in all PHP files.
  3. snake_case is allowed in test method names
public function test_something(): void
{
}
  1. All classes should be declared as either final or abstract
// Prohibited.
// Should be declared as either final or abstract
class foobar
{
}
  1. Filenames must match the class names.
  2. Detect commented-out code.
// Will warn about the next line
// require_once $file;
  1. Ban some built-in functions:
  • sizeof, use count
  • print, use echo
  • each, use foreach
  • is_null, use === null
  • create_function
  • var_dump
  • print_r
  • debug_print_backtrace
  • eval
  • extract
  1. Brackets are not required when including a file.
// will warn you about this
require_once($file);

// brackets are not required
require_once $file;
  1. Align multiple statements
$var         = 'value';
$longVarName = 'value';
  1. Array bracket spacing
$array      ['key'] = 'value';
$array[     'key'] = 'value';
$array['key'       ] = 'value';

// Will be formatted to
$array['key'] = 'value';
$array['key'] = 'value';
$array['key'] = 'value';
  1. Multiline array indentation
function check(): void
{
                return $myArray ===          [
                    'key1'      =>          'value',
                                'key-long-long-long'          =>  'value',
'key-medium'      =>      'value'
                    ];
}

// Will be formatted to
function check(): void
{
    return $myArray === [
      'key1'               => 'value',
      'key-long-long-long' => 'value',
      'key-medium'         => 'value',
    ];
}
  1. Semi-colon spacing
$var = 'value'      ;

// will be formatted to
$var = 'value';
  1. Language construct spacing
// For example
require$blah;
require_once        'test';
$a = new        stdClass();

// will be formatted to
require $blah;
require_once 'test';
$a = new stdClass();
  1. Logical operator spacing
$a = $b             && $c;
$a = $b &&              $c;

// will be formatted to
$a = $b && $c;
$a = $b && $c;
  1. Object operator spacing
$this   ->testThis();
$this->     testThis();

parent      ::testThis();
parent::    testThis();

// will be formatted to
$this->testThis();
$this->testThis();

parent::testThis();
parent::testThis();
  1. Cast spacing
(int)$var;
(int)       $var;

// will be formatted to
(int) $var;
(int) $var;
  1. String concatenation spacing
$var = 'Hello' .     ' World';
$var = 'Hello'      . ' World';
$var = 'Hello'      .
' World';

// will be formatted to
$var = 'Hello' . ' World';
$var = 'Hello' . ' World';
$var = 'Hello' . ' World';
  1. Echoed strings should not be bracketed
echo('Should not be bracketed');

// will be formatted to
echo 'Should not be bracketed';
  1. Double quote is used in escaped string only
echo "Double quote is not required here";

// will be formatted to
echo 'Double quote is not required here';
  1. Remove space before and after function body
function fn()
{

  $var = 1;

}

// will be formatted to
function fn()
{
  $var = 1;
}
  1. Spacing between functions
function fn1()
{
}

function fn2()
{
}
  1. Class member spacing
class Foo
{
    private $foo;

    private $bar;
}
  1. Support fluent interface
$obj->add('value 1')
    ->add('value 2')
    ->add('value 3');
  1. Disallow long array syntax
$arr = array(); // not allowedgit a
  1. Forbid final methods in final classes
  2. Remove unused use
  3. Fully qualified global functions
  4. Disallow group use
  5. Disallow multiple use per line
  6. Disallow use from same namespace
  7. Detect useless alias
  8. Reference used name only (SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly)
  9. Detect useless variable declaration
  10. Detect unused variables
  11. Detect unused function parameters
  12. Detect unused inherited variables passed to a closure
  13. Disallow one-line property doc comment