Linters
What is PHP_CodeSniffer? An introduction to PHPCSStandards' coding standard linters
PHP_CodeSniffer is a set of two PHP scripts, phpcs and phpcbf, that tokenize PHP files to detect violations of a defined coding standard and automatically correct them. It is positioned as a development tool that keeps code clean and consistent. It is the official continuation of the abandoned Squizlabs PHP_CodeSniffer package.
- Tool
- PHP_CodeSniffer
- Made by
- PHPCSStandards
- Official site
- github.com/PHPCSStandards/PHP_CodeSniffer
- Category
- Linters
Who makes PHP_CodeSniffer
PHP_CodeSniffer is maintained by PHPCSStandards. The project is the official continuation of the abandoned Squizlabs PHP_CodeSniffer package, so teams that relied on the older package have a maintained successor to follow.
The official website is linked below. It points to the project's GitHub repository, where the documentation is hosted on the GitHub wiki.
Official website: github.com/PHPCSStandards/PHP_CodeSniffer
What PHP_CodeSniffer is for
PHP_CodeSniffer checks PHP source against a coding standard. It tokenizes each file, compares the result with the rules of the chosen standard and reports every violation it finds.
The tool is split into two scripts. phpcs detects coding standard violations, and phpcbf automatically corrects them. Detection and repair are therefore separate commands, which lets a team report problems first and apply fixes when it is ready.
By default PHP_CodeSniffer uses the PSR12 coding standard. You can check a single file or an entire directory, and you can select another standard, such as PEAR, with the --standard option.
Who PHP_CodeSniffer is for
The vendor names four audiences: PHP developers, development teams, teams running CI servers and sniff developers.
Individual developers can run it against their own code. Teams and CI servers can configure project settings through a configuration file, so everyone checks against the same rules. Sniff developers, who write the individual rules that make up a standard, are served by a sniff developer API that the project versions deliberately.
Notable capabilities
PHP_CodeSniffer pairs detection with automatic repair and offers several ways to install and verify it. The capabilities below come from the project's own description.
- Detects coding standard violations with phpcs and corrects them automatically with phpcbf.
- Defaults to the PSR12 standard, with other standards such as PEAR selectable through --standard.
- Checks one file or a whole directory in a single run.
- Reads project settings from a configuration file, which suits teams and CI servers.
- Installs as Phar files for each command, system-wide or as a project dependency with Composer, as a project tool with Phive, or by running directly from a Git clone of the source.
- Signs release Phars with an official release key, and since version 3.10.3 PHAR provenance can be verified through GitHub Artifact Attestations.
- Requires PHP 7.2.0 or greater; individual sniffs may need additional external applications and scripts.
How PHP_CodeSniffer fits linters
Within the linters category, PHP_CodeSniffer concentrates on coding standards: whether PHP source follows an agreed set of style and structure rules. It does not describe itself as a general-purpose analyzer, and its stated purpose is keeping code clean and consistent.
For Linthelm's wider interest in linters, static analysis and code quality, it covers the consistency side of code quality. Because it reports violations and can fix them, it fits both local development and automated checks on a CI server.
PHP_CodeSniffer is released under the BSD-3-Clause license. It follows MAJOR.MINOR.PATCH versioning, and major versions are reserved for backwards-incompatible changes to commands, the ruleset.xml format or the sniff developer API. That gives teams a clear signal about when an upgrade may require changes to their setup.
Frequently asked questions
What is PHP_CodeSniffer used for?
It detects violations of a defined coding standard in PHP files and automatically corrects them. Detection is done by phpcs and correction by phpcbf.
Who develops PHP_CodeSniffer?
PHPCSStandards develops it. The project is the official continuation of the abandoned Squizlabs PHP_CodeSniffer package.
Which PHP version does PHP_CodeSniffer require?
It requires PHP 7.2.0 or greater. Individual sniffs may have additional requirements, such as external applications and scripts.
What is the default coding standard in PHP_CodeSniffer?
The default is PSR12. You can select another standard, such as PEAR, with the --standard option.
How do you install PHP_CodeSniffer?
You can download Phar files for each command, install it system-wide or as a project dependency with Composer, or use Phive as a project tool. You can also run it directly from a Git clone of the source.
Linthelm is independent and is not affiliated with PHPCSStandards. Product names belong to their owners. Features change; confirm details on the official website.