Skip to content

erickskrauch/php-cs-fixer-custom-fixers

Repository files navigation

PHP-CS-Fixer custom fixers

A set of custom fixers for PHP-CS-Fixer.

Latest Version on Packagist Total Downloads Software License Build Status

Installation

Run:

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

Then in your configuration file (.php-cs-fixer.php) register fixers and use them:

<?php
return (new \PhpCsFixer\Config())
    ->registerCustomFixers(new \ErickSkrauch\PhpCsFixer\Fixers())
    ->setRules([
        'ErickSkrauch/align_multiline_parameters' => true,
        // See the rest of the fixers below
    ]);

Fixers

Table of contents:

ErickSkrauch/align_multiline_parameters

Forces aligned or not aligned multiline function parameters:

--- Original
+++ New
@@ @@
  function foo(
      string $string,
-     int $index = 0,
-     $arg = 'no type',
-     ...$variadic,
+     int    $index    = 0,
+            $arg      = 'no type',
+         ...$variadic
  ): void {}

Configuration:

  • variables - when set to true, forces variables alignment. On false forces strictly no alignment. You can set it to null to disable touching of variables. Default: true.

  • defaults - when set to true, forces defaults alignment. On false forces strictly no alignment. You can set it to null to disable touching of defaults. Default: false.

ErickSkrauch/blank_line_around_class_body

Ensure that a class body contains one blank line after its definition and before its end:

--- Original
+++ New
@@ @@
  <?php
  class Test {
+
      public function func() {
          $obj = new class extends Foo {
+
              public $prop;
+
          }
      }
+
  }

Configuration:

  • apply_to_anonymous_classes - should this fixer be applied to anonymous classes? If it is set to false, than anonymous classes will be fixed to don't have empty lines around body. Default: true.

  • blank_lines_count - adjusts an amount of the blank lines. Default: 1.

ErickSkrauch/blank_line_before_return

This is extended version of the original blank_line_before_statement fixer. It applies only to return statements and only in cases, when on the current nesting level more than one statements.

--- Original
+++ New
@@ @@
 <?php
 public function foo() {
     $a = 'this';
     $b = 'is';
+
     return "$a $b awesome";
 }

 public function bar() {
     $this->foo();
     return 'okay';
 }

ErickSkrauch/line_break_after_statements

Ensures that there is one blank line above the next statements: if, switch, for, foreach, while, do-while and try-catch-finally.

--- Original
+++ New
@@ @@
 <?php
 $a = 123;
 if ($a === 123) {
     // Do something here
 }
+
 $b = [1, 2, 3];
 foreach ($b as $number) {
     if ($number === 3) {
         echo 'it is three!';
     }
 }
+
 $c = 'next statement';

ErickSkrauch/multiline_if_statement_braces

Ensures that multiline if statement body curly brace placed on the right line.

--- Original
+++ New
@@ @@
 <?php
 if ($condition1 === 123
- && $condition2 = 321) {
+ && $condition2 = 321
+) {
     // Do something here
 }

Configuration:

  • keep_on_own_line - should this place closing bracket on its own line? If it's set to false, than curly bracket will be placed right after the last condition statement. Default: true.

ErickSkrauch/ordered_overrides

Overridden and implemented methods must be sorted in the same order as they are defined in parent classes.

--- Original
+++ New
@@ @@
 <?php
 class Foo implements Serializable {

-    public function unserialize($data) {}
+    public function serialize() {}

-    public function serialize() {}
+    public function unserialize($data) {}

 }

Caveats:

  • This fixer is implemented against the PHP-CS-Fixer principle and relies on runtime, classes autoloading and reflection. If dependencies are missing or the autoloader isn't configured correctly, the fixer will not be able to discover the order of methods in parents.

  • Fixer prioritizes extends and applies implements afterwards. It searches for the deepest parents of classes and takes them as the basis for sorting, ignoring later reordering.

  • This fixer runs BEFORE the ordered_interfaces fixer, so you might need to run PHP-CS-Fixer twice when you're using this fixer to get proper result. See this discussion for more info.

ErickSkrauch/remove_class_name_method_usages (Yii2)

Replaces Yii2 BaseObject::className() usages with native ::class keyword, introduced in PHP 5.5.

--- Original
+++ New
@@ @@
  <?php
  use common\models\User;
  
- $className = User::className();
+ $className = User::class;