Running PHP_CodeSniffer with the -h or --help command line arguments will print a list of commands that PHP_CodeSniffer will respond to. The output of phpcs -h is shown below.
Usage: phpcs [-nwlvi] [--report=<report>] [--standard=<standard>] [--config-set key value] [--config-delete key] [--config-show] [--generator=<generator>] [--extensions=<extensions>] [--ignore=<patterns>] [--tab-width=<width>] <file> ... -n Do not print warnings -w Print both warnings and errors (on by default) -l Local directory only, no recursion -v[v][v] Print verbose output -i Show a list of installed coding standards --help Print this help message --version Print version information <file> One or more files and/or directories to check <extensions> A comma separated list of file extensions to check (only valid if checking a directory) <patterns> A comma separated list of patterns that are used to ignore directories and files <standard> The name of the coding standard to use <width> The number of spaces each tab represents <generator> The name of a doc generator to use (forces doc generation instead of checking) <report> Print either the "full", "xml", "checkstyle", "csv" or "summary" report (the "full" report is printed by default) |
Note: The --standard command line argument is optional, even if you have more than one coding standard installed. If no coding standard is specified, PHP_CodeSniffer will default to checking against the PEAR coding standard, or the standard you have set as the default. View instructions for setting the default coding standard.
The simplest way of using PHP_CodeSniffer is to provide the location of a file or folder for PHP_CodeSniffer to check. If a folder is provided, PHP_CodeSniffer will check all files it finds in that folder and all its sub-folders.
Note: If you do not want sub-folders checked, use the -l command line argument to force PHP_CodeSniffer to run locally in the folders specified.
In the example below, the first command tells PHP_CodeSniffer to check the myfile.inc file for coding standard errors while the second command tells PHP_CodeSniffer to check all PHP files in the my_dir directory.
Example 55-1. Checking a single file or folder
|
You can also specify multiple files and folders to check. The command below tells PHP_CodeSniffer to check the myfile.inc file and all files in the my_dir directory.
Example 55-2. Checking multiple files and folders
|
After PHP_CodeSniffer has finished processing your files, you will get an error report. The report lists both errors and warnings for all files that violated the coding standard. The output looks like this:
Example 55-3. Sample PHP_CodeSniffer output
|
If you don't want warnings included in the output, specify the -n command line argument.
Example 55-4. Sample PHP_CodeSniffer output with no warnings
|
By default, PHP_CodeSniffer will print a complete list of all errors and warnings it finds. This list can become quite long, especially when checking a large number of files at once. To print a summary report that only shows the number of errors and warnings for each file, use the --report=summary command line argument. The output will look like this:
Example 55-5. Sample PHP_CodeSniffer summary output
|
As with the full report, you can suppress the printing of warnings with the -n command line argument.
Example 55-6. Sample PHP_CodeSniffer summary output with no warnings
|
By default, PHP_CodeSniffer will run quietly, only printing the report of errors and warnings at the end. If you are checking a large number of files, you may have to wait a while to see the report. If you want to know what is happening, you can turn on verbose output.
With verbose output enabled, PHP_CodeSniffer will print the file that it is checking, show you how many tokens and lines the file contains, and let you know how long it took to process. The output will look like this:
Example 55-7. Sample PHP_CodeSniffer verbose output
|
PHP_CodeSniffer can have multiple coding standards installed to allow a single installation to be used with multiple projects. When checking PHP code, PHP_CodeSniffer can be told which coding standard to use. This is done using the --standard command line argument.
The example below checks the myfile.inc file for violations of the PEAR coding standard (installed by default).
Example 55-8. Specifying a coding standard to use
|
PHP_CodeSniffer can print you a list of the coding standards that are installed so that you can correctly specify a coding standard to use for testing. You can print this list by specifying the -i command line argument.
Example 55-9. Generating a list of installed coding standards
|