Skip to content

Commit

Permalink
Fix: Do not use reflection to create rule set
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Sep 17, 2023
1 parent 2c42452 commit 64e7c9c
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 67 deletions.
38 changes: 1 addition & 37 deletions psalm-baseline.xml
Expand Up @@ -12,9 +12,6 @@
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
<PossiblyUnusedMethod>
<code>__construct</code>
</PossiblyUnusedMethod>
</file>
<file src="src/RuleSet/Php54.php">
<MixedArgumentTypeCoercion>
Expand All @@ -23,9 +20,6 @@
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
<PossiblyUnusedMethod>
<code>__construct</code>
</PossiblyUnusedMethod>
</file>
<file src="src/RuleSet/Php55.php">
<MixedArgumentTypeCoercion>
Expand All @@ -34,9 +28,6 @@
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
<PossiblyUnusedMethod>
<code>__construct</code>
</PossiblyUnusedMethod>
</file>
<file src="src/RuleSet/Php56.php">
<MixedArgumentTypeCoercion>
Expand All @@ -45,9 +36,6 @@
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
<PossiblyUnusedMethod>
<code>__construct</code>
</PossiblyUnusedMethod>
</file>
<file src="src/RuleSet/Php70.php">
<MixedArgumentTypeCoercion>
Expand All @@ -56,9 +44,6 @@
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
<PossiblyUnusedMethod>
<code>__construct</code>
</PossiblyUnusedMethod>
</file>
<file src="src/RuleSet/Php71.php">
<MixedArgumentTypeCoercion>
Expand All @@ -67,9 +52,6 @@
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
<PossiblyUnusedMethod>
<code>__construct</code>
</PossiblyUnusedMethod>
</file>
<file src="src/RuleSet/Php72.php">
<MixedArgumentTypeCoercion>
Expand All @@ -78,9 +60,6 @@
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
<PossiblyUnusedMethod>
<code>__construct</code>
</PossiblyUnusedMethod>
</file>
<file src="src/RuleSet/Php73.php">
<MixedArgumentTypeCoercion>
Expand All @@ -89,9 +68,6 @@
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
<PossiblyUnusedMethod>
<code>__construct</code>
</PossiblyUnusedMethod>
</file>
<file src="src/RuleSet/Php74.php">
<MixedArgumentTypeCoercion>
Expand All @@ -100,9 +76,6 @@
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
<PossiblyUnusedMethod>
<code>__construct</code>
</PossiblyUnusedMethod>
</file>
<file src="src/RuleSet/Php80.php">
<MixedArgumentTypeCoercion>
Expand All @@ -111,9 +84,6 @@
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
<PossiblyUnusedMethod>
<code>__construct</code>
</PossiblyUnusedMethod>
</file>
<file src="src/RuleSet/Php81.php">
<MixedArgumentTypeCoercion>
Expand All @@ -122,9 +92,6 @@
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
<PossiblyUnusedMethod>
<code>__construct</code>
</PossiblyUnusedMethod>
</file>
<file src="src/RuleSet/Php82.php">
<MixedArgumentTypeCoercion>
Expand All @@ -133,9 +100,6 @@
<NonInvariantDocblockPropertyType>
<code>$rules</code>
</NonInvariantDocblockPropertyType>
<PossiblyUnusedMethod>
<code>__construct</code>
</PossiblyUnusedMethod>
</file>
<file src="src/Rules.php">
<MixedArgumentTypeCoercion>
Expand Down Expand Up @@ -187,7 +151,7 @@
<code>registerBuiltInFixers</code>
</InternalMethod>
<InvalidArgument>
<code><![CDATA[self::createRuleSet()->customFixers()]]></code>
<code><![CDATA[static::createRuleSet()->customFixers()]]></code>
</InvalidArgument>
<PossiblyUnusedMethod>
<code>provideValidHeader</code>
Expand Down
35 changes: 9 additions & 26 deletions test/Unit/RuleSet/AbstractRuleSetTestCase.php
Expand Up @@ -26,7 +26,7 @@ abstract class AbstractRuleSetTestCase extends Framework\TestCase
{
final public function testDefaults(): void
{
$ruleSet = self::createRuleSet();
$ruleSet = static::createRuleSet();

self::assertEquals($this->expectedCustomFixers(), $ruleSet->customFixers());
self::assertEquals($this->expectedName(), $ruleSet->name());
Expand All @@ -36,7 +36,7 @@ final public function testDefaults(): void

final public function testRuleSetDoesNotConfigureRulesThatAreNotRegistered(): void
{
$rules = self::createRuleSet()->rules();
$rules = static::createRuleSet()->rules();

$fixersThatAreRegistered = self::fixersThatAreRegistered();

Expand All @@ -63,7 +63,7 @@ static function (string $nameOfRule) use ($fixersThatAreRegistered): bool {

final public function testRuleSetDoesNotConfigureRulesThatAreDeprecated(): void
{
$rules = self::createRuleSet()->rules();
$rules = static::createRuleSet()->rules();

$fixersThatAreRegistered = self::fixersThatAreRegistered();

Expand All @@ -89,7 +89,7 @@ static function (string $nameOfRule) use ($fixersThatAreRegistered): bool {

final public function testRuleSetDoesNotConfigureRulesUsingDeprecatedConfigurationOptions(): void
{
$rules = self::createRuleSet()->rules();
$rules = static::createRuleSet()->rules();

$namesOfRules = \array_keys($rules->toArray());

Expand Down Expand Up @@ -141,7 +141,7 @@ final public function testRuleSetDoesNotConfigureRulesUsingDeprecatedConfigurati

final public function testRulesAndConfigurationOptionsAreSortedInRuleSet(): void
{
$rules = self::createRuleSet()->rules();
$rules = static::createRuleSet()->rules();

$sorted = self::sort($rules->toArray());

Expand All @@ -165,7 +165,7 @@ final public function testRulesAndConfigurationOptionsAreSortedInRuleSetTest():

final public function testHeaderCommentFixerIsDisabledByDefault(): void
{
$rules = self::createRuleSet()->rules();
$rules = static::createRuleSet()->rules();

self::assertArrayHasKey('header_comment', $rules->toArray());
self::assertFalse($rules->toArray()['header_comment']);
Expand All @@ -174,7 +174,7 @@ final public function testHeaderCommentFixerIsDisabledByDefault(): void
#[Framework\Attributes\DataProvider('provideValidHeader')]
final public function testHeaderCommentFixerIsEnabledIfHeaderIsProvided(string $header): void
{
$rules = self::createRuleSet($header)->rules();
$rules = static::createRuleSet($header)->rules();

self::assertArrayHasKey('header_comment', $rules->toArray());

Expand Down Expand Up @@ -254,24 +254,7 @@ final protected static function className(): string
/**
* @throws \RuntimeException
*/
final protected static function createRuleSet(?string $header = null): RuleSet
{
$className = self::className();

$reflection = new \ReflectionClass($className);

$ruleSet = $reflection->newInstance($header);

if (!$ruleSet instanceof RuleSet) {
throw new \RuntimeException(\sprintf(
'Class %s" does not implement interface "%s".',
$className,
RuleSet::class,
));
}

return $ruleSet;
}
abstract protected static function createRuleSet(?string $header = null): RuleSet;

/**
* @return array<string, Fixer\FixerInterface>
Expand All @@ -283,7 +266,7 @@ final protected static function fixersThatAreRegistered(): array
$fixerFactory->registerBuiltInFixers();

$fixersThatAreBuiltIn = $fixerFactory->getFixers();
$fixersThatShouldBeRegistered = \iterator_to_array(self::createRuleSet()->customFixers());
$fixersThatShouldBeRegistered = \iterator_to_array(static::createRuleSet()->customFixers());

/** @var array<Fixer\FixerInterface> $fixers */
$fixers = \array_merge(
Expand Down
8 changes: 4 additions & 4 deletions test/Unit/RuleSet/ExplicitRuleSetTestCase.php
Expand Up @@ -21,14 +21,14 @@ abstract class ExplicitRuleSetTestCase extends AbstractRuleSetTestCase
{
final public function testIsExplicitRuleSet(): void
{
$ruleSet = self::createRuleSet();
$ruleSet = static::createRuleSet();

self::assertInstanceOf(RuleSet\ExplicitRuleSet::class, $ruleSet);
}

final public function testRuleSetDoesNotConfigureRuleSets(): void
{
$rules = self::createRuleSet()->rules();
$rules = static::createRuleSet()->rules();

$rulesWithoutRulesForRuleSets = \array_filter(
$rules->toArray(),
Expand All @@ -46,7 +46,7 @@ static function (string $nameOfRule): bool {

final public function testRuleSetConfiguresAllRulesThatAreNotDeprecated(): void
{
$rules = self::createRuleSet()->rules();
$rules = static::createRuleSet()->rules();

$fixersThatAreRegisteredAndNotDeprecated = \array_filter(self::fixersThatAreRegistered(), static function (Fixer\FixerInterface $fixer): bool {
return !$fixer instanceof Fixer\DeprecatedFixerInterface;
Expand Down Expand Up @@ -74,7 +74,7 @@ final public function testRuleSetConfiguresAllRulesThatAreNotDeprecated(): void

final public function testRuleSetConfiguresAllRulesThatAreConfigurableAndNotDeprecatedWithAnExplicitConfigurationWithEveryOptionWhenTheyAreEnabled(): void
{
$rules = self::createRuleSet()->rules();
$rules = static::createRuleSet()->rules();

$namesOfRules = \array_keys($rules->toArray());

Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php53Test.php
Expand Up @@ -31,6 +31,11 @@
#[Framework\Attributes\UsesClass(Rules::class)]
final class Php53Test extends ExplicitRuleSetTestCase
{
protected static function createRuleSet(?string $header = null): RuleSet
{
return new RuleSet\Php53($header);
}

protected function expectedCustomFixers(): iterable
{
yield from [];
Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php54Test.php
Expand Up @@ -31,6 +31,11 @@
#[Framework\Attributes\UsesClass(Rules::class)]
final class Php54Test extends ExplicitRuleSetTestCase
{
protected static function createRuleSet(?string $header = null): RuleSet
{
return new RuleSet\Php54($header);
}

protected function expectedCustomFixers(): iterable
{
yield from [];
Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php55Test.php
Expand Up @@ -36,6 +36,11 @@ public function expectedCustomFixers(): iterable
yield from [];
}

protected static function createRuleSet(?string $header = null): RuleSet
{
return new RuleSet\Php55($header);
}

protected function expectedName(): Name
{
return Name::fromString('ergebnis (PHP 5.5)');
Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php56Test.php
Expand Up @@ -31,6 +31,11 @@
#[Framework\Attributes\UsesClass(Rules::class)]
final class Php56Test extends ExplicitRuleSetTestCase
{
protected static function createRuleSet(?string $header = null): RuleSet
{
return new RuleSet\Php56($header);
}

protected function expectedCustomFixers(): iterable
{
yield from [];
Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php70Test.php
Expand Up @@ -31,6 +31,11 @@
#[Framework\Attributes\UsesClass(Rules::class)]
final class Php70Test extends ExplicitRuleSetTestCase
{
protected static function createRuleSet(?string $header = null): RuleSet
{
return new RuleSet\Php70($header);
}

protected function expectedCustomFixers(): iterable
{
yield from [];
Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php71Test.php
Expand Up @@ -31,6 +31,11 @@
#[Framework\Attributes\UsesClass(Rules::class)]
final class Php71Test extends ExplicitRuleSetTestCase
{
protected static function createRuleSet(?string $header = null): RuleSet
{
return new RuleSet\Php71($header);
}

protected function expectedCustomFixers(): iterable
{
yield from [];
Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php72Test.php
Expand Up @@ -31,6 +31,11 @@
#[Framework\Attributes\UsesClass(Rules::class)]
final class Php72Test extends ExplicitRuleSetTestCase
{
protected static function createRuleSet(?string $header = null): RuleSet
{
return new RuleSet\Php72($header);
}

protected function expectedCustomFixers(): iterable
{
yield from [];
Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php73Test.php
Expand Up @@ -31,6 +31,11 @@
#[Framework\Attributes\UsesClass(Rules::class)]
final class Php73Test extends ExplicitRuleSetTestCase
{
protected static function createRuleSet(?string $header = null): RuleSet
{
return new RuleSet\Php73($header);
}

protected function expectedCustomFixers(): iterable
{
yield from [];
Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php74Test.php
Expand Up @@ -31,6 +31,11 @@
#[Framework\Attributes\UsesClass(Rules::class)]
final class Php74Test extends ExplicitRuleSetTestCase
{
protected static function createRuleSet(?string $header = null): RuleSet
{
return new RuleSet\Php74($header);
}

protected function expectedCustomFixers(): iterable
{
yield from [];
Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php80Test.php
Expand Up @@ -31,6 +31,11 @@
#[Framework\Attributes\UsesClass(Rules::class)]
final class Php80Test extends ExplicitRuleSetTestCase
{
protected static function createRuleSet(?string $header = null): RuleSet
{
return new RuleSet\Php80($header);
}

protected function expectedCustomFixers(): iterable
{
yield from [];
Expand Down
5 changes: 5 additions & 0 deletions test/Unit/RuleSet/Php81Test.php
Expand Up @@ -31,6 +31,11 @@
#[Framework\Attributes\UsesClass(Rules::class)]
final class Php81Test extends ExplicitRuleSetTestCase
{
protected static function createRuleSet(?string $header = null): RuleSet
{
return new RuleSet\Php81($header);
}

protected function expectedCustomFixers(): iterable
{
yield from [];
Expand Down

0 comments on commit 64e7c9c

Please sign in to comment.