From 278abbf3b8e550a86dd168b9ea0d09b3d3e887b5 Mon Sep 17 00:00:00 2001 From: Anyon Date: Fri, 8 May 2026 11:32:24 +0800 Subject: [PATCH] =?UTF-8?q?chore(quality):=20=E6=8E=A5=E5=85=A5=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E5=8C=96=E9=9D=99=E6=80=81=E5=88=86=E6=9E=90=E5=92=8C?= =?UTF-8?q?=20CI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 PHP-CS-Fixer 配置,并保留项目统一文件头。 - 新增 PHPStan 配置与 composer analyse/cs 脚本,删除 minimum-stability: dev。 - GitHub Actions 拆分静态分析、版本矩阵测试和最低依赖测试。 - 忽略 PHPStan 缓存,避免本地分析缓存进入仓库。 --- .github/workflows/ci.yml | 47 +++++++++++++++++ .gitignore | 1 + .php-cs-fixer.php | 109 +++++++++++++++++++++++++++++++++++++++ composer.json | 7 ++- phpstan.neon | 6 +++ 5 files changed, 169 insertions(+), 1 deletion(-) create mode 100644 .php-cs-fixer.php create mode 100644 phpstan.neon diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 365d96e..ecb5692 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,31 @@ permissions: contents: read jobs: + static-analysis: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.1' + extensions: json, openssl, simplexml + coverage: none + + - name: Validate composer metadata + run: composer validate --strict + + - name: Install dependencies + run: composer install --no-interaction --prefer-dist --no-progress + + - name: Check coding style + run: composer cs:check + + - name: Run static analysis + run: composer analyse + test: runs-on: ubuntu-latest strategy: @@ -36,3 +61,25 @@ jobs: - name: Run tests run: composer test + + lowest-dependencies: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.1' + extensions: json, openssl, simplexml + coverage: none + + - name: Validate composer metadata + run: composer validate --strict + + - name: Install lowest dependencies + run: composer update --prefer-lowest --prefer-stable --no-interaction --prefer-dist --no-progress + + - name: Run tests + run: composer test diff --git a/.gitignore b/.gitignore index 385f1ac..b2b24bd 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ vendor/ composer.lock .phpunit.cache/ +.phpstan.cache/ Cache/ Test/cert/ _test/cert/ diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..9bf8e7c --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,109 @@ + + */ +use PhpCsFixer\Config; +use PhpCsFixer\Finder; +use PhpCsFixer\Runner\Parallel\ParallelConfig; + +$header = <<<'EOF' +This file is part of HyperfAdmin. + +@Link https://thinkadmin.top +@Author Anyon +EOF; + +$config = new Config(); +$config->setRiskyAllowed(true)->setParallelConfig(new ParallelConfig(8, 24)); +$finder = Finder::create() + ->files() + ->in([__DIR__ . '/src', __DIR__ . '/tests']) + ->append([__FILE__]); + +return $config->setFinder($finder)->setUsingCache(false)->setRules([ + '@PSR2' => true, + '@Symfony' => true, + '@DoctrineAnnotation' => true, + '@PhpCsFixer' => true, + 'header_comment' => [ + 'comment_type' => 'PHPDoc', + 'header' => $header, + 'separate' => 'none', + 'location' => 'after_declare_strict', + ], + 'array_syntax' => [ + 'syntax' => 'short', + ], + 'list_syntax' => [ + 'syntax' => 'short', + ], + 'blank_line_before_statement' => [ + 'statements' => [ + 'declare', + ], + ], + 'general_phpdoc_annotation_remove' => [ + 'annotations' => [ + 'author', + ], + ], + 'ordered_imports' => [ + 'imports_order' => [ + 'class', 'function', 'const', + ], + 'sort_algorithm' => 'alpha', + ], + 'ordered_types' => [ + 'null_adjustment' => 'always_last', + 'sort_algorithm' => 'alpha', + ], + 'single_line_comment_style' => [ + 'comment_types' => [], + ], + 'yoda_style' => [ + 'always_move_variable' => false, + 'equal' => false, + 'identical' => false, + ], + 'phpdoc_align' => [ + 'align' => 'left', + ], + 'multiline_whitespace_before_semicolons' => [ + 'strategy' => 'no_multi_line', + ], + 'use_arrow_functions' => true, + 'constant_case' => [ + 'case' => 'lower', + ], + 'encoding' => true, // PHP代码必须只使用没有BOM的UTF-8 + 'line_ending' => true, // 所有的PHP文件编码必须一致 + 'single_quote' => true, // 简单字符串应该使用单引号代替双引号 + 'no_empty_statement' => true, // 不应该存在空的结构体 + 'standardize_not_equals' => true, // 使用 <> 代替 != + 'blank_line_after_namespace' => true, // 命名空间之后空一行 + 'no_empty_phpdoc' => true, // 不应该存在空的 phpdoc + 'no_empty_comment' => true, // 不应该存在空注释 + 'no_singleline_whitespace_before_semicolons' => true, // 禁止在关闭分号前使用单行空格 + 'concat_space' => ['spacing' => 'one'], // 连接字符是否需要空格,可选配置项 none:不需要 one:一个空格 + 'no_leading_import_slash' => true, // use 语句中取消前置斜杠 + 'cast_spaces' => ['space' => 'none'], + 'class_attributes_separation' => true, + 'combine_consecutive_unsets' => true, + 'declare_strict_types' => true, + 'lowercase_static_reference' => true, + 'linebreak_after_opening_tag' => true, + 'multiline_comment_opening_closing' => false, + 'no_useless_else' => true, + 'no_unused_imports' => true, + 'not_operator_with_successor_space' => false, + 'not_operator_with_space' => false, + 'ordered_class_elements' => true, + 'php_unit_strict' => false, + 'phpdoc_separation' => false, + 'phpdoc_to_comment' => false, +]); diff --git a/composer.json b/composer.json index d4028e5..cdec8d3 100644 --- a/composer.json +++ b/composer.json @@ -26,6 +26,8 @@ "psr/simple-cache": "^3.0" }, "require-dev": { + "friendsofphp/php-cs-fixer": "^3.75", + "phpstan/phpstan": "^1.12", "phpunit/phpunit": "^10.5 || ^11.5" }, "autoload": { @@ -39,8 +41,11 @@ } }, "scripts": { + "analyse": "@phpstan", + "cs:check": "@php vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --dry-run --diff", + "cs:fix": "@php vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php", + "phpstan": "@php vendor/bin/phpstan analyse -c phpstan.neon", "test": "@php vendor/bin/phpunit -c phpunit.xml" }, - "minimum-stability": "dev", "prefer-stable": true } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..7002e0c --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,6 @@ +parameters: + level: 5 + paths: + - src + - tests + tmpDir: .phpstan.cache