PHP_CodeSniffer installation and usage

Source: Internet
Author: User
Tags coding standards pyrus
1. introduction & nbsp; PHP_CodeSniffer is a package written in PHP5 in PEAR used to check and sniff whether PHP code violates a set of preset encoding standards, it is an essential development tool to ensure that your code is concise and consistent, and can even help programmers reduce semantic errors. & Nbsp; the encoding standard in PHP_CodeSniffer is a collection of sniffing files, each 1. Overview

? PHP_CodeSniffer is a package written in PHP5 in PEAR to check and sniff whether PHP code violates a set of preset encoding standards, it is an essential development tool to ensure that your code is concise and consistent, and can even help programmers reduce semantic errors.

? The encoding standard in PHP_CodeSniffer is a set of sniffing files. each sniffing file only checks part of the encoding standard. Once installed, you can check the coding standards of multiple different projects. What is the default encoding standard used by PHP_CodeSniffer? PEAR encoding standard.

2. install quick installation (php and pear must be installed first)

Pear install PHP_CodeSniffer

Users who use sublime text can directly install the plug-in phpcs. installation method (Ctrl + Shift + P-> pi (package install)-> phpcs, right-click to view the PHP Code Sniffer option)

Install Pyrus

Php pyrus. phar install pear/PHP_CodeSniffer

3. example

To check whether a file complies with the PEAR encoding standard, you only need to specify the file path:

Use PHP_CodeSniffer to check the file

$ Phpcs/path/to/code/myfile. php

FILE:/path/to/code/myfile. php

--------------------------------------------------------------------------------

FOUND 5 ERROR (S) AFFECTING 2 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

51 | ERROR | Missing function doc comment

88 | ERROR | Line not indented correctly; expected 9 spaces but found 6

--------------------------------------------------------------------------------

Alternatively, if you want to check the entire directory, you can specify the directory path instead of the file path:

Use PHP_CodeSniffer to check the Directory

$ Phpcs/path/to/code

FILE:/path/to/code/myfile. php

--------------------------------------------------------------------------------

FOUND 5 ERROR (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

51 | ERROR | Missing function doc comment

88 | ERROR | Line not indented correctly; expected 9 spaces but found 6

--------------------------------------------------------------------------------

FILE:/path/to/code/yourfile. php

--------------------------------------------------------------------------------

FOUND 1 ERROR (S) AND 1 WARNING (S) AFFECTING 1 LINE (S)

--------------------------------------------------------------------------------

21 | ERROR ?? | PHP keywords must be lowercase; expected "false" but found

??? | ???????? | "FALSE"

21 | WARNING | Equals sign not aligned with surrounding assignments

--------------------------------------------------------------------------------

More details: http://pear.php.net/manual/en/package.php.php-codesniffer.intro.php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.