antipodes/php-cs-fixer-config-antipodes

为Antipodes Medical提供配置工厂和多个规则集。

1.0.2 2022-03-21 13:52 UTC

This package is auto-updated.

Last update: 2024-09-21 19:05:19 UTC


README

composer require antipodes/php-cs-fixer-config-antipodes --dev

用法

配置

选择一个规则集

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

<?php

use PhpCsFixer\Finder;
use Antipodes\PhpCsFixer\Config\Factory;
use Antipodes\PhpCsFixer\Config\RuleSet\Php74;

$config = Factory::fromRuleSet(new Php74());

$finder = Finder::create()
                ->in([
                    __DIR__,
                ])
                ->name('*.php')
                ->ignoreDotFiles(true)
                ->ignoreVCS(true);

return $config
    ->setFinder($finder)
    ->setRiskyAllowed(true)
    ->setUsingCache(true);

带有覆盖规则的配置

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

<?php

use PhpCsFixer\Finder;
use Antipodes\PhpCsFixer\Config\Factory;
use Antipodes\PhpCsFixer\Config\RuleSet\Php74;

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

$finder = Finder::create()
                ->in([
                    __DIR__,
                ])
                ->name('*.php')
                ->ignoreDotFiles(true)
                ->ignoreVCS(true);

return $config
    ->setFinder($finder)
    ->setRiskyAllowed(true)
    ->setUsingCache(true);

Composer 脚本

如果你喜欢 composer 脚本,请将 coding-standards 脚本添加到 composer.json 文件中

 {
   "name": "foo/bar",
   "require": {
     "php": "^7.3",
   },
   "require-dev": {
     "antipodes/php-cs-fixer-config-antipodes": "~1.0.0"
+  },
+  "scripts": {
+    "lint": [
+      "./vendor/bin/php-cs-fixer fix -vvv --dry-run --show-progress=dots"
+    ],
+    "lint:fix": [
+      "./vendor/bin/php-cs-fixer fix -vvv --show-progress=dots"
+    ]
   }
 }

运行

composer lint

来检查代码。

运行

composer lint:fix

来自动修复代码。

GitHub Actions

如果你喜欢 GitHub Actions,请将 php-lint 任务添加到你的工作流程中

name: "🚨 PHP Linter"

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

jobs:
  php:
    name: PHP ${{ matrix.php }}
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, '[ci skip]')"
    strategy:
      fail-fast: false
      matrix:
        php: [ "7.4", "8.0" ]

    steps:
      - name: Checkout the project
        uses: actions/checkout@v2

      - name: Setup the PHP ${{ matrix.php }} environment on ${{ runner.os }}
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}

      - name: Add HTTP basic auth credentials
        run: echo '${{ secrets.COMPOSER_AUTH_JSON }}' > $GITHUB_WORKSPACE/auth.json

      - name: Install Composer dependencies
        run: composer install --no-progress --prefer-dist --optimize-autoloader --no-suggest

      - name: Execute the lint script
        run: composer run-script lint