From 6eb00f1e21eb93ad5b40702a1065501418ea8bbc Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Fri, 26 Apr 2024 10:24:32 +0200 Subject: [PATCH] Avoid duplicated rules when adding multiples standard --- src/Ruleset/Ruleset.php | 18 ++++++++++++------ tests/Ruleset/RulesetTest.php | 32 +++++++++++++------------------- 2 files changed, 25 insertions(+), 25 deletions(-) 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/tests/Ruleset/RulesetTest.php b/tests/Ruleset/RulesetTest.php index fc7b573b..ae0f0e0f 100644 --- a/tests/Ruleset/RulesetTest.php +++ b/tests/Ruleset/RulesetTest.php @@ -8,6 +8,7 @@ use TwigCsFixer\Rules\AbstractFixableRule; use TwigCsFixer\Rules\AbstractRule; use TwigCsFixer\Rules\RuleInterface; +use TwigCsFixer\Rules\String\SingleQuoteRule; use TwigCsFixer\Rules\Whitespace\BlankEOFRule; use TwigCsFixer\Rules\Whitespace\TrailingSpaceRule; use TwigCsFixer\Ruleset\Ruleset; @@ -24,33 +25,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