php-cs-fixer
Fix PHP code style automatically
TLDR
Execute code style fixing in the current directory
Execute code style fixing for a specific directory
Execute code style linting without applying changes
Execute code style fixes using specific rules
Display the rules that have been applied
Output the results in a different format
Display files that require fixing
Describe a rule or ruleset
SYNOPSIS
php-cs-fixer <command> [options] [arguments]
Commonly used for fixing code:
php-cs-fixer fix <path> [options]
PARAMETERS
<path>
The path to the file or directory to fix. Can be relative or absolute. Multiple paths can be specified.
--config=<file>
Path to the configuration file. Defaults to .php-cs-fixer.dist.php or .php-cs-fixer.php in the current directory. This file defines rulesets, cache file location, and directories to ignore.
--rules=<rules>
A comma-separated list of rules to apply. Rules can be prefixed with - to disable them. Example: --rules=@PSR12,-binary_operator_spaces. Overrides rules defined in the config file if specified.
--dry-run
Only shows what files would be fixed, without actually modifying them. Useful for previewing changes before applying them.
--diff
Shows the diff of the changes that would be applied. Requires --dry-run to be used if no actual changes are desired.
--path-mode=<override|intersection>
Specifies how paths provided via CLI interact with paths defined in the config file. override (default) replaces, intersection combines.
--allow-risky=<yes|no>
Whether to allow or disallow risky rules. Risky rules might change code behavior. Defaults to no unless explicitly allowed in the config file.
--cache-file=<file>
Path to the cache file. Defaults to .php-cs-fixer.cache. Caching improves performance on subsequent runs by only processing changed files.
--using-cache=<yes|no>
Enables or disables the use of the cache. Setting to no forces processing of all files.
--verbose, -v
Increases verbosity of messages. Use multiple times for more verbosity (e.g., -vvv).
--quiet, -q
Do not output any message.
--stop-on-error
Stop processing files on the first error encountered.
--help, -h
Display help for the command.
--version, -V
Display the application version.
DESCRIPTION
php-cs-fixer is a powerful command-line tool that automatically fixes PHP coding standard issues in your codebase. It ensures consistency and adherence to various coding standards like PSR-1, PSR-2, PSR-12, Symfony, and many others. By analyzing your PHP files, it applies a set of configurable "fixers" (rules) to modify the code in place, correcting formatting, spacing, syntax, and structural issues.
This tool helps maintain high code quality across projects and teams, reducing manual refactoring efforts and facilitating collaborative development. It can be integrated into CI/CD pipelines or Git hooks to enforce standards continuously.
CAVEATS
Applying php-cs-fixer, especially with risky rules enabled, can sometimes lead to unexpected changes or even break code if not properly understood or tested. It is highly recommended to run the fixer with the --dry-run --diff options first, and always commit your code to version control before applying fixes, to easily revert changes if necessary. Ensure you have a backup or are working in a version-controlled environment.
CONFIGURATION FILES
The behavior of php-cs-fixer is largely controlled by a configuration file, typically named .php-cs-fixer.dist.php or .php-cs-fixer.php. This file, written in PHP, allows you to:
1. Define specific rules or rulesets (e.g., @PSR12, @Symfony).
2. Exclude files or directories from being fixed.
3. Specify the cache file location.
4. Set options like allow-risky.
Using a configuration file ensures consistent application of rules across different environments and team members.
INTEGRATION
php-cs-fixer can be integrated into various development workflows:
1. CI/CD Pipelines: Automatically run the fixer (often with --dry-run --diff --stop-on-error for validation) as part of your continuous integration process to ensure all committed code adheres to standards.
2. Git Hooks: Use pre-commit hooks to automatically fix or check staged files before a commit is made, preventing non-compliant code from entering the repository.
3. IDE Integration: Many modern IDEs (like PhpStorm, VS Code) offer plugins or built-in support to run php-cs-fixer on file save or as a linter, providing instant feedback and auto-fixing capabilities.
HISTORY
php-cs-fixer was originally created by Fabien Potencier, the creator of the Symfony framework, to enforce Symfony's coding standards. Over time, it evolved into a standalone, general-purpose PHP code formatter and linter, supporting a wide array of coding standards (PSR-1, PSR-2, PSR-12, etc.) and custom rules. Its development is community-driven, with continuous updates and contributions, making it a widely adopted and essential tool in the PHP ecosystem for maintaining code quality and consistency.