php-cs-fixer
PHP-CS-Fixer is a comprehensive tool that automatically fixes PHP code to follow coding standards and style guidelines. It can repair code style violations and even risky issues, making it an essential formatter for maintaining consistent PHP codebases.
Key Features:
- Automatic Code Fixing: Repairs coding standard violations and formatting issues automatically
- Extensive Rule Set: 200+ built-in rules covering PSR-1, PSR-2, PSR-12, Symfony, and custom standards
- Risky Fixes Support: Can apply potentially risky fixes that modify code behavior when enabled
- Highly Configurable: Flexible configuration through PHP configuration files with rule customization
- Multiple Standards: Supports PSR standards, Symfony coding standards, and custom rule sets
- Dry-Run Mode: Preview changes without modifying files to review proposed fixes
- Incremental Fixing: Can target specific directories, files, or rule sets for focused improvements
- Performance Optimized: Fast execution with caching capabilities for large codebases
- Enable autofixes by adding
PHP_PHPCSFIXER
in APPLY_FIXES variable - Add "--allow-risky=yes" option in PHP_PHPCSFIXER_ARGUMENTS variable
php-cs-fixer documentation
- Version in MegaLinter: 3.83.0
- Visit Official Web Site
- See How to configure php-cs-fixer rules
- If custom
.php-cs-fixer.dist.php
config file isn't found, .php-cs-fixer.dist.php will be used
- If custom
- See How to disable php-cs-fixer rules in files
- See Index of problems detected by php-cs-fixer
Configuration in MegaLinter
- Enable php-cs-fixer by adding
PHP_PHPCSFIXER
in ENABLE_LINTERS variable - Disable php-cs-fixer by adding
PHP_PHPCSFIXER
in DISABLE_LINTERS variable
Variable | Description | Default value |
---|---|---|
PHP_PHPCSFIXER_ARGUMENTS | User custom arguments to add in linter CLI call Ex: -s --foo "bar" |
|
PHP_PHPCSFIXER_COMMAND_REMOVE_ARGUMENTS | User custom arguments to remove from command line before calling the linter Ex: -s --foo "bar" |
|
PHP_PHPCSFIXER_CLI_LINT_MODE | Override default CLI lint mode ⚠️ As default value is project, overriding might not work - file : Calls the linter for each file- list_of_files : Call the linter with the list of files as argument- project : Call the linter from the root of the project |
project |
PHP_PHPCSFIXER_FILE_EXTENSIONS | Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all filesEx: [".py", ""] |
[".php"] |
PHP_PHPCSFIXER_FILE_NAMES_REGEX | File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files Ex: ["Dockerfile(-.+)?", "Jenkinsfile"] |
Include every file |
PHP_PHPCSFIXER_PRE_COMMANDS | List of bash commands to run before the linter | None |
PHP_PHPCSFIXER_POST_COMMANDS | List of bash commands to run after the linter | None |
PHP_PHPCSFIXER_UNSECURED_ENV_VARIABLES | List of env variables explicitly not filtered before calling PHP_PHPCSFIXER and its pre/post commands | None |
PHP_PHPCSFIXER_CONFIG_FILE | php-cs-fixer configuration file nameUse LINTER_DEFAULT to let the linter find it |
.php-cs-fixer.dist.php |
PHP_PHPCSFIXER_RULES_PATH | Path where to find linter configuration file | Workspace folder, then MegaLinter default rules |
PHP_PHPCSFIXER_DISABLE_ERRORS | Run linter but consider errors as warnings | false |
PHP_PHPCSFIXER_DISABLE_ERRORS_IF_LESS_THAN | Maximum number of errors allowed | 0 |
PHP_PHPCSFIXER_CLI_EXECUTABLE | Override CLI executable | ['php-cs-fixer'] |
IDE Integration
Use php-cs-fixer in your favorite IDE to catch errors before MegaLinter !
IDE | Extension Name | Install | |
---|---|---|---|
netbeans | PHP-CS-Fixer | Visit Web Site | |
IDEA | php-cs-fixer | Visit Web Site | |
Sublime Text | sublime-phpcs | Visit Web Site | |
vim | vim-php-cs-fixer | Visit Web Site | |
Visual Studio Code | vscode-php-cs-fixer | Visit Web Site |
MegaLinter Flavors
This linter is available in the following flavors
Flavor | Description | Embedded linters | Info | |
---|---|---|---|---|
![]() |
all | Default MegaLinter Flavor | 126 | |
cupcake | MegaLinter for the most commonly used languages | 87 | ||
php | Optimized for PHP based projects | 54 |
Behind the scenes
How are identified applicable files
- File extensions:
.php
How the linting is performed
php-cs-fixer is called once on the whole project directory (project
CLI lint mode)
- filtering can not be done using MegaLinter configuration variables,it must be done using php-cs-fixer configuration or ignore file (if existing)
VALIDATE_ALL_CODEBASE: false
doesn't make php-cs-fixer analyze only updated files
Example calls
php-cs-fixer check myfile.php
php-cs-fixer check mydir
php-cs-fixer check --config .php-cs-fixer.php
Help content
Setting PHP_CS_FIXER_IGNORE_ENV environment variable is deprecated and will be removed in 4.0, use unsupportedPhpVersionAllowed config instead.
Description:
List commands
Usage:
list [options] [--] [<namespace>]
Arguments:
namespace The namespace name
Options:
--raw To output raw command list
--format=FORMAT The output format (txt, xml, json, or md) [default: "txt"]
--short To skip describing commands' arguments
-h, --help Display help for the given command. When no command is given display help for the list command
--silent Do not output any message
-q, --quiet Only errors are displayed. All other output is suppressed
-V, --version Display this application version
--ansi|--no-ansi Force (or disable --no-ansi) ANSI output
-n, --no-interaction Do not ask any interactive question
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
Help:
The list command lists all commands:
/root/.composer/vendor/bin/php-cs-fixer list
You can also display the commands for a specific namespace:
/root/.composer/vendor/bin/php-cs-fixer list test
You can also output the information in other formats by using the --format option:
/root/.composer/vendor/bin/php-cs-fixer list --format=xml
It's also possible to get raw list of commands (useful for embedding command runner):
/root/.composer/vendor/bin/php-cs-fixer list --raw
Installation on mega-linter Docker image
- Dockerfile commands :
# Parent descriptor install
RUN update-alternatives --install /usr/bin/php php /usr/bin/php84 110
COPY --from=composer/composer:2-bin /composer /usr/bin/composer
ENV PATH="/root/.composer/vendor/bin:${PATH}"
ENV PHP_CS_FIXER_IGNORE_ENV=true
# Linter install
# renovate: datasource=packagist depName=friendsofphp/php-cs-fixer
ARG PHP_FRIENDSOFPHP_PHP_CS_FIXER_VERSION=v3.83.0
RUN GITHUB_AUTH_TOKEN="$(cat /run/secrets/GITHUB_TOKEN)" && export GITHUB_AUTH_TOKEN && composer global require friendsofphp/php-cs-fixer:${PHP_FRIENDSOFPHP_PHP_CS_FIXER_VERSION} --with-all-dependencies