exolnet / phpcs-config
eXolnet 对 PHP CodeSniffer 的配置。
Requires
- php: >=7.2
Requires (Dev)
This package is auto-updated.
Last update: 2024-08-25 18:11:59 UTC
README
收集了各种编码风格工具的可共享配置,以确保我们项目中的配置一致性。
设置
composer require --dev exolnet/phpcs-config
- 运行以下命令以运行标准检查
vendor/bin/phpcs --standard=vendor/exolnet/phpcs-config .
您可以将此添加到您的 Travis YAML 文件中作为测试
script: - phpunit - vendor/bin/phpcs --standard=vendor/exolnet/phpcs-config .
排除文件
此标准包括对特殊支持 .phpcsignore
文件(未来,这应该被集成到 phpcs 中)。只需在您的根目录(您将从那里运行 phpcs
)中放置一个 .phpcsignore
文件。
此文件的格式类似于 .gitignore
和类似文件:每行一个模式,注释行应以 #
开头,只包含空白的行将被忽略。
# Exclude our tests directory.
tests/
# Exclude any file ending with ".inc"
*\.inc
请注意,模式应匹配 PHP_CodeSniffer 风格:*
被转换为 .*
以便使用,但所有其他字符都像正则表达式一样工作。
模式相对于 .phpcsignore
文件所在的目录。在加载时,它们将被转换为绝对模式:例如,在 /your/dir/.phpcsignore
中的 */tests/*
将成为正则表达式 /your/dir/.*/tests/.*
。 这与常规的 PHP_CodeSniffer 实践不同。
高级/扩展
如果您想添加更多规则(如特定于 laravel 的规则),您可以创建自己的自定义标准文件(例如 ruleset.xml
)。
<?xml version="1.0"?> <ruleset> <file>app</file> <file>config</file> <file>database</file> <file>routes</file> <file>tests</file> <!-- Use eXolnet Coding Standards --> <rule ref="vendor/exolnet/phpcs-config" /> <!-- Add Laravel-specific rules --> <rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace"> <exclude-pattern>database/*</exclude-pattern> </rule> </ruleset>
然后您可以在运行 phpcs 时引用此文件。
vendor/bin/phpcs --standard=ruleset.xml .
排除/禁用检查
您还可以自定义规则以排除不适用于项目的元素。
<rule ref="vendor/exolnet/phpcs-config"> <!-- Disable short array syntax --> <exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace" /> </rule>
规则也可以在行内禁用。您可以使用 // @codingStandardsIgnoreLine
注释来禁用 phpcs 规则,并使用 /* eslint disable ... */
注释来禁用 ESLint 规则。
要找出这些代码是什么,请在运行 phpcs
时指定 -s
,代码将一起输出。您可以指定完整的代码,或部分代码以禁用错误组。
安全性
如果您发现任何与安全相关的问题,请通过电子邮件 security@exolnet.com 而不是使用问题跟踪器。