Skip to content

Commit

Permalink
Avoid duplicated rules when adding multiples standard
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Apr 26, 2024
1 parent d3577e9 commit 6eb00f1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
18 changes: 12 additions & 6 deletions src/Ruleset/Ruleset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace TwigCsFixer\Ruleset;

use TwigCsFixer\Rules\ConfigurableRuleInterface;
use TwigCsFixer\Rules\FixableRuleInterface;
use TwigCsFixer\Rules\RuleInterface;
use TwigCsFixer\Standard\StandardInterface;
Expand All @@ -14,7 +15,7 @@
final class Ruleset
{
/**
* @var array<RuleInterface>
* @var array<string, RuleInterface>
*/
private array $rules = [];

Expand All @@ -28,26 +29,31 @@ public function allowNonFixableRules(bool $allowNonFixableRules = true): self
}

/**
* @return array<RuleInterface>
* @return list<RuleInterface>
*/
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);
}

/**
* @return $this
*/
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;
}
Expand Down
32 changes: 13 additions & 19 deletions tests/Ruleset/RulesetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 6eb00f1

Please sign in to comment.