friendsofphp/php-cs-fixer

A tool to automatically fix PHP code style

Fund package maintenance!
keradus

Installs: 164 824 212

Dependents: 18 561

Suggesters: 65

Security: 0

Stars: 12 866

Watchers: 207

Forks: 1 580

Open Issues: 141

Type:application

v3.64.0 2024-08-30 23:09 UTC

This package is auto-updated.

Last update: 2024-11-13 18:10:48 UTC


README

PHP CS Fixer logo

PHP Coding Standards Fixer

The PHP Coding Standards Fixer (PHP CS Fixer) tool fixes your code to follow standards; whether you want to follow PHP coding standards as defined in the PSR-1, PSR-2, etc., or other community driven ones like the Symfony one. You can also define your (team's) style through configuration.

It can modernize your code (like converting the pow function to the ** operator on PHP 5.6) and (micro) optimize it.

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.

Supported PHP Versions

  • PHP 7.4
  • PHP 8.0
  • PHP 8.1
  • PHP 8.2
  • PHP 8.3

Note Each new PHP version requires a huge effort to support the new syntax. That's why the latest PHP version might not be supported yet. If you need it, please, consider supporting the project in any convenient way, for example with code contribution or reviewing existing PRs. To run PHP CS Fixer on yet unsupported versions "at your own risk" - leverage the PHP_CS_FIXER_IGNORE_ENV.

Documentation

Installation

The recommended way to install PHP CS Fixer is to use Composer in a dedicated composer.json file in your project, for example in the tools/php-cs-fixer directory:

mkdir -p tools/php-cs-fixer
composer require --working-dir=tools/php-cs-fixer friendsofphp/php-cs-fixer

Or using the main composer.json:

composer require --dev friendsofphp/php-cs-fixer

For more details and other installation methods, see installation instructions.

Run with Docker

You can use pre-built Docker images to run php-cs-fixer.

docker run -it --rm -v $(pwd):/code ghcr.io/php-cs-fixer/php-cs-fixer:${FIXER_VERSION:-3-php8.3} fix src

$FIXER_VERSION used in example above is an identifier of a release you want to use, which is based on Fixer and PHP versions combined. There are different tags for each Fixer's SemVer level and PHP version with syntax <php-cs-fixer-version>-php<php-version>. For example:

  • 3.57.0-php7.4
  • 3.57-php8.0
  • 3-php8.3

Usage

Assuming you installed PHP CS Fixer as instructed above, you can run the following command to fix the PHP files in the src directory:

tools/php-cs-fixer/vendor/bin/php-cs-fixer fix src

See usage, list of built-in rules, list of rule sets and configuration file documentation for more details.

If you need to apply code styles that are not supported by the tool, you can create custom rules.

Editor Integration

Dedicated plugins exist for:

Community

The PHP CS Fixer is maintained on GitHub at https://github.com/PHP-CS-Fixer/PHP-CS-Fixer. Bug reports and ideas about new features are welcome there.

You can reach us in the GitHub Discussions regarding the project, configuration, possible improvements, ideas and questions. Please visit us there!

Contribute

The tool comes with quite a few built-in fixers, but everyone is more than welcome to contribute more of them.