Skip to content

Commit

Permalink
change composer.json
Browse files Browse the repository at this point in the history
  • Loading branch information
WangGeng committed Dec 24, 2019
2 parents e9af2fb + 2674681 commit 9bb4138
Show file tree
Hide file tree
Showing 10 changed files with 338 additions and 200 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/vendor/
vendor/
.idea
.DS_Store
composer.lock
composer.lock
49 changes: 39 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,49 @@

PHP code style check 利用git hook、phplint、phpcs在git commit的时候对php代码进行语法检测、代码风格检查,如果有问题,不允许提交。

# 前置要求
- Git已安装
- PHP安装并全局可用 Windows下载最新版php [php-7.3.8-nts-Win32-VC15-x64.zip](https://windows.php.net/downloads/releases/php-7.3.8-nts-Win32-VC15-x64.zip)
- Composer可用 [Windows下载 Composer](https://getcomposer.org/Composer-Setup.exe)

## 检测原理 及流程
- 每次git commit前是有hooks可以触发
- 开发特定脚本,在git hooks触发的时候执行
- 首先利用phpline检测提交文件的语法是否有错
- 再次利用phpcs检测php的风格规范是否是否符合特定的规范
- 符合规范 可以继续执行git push
- 不符合规范,本次commit失败,需要根据错误提示更改
- 再次 git add && git commit

# 使用
composer require --dev webergiles/php-csc
composer require --dev webergiles/php-csc "0.*"

安装成功之后执行`composer exec phpcsc install`该命令会检查phplint、phpcs的安装情况,并将git原有的pre-commit钩子备份,再将php-cc的pre-commit钩子拷贝至.git/hooks中。
该命令会根据本机系统检查phplint、phpcs的安装情况,并将git原有的pre-commit钩子备份,
再将php-csc的pre-commit钩子拷贝至.git/hooks中。

这样,在git commit之前,就会执行phplint和phpcs检查待提交的文件,如果不满足要求,则会组织代码提交
这样,在git commit之前,就会执行phplint和phpcs检查待提交的文件,如果不满足要求,则会阻止代码提交

# 指令
# 开放指令

指令 (composer exec -v phpcc {指令} | 用法
指令 (composer exec -v phpcsc {指令} | 用法
--- | --- |
install | 安装php-cc
remove | 移除php-cc
config | 配置
install | 安装php-csc
remove | 移除php-csc

# composer 自动挂载
在主项目composer 文件中增加事件
```json
"post-autoload-dump": [
"WeberGiles\\MountHooks\\PHPCodeChecker::hookInstall"
],
"pre-package-uninstall": [
"WeberGiles\\MountHooks\\PHPCodeChecker::hookUnstall"
]
```
可以在没次执行composer update 的时候去检测钩子挂载情况,自动挂载钩子

# 注意事项
- phpcsc的pre-commit会覆盖原有的pre-commit,但仍然会将它备份为pre-commit.bak.{timestamp}。所以之前有在pre-commit中插入操作,请谨慎安装。

#注意事项
phpcsc的pre-commit会覆盖原有的pre-commit,但仍然会将它备份为pre-commit.bak.{timestamp}。所以之前有在pre-commit中插入操作,请谨慎安装。
- Windows版本的PHPStorm 默认回车符\r\n 这是不符合PSR2 规范的 需要设置为\n
`File->Line Separators->LF - Unix and macOs (\n)`
107 changes: 27 additions & 80 deletions bin/phpcsc
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#!/usr/bin/env php
<?php

if (count($argv) < 2) {
//支持的提交动作
$action = [
'install', 'remove'
];

if (empty($argv[1]) || !in_array($argv[1], $action)) {
print<<<EOT
install: install php-csc,add pre-commit to .git/hooks,and rename the old pre-commit file to pre-commit.bak
remove: remove php-csc,remove .git/hooks/pre-commit,and mv pre-commit.bak to pre-commit
config: config php-csc,[phpcsc config key value] is to set config item [key] to value [value]
不支持的提交动作,请使用以下参数提交
install: 安装php-csc检测钩子到.git/hooks【copy pre-commit 到 .git/hooks/pre-commit】 并且备份原有钩子文件
remove: 删除php-csc检测钩子 .git/hooks/pre-commit 删除钩子不会回复原有备份文件
EOT;
exit(1);
}
Expand All @@ -17,36 +22,7 @@ switch ($argv[1]) {
case 'remove':
remove();
break;
case 'config':
if (isset($argv[2]) && isset($argv[3])) {
if ('phpcs_standard' == $argv[2] && !in_array(mb_strtoupper($argv[3]), ['MYSOURCE', 'PEAR', 'PSR1', 'PSR12', 'PSR2', 'SQUIZ', 'ZEND'])) {
echo "phpcs standard error! \n";
exit(0);
}
write_ini_file($argv[2], $argv[3]);
exit(0);
} elseif (isset($argv[2]) && !isset($argv[3])) {
$config_items = get_ini_file();
if (isset($config_items[$argv[2]])) {
echo $config_items[$argv[2]] . "\n";
} else {
echo "No config for key [{$argv[2]}]\n";
}
exit(0);
} else {
echo 'Need more params' . "\n";
}
break;
default:
echo 'No such command...' . "\n";
break;
}
print <<<EOT
install: install php-csc,add pre-commit to .git/hooks,and rename the old pre-commit file to pre-commit.bak
remove: remove php-csc,remove .git/hooks/pre-commit,and mv pre-commit.bak to pre-commit
config: config item-key [set-value]
EOT;
exit(0);

function install()
{
Expand All @@ -59,32 +35,39 @@ function install()

# check phplint
echo "Checking phplint install...\n";
exec('./vendor/bin/phplint --version', $phplint_check_rs, $return_var);
if ($return_var) {
exec('./vendor/bin/phplint --version', $phpLintCheckRs, $returnVar);
if ($returnVar) {
echo "Checking phplint failed! Please install phplint first!";
exit(1);
} else {
echo "Checking phplint success!\n";
echo $phplint_check_rs[0] . "\n";
echo $phpLintCheckRs[0] . "\n";
}

# check phpcs
echo "Checking phpcs install...\n";
exec('phpcs --version', $phpcs_check_rs, $return_var);
if ($return_var) {
exec('phpcs --version', $phpCsCheckRs, $returnVar);
if ($returnVar) {
echo "Checking phpcs failed! Please install phpcs first!";
exit(1);
} else {
echo "Checking phpcs success!\n";
echo $phpcs_check_rs[0] . "\n";
echo $phpCsCheckRs[0] . "\n";
}
# check&&mv pre-commit
if (is_file('./.git/hooks/pre-commit')) {
# check&&back pre-commit
if (is_file('./.git/hooks/pre-commit')
&& md5_file('./.git/hooks/pre-commit') != md5_file('./vendor/giles/php-csc/pre-commit')) {
exec('mv ./.git/hooks/pre-commit ./.git/hooks/pre-commit.bak.' . time());
}
exec('cp ./vendor/webergiles/php-csc/pre-commit ./.git/hooks');
if (is_file('./.git/hooks/pre-commit')
&& md5_file('./.git/hooks/pre-commit') == md5_file('./vendor/giles/php-csc/pre-commit')) {
echo "php-csc install success!\n";
exit(0);
}

echo "php-cc install success!\n";
exec('cp ./vendor/giles/php-csc/pre-commit ./.git/hooks');

echo "php-csc install success!\n";
exit(0);
}

Expand All @@ -96,42 +79,6 @@ function remove()
exec('rm -f .git/hooks/pre-commit');
}

# check old file
echo "Retrieve the old pre-commit file...\n";
if (is_file('.git/hooks/pre-commit.bak')) {
exec('mv .git/hooks/pre-commit.bak .git/hooks/pre-commit');
}

echo "Remove phpcc success!\n";
echo "Remove phpcsc success!\n";
exit(0);
}

function get_ini_file()
{
$file_name = './vendor/webergiles/php-csc/phpcsc.ini';
$str = file_get_contents($file_name);
$ini_list = explode("\n", $str);
$ini_items = array();
foreach ($ini_list as $item) {
$one_item = explode("=", $item);
if (isset($one_item[0]) && isset($one_item[1])) {
$ini_items[trim($one_item[0])] = trim($one_item[1]);
}
}
return $ini_items;
}

function write_ini_file($key, $value)
{
$config_file_path = './vendor/webergiles/php-csc/phpcsc.ini';
$content = file_get_contents($config_file_path);
if (strpos($content, "$key=")) {
$content = preg_replace("/$key=.*/", "$key=$value", $content);
} else {
$content .= "\n";
$content .= "$key=$value";
}
$file = fopen($config_file_path, 'w');
fwrite($file, $content);
return true;
}
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"name": "giles/php-csc",
"keywords": ["psr", "psr-11", "phpcs", "phpcsc"],
"license": "MIT",
"description": "PHP code style check 利用git hook、phplint、phpcs在git commit的时候对php代码进行语法检测、代码风格检查,如果有问题,不允许提交。",
"type": "library",
"require": {
"php": ">=7.1",
"squizlabs/php_codesniffer": "3.*",
"overtrue/phplint": "1.1.9"
"overtrue/phplint": "1.*"
},
"require-dev": {
"php": "^7."
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0"
},
"bin": [
"bin/phpcsc"
Expand All @@ -19,5 +21,10 @@
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Giles\\MountHooks\\": "src/"
}
},
"minimum-stability": "dev"
}
9 changes: 0 additions & 9 deletions phpcsc.ini

This file was deleted.

97 changes: 0 additions & 97 deletions pre-commit

This file was deleted.

Loading

0 comments on commit 9bb4138

Please sign in to comment.