narrowspark/php-cs-fixer-config

该包已被废弃且不再维护。未建议替代包。

为friendsofphp/php-cs-fixer提供配置,用于Narrowspark和Anolilab。

v6.3.4 2021-04-16 17:25 UTC

README

68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6e6172726f77737061726b2f7068702d63732d66697865722d636f6e6669672e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545382e302e302d3838393242462e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6e6172726f77737061726b2f7068702d63732d66697865722d636f6e6669672f6d61696e2e7376673f7374796c653d666c61742d737175617265 68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265

此存储库提供了https://github.com/FriendsOfPHP/PHP-CS-Fixer的配置,我们使用它来验证和强制执行Narrowspark和Anolilab中PHP代码的单一代码标准。

安装

通过Composer

$ composer require narrowspark/php-cs-fixer-config

使用方法

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

<?php
declare(strict_types=1);
use Narrowspark\CS\Config\Config;

$config = new Config();
$config->getFinder()
    ->files()
    ->in(__DIR__)
    ->exclude('.build')
    ->exclude('vendor')
    ->name('*.php')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true);

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

return $config;

Git

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

+ /.build/
 /vendor/

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

带有头部配置

💡可选地指定一个头部

<?php
declare(strict_types=1);

use Narrowspark\CS\Config\Config;

+$header = <<<EOF
+Copyright (c) 2020 Narrowspark
+
+For the full copyright and license information, please view
+the LICENSE file that was distributed with this source code.
+EOF;

-$config = new Narrowspark\CS\Config\Config();
+$config = new Narrowspark\CS\Config\Config($header);

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

return $config;

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

带有覆盖规则的配置

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

<?php
declare(strict_types=1);

use Narrowspark\CS\Config\Config;

- $config = new Config();
+ $config = new Config(null /* if you dont need a header */, [
    'mb_str_functions' => false,
    'strict_comparison' => false,
]);

$config->getFinder()
    ->files()
    ->in(__DIR__)
    ->exclude('.build')
    ->exclude('vendor')
    ->name('*.php')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true);

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

return $config;

Composer

如果您喜欢composer脚本,请将coding-standards脚本添加到composer.json

 {
   "name": "foo/bar",
   "require": {
     "php": "^7.3",
   },
   "require-dev": {
     "narrowspark/php-cs-fixer-config": "~1.0.0"
+  },
+  "scripts": {
+    "cs:check": [
+      "mkdir -p .build/php-cs-fixer",
+      "php-cs-fixer fix --diff --diff-format=udiff --verbose"
+    ]
   }
 }

运行

$ composer cs:check

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

Travis

如果您喜欢Travis CI,请将coding-standards阶段添加到您的作业中

 language: php

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

 jobs:
   include:
+    - stage: "Coding Standards"
+
+      php: 7.3
+
+      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

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/[email protected]
+
+      - name: "Disable Xdebug"
+        run: php7.3 --ini | grep xdebug | sed 's/,$//' | xargs sudo rm
+
+      - name: "Cache dependencies installed with composer"
+        uses: actions/[email protected]
+        with:
+          path: ~/.composer/cache
+          key: php7.3-composer-locked-${{ hashFiles('**/composer.lock') }}
+          restore-keys: |
+            php7.3-composer-locked-
+
+      - name: "Install locked dependencies with composer"
+        run: php7.3 $(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/[email protected]
+        with:
+          path: ~/.build/php-cs-fixer
+          key: php7.3-php-cs-fixer-${{ hashFiles('**/composer.lock') }}
+          restore-keys: |
+            php7.3-php-cs-fixer-
+
+      - name: "Run friendsofphp/php-cs-fixer"
+        run: php7.3 vendor/bin/php-cs-fixer fix --config=.php_cs --diff --diff-format=udiff --dry-run --verbose

测试

$ vendor/bin/phpunit

贡献

如果您想帮助,请查看问题列表并检查我们的贡献指南

注意:请注意,该项目以贡献者行为准则发布。通过参与此项目,您同意遵守其条款。

鸣谢

许可协议

Narrowspark http-emitter 是一个开源软件,其许可协议为 MIT 许可协议