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 [-nvi] [--report=<report>] [--standard=<standard>] [--extensions=<extensions>] <file> ... <file> : one or more files and/or directories to check <standard> : the name of the coding standard to use <report> : print either the "full" or "summary" report <extensions> : a comma separated list of file extensions to check (only valid if checking a directory) -n : do not print warnings -v[v][v] : print verbose output -i : show a list of installed coding standards -h --help : print this help message --version : print version information |
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.
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.
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.
Exemple 53-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.
Exemple 53-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:
Exemple 53-3. Sample PHP_CodeSniffer output
|
If you don't want warnings included in the output, specify the -n command line argument.
Exemple 53-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:
Exemple 53-5. Sample PHP_CodeSniffer summary output
|
As with the full report, you can suppress the printing of warnings with the -n command line argument.
Exemple 53-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:
Exemple 53-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).
Exemple 53-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.
Exemple 53-9. Generating a list of installed coding standards
|
Précédent | Sommaire | Suivant |
Introduction | Niveau supérieur | Advanced Usage |