dcandido / code-sniffer
CodeSniffer.
v1.6.0
2022-09-01 16:55 UTC
Requires
This package is auto-updated.
Last update: 2024-09-30 01:52:13 UTC
README
目录
要求
- squizlabs/php_codesniffer 3.6或更高版本
如何安装
composer require dcandido/quality-tools --dev
如何使用
启用规则
将其添加到项目的phpcs.xml
规则集中
<?xml version="1.0"?> <ruleset name="IMSRuleset"> <description>The coding standard for Project.</description> <rule ref="./vendor/dcandido/code-sniffer/ruleset.xml"/> <file>app</file> <file>bootstrap</file> <exclude-pattern>bootstrap/cache/*</exclude-pattern> <exclude-pattern>tests/Unit/*</exclude-pattern> <exclude-pattern>resources</exclude-pattern> <exclude-pattern>bootstrap/autoload.php</exclude-pattern> <exclude-pattern>*/migrations/*</exclude-pattern> <exclude-pattern>*/seeds/*</exclude-pattern> <exclude-pattern>vendor/*</exclude-pattern> <exclude-pattern>public/*</exclude-pattern> <exclude-pattern>*.js</exclude-pattern> <!-- Show progression --> <arg value="pv"/> <!-- p for progress, s to see list for each gitblame author, v to see authors with no violations, s include source codes in the report --> <arg name="report" value="summary"/><!-- summary, gitblame, source --> <arg name="colors"/> </ruleset>
使用方法
运行命令
./vendor/bin/phpcs
如果您有错误,您将看到以下内容
$ phpcs FILE: /path/to/code/file1.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/file2.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 --------------------------------------------------------------------------------
您可以使用phpcbf自动修复错误
./vendor/bin/phpcbf
PHPCBF RESULT SUMMARY ---------------------------------------------------------------------- FILE FIXED REMAINING ---------------------------------------------------------------------- .../path/to/code/file.php 7 0 .../path/to/code/file2.php 5 0
在PHPStorm中检查代码
有关如何在PhpStorm中设置CodeSniffer的详细信息,请参阅PhpStorm中的PHP Code Sniffer。