iodigital-com/php-code-sniffer-baseliner
Composer 安装命令:
composer require iodigital-com/php-code-sniffer-baseliner
包简介
IO PHP Code Sniffer Baseliner
README 文档
README
This tool enables you to integrate PHP_CodeSniffer into an existing
project by automatically adding phpcs:ignore and phpcs:disable/phpcs:enable instructions throughout the codebase
as a baseline. This allows you to make PHP_CodeSniffer pass without changing any source code, making it
possible to use PHP_CodeSniffer in e.g. continuous integration pipelines or git hooks. This way, you can enforce that
all new code adheres to your coding standard without touching the existing code.
Installation
Require the package with composer:
composer require --dev iodigital-com/php-code-sniffer-baseliner
It is also possible to install this package as a global composer dependency.
Usage
In order to add phpcs:ignore and phpcs:disable/phpcs:enable instructions throughout your project, run:
vendor/bin/phpcs-baseliner create-baseline
How does it work?
First, the tool runs vendor/bin/phpcs and captures the report. Based on the report output, it will add
// phpcs:ignore instructions to the source code for each violation. It will only ignore the sniffs that actually are
violated. In rare cases, adding these instructions could introduce new violations. Therefore, this process is repeated
until no violations are reported by phpcs.
Example
Let's say we want to enforce declare(strict_types = 1); statements and native property type hints using
PHP_CodeSniffer. The Slevomat Coding Standard has sniffs for this:
SlevomatCodingStandard.TypeHints.DeclareStrictTypes
and SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint. We install
Slevomat Coding Standard and add the sniffs to our ruleset in phpcs.xml.
If we now run vendor/bin/phpcs-baseliner create-baseline in our project, it will add ignore instructions in all files
not containing declare(strict_types = 1); statements or native property type declarations:
- <?php + <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes -- baseline class Foo { + // phpcs:ignore SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint -- baseline private $bar; }
In some cases, it is not possible to insert a // phpcs:ignore instruction directly above the violated line (e.g.
multi-line strings). In those cases, // phpcs:disable and // phpcs:enable instructions are added:
<?php
class Foo {
+ // phpcs:disable Generic.Files.LineLength.TooLong -- baseline
public const BAR = '
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas malesuada, lectus vitae vestibulum vulputate, mi morbi.';
+ // phpcs:enable Generic.Files.LineLength.TooLong -- baseline
}
Features
- Automatic indentation
- Ignoring a group of multiple exclusions per line, e.g.
// phpcs:ignore Generic.Files.LineLength.TooLong, Generic.Arrays.DisallowLongArraySyntax -- baseline - Merging new instructions with existing instructions
- Messages of existing instructions are merged as wel:
// phpcs:ignore Generic.Files.LineLength.TooLong, Generic.Arrays.DisallowLongArraySyntax -- existing message; baseline - Using
phpcs:disable/phpcs:enablewhen insertingphpcs:ignoreis not possible (i.e. for multi-line strings, including HEREDOCs and NOWDOCs) - Adding a star prefix when a violation is found within a comment block with stars, e.g.:
/* * phpcs:ignore Generic.Files.LineLength.TooLong * Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas malesuada, lectus vitae vestibulum vulputate, mi morbi. */
- All features are unit tested, see the
AddBaselineProcessorTestDataProviderclass for an extensive test data set.
Roadmap
- Support processing files that do not start with
<?phpon the first line. - Support processing files that contain
?>. - Support ignoring violations on the first line of a file that end with a multi-line string, example:
<?php echo 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas malesuada, lectus vitae vestibulum vulputate, mi morbi.'; ?>
- Support detection of and merging with older types of ignore instructions, such as
@phpcsSuppress.
iodigital-com/php-code-sniffer-baseliner 适用场景与选型建议
iodigital-com/php-code-sniffer-baseliner 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 41.05k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2023 年 10 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 iodigital-com/php-code-sniffer-baseliner 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 iodigital-com/php-code-sniffer-baseliner 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 41.05k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-23