Table of Contents
2.3 指定规范" >2.3 指定规范
2.4 查看帮助" >2.4 查看帮助
Home Backend Development PHP Tutorial What is PHP_CodeSniffer? How to install and use?

What is PHP_CodeSniffer? How to install and use?

Jan 11, 2022 pm 04:16 PM
php_codesniffer

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
Copy after login
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
Copy after login
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
Copy after login
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
Copy after login
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
Copy after login
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=*"
Copy after login

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.*"
    }
}
Copy after login
Execute the command after completion:

./vendor/bin/phpcs -h
./vendor/bin/phpcbf -h
Copy after login

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" %*
Copy after login
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   # 检查文件和目录
Copy after login

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
--------------------------------------------------------------------------------
Copy after login

如果不要显示警告,加个-n参数:

$ phpcs -n /path/to/code/myfile.php
Copy after login

仅显示检查结果概要:

$ phpcs --report=summary /path/to/code
Copy after login

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)
Copy after login

查看现有规范:

$ phpcs -i
Copy after login

2.4 查看帮助

$ phpcs -h
Copy after login

推荐学习:《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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

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.

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

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 permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

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

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

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

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

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

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

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

See all articles