search
HomeBackend DevelopmentPHP TutorialWhat 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.

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. If there is any infringement, please contact admin@php.cn delete
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool