PHPCS: PHP Code Sniffer
Tags: php, tutorial, productivity
Published: 2023-10-08 02:54:07

### About

PHP_CodeSniffer is a set of two PHP scripts; the main `phpcs` script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second `phpcbf` script to automatically correct coding standard violations.

### Requirements

PHP_CodeSniffer requires PHP version 5.4.0 or greater, although individual sniffs may have additional requirements such as external applications and scripts

### Installation

The easiest way to get started with PHP_CodeSniffer is to download the Phar files for each of the commands:

```
# Download using curl
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar

# Or download using wget
wget https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
wget https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar

# Then test the downloaded PHARs
php phpcs.phar -h
php phpcbf.phar -h
```

**Composer**

If you use Composer, you can install PHP_CodeSniffer system-wide with the following command:

```
composer global require "squizlabs/php_codesniffer=*"
```

Make sure you have the composer bin dir in your PATH. The default value is `~/.config/composer/vendor/bin/`, but you can check the value that you need to use by running `composer global config bin-dir --absolute`.

**Linux**

```
sudo apt install php-codesniffer
```

### Basic usage

To use PHPCS, simply enter the file or folder you want to validate. If the path to a folder is provided, PHPCS will recursively test all internal directories.

If you do not want subdirectories to be checked you must pass the `-l` argument in the PHPCS call.


Example of how to avoid recursive checking:
```bash
# composer
~/.config/composer/vendor/bin/phpcs -l app/

# linux
phpcs -l app/
```


How to test a file:

```bash
# composer
~/.config/composer/vendor/bin/phpcs app/classes/Product.php

# linux 
phpcs app/classes/Product.php
```


After PHPCS finishes analyzing your files, a report will be printed with the errors found. This report displays errors and warnings for all files that violated the code standard. The report follows the following pattern:

```
FILE: /home/zero/Documents/Linguagens/PHP/CodeSniffer/app/classes/Product.php
----------------------------------------------------------------------------------------------------------------------------
FOUND 162 ERRORS AND 4 WARNINGS AFFECTING 79 LINES
----------------------------------------------------------------------------------------------------------------------------
  2 | ERROR   | [ ] Missing file doc comment
  3 | ERROR   | [x] Spaces must be used to indent lines; tabs are not allowed
  3 | ERROR   | [x] Line indented incorrectly; expected 0 spaces, found 1
  3 | ERROR   | [ ] Missing doc comment for class Produto
  4 | ERROR   | [x] Spaces must be used to indent lines; tabs are not allowed
  5 | ERROR   | [x] Spaces must be used to indent lines; tabs are not allowed
  6 | ERROR   | [x] Spaces must be used to indent lines; tabs are not allowed
  6 | ERROR   | [x] Line indented incorrectly; expected 4 spaces, found 2
  6 | ERROR   | [ ] Private member variable "conn" must be prefixed with an underscore
  7 | ERROR   | [x] Spaces must be used to indent lines; tabs are not allowed
  7 | ERROR   | [x] Line indented incorrectly; expected 4 spaces, found 2
  7 | ERROR   | [ ] Private member variable "data" must be prefixed with an underscore
  8 | ERROR   | [x] Spaces must be used to indent lines; tabs are not allowed
  9 | ERROR   | [x] Spaces must be used to indent lines; tabs are not allowed
  9 | ERROR   | [x] Line indented incorrectly; expected 4 spaces, found 2
  9 | ERROR   | [ ] Missing doc comment for function setConnection()
 10 | ERROR   | [x] Spaces must be used to indent lines; tabs are not allowed
 10 | ERROR   | [x] Line indented incorrectly; expected at least 4 spaces, found 2
 11 | ERROR   | [x] Spaces must be used to indent lines; tabs are not allowed
 11 | ERROR   | [x] Line indented incorrectly; expected at least 8 spaces, found 3
 12 | ERROR   | [x] Spaces must be used to indent lines; tabs are not allowed
 12 | ERROR   | [x] Line indented incorrectly; expected 4 spaces, found 2
.
.
.
----------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 147 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------------------------------------------------------------
```
162 errors and 4 warnings were found in a total of 79 lines.

Errors marked with an `[x]` mean that the code sniffer can make an automatic correction. To carry out automatic fix:

```bash
# composer
~/.config/composer/vendor/bin/phpcbf app/classes/Product.php

# linux 
phpcbf app/classes/Product.php
```

```
PHPCBF RESULT SUMMARY
--------------------------------------------------------------------------------------------
FILE                                                                        FIXED  REMAINING
--------------------------------------------------------------------------------------------
/home/zero/Documents/Linguagens/PHP/CodeSniffer/app/classes/Product.php     147    19
--------------------------------------------------------------------------------------------
A TOTAL OF 147 ERRORS WERE FIXED IN 1 FILE
--------------------------------------------------------------------------------------------
```

147 errors were fixed and 19 were maintained, these must be corrected manually.

### How to specify a code pattern

When installing PHPCS, more than one code pattern is added to the project. To obtain a complete list of installed patterns, use the following command:

```bash
# composer
~/.config/composer/vendor/bin/phpcs -i

# linux
phpcs -i
```

The return should look like this:

```
The installed coding standards are MySource, PEAR, PSR1, PSR2, PSR12, Squiz and Zend
```

The return should look like this:To specify which code pattern PHPCS should follow when checking project files you can use the following argument `--standard` :

```bash
# composer
~/.config/composer/vendor/bin/phpcs --standard=PSR1 app/classes/Product.php

# linux 
phpcs --standard=PSR1 app/classes/Product.php
```

To use more than one pattern, you must enter a comma-separated list of patterns:

```bash
# composer
~/.config/composer/vendor/bin/phpcs --standard=PSR1,PSR12 app/classes/Product.php

# linux 
phpcs --standard=PSR1,PSR12 app/classes/Product.php
```