1、工作方式
1)本地质检:将工具安装在本地电脑依赖本地PHP解释器运行(推荐)。
2)远程质检:将工具安装在虚拟环境中依赖远程PHP解释器运行,响应速度慢(不推荐)。
2、安装所需资源
PHP解释器(根据项目需求下载对应的NTS版本)
解压后将文件夹移动到合适的位置(例如:D:\Users*\php\php8.1.16nts)。
配置环境变量,在当前用户或系统 path 中追加一条,内容为上一步php解释器存放位置的全路径,确定并退出(例如:D:\Users*\php\php8.1.16nts)。
安装composer(其作用是用来安装质检工具)
验证是否安装,重启 CMD 并输入 composer 显示安装的 composer 信息。
安装质检工具(--working-dir参数值自己定义目录)
composer require --working-dir=tools/php-cs-fixer friendsofphp/php-cs-fixer
创建并编写质检工具规则(也可自己定义规则,例如下图中 .php-cs-fixer.dist.php)
<?php
declare(strict_types=1);
/**
* @note Controller
* @author Kevin
*/
$header = <<<'EOF'
@note Controller
@author Lark
EOF;
$finder = PhpCsFixer\Finder::create()
->in([__DIR__]) // 作用域,__DIR__ 项目根目录
->name('*.php') // 作用的文件
->notName('*.blade.php') // 禁用的文件
->exclude('vendor') // 排除的目录
;
return (new PhpCsFixer\Config())
->setUsingCache(false)
->setRiskyAllowed(true) // 允许设置有风险的规则
->setRules([
'@PhpCsFixer' => true,
// 'header_comment' => [
// 'header' => $header,
// 'comment_type' => 'PHPDoc',
// ],
'declare_strict_types' => true,
'binary_operator_spaces' => ['operators' => [
'=' => 'align_single_space_minimal',
'.=' => 'align_single_space_minimal',
'=>' => 'align_single_space_minimal',
'??' => 'align_single_space_minimal',
'??=' => 'align_single_space_minimal',
]],
'no_superfluous_phpdoc_tags' => false,
'phpdoc_separation' => false,
'php_unit_construct' => true,
'php_unit_strict' => true,
])
->setFinder($finder)
;
注:这里建议将自定义质检规则放入 tools/php-cs-fixer 目录下,如:
3、进行phpstrom配置(本地、Homstead)
1)本地
- Homstead