// 安装后 将 ~/.composer/vendor/bin/ 添加到 PATH 环境变量中,否则会报命令找不到。
composer global require"squizlabs/php_codesniffer=*"
$ phpcs--help
Usage:phpcs[-nwlsaepqvi][-d key[=value]][--colors][--no-colors]
[--cache[=]][--no-cache][--tab-width=]
[--report=][--report-file=][--report-=
e>]
[--report-width=][--basepath=][--bootstrap=
>]
[--severity=][--error-severity=][--warning-severity=
erity>]
[--runtime-setkey value][--config-setkey value][--config-deletekey][--co
nfig-show]
[--standard=][--sniffs=][--exclude=]
[--encoding=][--parallel=][--generator=]
[--extensions=][--ignore=][--ignore-annotations]
[--stdin-path=][--file-list=]-...
-CheckSTDIN instead of local files and directories
-nDonot print warnings(shortcutfor--warning-severity=0)
-wPrintboth warnings and errors(thisis thedefault)
-lLocaldirectory only,no recursion
-sShowsniff codes in all reports
-aRuninteractively
-eExplaina standard by showing the sniffs it includes
-pShowprogress of the run
-qQuietmode;disables progress and verbose output
-mStoperror messages from being recorded
(saves a lot of memory,but stops many reports from being used)
-vPrintprocessed files
-vvPrintruleset and token output
-vvvPrintsniff processing information
-iShowa list of installed coding standards
-dSetthe[key]php.ini value to[value]or[true]ifvalue is omitted
--helpPrintthishelp message
--versionPrintversion information
--colorsUsecolors in output
--no-colorsDonot use colors in output(thisis thedefault)
--cacheCacheresults between runs
--no-cacheDonot cache results between runs(thisis thedefault)
--ignore-annotationsIgnoreall@codingStandardannotations in code comments
Usea specific fileforcaching(uses a temporary file by defaul
t)
A path to strip from the front of file paths inside reports
A comma separated list of files to run before processing begins
Oneor more files and/or directories to check
A file containing a list of files and/or directories to check(o
ne per line)
Theencoding of the files being checked(defaultis utf-8)
A comma separated list of file extensions to check
(extension filtering only valid when checking a directory)
Thetype of the file can be specified using:ext/type
e.g.,module/php,es/js
Useseither the"HTML","Markdown"or"Text"generator
(forces documentation generation instead of checking)
A comma separated list of patterns to ignore files and directori
es
Howmany files should be checked simultaneously(defaultis1)
Printeither the"full","xml","checkstyle","csv"
"json","junit","emacs","source","summary","diff"
"svnblame","gitblame","hgblame"or"notifysend"report
(the"full"report is printed bydefault)
Writethe report to the specified file path
Howmany columns wide screen reports should be printed
orsetto"auto"to use current screen width,where supported
Theminimum severity required to display an error or warning
A comma separated list of sniff codes to include or exclude from
checking
(all sniffs must be part of the specified standard)
Thename or path of the coding standard to use
Ifprocessing STDIN,the file path that STDIN will be processed
as
Thenumber of spaces each tab represents
$ phpcs-i
Theinstalled coding standards areMySource,PEAR,PSR1,PSR2,SquizandZend
$ phpcs--report=summary downloadexcel.php
PHP CODE SNIFFER REPORT SUMMARY
--------------------------------------------------------------------------------
FILE ERRORS WARNINGS
--------------------------------------------------------------------------------
downloadexcel.php515
--------------------------------------------------------------------------------
A TOTAL OF51ERROR(S)AND5WARNING(S)WERE FOUND IN1FILE(S)
--------------------------------------------------------------------------------
Time:1second,Memory:2.50Mb
$ phpcs downloadexcel.php
FILE:downloadexcel.php
--------------------------------------------------------------------------------
FOUND51ERROR(S)AND5WARNING(S)AFFECTING40LINE(S)
--------------------------------------------------------------------------------
1|ERROR|Endof line character is invalid;expected"\n"but found
||"\r\n"
1|ERROR|Missingfile doc comment
3|WARNING|Lineexceeds85characters;contains153characters
178|WARNING|Lineexceeds85characters;contains115characters
179|ERROR|Doccommentforvar[UNKNOWN]does not match actual variable
||name&$data at position1
179|ERROR|Missingparameter name at position1
179|ERROR|Missingcommentforparam"[ UNKNOWN ]"at position1
179|ERROR|Theremust be exactly one blank line before the tags in
||functioncomment
180|ERROR|Lastparameter comment requires a blank newline after it
180|ERROR|Thevariable namesforparameters UNKNOWN(1)and[UNKNOWN]
||(2)donot align
180|ERROR|Doccommentforvar[UNKNOWN]does not match actual variable
||name $fields at position2
180|ERROR|Missingparameter name at position2
180|ERROR|Missingcommentforparam"[ UNKNOWN ]"at position2
183|ERROR|Openingbrace should be on anewline
186|WARNING|Lineexceeds85characters;contains142characters
187|ERROR|Openingparenthesis of a multi-linefunctioncall must be the
||last content on the line
190|ERROR|Expected"if (...) {\n";found"if(...){\n"
190|ERROR|Theremust be a single space between the closing parenthesis
||and the opening brace of a multi-line IF statement;found0
||spaces
192|ERROR|Expected"} else {\n";found"}else{\n"
207|ERROR|Closingparenthesis of a multi-linefunctioncall must be on a
||line by itself
210|ERROR|Spacesmust be used to indent lines;tabs are not allowed
210|ERROR|Doccommentfor"$filename"missing
212|ERROR|Missing@returntag infunctioncomment
213|ERROR|Functionname"export_csv"is prefixedwitha package name but
||does not beginwitha capital letter
213|ERROR|Lineindented incorrectly;expected0spaces,found4
225|ERROR|Spacesmust be used to indent lines;tabs are not allowed
226|ERROR|Spacesmust be used to indent lines;tabs are not allowed
226|ERROR|Lineindented incorrectly;expected0spaces,found2
227|ERROR|Spacesmust be used to indent lines;tabs are not allowed
228|ERROR|Closingbrace indented incorrectly;expected2spaces,found8
229|ERROR|Lineindented incorrectly;expected0spaces,found8
232|ERROR|Lineindented incorrectly;expected4spaces,found12
233|ERROR|Lineindented incorrectly;expected8spaces,found16
234|WARNING|Lineexceeds85characters;contains148characters
237|ERROR|Lineindented incorrectly;expected4spaces,found12
243|ERROR|Lineindented incorrectly;expected0spaces,found8
247|ERROR|Lineindented incorrectly;expected0spaces,found8
248|ERROR|Lineindented incorrectly;expected4spaces,found12
250|ERROR|Lineindented incorrectly;expected8spaces,found16
252|ERROR|Lineindented incorrectly;expected8spaces,found16
258|ERROR|Lineindented incorrectly;expected0spaces,found8
265|ERROR|Lineindented incorrectly;expected0spaces,found8
266|ERROR|Expected"if (...) {\n";found"if(...){\n"
266|ERROR|Lineindented incorrectly;expected4spaces,found12
266|ERROR|Theremust be a single space between the closing parenthesis
||and the opening brace of a multi-line IF statement;found0
||spaces
269|WARNING|Lineexceeds85characters;contains94characters
270|ERROR|Lineindented incorrectly;expected4spaces,found12
271|ERROR|Lineindented incorrectly;expected8spaces,found16
272|ERROR|Lineindented incorrectly;expected12spaces,found20
274|ERROR|Expected"} elseif (...) {\n";found"} elseif(...) {\n"
274|ERROR|Lineindented incorrectly;expected12spaces,found20
277|ERROR|Lineindented incorrectly;expected8spaces,found16
279|ERROR|Nospace found after comma infunctioncall
281|ERROR|Lineindented incorrectly;expected8spaces,found16
285|ERROR|Nospace found after comma infunctioncall
285|ERROR|Nospace found after comma infunctioncall
--------------------------------------------------------------------------------
Time:1second,Memory:2.50Mb
// phpcbf downloadexcel.php 直接覆盖原文件,可以使用 --suffix 指定修复后的代码后缀,生成文件 downloadexcel.php.fixed
$ phpcbf downloadexcel.php--suffix=.fixed
PHPCBF RESULT SUMMARY
----------------------------------------------------------------------
FILE FIXED REMAINING
----------------------------------------------------------------------
downloadexcel.php5716
----------------------------------------------------------------------
A TOTAL OF57ERRORS WERE FIXED IN1FILE
----------------------------------------------------------------------
Time:5.36secs;Memory:5Mbhttps://github.com/overtrue/phpmd-rulesets
phpcs/path/to/code--suffix=.fixed