Neo Anderson's Blog

Laravel/lumen 项目中集成自动化语法校验Git-Hook

字数统计: 856阅读时长: 4 min
2022/01/11

团队的不断壮大,更新迭代, 在编码风格,有必要形成一套统一的规范, 本文档主要介绍了在GIT(VCS), 在各提交,推送等阶段,代码风格,编码规则验证,提示等功能组件的安装与配置;

基础依赖安装

更新composer.json 文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
"require-dev": {
"bruli/php-git-hooks": "~4.0"
...
}

"scripts": {
...

"post-install-cmd": [
"PhpGitHooks\\Infrastructure\\Composer\\ConfiguratorScript::buildConfig"
],
"post-update-cmd": [
"PhpGitHooks\\Infrastructure\\Composer\\ConfiguratorScript::buildConfig"
]
},


"config": {
"bin-dir": "bin"
}
升级全局依赖(之后项目根目录(包含composer.json文件)执行)
1
composer update -vvv
之后初始化hook配置,执行如下命令:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
composer install  

Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Nothing to install or update
Generating autoload files
> PhpGitHooks\Infrastructure\Composer\ConfiguratorScript::buildConfig
//是否开启pre-commit 提交钩子验证
Do you want enable PRE-COMMIT hook?: [Y/n]y
Write a right message for pre-commit hook: HEY, GOOD JOB!!
Write an error message for pre-commit hook: FIX YOUR FUCKING CODE!!
Do you want enable COMPOSER tool?: [Y/n]
Do you want enable JSONLINT tool?: [Y/n]
Do you want enable PHPLINT tool?: [Y/n]
Do you want enable PHPMD tool?: [Y/n]
Write options for PHPMD tool if you want use it: [NONE]--debug
Do you want enable PHPCS tool?: [Y/n]y
Which standard do you want to user for PHPCS tool?: [PSR1/PSR2/PHPCS/MySource/Zend/Squiz/PEAR]
Write ignore pattern for PHPCS, if you like: [No ignore pattern]*/resources/*,resources/*,vendor/*,*/tests/*,tests/*,*/data/*,*/vendor/*,*/storage/*
Do you want enable PHP-CS-FIXER tool?: [Y/n]
Enable psr0 level for PHP-CS-FIXER?: [Y/n]
Enable psr1 level for PHP-CS-FIXER?: [Y/n]
Enable psr2 level for PHP-CS-FIXER?: [Y/n]
Enable symfony level for PHP-CS-FIXER?: [Y/n]
Write options for PHP-CS-FIXER tool if you want use it: [NONE]
Do you want enable PHPUNIT tool?: [Y/n]n
Do you want enable STRICT COVERAGE tool?: [Y/n]y
Write minimum coverage to guard: [0.00]
Do you want enable GUARD COVERAGE tool?: [Y/n]
Write an error message for guard coverage:WARNING!!, your code coverage is lower.
Do you want enable COMMIT_MSG hook?: [Y/n]
Write a regular expression: [#[0-9]{2,7}]
Do you want enable PRE-PUSH hook?: [Y/n]
Write a right message for pre-push hook: PUSH IT!!
Write an error message for pre-push hook: YOU CAN NOT PUSH CODE!!
Do you want enable PHPUNIT tool?: [Y/n]n
Do you want enable STRICT COVERAGE tool?: [Y/n]
Write minimum coverage to guard: [0.00]
Do you want enable GUARD COVERAGE tool?: [Y/n]
Write an error message for guard coverage:WARNING!!, your code coverage is lower.
配置文件手工确认说明: 执行上述命令后,会产生名为 php-git-hooks.yml (项目根目录)的配置文件;可以视情况,手动开启/关闭相关校验(不推荐关闭)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
pre-commit:
enabled: true
execute:
composer: true
jsonlint: true
phplint: true
phpmd:
enabled: true
options: '--debug'
phpcs:
enabled: true
standard: 'PSR2'
ignore: '*/resources/*,resources/*,vendor/*,*/tests/*,tests/*,*/data/*,*/vendor/*,*/storage/*'
php-cs-fixer:
enabled: true
levels:
psr0: true
psr1: true
psr2: true
symfony: true
options: null
phpunit:
enabled: false
random-mode: false
options: null
strict-coverage:
enabled: true
minimum: !!float 0
guard-coverage:
enabled: true
message: 'WARNING!!, your code coverage is lower.'
message:
right-message: 'HEY, GOOD JOB!!'
error-message: 'FIX YOUR FUCKING CODE!!'
commit-msg:
enabled: true
regular-expression: '[#[0-9]{2,7}]'
pre-push:
enabled: true
execute:
phpunit:
enabled: false
random-mode: false
options: null
strict-coverage:
enabled: true
minimum: !!float 0
guard-coverage:
enabled: true
message: 'WARNING!!, your code coverage is lower.'
message:
right-message: 'PUSH IT!!'
error-message: 'YOU CAN NOT PUSH CODE!!'
使扩展生效
1
2
3
4
5
6
7
8
9
10
11
12
13
14
从composer组件中拷贝相关配置文件到项目根目录
cp vendor/bruli/php-git-hooks/phpunit.xml.dist ./
cp vendor/bruli/php-git-hooks/PmdRules.xml ./
...

开启pre-commit校验:
cp vendor/bruli/php-git-hooks/src/PhpGitHooks/Infrastructure/Hook/pre-commit .git/hooks

开启commit-msg校验:
cp vendor/bruli/php-git-hooks/src/PhpGitHooks/Infrastructure/Hook/commit-msg .git/hooks

开启pre-push校验
cp vendor/bruli/php-git-hooks/src/PhpGitHooks/Infrastructure/Hook/pre-push .git/hooks

参考文章

CATALOG
  1. 1. 基础依赖安装
    1. 1.1. 更新composer.json 文件
    2. 1.2. 升级全局依赖(之后项目根目录(包含composer.json文件)执行)
    3. 1.3. 之后初始化hook配置,执行如下命令:
    4. 1.4. 配置文件手工确认说明: 执行上述命令后,会产生名为 php-git-hooks.yml (项目根目录)的配置文件;可以视情况,手动开启/关闭相关校验(不推荐关闭)
    5. 1.5. 使扩展生效