How can I create a custom rule set for php-cs-fixer?
Asked Answered
R

3

10

How to create custom ruleset to be run by fix command with option --rules=@custom?

Like @PSR1, @PSR2, @Symfony, @PHP56Migration, @PHP70Migration, @PHP71Migration are made.

Php-cs-fixer version: 2.2.3

Only by inserting into vendor code -vendor/friendsofphp/php-cs-fixer/src/RuleSet.php:

private $setDefinitions = array(
        '@custom' => [
        'some_custom_fixer` => true,
        ]
)

Works as I expect, but RuleSet is final class. Is there a way to override or extend the setDefinitions when using custom config .php_cs?

Region answered 23/6, 2017 at 7:20 Comment(3)
Why not simplify list specific fixers? What exactly is your goal?Steffie
Like that: github.com/FriendsOfPHP/PHP-CS-Fixer/pull/2884Region
I see. Thanks for sharing.Steffie
B
6

Currently, there is no way to register custom ruleset how you register custom fixer. Although that, we were already thinking that it would be nice to introduce it, yet it is not a prio on our todo list. Feel free to write proposal how it shall be handled, and PR after initial discussion. https://github.com/FriendsOfPHP/PHP-CS-Fixer/

Behead answered 25/6, 2017 at 7:14 Comment(0)
P
3

In the meanwhile, create your own configuration instead and share it across projects.

For example:

Profile answered 25/6, 2017 at 8:19 Comment(0)
C
0

Found this homepage that says that the following should work:

<?php
// ...
return (new PhpCsFixer\Config())
    // ...
    ->registerCustomFixers([
        new CustomerFixer1(),
        new CustomerFixer2(),
    ])
    ->setRules([
        // ...
        'YourVendorName/custome_rule' => true,
        'YourVendorName/custome_rule_2' => true,
    ])
;

Personally I don't understand what to put in 'YourVendorName'...

Countersubject answered 16/10, 2023 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.