Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install and use PHP_CodeSniffer

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the relevant knowledge of how to install and use PHP_CodeSniffer, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to install and use PHP_CodeSniffer. Let's take a look at it.

PHP_CodeSniffer is an automated PHP code specification checking tool.

CodeSniffer has several built-in code specifications such as MySource, PEAR, PHPCS, PSR1, PSR2, 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 for checking code specifications and phpcbf for correcting code specifications.

1 installation

There are several ways to install PHP_CodeSniffer.

1.1 Executable file installation

Download (or directly download) the repository source code with the git command, and then execute it directly:

Git clone https://github.com/squizlabs/PHP_CodeSniffer.gitcd PHP_CodeSnifferphp bin/phpcs-hphp bin/phpcbf-h

To use a non-latest version, you can download it from PEAR at http://pear.php.net/package/PHP_CodeSniffer/download

For example, to decompress after downloading 2.9.1 pear, execute the command:

Php scripts/phpcs-hphp scripts/phpcbf-h

Unlike githuh downloads, the execution file is located in the scripts directory.

1.2 installation of phar files

Phar, or php archive, packages the php file into a file for service.

The Linux system downloads the phar file with the command:

Curl-OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.pharcurl-OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar

The Windows system uses a browser to access the two links above to download the two phar files.

Then execute it in the directory of the phar file:

Php phpcs.phar-hphp phpcbf.phar-h

You can see help messages for phpcs and phpcbf, indicating that the installation was successful.

Description:

This way is to ensure that the php executable has been added to the PATH environment, otherwise you have to specify an absolute directory

Each execution must place the two .phar files in the corresponding directory, and then run the php phpcs.phar xxx line command in that directory

1.3 pear installation mode

If pear (pear installation method) is installed locally, you can also install PHP_CodeSniffer through pear, command:

Pear install PHP_CodeSniffer

After installing through pear, the specification file for CodeSniffer is installed at: / path/to/pear/PHP/CodeSniffer/src/Standards.

1.4 composer installation mode

Composer installation is also very convenient, one command:

Composer global require "squizlabs/php_codesniffer=*"

Description: this requires that the composer command is already in the PATH environment variable.

You can also use it in composer.json files:

{"require-dev": {"squizlabs/php_codesniffer": "3.*"}}

Execute the command when you are finished:

. / vendor/bin/phpcs-h./vendor/bin/phpcbf-h1.5 configure phpcs to execute directly on the command line

In all of the above methods, except for the pear installation, to execute the phpcs command, you need to add php before it.

If it is provided by Linux, because the path to the php execution file is already in the environment PATH, add the path of bin (or scripts) to the PATH, and you can execute the phpcs command on the terminal.

But in the Windows system, the phpcs.bat file is actually executed, which in turn references the phpcs file in the same directory.

In phpcs.bat, we need to configure two variables to execute the phpcs command correctly in CMD.

As follows, you need to specify the absolute location of the php.exe and phpcs files:

If "% PHPBIN%" = "" set PHPBIN=D:\ php56n\ php.exeif not exist "% PHPBIN%" if "% PHP_PEAR_PHP_BIN%" neq "goto USE_PEAR_PATHGOTO RUN:USE_PEAR_PATHset PHPBIN=%PHP_PEAR_PHP_BIN%:RUN"% PHPBIN% "D:\ www\ PHP_CodeSniffer-2.9.1\ scripts\ phpcs"% *

Then add the path D:\ www\ PHP_CodeSniffer-2.9.1\ scripts\ to PATH, and you can execute phpcs in CMD.

Note: phpcbf also needs to be modified like this.

2 use

Above we see that PHP_CodeSniffer has two orders.

By default, PHP_CodeSniffer uses the PEAR specification to check the code.

2.1 use the command

The following command uses the default specification to check files and directories.

$phpcs / path/to/code/myfile.php # check file $phpcs / path/to/code # check all files in directories and subdirectories $phpcs-l / path/to/code # check all files in directory Does not include the subdirectory $phpcs / path/to/code/myfile.inc / path/to/code/my_dir # check files and directory 2.2 check results

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

$phpcs / path/to/code/myfile.phpFILE: / 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 Murray-

If you do not display a warning, add a-n parameter:

$phpcs-n / path/to/code/myfile.php

Only a summary of the inspection results is displayed:

$phpcs-- report=summary / path/to/code2.3 specification

You can use the-- standard parameter to specify one or more specifications to check.

$phpcs-- standard=PEAR / path/to/code/myfile.inc # using the built-in specification $phpcs-- standard=/path/to/MyStandard / path/to/code/myfile.inc # using the specification $phpcs-- standard=PEAR,PHPCS under the specified path / path/to/MyStandard file.php # uses multiple specifications $phpcs-- config-set default_standard Squiz # modifies the default specification to Squiz (originally PEAR)

View existing specifications:

$phpcs-i2.4 View help $phpcs-h this article on "how to install and use PHP_CodeSniffer" ends here, thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to install and use PHP_CodeSniffer". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report