localheinz/php-cs-fixer-config

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

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

1.24.0 2019-11-03 18:42 UTC

README

请使用 ergebnis/php-cs-fixer-config

php-cs-fixer-config

CI Status codecov Latest Stable Version Total Downloads

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

安装

运行

$ composer require --dev localheinz/php-cs-fixer-config

使用

配置

选择一个规则集

  • Localheinz\PhpCsFixer\RuleSet\Php71
  • Localheinz\PhpCsFixer\RuleSet\Php73

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

<?php

use Localheinz\PhpCsFixer\Config;

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

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

return $config;

Git

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

+ /.build/
 /vendor/

💡 个人建议使用 .build 目录来存储构建工件。

带标题的配置

💡 可选地指定标题

 <?php

 use Localheinz\PhpCsFixer\Config;

+$header = <<<EOF
+Copyright (c) 2017 Andreas Möller
+
+For the full copyright and license information, please view
+the LICENSE file that was distributed with this source code.
+
+@see https://github.com/localheinz/php-cs-fixer-config
+EOF;

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

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

 return $config;

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

<?php

/**
 * Copyright (c) 2017 Andreas Möller
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 *
 * @see https://github.com/localheinz/php-cs-fixer-config
 */

带覆盖规则的配置

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

 <?php

 use Localheinz\PhpCsFixer\Config;

-$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php71());
+$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php71(), [
+    '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

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

Composer 脚本

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

 {
   "name": "foo/bar",
   "require": {
     "php": "^7.2",
   },
   "require-dev": {
     "friendsofphp/php-cs-fixer": "~2.16.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:
   push:
     branches:
       - master
     tags:
       - "**"

 name: "Continuous Integration"

 jobs:
+  coding-standards:
+    name: "Coding Standards"
+
+    runs-on: ubuntu-latest
+
+    steps:
+      - name: "Checkout"
+        uses: actions/checkout@v1.1.0
+
+      - name: "Disable Xdebug"
+        run: php7.2 --ini | grep xdebug | sed 's/,$//' | xargs sudo rm
+
+      - name: "Cache dependencies installed with composer"
+        uses: actions/cache@v1.0.2
+        with:
+          path: ~/.composer/cache
+          key: php7.2-composer-locked-${{ hashFiles('**/composer.lock') }}
+          restore-keys: |
+            php7.2-composer-locked-
+
+      - name: "Install locked dependencies with composer"
+        run: php7.2 $(which 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@v1.0.2
+        with:
+          path: ~/.build/php-cs-fixer
+          key: php7.2-php-cs-fixer-${{ hashFiles('**/composer.lock') }}
+          restore-keys: |
+            php7.2-php-cs-fixer-
+
+      - name: "Run friendsofphp/php-cs-fixer"
+        run: php7.2 vendor/bin/php-cs-fixer fix --config=.php_cs --diff --diff-format=udiff --dry-run --verbose

Travis

如果你喜欢 Travis CI,在你的作业中添加一个 coding-standards 阶段

 language: php

 cache:
   directories:
     - $HOME/.composer/cache
+    - .build/php-cs-fixer

 jobs:
   include:
+    - stage: "Coding Standards"
+
+      php: 7.2
+
+      install:
+        - composer install --no-interaction --no-progress --no-suggest
+
+      before_script:
+        - mkdir -p .build/php-cs-fixer
+
+      script:
+        - vendor/bin/php-cs-fixer fix --config=.php_cs --diff --dry-run --verbose

变更日志

请参阅 CHANGELOG.md

贡献

请参阅 CONTRIBUTING.md

行为准则

请参阅CODE_OF_CONDUCT.md

许可证

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

鸣谢

本项目受到refinery29/php-cs-fixer-config的启发,该项目我从2015年8月7日到2017年5月29日在Refinery29公司工作期间创建并维护。