datana-gmbh / php-cs-fixer-config
2.3.0
2022-07-28 20:19 UTC
Requires
- php: ^7.2 || ^8.0
- friendsofphp/php-cs-fixer: ~2.19.3
Requires (Dev)
- composer/package-versions-deprecated: 1.11.99.5
- ergebnis/composer-normalize: ^2.19.0
- ergebnis/license: ^1.1.0
- ergebnis/phpstan-rules: ~0.15.3
- ergebnis/test-util: ^1.5.0
- infection/infection: ~0.15.3
- phpstan/extension-installer: ^1.1.0
- phpstan/phpstan: ~0.12.99
- phpstan/phpstan-deprecation-rules: ~0.12.6
- phpstan/phpstan-phpunit: ~0.12.22
- phpstan/phpstan-strict-rules: ~0.12.11
- phpunit/phpunit: ^8.5.27
This package is auto-updated.
Last update: 2022-12-21 08:03:12 UTC
README
为 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
。
行为准则
许可证
本软件包使用MIT许可证授权。
请查看LICENSE.md
。
致谢
本项目受到ergebnis/php-cs-fixer-config
的启发。