Skip to content
github-actions[bot] edited this page Sep 16, 2025 · 12 revisions

PHP_CodeSniffer is a set of two PHP scripts:

  1. the main phpcs script that tokenizes PHP files to detect violations of a defined coding standard; and
  2. a phpcbf script to automatically correct detected coding standard violations.

PHP_CodeSniffer is an essential development tool that ensures your code remains clean and consistent.

A coding standard in PHP_CodeSniffer is a collection of sniff files. Each sniff file checks one part of the coding standard only. Each sniff can yield multiple error codes, a different one for each aspect of the code which was checked and found non-compliant.

Multiple coding standards can be used within PHP_CodeSniffer so that the one installation can be used across multiple projects.
As of PHP_CodeSniffer 4.0.0, the default coding standard used by PHP_CodeSniffer is the PSR12 coding standard (previously, this was the PEAR standard).

Example

To check a file against the PSR12 coding standard, simply specify the file's location.

$ phpcs path/to/code/fileA.php

FILE: path/to/code/fileA.php
----------------------------------------------------------------------------------------------------
FOUND 6 ERRORS AND 1 WARNING AFFECTING 4 LINES
----------------------------------------------------------------------------------------------------
  1 | ERROR   | [x] Header blocks must be separated by a single blank line
 10 | ERROR   | [x] Expected 0 spaces after opening parenthesis; 1 found
 10 | ERROR   | [x] PHP parameter type declarations must be lowercase; expected "string" but found
    |         |     "String"
 10 | ERROR   | [x] Expected 1 space between type hint and argument "$param"; 2 found
 12 | WARNING | [ ] Line exceeds 120 characters; contains 136 characters
 13 | ERROR   | [x] Line indented incorrectly; expected at least 8 spaces, found 4
 13 | ERROR   | [x] TRUE, FALSE and NULL must be lowercase; expected "false" but found "FALSE"
----------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 6 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------------------------------------

Or, if you wish to check an entire directory, you can specify the directory location instead of a file.

$ phpcs /path/to/code

FILE: path/to/code/fileA.php
----------------------------------------------------------------------------------------------------
FOUND 6 ERRORS AND 1 WARNING AFFECTING 4 LINES
----------------------------------------------------------------------------------------------------
  1 | ERROR   | [x] Header blocks must be separated by a single blank line
 10 | ERROR   | [x] Expected 0 spaces after opening parenthesis; 1 found
 10 | ERROR   | [x] PHP parameter type declarations must be lowercase; expected "string" but found
    |         |     "String"
 10 | ERROR   | [x] Expected 1 space between type hint and argument "$param"; 2 found
 12 | WARNING | [ ] Line exceeds 120 characters; contains 136 characters
 13 | ERROR   | [x] Line indented incorrectly; expected at least 8 spaces, found 4
 13 | ERROR   | [x] TRUE, FALSE and NULL must be lowercase; expected "false" but found "FALSE"
----------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 6 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------------------------------------


FILE: path/to/code/fileC.php
------------------------------------------------------------------------
FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
------------------------------------------------------------------------
 4 | WARNING | [x] Usage of ELSE IF is discouraged; use ELSEIF instead
 5 | WARNING | [ ] Line exceeds 120 characters; contains 132 characters
------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------


FILE: path/to/code/fileB.php
----------------------------------------------------------------------------------------------
FOUND 1 ERROR AND 1 WARNING AFFECTING 2 LINES
----------------------------------------------------------------------------------------------
 3 | ERROR   | [x] PHP keywords must be lowercase; expected "namespace" but found "NAMESPACE"
 6 | WARNING | [x] Usage of ELSE IF is discouraged; use ELSEIF instead
----------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------------------------------
Clone this wiki locally