How to use the command phpcs (with examples)

How to use the command phpcs (with examples)

The phpcs command is used to tokenize PHP, JavaScript, and CSS files in order to detect violations of a defined set of coding standards. It is a powerful tool for ensuring code quality and adherence to coding standards. In this article, we will explore several use cases of this command along with example code and outputs.

Use case 1: Sniff the specified directory for issues

Code:

phpcs path/to/directory

Motivation: The motivation for using this example is to scan a specific directory for coding issues and violations of coding standards. This allows developers to easily identify areas of their codebase that need improvement.

Explanation: The command phpcs is followed by the path to the directory that needs to be scanned. By default, it uses the PEAR coding standard to check for violations.

Example Output:

FILE: /path/to/file.php
----------------------------------------------------------------------
FOUND 3 ERRORS AND 1 WARNING AFFECTING 2 LINES
----------------------------------------------------------------------
 43 | ERROR   | Missing file doc comment
 50 | ERROR   | Missing function doc comment
 58 | ERROR   | Line exceeds 120 characters; contains 122 characters
 73 | WARNING | Line exceeds 80 characters; contains 82 characters
----------------------------------------------------------------------
PHPCBF CAN FIX THE 3 MARKED SNIFF VIOLATIONS AUTOMATICALLY

Use case 2: Display a list of installed coding standards

Code:

phpcs -i

Motivation: The motivation for using this example is to get a list of all the coding standards that are currently installed. This information helps developers determine which coding standards they can use for validation.

Explanation: The -i flag is used to display a list of the installed coding standards. It provides the names of the coding standards that can be used with the --standard argument.

Example Output:

The installed coding standards are PEAR, MyCompany, Zend, Squiz, PSR12, PSR1, PSR2, PHPCS, and MyStandard

Use case 3: Specify a coding standard to validate against

Code:

phpcs path/to/directory --standard standard

Motivation: The motivation for using this example is to enforce a specific coding standard while scanning a directory for violations. This allows developers to ensure that their code follows a predefined set of rules.

Explanation: The --standard argument is used to specify the coding standard that needs to be validated against. it is followed by the name of the coding standard. The codebase in the specified directory will be checked for violations against this standard.

Example Output:

FILE: /path/to/file.php
----------------------------------------------------------------------
FOUND 2 ERRORS AND 0 WARNINGS AFFECTING 1 LINE
----------------------------------------------------------------------
 43 | ERROR   | Missing file doc comment
 50 | ERROR   | Missing function doc comment
----------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY

Use case 4: Specify comma-separated file extensions to include when sniffing

Code:

phpcs path/to/directory --extensions file_extension(s)

Motivation: The motivation for using this example is to specify specific file extensions to include when searching for code violations in a directory. This is useful when focusing on a specific type of file.

Explanation: The --extensions argument is used to specify the file extensions to include when sniffing. It is followed by the desired file extension(s) separated by commas. Only files with these extensions will be considered during the scan.

Example Output:

FILE: /path/to/file.php
----------------------------------------------------------------------
FOUND 2 ERRORS AND 0 WARNINGS AFFECTING 1 LINE
----------------------------------------------------------------------
 43 | ERROR   | Missing file doc comment
 50 | ERROR   | Missing function doc comment
----------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY

Use case 5: Specify the format of the output report

Code:

phpcs path/to/directory --report format

Motivation: The motivation for using this example is to customize the format of the output report generated by phpcs. This allows developers to receive the output in a format that best suits their needs.

Explanation: The --report argument is used to specify the format of the output report. It is followed by the desired format, such as full, xml, json, or summary. The output report will be generated in the specified format.

Example Output:

FILE: /path/to/file.php
----------------------------------------------------------------------
FOUND 1 ERROR AND 0 WARNINGS AFFECTING 1 LINE
----------------------------------------------------------------------
 50 | ERROR   | Missing function doc comment
----------------------------------------------------------------------
PHPCBF CAN FIX THE MARKED SNIFF VIOLATIONS AUTOMATICALLY (TO REVERT CHANGES USE --no-patch)

Use case 6: Set config variables to be used during the process

Code:

phpcs path/to/directory --config-set key value

Motivation: The motivation for using this example is to set specific config variables to be used during the phpcs process. This allows developers to customize various aspects of the coding standard validation.

Explanation: The --config-set argument is used to set specific config variables for the phpcs process. It is followed by the key-value pair of the config variable that needs to be set. This config variable will then be used during the validation process.

Example Output: N/A (No output is generated when setting config variables)

Use case 7: Load files before processing

Code:

phpcs path/to/directory --bootstrap file(s)

Motivation: The motivation for using this example is to load specific files before the phpcs process begins. This is useful when additional files or dependencies need to be loaded before the validation process.

Explanation: The --bootstrap argument is used to specify a comma-separated list of files to load before the phpcs process. These files will be loaded and processed alongside the files in the specified directory.

Example Output: N/A (No output is generated when loading files before processing)

Use case 8: Don’t recurse into subdirectories

Code:

phpcs path/to/directory -l

Motivation: The motivation for using this example is to prevent recursive scanning into subdirectories. This is useful when developers want to restrict the analysis to the files in the specified directory only.

Explanation: The -l flag is used to disable recursion into subdirectories. When this flag is specified, phpcs will only scan the files present in the specified directory and will not traverse into any subdirectories.

Example Output:

FILE: /path/to/file.php
----------------------------------------------------------------------
FOUND 1 ERROR AND 0 WARNINGS AFFECTING 1 LINE
----------------------------------------------------------------------
 50 | ERROR   | Missing function doc comment
----------------------------------------------------------------------
PHPCBF CAN FIX THE MARKED SNIFF VIOLATIONS AUTOMATICALLY (TO REVERT CHANGES USE --no-patch)

Conclusion:

The phpcs command is a powerful tool for detecting violations of coding standards in PHP, JavaScript, and CSS files. By using the various options provided by the command, developers can customize the validation process based on their specific needs. This article has covered several use cases of the command along with example code and outputs, enabling developers to get started with phpcs and improve the quality of their codebases.

Related Posts

Using the nfcd Command (with Examples)

Using the nfcd Command (with Examples)

Starting the daemon To start the nfcd daemon, simply run the command nfcd.

Read More
How to use the command `lsyncd` (with examples)

How to use the command `lsyncd` (with examples)

The lsyncd command is a utility that watches files and directories and runs rsync when they change.

Read More
How to use the command 'rdpsign' (with examples)

How to use the command 'rdpsign' (with examples)

The ‘rdpsign’ command is a tool provided by Microsoft that allows users to sign Remote Desktop Protocol (RDP) files.

Read More