Home > Article > Backend Development > What is PHP_CodeSniffer? How to install and use?
This article introduces PHP_CodeSniffer to you, as well as its installation and usage tutorials. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
PHP_CodeSniffer is an automated PHP code specification checking tool.
CodeSniffer built-in MySource
, PEAR
, PHPCS
, PSR1
, PSR2
, ## Several sets of code specifications such as #Squiz and
Zend.
can also add your own code specifications.
phpcs is used to
check code specifications, phpcbf is used to
correct code specifications.
git clone https://github.com/squizlabs/PHP_CodeSniffer.git cd PHP_CodeSniffer php bin/phpcs -h php bin/phpcbf -hTo use non- The latest version can be downloaded from PEAR at the address: http://pear.php.net/package/PHP_CodeSniffer/download. For example, use
2.9.1 and unzip it after downloading PEAR. The executed command is:
php scripts/phpcs -h php scripts/phpcbf -hDifferent from githuh download, the execution file is in the
scripts directory.
php archive, which packages the php file 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 curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.pharFor 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 php phpcbf.phar -hYou can see the help information of phpcs and phpcbf, indicating that the installation is successful.
Note:
files into the corresponding directory, and then run php in that directory phpcs.phar xxx
Line command
pear Installation method ), you can also install PHP_CodeSniffer through pear, command:
pear install PHP_CodeSnifferAfter installation through pear, the CodeSniffer specification file will be installed at:
/path/to/pear/PHP/CodeSniffer /src/Standards.
composer global require "squizlabs/php_codesniffer=*"
Instructions: here The composer command is required to be in the PATH
environment variable.
{ "require-dev": { "squizlabs/php_codesniffer": "3.*" } }Execute the command after completion:
./vendor/bin/phpcs -h ./vendor/bin/phpcbf -h
phpcs command in other methods, you need to add php in front.
PATH, so add the path of
bin (or
scripts)
PATH, you can execute the
phpcs command in the terminal.
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 locations of thephp.exe and
phpcs files:
if "%PHPBIN%" == "" set PHPBIN=D:\php56n\php.exe if not exist "%PHPBIN%" if "%PHP_PEAR_PHP_BIN%" neq "" goto USE_PEAR_PATH GOTO RUN :USE_PEAR_PATH set PHPBIN=%PHP_PEAR_PHP_BIN% :RUN "%PHPBIN%" "D:\www\PHP_CodeSniffer-2.9.1\scripts\phpcs" %*Then change the path D:\www\PHP_CodeSniffer-2.9 Add .1\scripts\ to PATH, and you can execute phpcs in CMD.
Note: phpcbf also needs such modification.
$ phpcs /path/to/code/myfile.php # 检查文件 $ phpcs /path/to/code # 检查目录和子目录下的所有文件 $ phpcs -l /path/to/code # 检查目录下的所有文件,不包括子目录 $ phpcs /path/to/code/myfile.inc /path/to/code/my_dir # 检查文件和目录
$ phpcs /path/to/code/myfile.php FILE: /path/to/code/myfile.php -------------------------------------------------------------------------------- FOUND 5 ERROR(S) AND 1 WARNING(S) AFFECTING 5 LINE(S) -------------------------------------------------------------------------------- 2 | ERROR | Missing file doc comment 20 | ERROR | PHP keywords must be lowercase; expected "false" but found | | "FALSE" 47 | ERROR | Line not indented correctly; expected 4 spaces but found 1 47 | WARNING | Equals sign not aligned with surrounding assignments 51 | ERROR | Missing function doc comment 88 | ERROR | Line not indented correctly; expected 9 spaces but found 6 --------------------------------------------------------------------------------
如果不要显示警告,加个-n
参数:
$ phpcs -n /path/to/code/myfile.php
仅显示检查结果概要:
$ phpcs --report=summary /path/to/code
可以使用 -- standard
参数指定一个或多个规范来检查。
$ phpcs --standard=PEAR /path/to/code/myfile.inc # 使用内置规范 $ phpcs --standard=/path/to/MyStandard /path/to/code/myfile.inc # 使用指定路径下的规范 $ phpcs --standard=PEAR,PHPCS,/path/to/MyStandard file.php # 使用多个规范 $ phpcs --config-set default_standard Squiz # 修改默认规范为Squiz(原本是PEAR)
查看现有规范:
$ phpcs -i
$ phpcs -h
推荐学习:《PHP视频教程》
The above is the detailed content of What is PHP_CodeSniffer? How to install and use?. For more information, please follow other related articles on the PHP Chinese website!