Skip to content

Commit

Permalink
Avoid duplicated rules when adding multiple standards (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Apr 26, 2024
1 parent d3577e9 commit 8b843a1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 31 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
2 changes: 1 addition & 1 deletion src/Standard/StandardInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
interface StandardInterface
{
/**
* @return RuleInterface[]
* @return list<RuleInterface>
*/
public function getRules(): array;
}
4 changes: 2 additions & 2 deletions tests/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
35 changes: 13 additions & 22 deletions tests/Ruleset/RulesetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down

0 comments on commit 8b843a1

Please sign in to comment.