Guide · Open source
How to use PHP-CS-Fixer: install, fix and check PHP code style
Updated · written with AI from the project's own documentation and checked against our catalogue data
PHP CS Fixer is a command-line tool that finds coding style problems in PHP code and fixes them automatically so the code follows a chosen standard. This guide is for PHP developers and teams who want consistent style without fixing every issue by hand. By the end you will know how to install it with Composer, create a starting configuration, and run the fix and check commands.
Quick start
- 1Check your PHP and Composer versions. Confirm you run PHP 7.4 to 8.5 and have Composer available.
- 2Install the package. Run composer require --dev friendsofphp/php-cs-fixer, or the shim package if dependencies conflict.
- 3Create a config. Run ./vendor/bin/php-cs-fixer init to generate a base configuration.
- 4Choose rule sets. Edit the configuration file to select @PER-CS, @Symfony, @PhpCsFixer or your own rules.
- 5Check your code. Run ./vendor/bin/php-cs-fixer check to see which files need changes.
- 6Fix your code. Run ./vendor/bin/php-cs-fixer fix and review the resulting diff.
What PHP-CS-Fixer does and what you need first
PHP CS Fixer fixes your code to follow coding standards. The README notes that fixing style problems by hand is tedious on large projects, and this tool both detects and fixes them.
It has built-in rule sets: @PER-CS (PHP-FIG's PER Coding Style), @Symfony, and its own opinionated @PhpCsFixer. You can also define your team's style in a configuration file. It can help modernise code toward newer PHP and PHPUnit syntax through rule sets such as @autoPHPMigration and @autoPHPUnitMigration:risky.
- PHP 7.4 to PHP 8.5 is the supported range.
- Composer is required for the recommended installation.
- The tool only targets PHP code.
Install PHP-CS-Fixer with Composer
The recommended way to install is Composer. Install it as a development dependency of your project.
If you hit dependency conflicts, the README suggests an alternative package. The README also points to the installation instructions for other methods, including Docker and CI.
- Recommended package: friendsofphp/php-cs-fixer
- Alternative if dependency conflicts occur: php-cs-fixer/shim
composer require --dev friendsofphp/php-cs-fixer
## or when facing conflicts in dependencies:
composer require --dev php-cs-fixer/shimCreate a starting configuration with init
Assuming you installed it with Composer, the README says you can initialise a base configuration for your project with the init command. The configuration file is where you choose rule sets and define your team's style. See the configuration documentation for the file's details.
./vendor/bin/php-cs-fixer initRun fix to correct code and check to report issues
To automatically fix your project, run fix. To only check whether changes are needed, without modifying files, run check. Use check in continuous integration if you want the build to fail on style problems.
The README points to the usage documentation, the list of built-in rules and the list of rule sets for more options.
- fix: applies the changes to your files.
- check: reports whether changes are needed.
./vendor/bin/php-cs-fixer fix
./vendor/bin/php-cs-fixer checkHandle unsupported PHP versions and risky rules
If you run a PHP version newer than the supported range, the README describes an option to run anyway, but at your own risk. Use it only if you accept that risk.
Some migration rules are marked risky, for example in @autoPHPMigration:risky and @autoPHPUnitMigration:risky. Review their changes before applying them, because they may change behaviour.
- Unsupported PHP: use the --allow-unsupported-php-version=yes option.
- Risky rule sets: review changes before committing them.
Use PHP-CS-Fixer in your editor
The README lists native support in PhpStorm. Community plugins exist for NetBeans, Sublime Text, Vim and VS Code. Check each plugin's own documentation for setup.
- Native: PhpStorm
- Community plugins: NetBeans, Sublime Text, Vim, VS Code
What people use it for
- Keeping style consistent across a team's PHP code without manual review comments.
- Fixing style across a large project where hand-fixing is tedious.
- Following the PER-CS or Symfony coding standards.
- Migrating older PHP or PHPUnit code toward newer syntax, with risky rules reviewed first.
Frequently asked questions
How do I install PHP-CS-Fixer?
The README recommends Composer: run composer require --dev friendsofphp/php-cs-fixer. If you get dependency conflicts, use composer require --dev php-cs-fixer/shim instead. Other methods are described in the installation documentation.
What PHP version does PHP-CS-Fixer need?
The README lists support for PHP 7.4 to PHP 8.5. Running on newer, unsupported versions is possible with the --allow-unsupported-php-version=yes option, but the README says to do so at your own risk.
What is the difference between fix and check?
According to the README, fix automatically corrects your project, while check only reports whether changes are needed. Use check when you want to verify style without changing files.
Is PHP-CS-Fixer free to use?
The project is released under the MIT licence, as listed on its repository. Check the LICENSE file in the repository for the full terms.
How does PHP-CS-Fixer compare to PHP_CodeSniffer?
Both are PHP coding standards tools. PHP-CS-Fixer's README emphasises that it fixes issues automatically, not just detects them. Other comparable projects include PHP_CodeSniffer, Laravel Pint, Psalm and PHPStan, which you can evaluate against your needs.
Can PHP-CS-Fixer use the PSR-12 or PER-CS standard?
PHP-CS-Fixer ships with the @PER-CS rule set, which follows PHP-FIG's PER Coding Style. The README also lists @Symfony and its own @PhpCsFixer set, and you can define your own style in the configuration file.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
PHP-CS-Fixer suits PHP teams and large projects that want automatic, consistent style fixes and possibly a path to newer PHP or PHPUnit syntax. Skip it if your code is not PHP, if you run PHP versions outside 7.4 to 8.5 and do not accept the risk, or if you want a checks-only tool.
Install options & downloads