wdes / 编码规范
Wdes编码规范
v3.3.2
2023-12-16 21:44 UTC
Requires
- php: ^7.2 || ^8.0
- slevomat/coding-standard: ^8.14.1
- squizlabs/php_codesniffer: ^3.8.0
README
Wdes编码规范
为了与PHP < 5.4
兼容而禁用的规则
Generic.Arrays.DisallowLongArraySyntax
为了与PHP < 7
兼容而禁用的规则
SlevomatCodingStandard.Classes.ClassConstantVisibility.MissingConstantVisibility
如何添加它
- 运行
composer require --dev wdes/coding-standard
- 创建文件:
phpcs.xml
,内容如下
<?xml version="1.0" encoding="UTF-8"?> <ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"> <!-- Show progress and sniff codes --> <arg value="ps"/> <!-- Cache file --> <arg name="cache" value=".php_cs.cache"/> <!-- Enable colors --> <arg name="colors"/> <!-- Make sniff report relative --> <arg name="basepath" value="."/> <file>.</file> <exclude-pattern>*/tmp/*</exclude-pattern> <exclude-pattern>*/vendor/*</exclude-pattern> <exclude-pattern>*/build/*</exclude-pattern> <exclude-pattern>*/node_modules/*</exclude-pattern> <rule ref="Wdes"></rule> </ruleset>
-
如果您已安装了 phpcs,请从开发依赖中移除它,我们为您安装。使用
composer remove squizlabs/php_codesniffer --dev
-
运行
./vendor/bin/phpcs
来查看编码规范错误 -
运行
./vendor/bin/phpcbf
来尝试修复它们
推荐的 composer.json
配置
"scripts": { "phpcbf": "@php phpcbf", "phpcs": "@php phpcs", ... }, "config": { ... "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true } },
如何排除规则
排除规则的示例方法
<?xml version="1.0" encoding="UTF-8"?> <ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd"> <!-- Show progress and sniff codes --> <arg value="ps"/> <!-- Cache file --> <arg name="cache" value=".php_cs.cache"/> <arg name="colors"/> <!-- Paths to check --> <file>src</file> <file>scripts</file> <file>examples</file> <file>tests</file> <!-- Exclude files from phpcs --> <exclude-pattern>*/src/Resources/themes/*</exclude-pattern> <exclude-pattern>*/tests/phar/data/**/*</exclude-pattern> <!-- Exclude a rule from the Wdes standard --> <rule ref="Wdes"> <exclude name="Generic.Formatting.MultipleStatementAlignment.NotSameWarning"/> </rule> <!-- Or lower the severity to zero --> <rule ref="Generic.Files.LineLength.TooLong"> <severity>0</severity> </rule> <!-- Or exclude the rule --> <exclude name="PSR1.Files.SideEffects.FoundWithSymbols"/> <!-- Or remove the rule for all files --> <rule ref="Generic.PHP.NoSilencedErrors.Discouraged"> <exclude-pattern>*</exclude-pattern> </rule> <!-- For a folder --> <rule ref="PSR1.Classes.ClassDeclaration.MissingNamespace"> <exclude-pattern>db/migrations/*</exclude-pattern> </rule> <!-- For a file --> <rule ref="Squiz.PHP.DiscouragedFunctions.Found"> <exclude-pattern>src/libs/Logger.php</exclude-pattern> </rule> </ruleset>