hks-systeme/php-cs-fixer-config

为 friendsofphp/php-cs-fixer 提供配置工厂和多个规则集。

2.9.0 2022-02-07 21:57 UTC

README

Integrate Merge Prune Release Renew Triage

Code Coverage Type Coverage

Latest Stable Version Total Downloads

friendsofphp/php-cs-fixer 提供配置工厂和多个规则集。

安装

运行

$ composer require --dev hks-systeme/php-cs-fixer-config

使用方法

配置

选择一个规则集

在项目的根目录下创建一个配置文件 .php-cs-fixer.php

<?php

use HKS\PhpCsFixer\Config;

$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php74());

$config->getFinder()->in(__DIR__);
$config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/.php-cs-fixer.cache');

return $config;

Git

所有配置示例都使用了缓存功能,如果你想使用它,你应该将缓存目录添加到 .gitignore

+ /.build/
 /vendor/

💡 个人而言,我更喜欢使用 .build 目录来存储构建工件。

带有头部信息的配置

💡 可选:指定一个头部信息

 <?php

 use HKS\PhpCsFixer\Config;

+$header = <<<EOF
+Copyright (c) 2021 HKS Systeme GmbH
+
+For the full copyright and license information, please view
+the LICENSE file that was distributed with this source code.
+
+@see https://github.com/hks-systeme/php-cs-fixer-config
+EOF;

-$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php74());
+$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php74($header));

 $config->getFinder()->in(__DIR__);
 $config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/.php-cs-fixer.cache');

 return $config;

这将启用并配置 HeaderCommentFixer,以便在 PHP 文件中添加文件头部,例如

<?php

/**
 - Copyright (c) 2021 HKS Systeme GmbH
 *
 - For the full copyright and license information, please view
 - the LICENSE file that was distributed with this source code.
 *
 - @see https://github.com/hks-systeme/php-cs-fixer-config
 */

带有覆盖规则的配置

💡 可选:通过传递一个要合并的规则数组来覆盖规则集的规则

 <?php

 use HKS\PhpCsFixer\Config;

-$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php74());
+$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php74(), [
+    'mb_str_functions' => false,
+    'strict_comparison' => false,
+]);

 $config->getFinder()->in(__DIR__);
 $config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/.php-cs-fixer.cache');

 return $config;

Makefile

如果你喜欢 Makefile,创建一个带有 coding-standards 目标的 Makefile

+.PHONY: coding-standards
+coding-standards: vendor
+	 mkdir -p .build/php-cs-fixer
+	 vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --diff --verbose

 vendor: composer.json composer.lock
     composer validate
     composer install

运行

$ make coding-standards

以自动修复编码标准违规。

Composer 脚本

如果你喜欢 composer 脚本,在 composer.json 中添加一个 coding-standards 脚本

 {
   "name": "foo/bar",
   "require": {
     "php": "^7.4",
   },
   "require-dev": {
     "hks-systeme/php-cs-fixer-config": "~1.0.0"
+  },
+  "scripts": {
+    "coding-standards": [
+      "mkdir -p .build/php-cs-fixer",
+      "php-cs-fixer fix --diff --verbose"
+    ]
   }
 }

运行

$ composer coding-standards

以自动修复编码标准违规。

GitHub Actions

如果你喜欢 GitHub Actions,在你的工作流程中添加一个 coding-standards 作业

 on:
   pull_request: null
   push:
     branches:
       - main

 name: "Integrate"

 jobs:
+  coding-standards:
+    name: "Coding Standards"
+
+    runs-on: ubuntu-latest
+
+    strategy:
+      matrix:
+        php-version:
+          - "7.4"
+
+    steps:
+      - name: "Checkout"
+        uses: "actions/checkout@v2"
+
+      - name: "Install PHP with extensions"
+        uses: "shivammathur/setup-php@v2"
+        with:
+          coverage: "none"
+          php-version: "${{ matrix.php-version }}"
+
+      - name: "Cache dependencies installed with composer"
+        uses: "actions/cache@v2"
+        with:
+          path: "~/.composer/cache"
+          key: "php-${{ matrix.php-version }}-composer-${{ hashFiles('composer.lock') }}"
+          restore-keys: "php-${{ matrix.php-version }}-composer-"
+
+      - name: "Install locked dependencies with composer"
+        run: "composer install --no-interaction --no-progress --no-suggest"
+
+      - name: "Create cache directory for friendsofphp/php-cs-fixer"
+        run: mkdir -p .build/php-cs-fixer
+
+      - name: "Cache cache directory for friendsofphp/php-cs-fixer"
+        uses: "actions/cache@v2"
+        with:
+          path: "~/.build/php-cs-fixer"
+          key: "php-${{ matrix.php-version }}-php-cs-fixer-${{ github.sha }}"
+          restore-keys: "php-${{ matrix.php-version }}-php-cs-fixer-"
+
+      - name: "Run friendsofphp/php-cs-fixer"
+       run: "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --diff --dry-run --verbose"

变更日志

请查看 CHANGELOG.md

贡献

请查看 .github/CONTRIBUTING.md

行为准则

请查看 CODE_OF_CONDUCT.md

许可证

此软件包使用 MIT 许可证授权。

请查看 LICENSE.md

鸣谢

本项目灵感来源于 ergebnis/php-cs-fixer-config