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 http://get.sensiolabs.org/php-cs-fixer.phar -O php-cs-fixer
or with curl:
$ curl http://get.sensiolabs.org/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 fabpot/php-cs-fixer
Then make sure you have
~/.composer/vendor/bin in your
you're good to go:
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 fabpot/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 can be used to set the output format of the results;
txt (default one),
--verbose option will show the applied fixers. When using the
txt format it will also displays progress notifications.
--rules option limits the rules to apply on the
php php-cs-fixer.phar fix /path/to/project --rules=@PSR2
By default, all PSR fixers are run.
--rules 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 --rules=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 --rules=-short_tag,-indentation
When using combinations of exact and blacklist fixers, applying exact fixers along with above blacklisted results:
php php-cs-fixer.phar fix /path/to/project --rules=@Symfony,-@PSR1,-return,strict
A combination of
display a summary of proposed fixes, leaving your files unchanged.
--allow-risky option allows you to set whether riskys fixer may run. Default value is taken from config file.
Risky fixer is a fixer, which could change code behaviour. By default no risky fixers are run.
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:
Master functions shall be used instead of aliases.
Align double arrow symbols in consecutive lines.
Align equals symbols in consecutive lines.
In array declaration, there MUST NOT be a space before each comma.
In array declaration, there MUST be a white wspace after each comma.
Ensure there is no code on the same line as the PHP open tag and it is followed by a blankline.
The body of each structure MUST be enclosed by braces. Braces should be properly placed. Body of braces should be properly indented.
Concatenation should be used with at least one whitespace around.
Concatenation should be used without spaces.
Operator => should not be surrounded by multi-line whitespaces.
Remove duplicated semicolons.
The keyword elseif should be used instead of else if so that all control keywords look like single words.
A return statement wishing to return nothing should be simply "return".
PHP code MUST use only UTF-8 without BOM (remove BOM).
A file must always end with a single empty line feed.
Replace deprecated ereg regular expression functions with preg. (Risky fixer!)
Removes extra empty lines.
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.
Add missing space between function's argument and its typehint.
Add, replace or remove header comment.
Include and file path should be divided with a single space. File path should not be placed under brackets.
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.
Remove trailing commas in list function calls.
Logical NOT operators (!) should have leading and trailing whitespaces.
Logical NOT operators (!) should have one trailing whitespace.
Arrays should use the long syntax.
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.
Methods must be separated with one blank line.
PHP multi-line arrays should have a trailing comma.
Multi-line whitespace before closing semicolon are prohibited.
There MUST be one use keyword per declaration.
The namespace declaration line shouldn't contain leading whitespace.
All instances created with new keyword must be followed by braces.
Ensure there is no code on the same line as the PHP open tag.
There should be no empty lines after class opening brace.
There should be no blank lines before a namespace declaration.
There should not be blank lines between docblock and the documented element.
There should not be space before or after object T_OBJECT_OPERATOR.
Binary operators should be surrounded by at least one space.
Ordering use statements.
There MUST NOT be a space after the opening parenthesis. There MUST NOT be a space before the closing parenthesis.
Convert PHP4-style constructors to __construct. (Risky fixer!)
The closing ?> tag MUST be omitted from files containing only PHP.
PHPUnit assertion method calls like "->assertSame(true, $foo)" should be written with dedicated method like "->assertTrue($foo)". (Risky fixer!)
PHPUnit methods like "assertSame" should be used instead of "assertEquals". (Risky fixer!)
All items of the @param, @throws, @return, @var, and @type phpdoc tags must be aligned vertically.
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.
Annotations in phpdocs should be ordered so that param annotations come first, then throws annotations, then return annotations.
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 summary should end in either a full stop, exclamation mark, or question mark.
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 should always be written as @type.
@var and @type annotations should not contain the variable name.
Pre incrementation/decrementation should be used if possible.
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. (Risky fixer!)
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.
PHP arrays should use the PHP 5.4 short-syntax.
Replace short-echo <?= with long format <?php echo syntax.
PHP code must use the long <?php ?> tags or the short-echo <?= ?> tags; it must not use the other tag variations.
PHP single-line arrays should not have trailing comma.
There should be exactly one blank line before a namespace declaration.
Each namespace use MUST go on its own line and there MUST be one blank line after the use statements block.
Convert double quotes to single quotes for simple strings.
Single-line whitespace before closing semicolon are prohibited.
A single space should be between cast and variable.
Replace all <> with !=.
Comparison should be strict. (Risky fixer!)
Functions should be used with $strict param. (Risky fixer!)
Standardize spaces around ternary operator.
Remove trailing whitespace at the end of non-blank lines.
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.
Unused use statements must be removed.
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.
Remove trailing whitespace at the end of blank lines.
--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
project configuration in a
.php_cs.dist file in the root directory
of your project. The file must return an instance of
which lets you configure the rules, the files and directories that
need to be analyzed. You may also create
.php_cs file, which is
the local configuration that will be used instead of the project configuration. It
is a good practice to add that file into your
--config-file option you can specify the path to the
The example below will add two fixers to the default list of PSR2 set fixers:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php $finder = Symfony\CS\Finder\DefaultFinder::create() ->exclude('somedir') ->in(__DIR__) ; return Symfony\CS\Config\Config::create() ->setRules(array( '@PSR2' => true, 'strict_param' => true, 'short_array_syntax' => true, )) ->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
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php $finder = Symfony\CS\Finder\DefaultFinder::create() ->exclude('somedir') ->in(__DIR__) ; return Symfony\CS\Config\Config::create() ->setRules(array( '@Symfony' => true, 'short_tag' => false, )) ->finder($finder) ;
--using-cache option with yes or no you can set if the caching
mechanism should be used.
The caching mechanism is enabled by default. This will speed up further runs by fixing only files that were modified since the last run. The tool will fix all files if the tool version has changed or the list of fixers has changed. Cache is supported only for tool downloaded as phar file or installed via composer.
Cache can be disabled via
--using-cache option or config file:
1 2 3 4 5
<?php return Symfony\CS\Config\Config::create() ->setUsingCache(false) ;
Cache file can be specified via
--cache-file option or config file:
1 2 3 4 5
<?php return Symfony\CS\Config\Config::create() ->setCacheFile(__DIR__.'/.php_cs.cache') ;
fabpot/php-cs-fixer as a dev` dependency:
$ ./composer.phar require --dev fabpot/php-cs-fixer
Create a build file to run
php-cs-fixer on Travis. It's advisable to create a dedicated directory
for PHP CS Fixer cache files and have Travis cache it between builds.
1 2 3 4 5 6 7 8 9 10 11 12
language: php php: - 5.5 sudo: false cache: directories: - "$HOME/.composer/cache" - "$HOME/.php-cs-fixer" before_script: - mkdir -p "$HOME/.php-cs-fixer" script: - vendor/bin/php-cs-fixer fix --cache-file "$HOME/.php-cs-fixer/.php_cs.cache" --dry-run --diff --verbose
Note: This will only trigger a build if you have a subscription for Travis or are using their free open source plan.
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 rules 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).