php-cs-fixer
PHP-CS-Fixer is able to fix rule violations detected, and even those marked as (risky)
- 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.64.0
- Visit Official Web Site
- If custom
.php-cs-fixer.dist.php
config file isn't found, .php-cs-fixer.dist.php will be used
- If custom
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 | 124 | ||
cupcake | MegaLinter for the most commonly used languages | 83 | ||
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
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
-q, --quiet Do not output any message
-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 GITHUB_AUTH_TOKEN="$(cat /run/secrets/GITHUB_TOKEN)" \
&& export GITHUB_AUTH_TOKEN \
&& wget --tries=5 -q -O phive.phar https://phar.io/releases/phive.phar \
&& wget --tries=5 -q -O phive.phar.asc https://phar.io/releases/phive.phar.asc \
&& PHAR_KEY_ID="0x6AF725270AB81E04D79442549D8A98B29B2D5D79" \
&& ( gpg --keyserver hkps://keys.openpgp.org --recv-keys "$PHAR_KEY_ID" \
|| gpg --keyserver hkps://keyserver.ubuntu.com --recv-keys "$PHAR_KEY_ID" \
|| gpg --keyserver keyserver.pgp.com --recv-keys "$PHAR_KEY_ID" \
|| gpg --keyserver pgp.mit.edu --recv-keys "$PHAR_KEY_ID" ) \
&& gpg --verify phive.phar.asc phive.phar \
&& chmod +x phive.phar \
&& mv phive.phar /usr/local/bin/phive \
&& rm phive.phar.asc \
&& update-alternatives --install /usr/bin/php php /usr/bin/php83 110
COPY --from=composer/composer:2-bin /composer /usr/bin/composer
ENV PATH="/root/.composer/vendor/bin:${PATH}"
# Linter install
RUN GITHUB_AUTH_TOKEN="$(cat /run/secrets/GITHUB_TOKEN)" && export GITHUB_AUTH_TOKEN && composer global require friendsofphp/php-cs-fixer