datana-gmbh/php-cs-fixer-config

此包已被弃用且不再维护。作者建议使用 ergebnis/php-cs-fixer-config 包。

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

2.3.0 2022-07-28 20:19 UTC

README

Integrate Prune Release Renew

Code Coverage

Latest Stable Version Total Downloads

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

安装

运行

$ composer require --dev datana-gmbh/php-cs-fixer-config

用法

配置

选择一个规则集

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

<?php

use Datana\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 Datana\PhpCsFixer\Config;

+$header = <<<EOF
+Copyright (c) 2021 Datana GmbH
+
+For the full copyright and license information, please view
+the LICENSE file that was distributed with this source code.
+
+@see https://github.com/datana-gmbh/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 Datana GmbH
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 *
 * @see https://github.com/datana-gmbh/php-cs-fixer-config
 */

带覆盖规则的配置

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

 <?php

 use Datana\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,创建一个包含 coding-standards 目标的 Makefile,以自动修复编码标准违规。

+.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

to automatically fix coding standard violations.

Composer 脚本

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

 {
   "name": "foo/bar",
   "require": {
     "php": "^7.4",
   },
   "require-dev": {
     "datana-gmbh/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

to automatically fix coding standard violations.

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的启发。