Home  >  Article  >  Backend Development  >  What is PHP_CodeSniffer? How to install and use?

What is PHP_CodeSniffer? How to install and use?

藏色散人
藏色散人forward
2022-01-11 16:16:404617browse

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.

Of course, you

can also add 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, 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
cd PHP_CodeSniffer
php bin/phpcs -h
php bin/phpcbf -h
To 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 -h
Different from githuh download, the execution file is in the

scripts directory.

1.2 phar file installation method

phar is also

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.phar
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
php phpcbf.phar -h
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
  • Every time you execute, you must put these two .phar files into the corresponding directory, and then run php in that directory phpcs.phar xxxLine command

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
After installation through pear, the CodeSniffer specification file will be installed at:

/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=*"

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

can also be used in the composer.json file:

{
    "require-dev": {
        "squizlabs/php_codesniffer": "3.*"
    }
}
Execute the command after completion:

./vendor/bin/phpcs -h
./vendor/bin/phpcbf -h

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 path of bin (or scripts) PATH, 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 locations of the

php.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.

2 Using

Above we see 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                        # 检查文件
$ phpcs /path/to/code                                   # 检查目录和子目录下的所有文件
$ phpcs -l /path/to/code                                # 检查目录下的所有文件,不包括子目录
$ phpcs /path/to/code/myfile.inc /path/to/code/my_dir   # 检查文件和目录

2.2 Check results

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

$ 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

2.3 指定规范

可以使用 -- 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

2.4 查看帮助

$ 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!

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