Home  >  Article  >  Backend Development  >  PHP_CodeSniffer installation and usage tutorial (automatic code inspection specification tool)

PHP_CodeSniffer installation and usage tutorial (automatic code inspection specification tool)

angryTom
angryTomforward
2019-10-16 09:23:012809browse

PHP_CodeSniffer installation and usage tutorial (automatic code inspection specification tool)

We all pay attention to code specifications in our development. If you are an individual developer, you only need to understand whether the code is standardized or not, but in team collaboration, the code Regulations are particularly important. Below, we introduce PHP_CodeSniffer, a tool that automatically checks code specifications.

PHP_CodeSniffer is an automated PHP code specification checking tool.

CodeSniffer has built-in several sets of code specifications such as MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend.

Of course, you can alsoadd your own code specifications.

PHP_CodeSniffer warehouse address: https://github.com/squizlabs/PHP_CodeSniffer

PHP_CodeSniffer version release address: http://pear.php.net/package/PHP_CodeSniffer

PHP_CodeSniffer contains two tools, phpcs is used to check code specifications, and phpcbf is used tocorrect code specifications.

1 Installation

There are several ways to install PHP_CodeSniffer.

1.1 Installation by executable file

Use git command to download (or download directly) the warehouse source code, and then execute it directly:

git clone https://github.com/squizlabs/PHP_CodeSniffer.git<br/>cd PHP_CodeSniffer<br/>php bin/phpcs -h<br/>php bin/phpcbf -h<br/>

To use non- The latest version can be downloaded from PEAR at: http://pear.php.net/package/PHP_CodeSniffer/download

For example, if you want to use 2.9.1, after downloading PEAR and decompressing it, the command to execute is:

php scripts/phpcs -h<br/>php scripts/phpcbf -h<br/>

Different from githuh download, the execution file is in the scripts directory.

1.2 phar file installation method

phar is php archive, which packages php files into one file for service.

Linux system uses the command to download the phar file:

curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar<br/>curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar<br/>

For Windows system, use the browser to access the two links above and download the two phar files.

Then execute in the directory of the phar file:

php phpcs.phar -h<br/>php phpcbf.phar -h<br/>

You can see the help information of phpcs and phpcbf, indicating that the installation is successful.

Note:

This method must ensure that the php executable file has been added to the PATH environment, otherwise the absolute directory must be specified.

These two must be added each time it is executed. phar file into the corresponding directory, and then run the php phpcs.phar xxx line command in that directory

1.3 pear installation method

If pear is installed locally (pear installation method), you can also install PHP_CodeSniffer through pear, command:

pear install PHP_CodeSniffer<br/>

After installation through pear, the CodeSniffer specification file will be installed in: /path/to/pear/PHP/CodeSniffer/src/Standards.

1.4 composer installation method

composer installation is also very convenient, one command:

composer global require "squizlabs/php_codesniffer=*"<br/>

Note: The composer command is required here to be in the PATH environment variable .

Can also be used in the composer.json file:

{<br/>    "require-dev": {<br/>        "squizlabs/php_codesniffer": "3.*"<br/>    }<br/>}<br/>

Execute the command after completion:

./vendor/bin/phpcs -h<br/>./vendor/bin/phpcbf -h<br/>

1.5 Configure phpcs and execute it directly in the command line

Among the above methods, except for the pear installation method, if you want to execute the phpcs command in other methods, you need to add php in front.

If it is provided by Linux, because the php execution file path is already in the environment PATH, so add the bin (or scripts) path to the PATH and you can execute the phpcs command in the terminal.

But in the Windows system, the phpcs.bat file is actually executed, and this file references the phpcs file in the same directory.

In phpcs.bat, we need to configure two variables to correctly execute the phpcs command in CMD.

As follows, you need to specify the absolute location of the php.exe and phpcs files:

if "%PHPBIN%" == "" set PHPBIN=D:\php56n\php.exe<br/>if not exist "%PHPBIN%" if "%PHP_PEAR_PHP_BIN%" neq "" goto USE_PEAR_PATH<br/>GOTO RUN<br/>:USE_PEAR_PATH<br/>set PHPBIN=%PHP_PEAR_PHP_BIN%<br/>:RUN<br/>"%PHPBIN%" "D:\www\PHP_CodeSniffer-2.9.1\scripts\phpcs" %*<br/>

Then add the path D:\www\PHP_CodeSniffer-2.9.1\scripts\ to PATH, and you can Execute phpcs in CMD.

Note: phpcbf also needs such modification.

2 Use

We saw above that PHP_CodeSniffer has two commands.

By default, PHP_CodeSniffer uses the PEAR specification to check code.

2.1 Use the command

The following command uses the default specification to check files and directories.

$ phpcs /path/to/code/myfile.php                        # 检查文件<br/>$ phpcs /path/to/code                                   # 检查目录和子目录下的所有文件<br/>$ phpcs -l /path/to/code                                # 检查目录下的所有文件,不包括子目录<br/>$ phpcs /path/to/code/myfile.inc /path/to/code/my_dir   # 检查文件和目录<br/>

2.2 Check results

By default, the check results include errors and warnings, as follows:

$ phpcs /path/to/code/myfile.php<br/>FILE: /path/to/code/myfile.php<br/>--------------------------------------------------------------------------------<br/>FOUND 5 ERROR(S) AND 1 WARNING(S) AFFECTING 5 LINE(S)<br/>--------------------------------------------------------------------------------<br/>  2 | ERROR   | Missing file doc comment<br/> 20 | ERROR   | PHP keywords must be lowercase; expected "false" but found<br/>    |         | "FALSE"<br/> 47 | ERROR   | Line not indented correctly; expected 4 spaces but found 1<br/> 47 | WARNING | Equals sign not aligned with surrounding assignments<br/> 51 | ERROR   | Missing function doc comment<br/> 88 | ERROR   | Line not indented correctly; expected 9 spaces but found 6<br/>--------------------------------------------------------------------------------<br/>

If you do not want to display warnings, add - n parameter:

$ phpcs -n /path/to/code/myfile.php<br/>

Only display the check result summary:

$ phpcs --report=summary /path/to/code<br/>

2.3 Specify specifications

You can use the --standard parameter to specify one or more specifications Come check it out.

$ phpcs --standard=PEAR /path/to/code/myfile.inc                      # 使用内置规范<br/>$ phpcs --standard=/path/to/MyStandard /path/to/code/myfile.inc       # 使用指定路径下的规范<br/>$ phpcs --standard=PEAR,PHPCS,/path/to/MyStandard file.php            # 使用多个规范<br/>$ phpcs --config-set default_standard Squiz                           # 修改默认规范为Squiz(原本是PEAR)<br/>

View existing specifications:

$ phpcs -i<br/>

2.4 View help

$ phpcs -h<br/>

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of PHP_CodeSniffer installation and usage tutorial (automatic code inspection specification tool). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:www.awaimai.com. If there is any infringement, please contact admin@php.cn delete