dvsa / coding-standards
驾驶和车辆标准局(DVSA)中使用的PHP编码标准。符合PSR规范。
Requires
- php: ^7.4|^8.0
- squizlabs/php_codesniffer: ^3.1
Requires (Dev)
Suggests
- friendsofphp/php-cs-fixer: To use the shared coding standards ruleset for PHP CS Fixer
This package is auto-updated.
Last update: 2024-09-17 21:59:15 UTC
README
为驾驶员和车辆标准局(DVSA)使用的PHP CodeSniffer和PHP-CS-Fixer提供自定义规则集。PHP编码标准由DVSA的开发者社区维护和更新。有关如何进行更改的更多信息,请参阅CONTRIBUTING.md。
先决条件
安装
composer require --dev dvsa/coding-standards
或者,如果您要全局安装编码标准工具,请使用
composer require global dvsa/coding-standards
如何设置
有关如何在代码编辑器/IDE中运行/集成的帮助,请参阅集成或如何运行部分。
PHP CodeSniffer
仓库: https://github.com/squizlabs/PHP_CodeSniffer。
- 在项目的根目录中创建一个
phpcs.dist.xml
文件。示例<?xml version="1.0"?> <ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="DVSA" xsi:noNamespaceSchemaLocation="./vendor/squizlabs/php_codesniffer/phpcs.xsd"> <file>./path/to/directory</file> <file>./path/to/file.php</file> <exclude-pattern>*/vendor/*</exclude-pattern> <rule ref="./vendor/dvsa/coding-standards/src/Profiles/DVSA/CS/ruleset.xml" /> </ruleset>
PHP-CS-Fixer
仓库: https://github.com/FriendsOfPHP/PHP-CS-Fixer。
由于php-cs-fixer
中依赖项的数量,它必须手动安装。
- 推荐使用composer-bin-plugin安装。
$ composer require --dev bamarni/composer-bin-plugin
- 安装
php-cs-fixer
。建议在单独的工作目录中安装PHP-CS-Fixer
$ composer bin php-cs-fixer require friendsofphp/php-cs-fixer dvsa/coding-standards
- 在项目的根目录中创建一个
.php-cs-fixer.dist.php
文件。 - 将
.php-cs-fixer.cache
添加到您的.gitignore
。缓存文件名可以使用$cacheFilename
参数更改(默认:.php-cs-fixer.cache
)。 - 配置并返回一个
PhpCsFixer\ConfigInterface
对象。此仓库提供了一个预配置的类<?php $cacheFilename = '.php-cs-fixer.cache'; $finder = PhpCsFixer\Finder::create() ->exclude('vendor') ->in(__DIR__); // Any additional project rules/rule sets. $additionalRules = [ // Rule sets '@PHP80Migration:risky' => true, '@PHP81Migration' => true, '@Symfony' => true, '@Symfony:risky' => true, // Individual rules 'protected_to_private' => false, ]; return (new \Dvsa\PhpCodingStandards\PhpCsFixer\Config())($finder, $additionalRules, $cacheFilename);
提示:将.php-cs-fixer.php
添加到您的.gitignore
以在本地启用额外的规则。
如何运行
GitHooks
建议使用工具来处理GitHooks功能,因为设置钩子(符号链接/复制)需要手动步骤。有许多工具可以用来处理GitHooks。
Husky & lint-staged (NPM)
仓库: Husky & lint-staged 包。
- 使用NPM安装
$ npm i --save-dev husky lint-staged
- 配置Husky
$ npm set-script prepare "husky install"
$ npm run prepare
- 创建
.lintstagedrc
{ "**/*.php": [ "php ./vendor/bin/php-cs-fixer fix --diff --quiet", "php ./vendor/bin/phpcs --standard=./phpcs.dist.xml --report=diff --colors" ] }
- 将
lint-staged
添加到pre-commit
钩子中
$ npx husky add .husky/pre-commit "lint-staged"
上述pre-commit钩子将利用PHP-CS-Fixer和PHPCS。修复器将在PHP CodeSniffer之前运行。
集成
JetBrains (IntelliJ/PHPStorm)
JetBrains提供了全面的文档来设置代码质量工具。
PHP CodeSniffer规则集路径: /phpcs.dist.xml
PHP CS Fixer规则集路径: /.php-cs-fixer.dist.php
许可证
请参阅 LICENSE.md
贡献
请参阅 CONTRIBUTING.md
致谢
此编码标准是在PSR2标准之上进行微调的结果,之所以能够实现,正是因为有了这个基础。然而,特别要感谢Chris EmersonBJSS在VOL项目期间花费的时间和精力,对这项工作进行了整理并持续推进。