A set of custom fixers for PHP CS Fixer

v2.5.0 2021-05-04 00:57 UTC


Latest stable version PHP version License Tests Downloads

CI Status Code coverage Mutation testing badge Psalm type coverage

A set of custom fixers for PHP CS Fixer.


PHP CS Fixer: custom fixers can be installed by running:

composer require --dev kubawerlos/php-cs-fixer-custom-fixers


In your PHP CS Fixer configuration register fixers and use them:

 return PhpCsFixer\Config::create()
+    ->registerCustomFixers(new PhpCsFixerCustomFixers\Fixers())
         '@PSR2' => true,
         'array_syntax' => ['syntax' => 'short'],
+        PhpCsFixerCustomFixers\Fixer\NoLeadingSlashInGlobalNamespaceFixer::name() => true,
+        PhpCsFixerCustomFixers\Fixer\PhpdocNoSuperfluousParamFixer::name() => true,



Comments must be surrounded by spaces.

+/* foo */


The configured functions must be commented out. Risky: when any of the configured functions have side effects or are overwritten. Configuration options:

  • functions (array): list of functions to comment out; defaults to ['print_r', 'var_dump', 'var_export']


Data provider names used only once must match the name of the test. Risky: when relying on name of data provider function. Configuration options:

  • prefix (string): prefix that replaces "test"; defaults to 'provide'
  • suffix (string): suffix to be added at the end"; defaults to 'Cases'
 class FooTest extends TestCase {
-     * @dataProvider dataProvider
+     * @dataProvider provideHappyPathCases
     public function testHappyPath() {}
-    public function dataProvider() {}
+    public function provideHappyPathCases() {}


The types returned by data providers must be iterable. Risky: when relying on signature of data provider.

 class FooTest extends TestCase {
      * @dataProvider provideHappyPathCases
     public function testHappyPath() {}
-    public function provideHappyPathCases(): array {}
+    public function provideHappyPathCases(): iterable {}


Data providers must be static.

 class FooTest extends TestCase {
      * @dataProvider provideHappyPathCases
     public function testHappyPath() {}
-    public function provideHappyPathCases() {}
+    public static function provideHappyPathCases() {}


Classes defined internally by extension or core must be referenced with the correct case.

-$foo = new STDClass();
+$foo = new stdClass();


Multiline comments or PHPDocs must contain an opening and closing line with no additional content.

-/** Hello
+ * Hello
  * World!


There can be no commented out code.



There can be no comments generated by Doctrine Migrations.

 namespace Migrations;
 use Doctrine\DBAL\Schema\Schema;
- * Auto-generated Migration: Please modify to your needs!
- */
 final class Version20180609123456 extends AbstractMigration
     public function up(Schema $schema)
-        // this up() migration is auto-generated, please modify it to your needs
         $this->addSql("UPDATE t1 SET col1 = col1 + 1");
     public function down(Schema $schema)
-        // this down() migration is auto-generated, please modify it to your needs
         $this->addSql("UPDATE t1 SET col1 = col1 - 1");


There can be no duplicate array keys. Configuration options:

  • ignore_expressions (bool): whether to keep duplicated expressions (as they might return different values) or not; defaults to true
 $x = [
-    "foo" => 1,
     "bar" => 2,
     "foo" => 3,


There can be no duplicate use statements.

 namespace FooBar;
 use Foo;
-use Foo;
 use Bar;


There can be no imports from the global namespace.

 namespace Foo;
-use DateTime;
 class Bar {
-    public function __construct(DateTime $dateTime) {}
+    public function __construct(\DateTime $dateTime) {}


Classes in the global namespace cannot contain leading slashes.

-$x = new \Foo();
+$x = new Foo();
 namespace Bar;
 $y = new \Baz();


There can be no nullable boolean types. Risky: when the null is used.

-function foo(?bool $bar) : ?bool
+function foo(bool $bar) : bool
      return $bar;


There can be no comments generated by PhpStorm.

- * Created by PhpStorm.
- * User: root
- * Date: 01.01.70
- * Time: 12:00
- */
 namespace Foo;


There can be no comments generated by PhpStorm. Risky: when rely on reference.

-function foo(&$x) {}
+function foo($x) {}


There should be no superfluous concatenation of strings. Configuration options:

  • allow_preventing_trailing_spaces (bool): whether to keep concatenation if it prevents having trailing spaces in string; defaults to false
-echo 'foo' . 'bar';
+echo 'foobar';


There must be no useless comments.

- * Class Foo
  * Class to do something
 class Foo {
-     * Get bar
     function getBar() {}


There can be no comments generated by Doctrine ORM.

- * FooRepository
- *
- * This class was generated by the Doctrine ORM. Add your own custom
- * repository methods below.
- */
 class FooRepository extends EntityRepository {}


There can be no useless parentheses.



There must be no sprintf calls with only the first argument. DEPRECATED: use no_useless_sprintf instead. Risky: when the sprintf function is overridden.

-$foo = sprintf('bar');
+$foo = 'bar';


The strlen or mb_strlen functions should not be compared against 0. Risky: when the function strlen is overridden.

-$isEmpty = strlen($string) === 0;
-$isNotEmpty = strlen($string) > 0;
+$isEmpty = $string === '';
+$isNotEmpty = $string !== '';


Numeric literals must have configured separators. Configuration options:

  • binary (bool, null): whether add, remove or ignore separators in binary numbers.; defaults to false
  • decimal (bool, null): whether add, remove or ignore separators in decimal numbers.; defaults to false
  • float (bool, null): whether add, remove or ignore separators in float numbers.; defaults to false
  • hexadecimal (bool, null): whether add, remove or ignore separators in hexadecimal numbers.; defaults to false
  • octal (bool, null): whether add, remove or ignore separators in octal numbers.; defaults to false
-echo 0b01010100_01101000; // binary
-echo 135_798_642; // decimal
-echo 1_234.456_78e-4_321; // float
-echo 0xAE_B0_42_FC; // hexadecimal
-echo 0123_4567; // octal
+echo 0b0101010001101000; // binary
+echo 135798642; // decimal
+echo 1234.45678e-4321; // float
+echo 0xAEB042FC; // hexadecimal
+echo 01234567; // octal


Operators - when multiline - must always be at the beginning or at the end of the line. DEPRECATED: use operator_linebreak instead. Configuration options:

  • only_booleans (bool): whether to limit operators to only boolean ones; defaults to false
  • position ('beginning', 'end'): whether to place operators at the beginning or at the end of the line; defaults to 'beginning'
 function foo() {
-    return $bar ||
-        $baz;
+    return $bar
+        || $baz;


PHPUnit fail, markTestIncomplete and markTestSkipped functions should not be followed directly by return. Risky: when original PHPUnit methods are overwritten.

 class FooTest extends TestCase {
     public function testFoo() {
-        return;


The @var annotations must be used correctly in code.

-/** @var Foo $foo */
 $bar = new Foo();


There must be no superfluous parameters in PHPDoc.

  * @param bool $b
- * @param int $i
  * @param string $s this is string
- * @param string $s duplicated
 function foo($b, $s) {}


Only the listed annotations are allowed in PHPDoc. Configuration options:

  • elements (array): list of annotations to keep in PHPDoc; defaults to []
  * @author John Doe
- * @package foo
- * @subpackage bar
  * @version 1.0
 function foo_bar() {}


The @param annotations must be in the same order as the function parameters.

+ * @param int $a
  * @param int $b
- * @param int $a
  * @param int $c
 function foo($a, $b, $c) {}


The @param annotations must have a type.

  * @param string $foo
- * @param        $bar
+ * @param mixed  $bar
 function a($foo, $bar) {}


In PHPDoc, the class or interface element self should be preferred over the class name itself.

 class Foo {
-     * @var Foo
+     * @var self
      private $instance;


The @var annotations must be on a single line if they are the only content.

 class Foo {
-    /**
-     * @var string
-     */
+    /** @var string */
     private $name;


PHPDoc types must be trimmed.

- * @param null | string $x
+ * @param null|string $x
 function foo($x) {}


Statements not followed by a semicolon must be followed by a single space. Configuration options:

  • allow_linebreak (bool): whether to allow statement followed by linebreak; defaults to false
-$foo = new    Foo();
+$foo = new Foo();
+echo $foo->bar();


Statements not preceded by a line break must be preceded by a single space.

-$foo =new Foo();
+$foo = new Foo();


A class that implements the __toString () method must explicitly implement the Stringable interface.

-class Foo
+class Foo implements \Stringable
    public function __toString()
         return "Foo";


Request feature or report bug by creating issue.

Alternatively, fork the repo, develop your changes, regenerate README.md:

php ./dev-tools/readme > ./README.md

make sure all checks pass:

composer verify
composer infection

and submit pull request.