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.
- PHP_CodeSniffer warehouse address:
- https://github.com/squizlabs/PHP_CodeSniffer PHP_CodeSniffer version release address:
- http://pear. php.net/package/PHP_CodeSniffer
phpcs is used to
check code specifications, phpcbf is used to
correct 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
2.9.1 and unzip it after downloading PEAR. The executed command is:
php scripts/phpcs -h php scripts/phpcbf -h
scripts directory.
1.2 phar file installation method
phar is alsophp 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
php phpcs.phar -h php phpcbf.phar -h
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 xxx
Line 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
/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.
{ "require-dev": { "squizlabs/php_codesniffer": "3.*" } }
./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 thephpcs 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" %*
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.
