LinuxCommandLibrary

phpcs

Check PHP code against coding standards

TLDR

Sniff the specified directory for issues (defaults to the PEAR standard)

$ phpcs [path/to/directory]
copy

Display a list of installed coding standards
$ phpcs -i
copy

Specify a coding standard to validate against
$ phpcs [path/to/directory] --standard [standard]
copy

Specify comma-separated file extensions to include when sniffing
$ phpcs [path/to/directory] --extensions [file_extension1,file_extension2,...]
copy

Specify the format of the output report (e.g. full, xml, json, summary)
$ phpcs [path/to/directory] --report [format]
copy

Set configuration variables to be used during the process
$ phpcs [path/to/directory] --config-set [key] [value]
copy

A comma-separated list of files to load before processing
$ phpcs [path/to/directory] --bootstrap [path/to/file1,path/to/file2,...]
copy

Don't recurse into subdirectories
$ phpcs [path/to/directory] -l
copy

SYNOPSIS

phpcs [options] <file(s) or directory(s)>

PARAMETERS

--standard=<standard name>
    Specifies the coding standard to use (e.g., PSR1, PSR2, Squiz, MySource). If not provided, it uses the default standard configured in the phpcs configuration.

--sniffs=<sniff names>
    Comma-separated list of specific sniffs to include in the code analysis.

--exclude=<sniff names>
    Comma-separated list of specific sniffs to exclude from the code analysis.

--extensions=<extensions>
    Comma-separated list of file extensions to check (e.g., php,inc,js).

--ignore=<patterns>
    Comma-separated list of patterns to ignore files or directories.

--report=<report type>
    Specifies the output report format (e.g., full, xml, csv, summary).

--severity=<severity>
    Sets the minimum severity of errors to display.

--error-severity=<severity>
    Sets the severity of errors to display. Defaults to 5.

--warning-severity=<severity>
    Sets the severity of warnings to display. Defaults to 5.

--tab-width=<width>
    Sets the tab width for indentation checking.

--encoding=<encoding>
    Sets the encoding of the files being checked.

--bootstrap=<file>
    A file to include before processing.

--config-set=<name value>
    Set a configuration value

-n
    Do not print warnings. (alias of --warning-severity=0)

-q
    Quiet mode; suppresses all output except error reports

-v[v][v]
    Increase verbosity level.

--help
    Displays help information.

DESCRIPTION

phpcs is a command-line tool that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard. It is an essential tool for maintaining consistent code style within PHP projects. It allows developers to automatically check their code against a set of rules, such as those defined by PSR-1, PSR-2, the Symfony coding standard, or even custom standards. This helps improve code readability, maintainability, and reduces the likelihood of introducing bugs due to stylistic inconsistencies. The tool is highly configurable, allowing projects to tailor the rules to their specific needs.

phpcs supports various output formats, making it easy to integrate into continuous integration (CI) pipelines, IDEs, and other development tools.

CAVEATS

Performance can be affected by the number of files and the complexity of the coding standard used. It's essential to configure phpcs to focus only on relevant files and sniffs to optimize execution time.

CONFIGURATION FILES

phpcs can be configured using a phpcs.xml or phpcs.xml.dist file in the root of your project. This file allows you to specify the coding standard to use, include or exclude sniffs, and define other configuration options. This allows for project specific rule sets to be defined and enforced.

HISTORY

phpcs was developed to provide a consistent and automated way to enforce coding standards in PHP projects. It has evolved over time, with support for more standards, improved performance, and better integration with other tools. The initial versions focused on core coding style rules, while later versions expanded to include checks for security vulnerabilities and best practices.

SEE ALSO

phpcbf(1)

Copied to clipboard