php-collective/code-sniffer
Composer 安装命令:
composer require --dev php-collective/code-sniffer
包简介
PhpCollective Code Sniffer Standards
README 文档
README
This sniffer package follows PSR-2 completely and ships with a lot of additional fixers on top (incl. PSR-12). Please see the PhpCollective Coding conventions for details.
Documentation
See docs.
Upstream docs: squizlabs/PHP_CodeSniffer/wiki
Usage
How to use in projects
Make sure you include the sniffer as require-dev dependency:
composer require --dev php-collective/code-sniffer
Then set up a phpcs.xml file in your project ROOT, e.g.:
<?xml version="1.0"?> <ruleset name="your-app-name"> <file>src/</file> <file>tests/</file> <rule ref="vendor/php-collective/code-sniffer/PhpCollective/ruleset.xml"/> <!-- Here you can add more or also silence existing ones --> </ruleset>
How to use in any project
You can manually invoke the phpcs/phpcbf commands:
vendor/bin/phpcs
vendor/bin/phpcbf
The command phpcs just sniffs, phpcbf fixes.
It is recommended to set up composer scripts to have shortcuts:
"scripts": {
"cs-check": "phpcs -nps",
"cs-fix": "phpcbf -nps"
}
Then you can run composer cs-c and composer cs-f and don't have to worry about the acronyms.
Standards
You can always switch the standard to the stricter one named PhpCollectiveStrict.
It is an extension of the PhpCollective standard with its own (strict) sniffs added on top.
How to include in your IDE
E.g. for PHPStorm:
- Open Settings -> Tools -> External Tools
- Add a new tool named "cs-sniffer" and set Program to
$ProjectFileDir$/vendor/bin/phpcs, Parameters to--standard=$ProjectFileDir$/vendor/php-collective/code-sniffer/PhpCollective/ruleset.xml -p $FilePath$and Working directory to$ProjectFileDir$. - Add a new tool named "cs-fixer" and set Program to
$ProjectFileDir$/vendor/bin/phpcbf, Parameters to--standard=$ProjectFileDir$/vendor/php-collective/code-sniffer/PhpCollective/ruleset.xml -v $FilePath$and Working directory to$ProjectFileDir$. - Remove the "Open console" if you don't want to see any output here for the fixer.
- Now set up your hotkeys under Settings -> Keymap (search for cs-sniffer and cs-fixer). E.g.
Control + Commafor sniffing, andControl + Dotfor fixing.
You can also set up file watchers, but here you should better only whitelist certain sniffs that only add things and don't remove anything.
How to configure the default rule set
In order to simplify command line interface, phpcs allows to specify default rule set in and standards path the following way.
Assuming the following directory structure:
vendor/php-collective/code-sniffer/ # Base directory
|_ PhpCollective/ # Rule set name
|_ ruleset.xml # Rule set
The base directory and rule set can be used in configuration now.
vendor/bin/phpcs --config-set installed_paths vendor/php-collective/code-sniffer/
vendor/bin/phpcs --config-set default_standard PhpCollective
You might need to specify full directory path. Now the tools can be used without --standard switch.
Make sure that you include the PhpCollective core standard ruleset in your custom one, e.g.:
<?xml version="1.0"?> <ruleset name="PhpCollectiveProject"> <description> PhpCollective Coding Standard for Project. Extends main PhpCollective Coding Standard. All sniffs in ./Sniffs/ will be auto loaded </description> <rule ref="vendor/php-collective/code-sniffer/PhpCollective/ruleset.xml"/> <!-- Define your own sniffs here --> </ruleset>
If you want to use the PhpCollectiveStrict standard in your project, you should replace the string:
<rule ref="vendor/php-collective/code-sniffer/PhpCollective/ruleset.xml"/>
with this one:
<rule ref="vendor/php-collective/code-sniffer/PhpCollectiveStrict/ruleset.xml"/>
php-collective/code-sniffer 适用场景与选型建议
php-collective/code-sniffer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 342.91k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2023 年 10 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「static analysis」 「standards」 「phpcs」 「codesniffer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 php-collective/code-sniffer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 php-collective/code-sniffer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 php-collective/code-sniffer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
TwigStan is a static analyzer for Twig templates powered by PHPStan
Sentiment analysis library for PHP.
Database Standardization and Analysis Tool for Laravel
Enforce architecture by defining modules with allowed dependencies. Detects forbidden, uncovered, missing and unused module dependencies in PHP projects.
统计信息
- 总下载量: 342.91k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 19
- 依赖项目数: 23
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-07