A set of custom fixers for PHP-CS-Fixer.
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
]);
Table of contents:
ErickSkrauch/align_multiline_parameters
- Align multiline function params (or remove alignment).ErickSkrauch/blank_line_around_class_body
- Add space inside class body.ErickSkrauch/blank_line_before_return
- Add blank line beforereturn
.ErickSkrauch/line_break_after_statements
- Add blank line after control structures.ErickSkrauch/multiline_if_statement_braces
- Fix brace position for multilineif
statements.ErickSkrauch/ordered_overrides
- Sort overridden methods.ErickSkrauch/remove_class_name_method_usages
- Replace::className()
with:class
(Yii2).
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 totrue
, forces variables alignment. Onfalse
forces strictly no alignment. You can set it tonull
to disable touching of variables. Default:true
. -
defaults
- when set totrue
, forces defaults alignment. Onfalse
forces strictly no alignment. You can set it tonull
to disable touching of defaults. Default:false
.
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 tofalse
, 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
.
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';
}
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';
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 tofalse
, than curly bracket will be placed right after the last condition statement. Default:true
.
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 appliesimplements
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.
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;