isaac / php-code-sniffer-standard
Requires
- php: ~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0
- dealerdirect/phpcodesniffer-composer-installer: ^0.7
- phpcompatibility/php-compatibility: ^9.3
- slevomat/coding-standard: ^7.0.19 || ^8.0
- squizlabs/php_codesniffer: ^3.6.0
Requires (Dev)
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.0
- phpstan/phpstan-strict-rules: ^1.0
- phpunit/phpunit: ^9.5
Conflicts
- isaac/php-code-sniffer: *
- dev-develop
- v28.2.0
- v28.1.0
- v28.0.0
- v27.0.0
- v26.0.0
- v25.1.0
- v25.0.0
- v24.0.0
- v23.0.0
- v22.0.0
- v21.0.0
- v20.0.0
- v19.0.0
- v18.2.0
- v18.1.0
- v18.0.0
- v17.0.1
- 17.0.0
- v16.0.0
- v15.1.0
- v15.0.1
- v15.0.0
- v14.0.1
- v14.0.0
- v13.0.2
- v13.0.1
- v13.0.0
- v12.0.0
- v11.0.0
- v10.0.0
- v9.0.0
- 8.0.0
- v7.0.1
- v7.0.0
- v6.0.0
- v5.0.0
- v4.0.0
- v3.0.1
- v3.0.0
- v2.0.1
- v2.0.0
- v1.0.1
- v1.0.0
- dev-master
- dev-feature/remove-php-7-3-support
- dev-feature/add-php-8-2-support
- dev-feature/add-support-for-slevomat/coding-standard-v8
- dev-feature/add-allow-plugins-config
- dev-feature/add-disallow-goto-operator-sniff
- dev-feature/add-generic-php-backtickoperator-sniff
- dev-feature/disallow-all-superglobals
- dev-feature/add-unused-variable-sniff
- dev-fix/remove-explicit-squiz-whitespace-superfluouswhitespace-reference
- dev-fix/remove-explicit-generic-files-lineendings-reference
- dev-fix/remove-explicit-psr12-operator-spacing-sniff
- dev-feature/remove-useless-overriding-method-rule
- dev-feature/update-phpcs-configuration
- dev-feature/remove-phpcs-ignore
- dev-feature/run-phpstan-in-test-workflow
- dev-feature/upgrade-phpstan-to-version-1
- dev-feature/add-php-8-1-support-and-remove-php-7-2-support
- dev-feature/add-php-8-1-support
- dev-feature/ICOMP-614-upgrade-php-version
- dev-feature/ICOMP-614-upgrade-php-version-0794d55e-fea2-4f28-917d-5b38274ec10f
- dev-feature/ICOMP-614-upgrade-php-version-7a2b7ebf-69aa-4f29-9f92-dd1126a12165
- dev-add-project-inventory-json-file
- dev-feature/replace-object-calisthenics-metrics-maxnestinglevel-rule
- dev-fix/remove-explicit-reference-to-isaac-rules
- dev-feature/add-sniff-to-disallow-spaces-surrounding-object-operators
- dev-feature/add-php-8-0-support
- dev-fix/resolve-php-7-2-and-7-3-incompatibilities
This package is auto-updated.
Last update: 2023-07-14 10:23:18 UTC
README
此存储库已存档并重命名,移动到 iO PHP_CodeSniffer Standard。功能嗅探和更改将在 iO 存储库中处理。
要使用 iodigital-com/php-code-sniffer-standard
替换 isaac/php-code-sniffer-standard
,请执行以下步骤
-
从
composer.json
中移除isaac/php-code-sniffer-standard
composer remove --dev --no-update isaac/php-code-sniffer-standard
-
安装
iodigital-com/php-code-sniffer-standard
composer require --dev iodigital-com/php-code-sniffer-standard
注意:如果您不在最新版本,您可能希望在要求新包时包含版本约束。
-
在您的项目的
phpcs.xml
中,将<rule ref="ISAAC"/>
替换为<rule ref="IO"/>
。 -
在
phpcs.xml
和 PHP 文件中替换对特定 ISAAC 嗅探的任何引用,用对 IO 嗅探的引用替换。这可以通过在整个项目中搜索嗅探名称并使用新嗅探名称替换它们来完成搜索 替换为 ISAAC.Classes.MethodPerClassLimit IO.Classes.MethodPerClassLimit ISAAC.Classes.PropertyPerClassLimit IO.Classes.PropertyPerClassLimit ISAAC.ControlStructures.DisallowGotoOperator IO.ControlStructures.DisallowGotoOperator ISAAC.ControlStructures.DisallowNullCoalesceOperator IO.ControlStructures.DisallowNullCoalesceOperator ISAAC.Namespaces.MultipleLinesPerUse IO.Namespaces.MultipleLinesPerUse
通过执行以下操作来验证 PHP_CodeSniffer 是否仍然正确工作
vendor/bin/phpcs
ISAAC PHP_CodeSniffer Standard
通过 ISAAC 规则扩展默认的 PHP_CodeSniffer
注意:向此包添加新的 phpcs-rules 必须导致主要版本更新!
安装
要求包
composer require --dev isaac/php-code-sniffer-standard
设置
在您的项目的根目录中创建一个 phpcs.xml
文件,并包含默认的 ISAAC 规则集
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="phpcs-isaac">
<!-- include root folder of project -->
<file>.</file>
<!-- exclude paths -->
<exclude-pattern>./src/Migrations</exclude-pattern>
<exclude-pattern>./vendor</exclude-pattern>
<!-- include all rules in isaac ruleset -->
<rule ref="ISAAC"/>
</ruleset>
更改规则集名称,修改排除路径和/或为您的项目包含自定义规则集。
PHPCompatibility
要充分利用 PHPCompatibility 标准,您应该指定一个测试版本进行检查。这将启用对已弃用/删除的 PHP 功能的检查以及使用新 PHP 功能的代码的检测。通过在 phpcs.xml
中添加一个配置规则来包含测试版本。示例
<config name="testVersion" value="7.0"/> <!-- check for compatability with php 7.0 -->
<config name="testVersion" value="7.1-"/> <!-- check for 7.1 and higher -->
<config name="testVersion" value="7.0-7.2"/> <!-- check within range 7.0 to 7.2 -->
有关更多信息,请参阅: https://github.com/PHPCompatibility/PHPCompatibility#using-a-custom-ruleset
使用方法
由于您现在在项目的根目录中有一个 phpcs.xml
文件,您现在可以运行默认的 phpcs 命令: vendor/bin/phpcs
。
忽略嗅探违规
有时无法解决嗅探违例。在这种情况下,应使用phpcs:ignore
和phpcs:disable
/ phpcs:enable
注释来忽略违例。
为了做到这一点,请采取以下方法:
- 仅忽略导致违例的部分文件,而不是整个文件。如果确实需要忽略整个文件,可以使用
phpcs:ignoreFile
注释或更好的方法是向项目的ruleset.xml
添加一个<exclude-pattern>
。 - 优先使用
phpcs:ignore
而不是phpcs:disable
和phpcs:enable
,即在可能的情况下使用phpcs:ignore
,并且当phpcs:ignore
的位置不会引入其他嗅探违例时,否则使用phpcs:disable
和phpcs:enable
。理由:当添加新代码或移动现有代码(例如重构代码)时,使用phpcs:disable
和phpcs:enable
可能会禁用比最初打算的更多代码。 - 始终明确指出要忽略的确切嗅探或嗅探组合,使用完整的嗅探名称,而不是仅使用嗅探组。例如,使用
phpcs:ignore Squiz.WhiteSpace.FunctionSpacing.BeforeFirst, Squiz.WhiteSpace.FunctionSpacing.AfterLast
而不是phpcs:ignore Squiz.WhiteSpace.FunctionSpacing
或没有任何参数的phpcs:ignore
。 - 优先将
phpcs:ignore
注释放置在违例之前的单独一行,而不是放置在违例所在的行上。理由:当忽略多个嗅探时,phpcs:ignore
注释可能会迅速超过行长度限制;当将phpcs:ignore
注释放置在违例之前的单独一行时,不会进行此检查,而当将phpcs:ignore
注释放置在违例所在的行上时,则会进行检查。 - 使用
--
后跟简短的解释来添加忽略嗅探的原因。
示例
try { $this->logger->log(LogLevel::INFO, new DateTimeImmutable()); //phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- DateTimeImmutable creation cannot fail in this case } catch (Exception $exception) { }
贡献
如果您想进行贡献,请创建一个带有每个合并请求一个嗅探的合并请求。请在描述中提供一个示例,说明嗅探的内容,并附上良好和不良代码片段。