The PHP Coding Standards Fixer tool fixes most issues in your code when you want to follow the PHP coding standards as defined in the PSR-1 and PSR-2 documents and many more.
If you are already using a linter to identify coding standards problems in your code, you know that fixing them by hand is tedious, especially on large projects. This tool does not only detect them, but also fixes them for you.
PHP needs to be a minimum version of PHP 5.3.6.
You can run these commands to easily access
php-cs-fixer from anywhere on
$ wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v1.12.3/php-cs-fixer.phar -O php-cs-fixer
or with curl:
$ curl -L https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v1.12.3/php-cs-fixer.phar -o php-cs-fixer
$ sudo chmod a+x php-cs-fixer $ sudo mv php-cs-fixer /usr/local/bin/php-cs-fixer
Then, just run
To install PHP CS Fixer, install Composer and issue the following command:
$ ./composer.phar global require friendsofphp/php-cs-fixer
Please be aware that before v1.12 package name was different:
$ ./composer.phar global require fabpot/php-cs-fixer
Then make sure you have
~/.composer/vendor/bin in your
you're good to go:
$ export PATH="$PATH:$HOME/.composer/vendor/bin"
self-update command tries to update
$ php php-cs-fixer.phar self-update
You can update
php-cs-fixer through this command:
$ sudo php-cs-fixer self-update
You can update
php-cs-fixer through this command:
$ ./composer.phar global update friendsofphp/php-cs-fixer
You can update
php-cs-fixer through this command:
$ brew upgrade php-cs-fixer
fix command tries to fix as much coding standards
problems as possible on a given file or files in a given directory and its subdirectories:
$ php php-cs-fixer.phar fix /path/to/dir $ php php-cs-fixer.phar fix /path/to/file
--format option for the output format. Supported formats are
txt (default one),
--verbose option will show the applied fixers. When using the
txt format it will also displays progress notifications.
--level option limits the fixers to apply on the
1 2 3 4
$ php php-cs-fixer.phar fix /path/to/project --level=psr0 $ php php-cs-fixer.phar fix /path/to/project --level=psr1 $ php php-cs-fixer.phar fix /path/to/project --level=psr2 $ php php-cs-fixer.phar fix /path/to/project --level=symfony
By default, all PSR-2 fixers and some additional ones are run. The "contrib
level" fixers cannot be enabled via this option; you should instead set them
manually by their name via the
--fixers option lets you choose the exact fixers to
apply (the fixer names must be separated by a comma):
$ php php-cs-fixer.phar fix /path/to/dir --fixers=linefeed,short_tag,indentation
You can also blacklist the fixers you don't want by placing a dash in front of the fixer name, if this is more convenient,
$ php php-cs-fixer.phar fix /path/to/dir --fixers=-short_tag,-indentation
When using combination with exact and blacklist fixers, apply exact fixers along with above blacklisted result:
$ php php-cs-fixer.phar fix /path/to/dir --fixers=linefeed,-short_tag
A combination of
display summary of proposed fixes, leaving your files unchanged.
The command can also read from standard input, in which case it won't automatically fix anything:
$ cat foo.php | php php-cs-fixer.phar fix --diff -
Choose from the list of available fixers:
Classes must be in a path that matches their namespace, be at least one namespace deep, and the class name should match the file name.
PHP code MUST use only UTF-8 without BOM (remove BOM).
PHP code must use the long <?php ?> tags or the short-echo <?= ?> tags; it must not use the other tag variations.
The body of each structure MUST be enclosed by braces. Braces should be properly placed. Body of braces should be properly indented.
Whitespace around the key words of a class, trait or interfaces definition should be one space.
The keyword elseif should be used instead of else if so that all control keywords looks like single words.
A file must always end with a single empty line feed.
When making a method or function call, there MUST NOT be a space between the method or function name and the opening parenthesis.
Spaces should be properly placed in a function declaration.
Code MUST use an indent of 4 spaces, and MUST NOT use tabs for indenting.
There MUST be one blank line after the namespace declaration.
All PHP files must use the Unix LF (linefeed) line ending.
The PHP constants true, false, and null MUST be in lower case.
PHP keywords MUST be in lower case.
In method arguments and method call, there MUST NOT be a space before each comma and there MUST be one space after each comma.
There MUST be one use keyword per declaration.
There MUST be no trailing spaces inside comments and phpdocs.
There MUST NOT be a space after the opening parenthesis. There MUST NOT be a space before the closing parenthesis.
The closing ?> tag MUST be omitted from files containing only PHP.
Each namespace use MUST go on its own line and there MUST be one blank line after the use statements block.
A case should be followed by a colon and not a semicolon.
Removes extra spaces between colon and case value.
Remove trailing whitespace at the end of non-blank lines.
Visibility MUST be declared on all properties and methods; abstract and final MUST be declared before the visibility; static MUST be declared after the visibility.
In array declaration, there MUST NOT be a whitespace before each comma.
In array declaration, there MUST be a whitespace after each comma.
Ensure there is no code on the same line as the PHP open tag and it is followed by a blankline.
Concatenation should be used without spaces.
Equal sign in declare statement should not be surrounded by spaces.
Operator => should not be surrounded by multi-line whitespaces.
Remove duplicated semicolons.
Removes extra empty lines.
Add missing space between function's argument and its typehint.
Single line comments should use double slashes (//) and not hash (#).
Convert heredoc to nowdoc if possible.
Include/Require and file path should be divided with a single space. File path should not be placed under brackets.
Implode function should be used instead of join function.
Remove trailing commas in list function calls.
Cast should be written in lower case.
In method arguments there must not be arguments with default values before non-default ones.
PHP multi-line arrays should have a trailing comma.
The namespace declaration line shouldn't contain leading whitespace.
Function defined by PHP should be called using the correct casing.
All instances created with new keyword must be followed by braces.
There should be no empty lines after class opening brace.
There should not be an empty comments.
There should not be blank lines between docblock and the documented element.
There should not be empty PHPDoc blocks.
Remove useless semicolon statements.
There should not be space before or after object T_OBJECT_OPERATOR.
Binary operators should be surrounded by at least one space.
Phpdocs annotation descriptions should not end with a full stop.
Docblocks should have the same indentation as the documented subject.
Fix phpdoc inline tags, make inheritdoc always inline.
@access annotations should be omitted from phpdocs.
@return void and @return null annotations should be omitted from phpdocs.
@package and @subpackage annotations should be omitted from phpdocs.
All items of the @param, @throws, @return, @var, and @type phpdoc tags must be aligned vertically.
Scalar types should always be written in the same form. "int", not "integer"; "bool", not "boolean"; "float", not "real" or "double".
Annotations in phpdocs should be grouped together so that annotations of the same type immediately follow each other, and annotations of a different type are separated by a single blank line.
Phpdocs short descriptions should end in either a full stop, exclamation mark, or question mark.
Single line @var PHPDoc should have proper spacing.
Docblocks should only be used on structural elements.
Phpdocs should start and end with content, excluding the very first and last line of the docblocks.
@type should always be written as @var.
The correct case must be used for standard PHP types in phpdoc.
@var and @type annotations should not contain the variable name.
Pre incrementation/decrementation should be used if possible.
Converts print language construct to echo if possible.
Remove leading slashes in use clauses.
Removes line breaks between use statements.
An empty line feed should precede a return statement.
Inside a classy element "self" should be preferred to the class name itself.
Short cast bool using double exclamation mark should not be used.
Cast "(boolean)" and "(integer)" should be written as "(bool)" and "(int)". "(double)" and "(real)" as "(float)".
PHP single-line arrays should not have trailing comma.
There should be exactly one blank line before a namespace declaration.
Convert double quotes to single quotes for simple strings.
Fix whitespace after a semicolon.
Single-line whitespace before closing semicolon are prohibited.
A single space should be between cast and variable.
Replace all <> with !=.
Standardize spaces around ternary operator.
Arrays should be formatted like function/method arguments, without leading or trailing single line space.
Unalign double arrow symbols.
Unalign equals symbols.
Unary operators should be placed adjacent to their operands.
Removes unneeded parentheses around control statements.
Unused use statements must be removed.
Remove trailing whitespace at the end of blank lines.
Align double arrow symbols in consecutive lines.
Align equals symbols in consecutive lines.
Converts ::class keywords to FQCN strings.
Calling unset on multiple items should be done in one call.
Concatenation should be used with at least one whitespace around.
Converts echo language construct to print if possible.
A return statement wishing to return nothing should be simply "return".
Replace deprecated ereg regular expression functions with preg. Warning! This could change code behavior.
Add, replace or remove header comment.
Logical NOT operators (!) should have leading and trailing whitespaces.
Logical NOT operators (!) should have one trailing whitespace.
Arrays should use the long syntax.
Replace non multibyte-safe functions with corresponding mb function. Warning! This could change code behavior.
Multi-line whitespace before closing semicolon are prohibited.
Ensure there is no code on the same line as the PHP open tag.
There should be no blank lines before a namespace declaration.
There should not be useless else cases.
There should not be an empty return statement at the end of a function.
Ordering use statements.
Convert PHP4-style constructors to __construct. Warning! This could change code behavior.
PHPUnit assertion method calls like "->assertSame(true, $foo)" should be written with dedicated method like "->assertTrue($foo)". Warning! This could change code behavior.
PHPUnit assertions like "assertInternalType", "assertFileExists", should be used over "assertTrue". Warning! This could change code behavior.
PHPUnit methods like "assertSame" should be used instead of "assertEquals". Warning! This could change code behavior.
Annotations in phpdocs should be ordered so that param annotations come first, then throws annotations, then return annotations.
@var should always be written as @type.
PHP arrays should use the PHP 5.4 short-syntax.
Replace short-echo <?= with long format <?php echo syntax.
Ensures deprecation notices are silenced. Warning! This could change code behavior.
Comparison should be strict. Warning! This could change code behavior.
Functions should be used with $strict param. Warning! This could change code behavior.
--config option customizes the files to analyse, based
on some well-known directory structures:
# For the Symfony 2.3+ branch $ php php-cs-fixer.phar fix /path/to/sf23 --config=sf23
Choose from the list of available configurations:
--dry-run option displays the files that need to be
fixed but without actually modifying them:
$ php php-cs-fixer.phar fix /path/to/code --dry-run
Instead of using command line options to customize the fixer, you can save the
configuration in a
.php_cs file in the root directory of
your project. The file must return an instance of
Symfony\CS\ConfigInterface, which lets you configure the fixers, the level, the files,
and directories that need to be analyzed. The example below will add two contrib fixers
to the default list of symfony-level fixers:
1 2 3 4 5 6 7 8 9 10 11 12
<?php $finder = Symfony\CS\Finder::create() ->exclude('somedir') ->notPath('src/Symfony/Component/Translation/Tests/fixtures/resources.php') ->in(__DIR__) ; return Symfony\CS\Config::create() ->fixers(array('strict_param', 'short_array_syntax')) ->finder($finder) ;
exclude will work only for directories, so if you need to exclude file, try
See Symfony\Finder online documentation for other Finder methods.
If you want complete control over which fixers you use, you may use the empty level and then specify all fixers to be used:
1 2 3 4 5 6 7 8 9 10 11
<?php $finder = Symfony\CS\Finder::create() ->in(__DIR__) ; return Symfony\CS\Config::create() ->level(Symfony\CS\FixerInterface::NONE_LEVEL) ->fixers(array('trailing_spaces', 'encoding')) ->finder($finder) ;
You may also use a blacklist for the Fixers instead of the above shown whitelist approach.
The following example shows how to use all
symfony Fixers but the
Note the additional
- in front of the Fixer name.
1 2 3 4 5 6 7 8 9 10 11
<?php $finder = Symfony\CS\Finder::create() ->exclude('somedir') ->in(__DIR__) ; return Symfony\CS\Config::create() ->fixers(array('-psr0')) ->finder($finder) ;
symfony level is set by default, you can also change the default level:
1 2 3 4 5
<?php return Symfony\CS\Config::create() ->level(Symfony\CS\FixerInterface::PSR2_LEVEL) ;
In combination with these config and command line options, you can choose various usage.
For example, default level is
symfony, but if you also don't want to use
psr0 fixer, you can specify the
But if you use the
--fixers option with only exact fixers,
only those exact fixers are enabled whether or not level is set.
--config-file option you can specify the path to the
You can enable caching by returning a custom config with caching enabled. This will speed up further runs.
1 2 3 4 5
<?php return Symfony\CS\Config::create() ->setUsingCache(true) ;
The tool comes with quite a few built-in fixers and finders, but everyone is more than welcome to contribute more of them.
A fixer is a class that tries to fix one CS issue (a
Fixer class must
A config knows about the CS level and the files and directories that must be scanned by the tool when run in the directory of your project. It is useful for projects that follow a well-known directory structures (like for Symfony projects for instance).