diff --git a/src/Ruleset/Ruleset.php b/src/Ruleset/Ruleset.php index dc1be327..d825626e 100644 --- a/src/Ruleset/Ruleset.php +++ b/src/Ruleset/Ruleset.php @@ -4,6 +4,7 @@ namespace TwigCsFixer\Ruleset; +use TwigCsFixer\Rules\ConfigurableRuleInterface; use TwigCsFixer\Rules\FixableRuleInterface; use TwigCsFixer\Rules\RuleInterface; use TwigCsFixer\Standard\StandardInterface; @@ -14,7 +15,7 @@ final class Ruleset { /** - * @var array + * @var array */ private array $rules = []; @@ -28,18 +29,18 @@ public function allowNonFixableRules(bool $allowNonFixableRules = true): self } /** - * @return array + * @return list */ public function getRules(): array { if (!$this->allowNonFixableRules) { - return array_filter( + return array_values(array_filter( $this->rules, static fn (RuleInterface $rule): bool => $rule instanceof FixableRuleInterface, - ); + )); } - return $this->rules; + return array_values($this->rules); } /** @@ -47,7 +48,12 @@ public function getRules(): array */ public function addRule(RuleInterface $rule): self { - $this->rules[] = $rule; + $config = $rule instanceof ConfigurableRuleInterface + ? $rule->getConfiguration() + : null; + $key = $rule::class.md5(serialize($config)); + + $this->rules[$key] = $rule; return $this; } diff --git a/src/Standard/StandardInterface.php b/src/Standard/StandardInterface.php index cc4c41f7..98d2e591 100644 --- a/src/Standard/StandardInterface.php +++ b/src/Standard/StandardInterface.php @@ -9,7 +9,7 @@ interface StandardInterface { /** - * @return RuleInterface[] + * @return list */ public function getRules(): array; } diff --git a/tests/Config/ConfigTest.php b/tests/Config/ConfigTest.php index ad4d2fa0..21b7d90d 100644 --- a/tests/Config/ConfigTest.php +++ b/tests/Config/ConfigTest.php @@ -31,8 +31,8 @@ public function testConfigRuleset(): void $ruleset = $config->getRuleset(); static::assertEquals( - array_values($genericStandard->getRules()), - array_values($ruleset->getRules()) + $genericStandard->getRules(), + $ruleset->getRules() ); $ruleset = new Ruleset(); diff --git a/tests/Ruleset/RulesetTest.php b/tests/Ruleset/RulesetTest.php index fc7b573b..93a16a70 100644 --- a/tests/Ruleset/RulesetTest.php +++ b/tests/Ruleset/RulesetTest.php @@ -7,9 +7,7 @@ use PHPUnit\Framework\TestCase; use TwigCsFixer\Rules\AbstractFixableRule; use TwigCsFixer\Rules\AbstractRule; -use TwigCsFixer\Rules\RuleInterface; -use TwigCsFixer\Rules\Whitespace\BlankEOFRule; -use TwigCsFixer\Rules\Whitespace\TrailingSpaceRule; +use TwigCsFixer\Rules\String\SingleQuoteRule; use TwigCsFixer\Ruleset\Ruleset; use TwigCsFixer\Standard\StandardInterface; @@ -24,33 +22,26 @@ public function testStartWithNoRule(): void public function testAddAndRemoveRule(): void { $ruleset = new Ruleset(); - $rule = self::createStub(RuleInterface::class); + $rule1 = new SingleQuoteRule(true); + $rule2 = new SingleQuoteRule(false); - $ruleset->addRule($rule); + $ruleset->addRule($rule1); static::assertCount(1, $ruleset->getRules()); - $ruleset->addRule($rule); - static::assertCount(2, $ruleset->getRules()); - - $ruleset->overrideRule($rule); + $ruleset->addRule($rule1); static::assertCount(1, $ruleset->getRules()); - $ruleset->removeRule($rule::class); - static::assertCount(0, $ruleset->getRules()); - } + $ruleset->addRule($rule2); + static::assertCount(2, $ruleset->getRules()); - public function testAddStandard(): void - { - $ruleset = new Ruleset(); + $ruleset->removeRule(SingleQuoteRule::class); + static::assertCount(0, $ruleset->getRules()); - // Using real rule to have different class name - $rule1 = new BlankEOFRule(); - $rule2 = new TrailingSpaceRule(); - $standard = self::createStub(StandardInterface::class); - $standard->method('getRules')->willReturn([$rule1, $rule2]); + $ruleset->addRule($rule1); + static::assertCount(1, $ruleset->getRules()); - $ruleset->addStandard($standard); - static::assertCount(2, $ruleset->getRules()); + $ruleset->overrideRule($rule2); + static::assertCount(1, $ruleset->getRules()); } public function testAllowNonFixableRules(): void