 php-cs-fixer
php-cs-fixer
PHP-CS-Fixer is able to fix rule violations detected, and even those marked as (risky)
- Enable autofixes by adding PHP_PHPCSFIXERin APPLY_FIXES variable
- Add "--allow-risky=yes" option in PHP_PHPCSFIXER_ARGUMENTS variable
php-cs-fixer documentation
- Version in MegaLinter: 3.68.5
- Visit Official Web Site- If custom .php-cs-fixer.dist.phpconfig file isn't found, .php-cs-fixer.dist.php will be used
 
- If custom 
Configuration in MegaLinter
- Enable php-cs-fixer by adding PHP_PHPCSFIXERin ENABLE_LINTERS variable
- Disable php-cs-fixer by adding PHP_PHPCSFIXERin 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_DEFAULTto 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 | 125 | |
| cupcake | MegaLinter for the most commonly used languages | 85 | ||
| php | Optimized for PHP based projects | 53 | 
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: falsedoesn'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
PHP needs to be a minimum version of PHP 7.4.0 and maximum version of PHP 8.3.*.
Current PHP version: 8.4.3.
Ignoring environment requirements because `PHP_CS_FIXER_IGNORE_ENV` is set. Execution may be unstable.
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
RUN GITHUB_AUTH_TOKEN="$(cat /run/secrets/GITHUB_TOKEN)" && export GITHUB_AUTH_TOKEN && composer global require friendsofphp/php-cs-fixer