locomotivemtl / charcoal-coding-standard
Charcoal 编码规范
Requires
- php: >=5.6.0 || >=7.0
- squizlabs/php_codesniffer: ^3.3
Requires (Dev)
- phpunit/phpunit: ^5.0 || ^6.0 || ^7.0
This package is auto-updated.
Last update: 2024-08-26 05:06:27 UTC
README
这是 PHP_CodeSniffer 规则集,用于检查存储库是否遵循 Charcoal 框架使用的标准。
标准
Charcoal 编码规范扩展并扩展了 PSR-1 和 PSR-2 PSR-12。
有关完整执行参考,请参阅 Charcoal/ruleset.xml
。
安装
1. 项目安装
您可以将 Charcoal 编码规范作为您项目的 Composer 依赖项进行安装。
-
通过 Composer 安装包
$ composer require --dev locomotivemtl/charcoal-coding-standard
-
使用 PHP_CodeSniffer 注册新标准
$ ./vendor/bin/phpcs --config-set installed_paths ./vendor/locomotivemtl/charcoal-coding-standard
第二个命令将标准注册到
phpcs
。确保不要覆盖现有的installed_paths
选项的值。 -
在您的存储库的根目录中创建一个包含以下内容的
phpcs.xml
文件<?xml version="1.0"?> <ruleset name="Charcoal Package"> <rule ref="Charcoal" /> </ruleset>
您可以从该文件中包含或排除 sniffs。有关自定义的参考,请参阅 PHP_CodeSniffer 注释规则集。
注意:如果您没有使用 PHP_CodeSniffer 注册 Charcoal 编码规范,您可以使用其路径而不是其名称引用标准
<rule ref="./vendor/locomotivemtl/charcoal-coding-standard/Charcoal/ruleset.xml" />
示例:
phpcs.xml.example
。 -
将脚本添加到您的
composer.json
"scripts": { "cs-check": "phpcs --colors -p ./src ./tests", "cs-fix": "phpcbf --colors ./src ./tests" }
2. 全局安装
您还可以全局安装 Charcoal 编码规范
-
通过 Composer 安装包
$ composer global require locomotivemtl/charcoal-coding-standard
-
使用 PHP_CodeSniffer 注册新标准
$ phpcs --config-set installed_paths ~/.composer/vendor/locomotivemtl/charcoal-coding-standard
第二个命令将标准注册到
phpcs
。确保不要覆盖现有的installed_paths
选项的值。
用法
Sniffing
根据您如何安装 Charcoal 编码规范,您可以执行以下操作
使用以下任一方法检测此标准违反情况
$ phpcs --standard=Charcoal /path/to/some/file/to/sniff.php $ ./vendor/bin/phpcs --standard=Charcoal /path/to/some/file/to/sniff.php $ composer cs-check
使用以下方法自动修复此标准的违反情况
$ phpcbf --standard=Charcoal /path/to/some/file/to/sniff.php $ ./vendor/bin/phpcbf --standard=Charcoal /path/to/some/file/to/sniff.php $ composer cs-fix
有关更多信息,请参阅 CLI 文档。
注意:如果您没有使用 PHP_CodeSniffer 注册 Charcoal 编码规范,您可以使用其路径而不是其名称引用标准
$ ./vendor/bin/phpcs --standard=./vendor/locomotivemtl/charcoal-coding-standard /path/to/code
Linting
Charcoal 编码规范提供了一个简单但方便的 bash 脚本包装器,围绕 php -l
递归地同时对 PHP 文件进行语法检查,使用多个进程同时执行。
Usage:
phplint [options] [<path>]...
Arguments:
path One or more paths to files or directories to search for PHP files.
Defaults to ./src and ./tests.
Options:
-h, --help Display this help message and exit.
-V, --version Display version information and exit.
示例
$ phplint src/Http/Controllers/ src/Providers/RouteServiceProvider.php
注意:脚本不是
composer.json
的一部分;当安装包时,它不会添加到指定的项目中。您必须从供应商包中调用脚本,或者您必须手动将其符号链接到您的bin-dir
。
您可以从您的项目中运行脚本
$ ./vendor/locomotivemtl/charcoal-coding-standard/bin/phplint ./src ./tests
您还可以将脚本符号链接到您的项目的
bin-dir
$ cd $(composer config bin-dir) $ ln -s ../locomotivemtl/charcoal-coding-standard/bin/phplint ./phplint有关检查 PHP 文件的更高级和可配置的工具,请参阅 overtrue/phplint。
致谢
- Chauncey McAskill chauncey@locomotive.ca
- Mathieu Ducharme mat@locomotive.ca
许可
Charcoal 在 MIT 许可下授权。有关详细信息,请参阅 LICENSE。