Skip to content

Commit

Permalink
fix: 支持php-cs-fixer 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
titrxw committed May 28, 2021
1 parent 953a731 commit 2aeea00
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"type": "composer-plugin",
"require": {
"composer-plugin-api": "*",
"friendsofphp/php-cs-fixer": "^2.15"
"friendsofphp/php-cs-fixer": "^3.0"
},
"require-dev": {
"composer/composer": "~1.0"
"composer/composer": "~2.0"
},
"autoload": {
"psr-4" : {
Expand Down
48 changes: 26 additions & 22 deletions src/Helper/.php_cs → src/Helper/.php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
<?php

/**
* 使用说明
* WeEngine System
*
* 基于 php-cs-fixer 插件,请先使用composer安装此插件
* (c) We7Team 2019 <https://www.w7.cc>
*
* 1、Preferences -> Tools -> External Tools 添加工具
* 2、添加 php-cs-fixer 工具,参如如下:
* Name: php-fixer
* Description: php-fixer
* Program: /{项目根目录}/vendor/friendsofphp/php-cs-fixer/php-cs-fixer
* Arguments: --config=$ProjectFileDir$/.php_cs --verbose fix "$FileDir$/$FileName$"
* Working directory: 同Program
* 3、Keymap -> External Tools -> php-fixer 设置快捷键 Alt+f
* 4、打开要格式化的文件,按 Alt+f 保存即可
* This is not a free software
* Using it under the license terms
* visited https://www.w7.cc for more details
*/

include_once 'vendor/autoload.php';

define('SOFT_NAME', 'WeEngine Api System');
const SOFT_NAME = 'WeEngine Api System';

$finder = PhpCsFixer\Finder::create()
->files()
Expand All @@ -27,29 +22,38 @@
->ignoreDotFiles(true)
->ignoreVCS(true);

$fixers = array(
$fixers = [
'@PSR2' => true,
'single_quote' => true, //简单字符串应该使用单引号代替双引号;
'no_unused_imports' => true, //删除没用到的use
'no_singleline_whitespace_before_semicolons' => true, //禁止只有单行空格和分号的写法;
'no_empty_statement' => true, //多余的分号
'no_extra_consecutive_blank_lines' => true, //多余空白行
'no_extra_blank_lines' => true, //多余空白行
'no_blank_lines_after_phpdoc' => true, //注释和代码中间不能有空行
'no_empty_phpdoc' => true, //禁止空注释
'phpdoc_indent' => true, //注释和代码的缩进相同
'no_blank_lines_after_class_opening' => true, //类开始标签后不应该有空白行;
'include' => true, //include 和文件路径之间需要有一个空格,文件路径不需要用括号括起来;
'no_trailing_comma_in_list_call' => true, //删除 list 语句中多余的逗号;
'no_leading_namespace_whitespace' => true, //命名空间前面不应该有空格;
'standardize_not_equals' => true, //使用 <> 代替 !=;
'blank_line_after_opening_tag' => true, //PHP开始标记后换行
'indentation_type' => true,
'concat_space' => [
'spacing' => 'one',
],
'space_after_semicolon' => [
'remove_in_empty_for_expressions' => true,
],
'header_comment' => [
'comment_type' => 'PHPDoc',
'header' => SOFT_NAME . " \r\n\r\n(c) We7Team 2019 <https://www.w7.cc> \r\n\r\nThis is not a free software \r\nUsing it under the license terms\r\nvisited https://www.w7.cc for more details",
'comment_type' => 'PHPDoc',
'header' => SOFT_NAME . " \r\n\r\n(c) We7Team 2019 <https://www.w7.cc> \r\n\r\nThis is not a free software \r\nUsing it under the license terms\r\nvisited https://www.w7.cc for more details",
],
'braces' => ['position_after_functions_and_oop_constructs' => 'same'], //设置大括号换行,暂时根本Psr
//'binary_operator_spaces' => ['default' => 'align_single_space'], //等号对齐、数字箭头符号对齐
);
return PhpCsFixer\Config::create()
'braces' => ['position_after_functions_and_oop_constructs' => 'same']

];
$config = new \PhpCsFixer\Config();
return $config
->setRules($fixers)
->setFinder($finder)
->setIndent("\t")
->setUsingCache(false);
->setIndent("\t");
4 changes: 2 additions & 2 deletions src/Helper/pre-commit-cs-fix
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
echo "php-cs-fixer pre commit hook start"

PHP_CS_FIXER="vendor/bin/php-cs-fixer"
PHP_CS_CONFIG=".php_cs"
PHP_CS_CONFIG=".php-cs-fixer.php"
CHANGED_FILES=$(git diff --cached --name-only --diff-filter=ACM -- '*.php')

echo $CHANGED_FILES

if [ -n "$CHANGED_FILES" ]; then
$PHP_CS_FIXER --config=.php_cs --verbose fix $CHANGED_FILES;
$PHP_CS_FIXER --config=$PHP_CS_CONFIG --verbose fix $CHANGED_FILES;
git add $CHANGED_FILES;
fi

Expand Down
15 changes: 8 additions & 7 deletions src/Plugin/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,21 @@ public function installGitPreHook() {
throw new \Exception('not a git project');
}

if (!file_exists($projectDir . '/.php_cs')) {
$filesystem->copy(dirname(__DIR__) . '/Helper/.php_cs', $projectDir . '/.php_cs');
$fixFileName = 'pre-commit-cs-fix-30';
if (!file_exists($projectDir . '/.php-cs-fixer.php')) {
$filesystem->copy(dirname(__DIR__) . '/Helper/.php-cs-fixer.php', $projectDir . '/.php-cs-fixer.php');
}
if (file_exists($hookDir . 'pre-commit-cs-fix')) {
if (file_exists($hookDir . $fixFileName)) {
return true;
}

$filesystem->copy(dirname(__DIR__) . '/Helper/pre-commit-cs-fix', $hookDir . 'pre-commit-cs-fix');
$list[] = $hookDir . 'pre-commit-cs-fix';
$filesystem->copy(dirname(__DIR__) . '/Helper/pre-commit-cs-fix', $hookDir . $fixFileName);
$list[] = $hookDir . $fixFileName;

if (file_exists($hookDir . 'pre-commit')) {
file_put_contents($hookDir . 'pre-commit', "\n exec " . $hookDir . 'pre-commit-cs-fix', FILE_APPEND);
file_put_contents($hookDir . 'pre-commit', "\n exec " . $hookDir . $fixFileName, FILE_APPEND);
} else {
file_put_contents($hookDir . 'pre-commit', "#!/bin/bash \n exec " . $hookDir . 'pre-commit-cs-fix');
file_put_contents($hookDir . 'pre-commit', "#!/bin/bash \n exec " . $hookDir . $fixFileName);
$list[] = $hookDir . 'pre-commit';
}
$this->changePermission($list);
Expand Down

0 comments on commit 2aeea00

Please sign in to comment.