This package provides a configuration factory and rule set factories for friendsofphp/php-cs-fixer
.
Run
composer require --dev ergebnis/php-cs-fixer-config
Pick one of the rule sets:
Ergebnis\PhpCsFixer\RuleSet\Php53
Ergebnis\PhpCsFixer\RuleSet\Php54
Ergebnis\PhpCsFixer\RuleSet\Php55
Ergebnis\PhpCsFixer\RuleSet\Php56
Ergebnis\PhpCsFixer\RuleSet\Php70
Ergebnis\PhpCsFixer\RuleSet\Php71
Ergebnis\PhpCsFixer\RuleSet\Php72
Ergebnis\PhpCsFixer\RuleSet\Php73
Ergebnis\PhpCsFixer\RuleSet\Php74
Ergebnis\PhpCsFixer\RuleSet\Php80
Ergebnis\PhpCsFixer\RuleSet\Php81
Ergebnis\PhpCsFixer\RuleSet\Php82
Create a configuration file .php-cs-fixer.php
in the root of your project:
<?php
declare(strict_types=1);
use Ergebnis\PhpCsFixer\Config;
$ruleSet = Config\RuleSet\Php82::create();
$config = Config\Factory::fromRuleSet($ruleSet);
$config->getFinder()->in(__DIR__);
$config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/.php-cs-fixer.cache');
return $config;
All configuration examples use the caching feature, and if you want to use it as well, you should add the cache directory to .gitignore
:
+ /.build/
/vendor/
💡 Optionally specify a header:
<?php
declare(strict_types=1);
use Ergebnis\PhpCsFixer\Config;
+$header = <<<EOF
+Copyright (c) 2023 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/ergebnis/php-cs-fixer-config
+EOF;
-$ruleSet = Config\RuleSet\Php82::create();
+$ruleSet = Config\RuleSet\Php82::create()->withHeader($header);
$config = Config\Factory::fromRuleSet($ruleSet);
$config->getFinder()->in(__DIR__);
$config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/.php-cs-fixer.cache');
return $config;
This will enable and configure the HeaderCommentFixer
, so that file headers will be added to PHP files, for example:
<?php
declare(strict_types=1);
+/**
+ * Copyright (c) 2023 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/ergebnis/php-cs-fixer-config
+ */
💡 Optionally override rules from a rule set by passing a set of rules to be merged in:
<?php
declare(strict_types=1);
use Ergebnis\PhpCsFixer\Config;
-$ruleSet = Config\RuleSet\Php82::create();
+$ruleSet = Config\RuleSet\Php82::create()->withRules(Config\Rules::fromArray([
+ 'mb_str_functions' => false,
+ 'strict_comparison' => false,
+]);
$config = Config\Factory::fromRuleSet($ruleSet);
$config->getFinder()->in(__DIR__);
$config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/.php-cs-fixer.cache');
return $config;
💡 Optionally register and configure rules for custom fixers:
<?php
declare(strict_types=1);
use Ergebnis\PhpCsFixer\Config;
use FooBar\Fixer;
-$ruleSet = Config\RuleSet\Php82::create();
+$ruleSet = Config\RuleSet\Php82::create()
+ ->withCustomFixers(Config\Fixers::fromFixers(
+ new Fixer\BarBazFixer(),
+ new Fixer\QuzFixer(),
+ ))
+ ->withRules(Config\Rules::fromArray([
+ 'FooBar/bar_baz' => true,
+ 'FooBar/quz' => [
+ 'qux => false,
+ ],
+ ]))
+]);
$config = Config\Factory::fromRuleSet($ruleSet);
$config->getFinder()->in(__DIR__);
$config->setCacheFile(__DIR__ . '/.build/php-cs-fixer/.php-cs-fixer.cache');
return $config;
If you like Makefile
s, create a Makefile
with a coding-standards
target:
+.PHONY: coding-standards
+coding-standards: vendor
+ mkdir -p .build/php-cs-fixer
+ vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --diff --verbose
vendor: composer.json composer.lock
composer validate
composer install
Run
make coding-standards
to automatically fix coding standard violations.
If you like composer
scripts, add a coding-standards
script to composer.json
:
{
"name": "foo/bar",
"require": {
"php": "^8.1",
},
"require-dev": {
"ergebnis/php-cs-fixer-config": "~5.1.0"
+ },
+ "scripts": {
+ "coding-standards": [
+ "mkdir -p .build/php-cs-fixer",
+ "php-cs-fixer fix --diff --verbose"
+ ]
}
}
Run
composer coding-standards
to automatically fix coding standard violations.
If you like GitHub Actions, add a coding-standards
job to your workflow:
on:
pull_request: null
push:
branches:
- main
name: "Integrate"
jobs:
+ coding-standards:
+ name: "Coding Standards"
+
+ runs-on: ubuntu-latest
+
+ strategy:
+ matrix:
+ php-version:
+ - "8.1"
+
+ steps:
+ - name: "Checkout"
+ uses: "actions/checkout@v2"
+
+ - name: "Set up PHP"
+ 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 --ansi --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.ref_name }}"
+ restore-keys: |
+ php-${{ matrix.php-version }}-php-cs-fixer-main
+ php-${{ matrix.php-version }}-php-cs-fixer-
+
+ - name: "Run friendsofphp/php-cs-fixer"
+ run: "vendor/bin/php-cs-fixer fix --ansi --config=.php-cs-fixer.php --diff --dry-run --verbose"
The maintainers of this package record notable changes to this project in a changelog.
The maintainers of this package suggest following the contribution guide.
The maintainers of this package ask contributors to follow the code of conduct.
The maintainers of this package provide limited support.
You can support the maintenance of this package by sponsoring @localheinz or requesting an invoice for services related to this package.
This package supports PHP versions with active support.
The maintainers of this package add support for a PHP version following its initial release and drop support for a PHP version when it has reached its end of active support.
This package has a security policy.
This package uses the MIT license.
This package is inspired by and also replaces localheinz/php-cs-fixer-config
.
This package requires and enables custom fixers from the following packages:
Follow @localheinz and @ergebnis on Twitter.