Skip to content
github-actions[bot] edited this page Jun 23, 2025 · 5 revisions

PHP_CodeSniffer is a set of two PHP scripts:

  1. the main phpcs script that tokenizes PHP, JavaScript and CSS 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. The default coding standard used by PHP_CodeSniffer is the PEAR coding standard.

Example

To check a file against the default coding standard, simply specify the file's location. The default coding standard in PHP_CodeSniffer 3.x is PEAR; the default coding standard in PHP_CodeSniffer 3.x is PSR12.

$ phpcs path/to/code/myfile.php

FILE: path/to/code/myfile.php
--------------------------------------------------------------------------------
FOUND 10 ERRORS AND 2 WARNINGS AFFECTING 5 LINES
--------------------------------------------------------------------------------
  4 | WARNING | [ ] PHP version not specified
  4 | ERROR   | [ ] Missing @category tag in file comment
  4 | ERROR   | [ ] Missing @package tag in file comment
  4 | ERROR   | [ ] Missing @author tag in file comment
  4 | ERROR   | [ ] Missing @license tag in file comment
  4 | ERROR   | [ ] Missing @link tag in file comment
  8 | ERROR   | [ ] Missing doc comment for class Bar
  8 | ERROR   | [x] Opening brace of a class must be on the line after the
    |         |     definition
  9 | ERROR   | [ ] Missing doc comment for function baz()
 11 | WARNING | [ ] Line exceeds 85 characters; contains 136 characters
 12 | ERROR   | [x] Line indented incorrectly; expected at least 8 spaces,
    |         |     found 4
 12 | ERROR   | [x] TRUE, FALSE and NULL must be lowercase; expected "false"
    |         |     but found "FALSE"
--------------------------------------------------------------------------------
PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------

Time: 23ms; Memory: 6MB

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/myfile.php
--------------------------------------------------------------------------------
FOUND 5 ERROR(S) AFFECTING 5 LINE(S)
--------------------------------------------------------------------------------
  2 | ERROR | Missing file doc comment
 20 | ERROR | PHP keywords must be lowercase; expected "false" but found "FALSE"
 47 | ERROR | Line not indented correctly; expected 4 spaces but found 1
 51 | ERROR | Missing function doc comment
 88 | ERROR | Line not indented correctly; expected 9 spaces but found 6
--------------------------------------------------------------------------------

FILE: /path/to/code/yourfile.php
--------------------------------------------------------------------------------
FOUND 1 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
 21 | ERROR   | PHP keywords must be lowercase; expected "false" but found
    |         | "FALSE"
 21 | WARNING | Equals sign not aligned with surrounding assignments
--------------------------------------------------------------------------------
Clone this wiki locally