gansel-rechtsanwaelte/php-cs-fixer-config

此包已被弃用且不再维护。没有建议的替代包。

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

2.2.0 2022-04-11 07:10 UTC

README

Integrate Prune Release Renew

Code Coverage

Latest Stable Version Total Downloads

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

安装

运行

$ composer require --dev gansel-rechtsanwaelte/php-cs-fixer-config

用法

配置

选择一个规则集

在项目根目录创建一个配置文件 .php_cs

<?php

use Gansel\PhpCsFixer\Config;

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

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

return $config;

Git

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

+ /.build/
 /vendor/

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

带头部的配置

💡 可选:指定一个头部

 <?php

 use Gansel\PhpCsFixer\Config;

+$header = <<<EOF
+Copyright (c) 2021 Gansel Rechtsanwälte
+
+For the full copyright and license information, please view
+the LICENSE file that was distributed with this source code.
+
+@see https://github.com/gansel-rechtsanwaelte/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.cache');

 return $config;

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

<?php

/**
 * Copyright (c) 2021 Gansel Rechtsanwälte
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 *
 * @see https://github.com/gansel-rechtsanwaelte/php-cs-fixer-config
 */

带覆盖规则的配置

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

 <?php

 use Gansel\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.cache');

 return $config;

Makefile

如果你喜欢 Makefile,创建一个 Makefile,并添加一个 coding-standards 目标

+.PHONY: coding-standards
+coding-standards: vendor
+	 mkdir -p .build/php-cs-fixer
+	 vendor/bin/php-cs-fixer fix --config=.php_cs --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": {
     "gansel-rechtsanwaelte/php-cs-fixer-config": "~1.0.0"
+  },
+  "scripts": {
+    "coding-standards": [
+      "mkdir -p .build/php-cs-fixer",
+      "php-cs-fixer fix --diff --diff-format=udiff --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 --diff --diff-format=udiff --dry-run --verbose"

变更日志

请查看 CHANGELOG.md

贡献

请查看 CONTRIBUTING.md

行为准则

请查看 CODE_OF_CONDUCT.md

许可协议

此软件包采用 MIT 许可协议授权。

请查看 LICENSE.md

致谢

本项目受到 ergebnis/php-cs-fixer-config 的启发。