erickskrauch / php-cs-fixer-custom-fixers
A set of custom fixers for PHP-CS-Fixer
Installs: 737 947
Dependents: 10
Suggesters: 0
Security: 0
Stars: 20
Watchers: 4
Forks: 1
Open Issues: 2
Requires
- php: ^7.4 || ^8.0
- friendsofphp/php-cs-fixer: ^3
Requires (Dev)
- ely/php-code-style: ^1
- ergebnis/composer-normalize: ^2.28
- php-cs-fixer/phpunit-constraint-isidenticalstring: ^1.2
- phpspec/prophecy: ^1.15
- phpspec/prophecy-phpunit: ^2.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.11.x-dev
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/phpunit: ^9.5
- phpunitgoodpractices/polyfill: ^1.5
- phpunitgoodpractices/traits: ^1.9.1
- symfony/phpunit-bridge: ^6.0
README
A set of custom fixers for PHP-CS-Fixer.
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
- 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).
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 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
.
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 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
.
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 tofalse
, 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 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.
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;