Skip to content

Commit

Permalink
Enhancement: Assert that deprecated fixers are not enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Nov 1, 2020
1 parent 50c1354 commit f7d8449
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
3 changes: 3 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<code>getFixers</code>
<code>registerBuiltInFixers</code>
</InternalMethod>
<MissingClosureParamType occurrences="1">
<code>$ruleConfiguration</code>
</MissingClosureParamType>
<MixedMethodCall occurrences="1">
<code>getRules</code>
</MixedMethodCall>
Expand Down
58 changes: 51 additions & 7 deletions test/Unit/RuleSet/AbstractRuleSetTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,37 @@ final public function testRulesDoNotContainRuleSets(): void
));
}

final public function testRulesDoNotEnableDeprecatedFixers(): void
{
$fixers = self::builtInFixers();

$rulesEnablingDeprecatedFixers = \array_filter(
self::createRuleSet()->rules(),
static function ($ruleConfiguration, string $ruleName) use ($fixers): bool {
if (false === $ruleConfiguration) {
return false;
}

if (!\array_key_exists($ruleName, $fixers)) {
return false;
}

$fixer = $fixers[$ruleName];

return $fixer instanceof Fixer\DeprecatedFixerInterface;
},
\ARRAY_FILTER_USE_BOTH
);

$namesOfEnabledDeprecatedFixers = \array_keys($rulesEnablingDeprecatedFixers);

self::assertEmpty($namesOfEnabledDeprecatedFixers, \sprintf(
"Failed asserting that deprecated fixers with names\n\n%s\n\nare not enabled in rule set \"%s\".",
' - ' . \implode("\n - ", $namesOfEnabledDeprecatedFixers),
static::className()
));
}

/**
* @phpstan-return class-string
*
Expand Down Expand Up @@ -228,20 +259,33 @@ final protected static function createRuleSet(?string $header = null): Config\Ru
}

/**
* @return array<int, string>
* @return array<string, Fixer\FixerInterface>
*/
private static function builtInFixerNames(): array
private static function builtInFixers(): array
{
$fixerFactory = FixerFactory::create();

$fixerFactory->registerBuiltInFixers();

/** @var array<int, string> $builtInFixerNames */
$builtInFixerNames = \array_values(\array_map(static function (Fixer\FixerInterface $fixer): string {
return $fixer->getName();
}, $fixerFactory->getFixers()));
$fixers = $fixerFactory->getFixers();

/** @var array<string, Fixer\FixerInterface> $builtInFixers */
$builtInFixers = \array_combine(
\array_map(static function (Fixer\FixerInterface $fixer): string {
return $fixer->getName();
}, $fixers),
$fixers
);

return $builtInFixers;
}

return $builtInFixerNames;
/**
* @return array<int, string>
*/
private static function builtInFixerNames(): array
{
return \array_keys(self::builtInFixers());
}

/**
Expand Down

0 comments on commit f7d8449

Please sign in to comment.