fattureincloud / php-code-standard
PHP CS Fixer 编码标准,扩展自 @PhpCsFixer。
1.1.4
2023-11-13 16:56 UTC
Requires
- php: >=5.4.0
README
PHP CS Fixer 编码标准,扩展自 @PhpCsFixer。
先决条件
您需要在项目中本地安装 PHP CS Fixer,并配置 husky + lint-staged,才能确保所有功能正常运行。
安装 PHP CS Fixer
composer require friendsofphp/php-cs-fixer --dev
安装 husky + lint-staged
创建一个像这样的 package.json
{
"description": "For automation purposes",
"license": "MIT",
"devDependencies": {
"husky": "^4.2.5",
"lint-staged": "^10.2.7"
},
"lint-staged": {},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
}
}
运行 npm install
。
将这些行添加到 composer.json
"scripts": {
...
"post-install-cmd": "npm_config_loglevel=silent npm install",
"post-update-cmd": "npm_config_loglevel=silent npm install",
...
}
最后,记得将 node_modules/
添加到 .gitignore
。
安装和用法
composer require fattureincloud/php-code-standard --dev
添加 lint-staged 行动
将以下行作为 lint-staged 行动放入 package.json
,以便在提交时执行 PHP CS Fixer。
"lint-staged": {
...
"*.php": "php ./vendor/bin/php-cs-fixer fix --allow-risky=yes --config ./vendor/fattureincloud/php-code-standard/.php-cs-fixer.php"
...
},
添加 composer 脚本(可选)
在 composer.json
的 scripts 属性下添加以下行。
"scripts": {
...
"php-cs-fixer": "./vendor/bin/php-cs-fixer --allow-risky=yes --config=./vendor/fattureincloud/php-code-standard/.php-cs-fixer.php"
...
}
PHPStorm
启用检查
- 在“质量工具”下搜索“PHP CS Fixer”
- 添加本地配置,路径为
$PROJECT_DIR$/vendor/friendsofphp/php-cs-fixer/php-cs-fixer
- 在“检查”下搜索“PHP CS Fixer 验证”
- 标记“允许 ricky 规则”
- 选择自定义规则集
- 点击三个点并输入
<your-project-directory>/vendor/fattureincloud/php-code-standard/.php-cs-fixer.php
配置文件监视器
创建一个具有以下值的文件监视器
Name: php-cs-fixer
File type: PHP
Scope: Current File
Program: $ProjectFileDir$/vendor/friendsofphp/php-cs-fixer/php-cs-fixer
Arguments: fix --allow-risky=yes --config=$ProjectFileDir$/vendor/fattureincloud/php-code-standard/.php-cs-fixer.php $FileDir$/$FileName$
Output paths to refresh: $FileDir$/$FileName$
取消所有高级选项的标记。
配置外部工具(可选)
创建一个具有以下值的外部工具
Name: Run PHP CS Fixer
Description: Run PHP CS Fixer
Group: External Tools
Program: $ProjectFileDir$/vendor/friendsofphp/php-cs-fixer/php-cs-fixer
Arguments: fix --allow-risky=yes --config=$ProjectFileDir$/vendor/fattureincloud/php-code-standard/.php-cs-fixer.php --verbose $FilePath$
Working directory: $ProjectFileDir$
标记 执行后同步文件
和 打开控制台以显示工具输出
。
VSCode
待办事项