Skip to content

phpcs GitHub last commit

phpcs documentation

PHP_CodeSniffer - GitHub

Configuration in MegaLinter

Variable Description Default value
PHP_PHPCS_ARGUMENTS User custom arguments to add in linter CLI call
Ex: -s --foo "bar"
PHP_PHPCS_FILTER_REGEX_INCLUDE Custom regex including filter
Ex: (src\|lib)
Include every file
PHP_PHPCS_FILTER_REGEX_EXCLUDE Custom regex excluding filter
Ex: (test\|examples)
Exclude no file
PHP_PHPCS_CLI_LINT_MODE Override default CLI lint mode
- 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
list_of_files
PHP_PHPCS_FILE_EXTENSIONS Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all files
Ex: [".py", ""]
[".php"]
PHP_PHPCS_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_PHPCS_PRE_COMMANDS List of bash commands to run before the linter None
PHP_PHPCS_POST_COMMANDS List of bash commands to run after the linter None
PHP_PHPCS_CONFIG_FILE phpcs configuration file name
Use LINTER_DEFAULT to let the linter find it
phpcs.xml
PHP_PHPCS_RULES_PATH Path where to find linter configuration file Workspace folder, then MegaLinter default rules
PHP_PHPCS_DISABLE_ERRORS Run linter but consider errors as warnings false
PHP_PHPCS_DISABLE_ERRORS_IF_LESS_THAN Maximum number of errors allowed 0

IDE Integration

Use phpcs in your favorite IDE to catch errors before MegaLinter !

IDE Extension Name Install
Atom linter-phpcs Visit Web Site
Atom atom-phpcs Visit Web Site
IDEA phpcs Visit Web Site
Sublime Text sublime-phpcs Visit Web Site
Sublime Text SublimeLinter-phpcs Visit Web Site
Visual Studio Code vscode-phpcs Install in VsCode

MegaLinter Flavours

This linter is available in the following flavours

Flavor Description Embedded linters Info
all Default MegaLinter Flavor 97 Docker Image Size (tag) Docker Pulls
php Optimized for PHP based projects 46 Docker Image Size (tag) Docker Pulls

Behind the scenes

How are identified applicable files

  • File extensions: .php

How the linting is performed

  • phpcs is called once with the list of files as arguments

Example calls

phpcs myfile.php
phpcs --standard=phpcs.xml myfile.php
phpcs --standard=phpcs.xml mydir/ myfile.php

Help content


Usage: phpcs [-nwlsaepqvi] [-d key[=value]] [--colors] [--no-colors]
  [--cache[=<cacheFile>]] [--no-cache] [--tab-width=<tabWidth>]
  [--report=<report>] [--report-file=<reportFile>] [--report-<report>=<reportFile>]
  [--report-width=<reportWidth>] [--basepath=<basepath>] [--bootstrap=<bootstrap>]
  [--severity=<severity>] [--error-severity=<severity>] [--warning-severity=<severity>]
  [--runtime-set key value] [--config-set key value] [--config-delete key] [--config-show]
  [--standard=<standard>] [--sniffs=<sniffs>] [--exclude=<sniffs>]
  [--encoding=<encoding>] [--parallel=<processes>] [--generator=<generator>]
  [--extensions=<extensions>] [--ignore=<patterns>] [--ignore-annotations]
  [--stdin-path=<stdinPath>] [--file-list=<fileList>] [--filter=<filter>] <file> - ...

 -     Check STDIN instead of local files and directories
 -n    Do not print warnings (shortcut for --warning-severity=0)
 -w    Print both warnings and errors (this is the default)
 -l    Local directory only, no recursion
 -s    Show sniff codes in all reports
 -a    Run interactively
 -e    Explain a standard by showing the sniffs it includes
 -p    Show progress of the run
 -q    Quiet mode; disables progress and verbose output
 -m    Stop error messages from being recorded
       (saves a lot of memory, but stops many reports from being used)
 -v    Print processed files
 -vv   Print ruleset and token output
 -vvv  Print sniff processing information
 -i    Show a list of installed coding standards
 -d    Set the [key] php.ini value to [value] or [true] if value is omitted

 --help                Print this help message
 --version             Print version information
 --colors              Use colors in output
 --no-colors           Do not use colors in output (this is the default)
 --cache               Cache results between runs
 --no-cache            Do not cache results between runs (this is the default)
 --ignore-annotations  Ignore all phpcs: annotations in code comments

 <cacheFile>    Use a specific file for caching (uses a temporary file by default)
 <basepath>     A path to strip from the front of file paths inside reports
 <bootstrap>    A comma separated list of files to run before processing begins
 <encoding>     The encoding of the files being checked (default is utf-8)
 <extensions>   A comma separated list of file extensions to check
                The type of the file can be specified using: ext/type
                e.g., module/php,es/js
 <file>         One or more files and/or directories to check
 <fileList>     A file containing a list of files and/or directories to check (one per line)
 <filter>       Use either the "gitmodified" or "gitstaged" filter,
                or specify the path to a custom filter class
 <generator>    Use either the "HTML", "Markdown" or "Text" generator
                (forces documentation generation instead of checking)
 <patterns>     A comma separated list of patterns to ignore files and directories
 <processes>    How many files should be checked simultaneously (default is 1)
 <report>       Print either the "full", "xml", "checkstyle", "csv"
                "json", "junit", "emacs", "source", "summary", "diff"
                "svnblame", "gitblame", "hgblame" or "notifysend" report,
                or specify the path to a custom report class
                (the "full" report is printed by default)
 <reportFile>   Write the report to the specified file path
 <reportWidth>  How many columns wide screen reports should be printed
                or set to "auto" to use current screen width, where supported
 <severity>     The minimum severity required to display an error or warning
 <sniffs>       A comma separated list of sniff codes to include or exclude from checking
                (all sniffs must be part of the specified standard)
 <standard>     The name or path of the coding standard to use
 <stdinPath>    If processing STDIN, the file path that STDIN will be processed as
 <tabWidth>     The number of spaces each tab represents

Installation on mega-linter Docker image

  • Dockerfile commands :
# Parent descriptor install
RUN 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="0x9D8A98B29B2D5D79" \
    && ( gpg --keyserver keyserver.pgp.com --recv-keys "$PHAR_KEY_ID" \
        || gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$PHAR_KEY_ID" \
        || gpg --keyserver pgp.mit.edu --recv-keys "$PHAR_KEY_ID" \
        || gpg --keyserver hkps://keyserver.ubuntu.com --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/php7 100 \
    && update-alternatives --install /usr/bin/php php /usr/bin/php8 10

# Linter install
RUN phive --no-progress install phpcs -g --trust-gpg-keys 31C7E470E2138192

Example success log

Results of phpcs linter (version 3.5.8)
See documentation on https://megalinter.github.io/descriptors/php_phpcs/
-----------------------------------------------

[SUCCESS] .automation/test/php/php_good_1.php


[SUCCESS] .automation/test/php/php_good_2.php


Example error log

Results of phpcs linter (version 3.5.8)
See documentation on https://megalinter.github.io/descriptors/php_phpcs/
-----------------------------------------------

[ERROR] .automation/test/php/php_bad_1.php

    FILE: .automation/test/php/php_bad_1.php
    ----------------------------------------------------------------------
    FOUND 7 ERRORS AFFECTING 1 LINE
    ----------------------------------------------------------------------
     3 | ERROR | [x] Expected at least 1 space before "-"; 0 found
     3 | ERROR | [x] Expected at least 1 space after "-"; 0 found
     3 | ERROR | [x] Expected at least 1 space before "="; 0 found
     3 | ERROR | [x] Expected at least 1 space after "="; 0 found
     3 | ERROR | [x] Expected at least 1 space before "-"; 0 found
     3 | ERROR | [x] Expected at least 1 space before "-"; 0 found
     3 | ERROR | [x] Expected at least 1 space after "-"; 0 found
    ----------------------------------------------------------------------
    PHPCBF CAN FIX THE 7 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------

    Time: 49ms; Memory: 6MB

[ERROR] .automation/test/php/php_bad_2.php

    FILE: .automation/test/php/php_bad_2.php
    ----------------------------------------------------------------------
    FOUND 4 ERRORS AND 1 WARNING AFFECTING 4 LINES
    ----------------------------------------------------------------------
      1 | WARNING | [ ] A file should declare new symbols (classes,
        |         |     functions, constants, etc.) and cause no other
        |         |     side effects, or it should execute logic with
        |         |     side effects, but should not do both. The first
        |         |     symbol is defined on line 6 and the first side
        |         |     effect is on line 10.
      6 | ERROR   | [x] Opening brace should be on a new line
     14 | ERROR   | [x] Space before opening parenthesis of function call
        |         |     prohibited
     15 | ERROR   | [x] Newline required after opening brace
     15 | ERROR   | [x] Closing brace must be on a line by itself
    ----------------------------------------------------------------------
    PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------

    Time: 45ms; Memory: 6MB