Skip to content

Commit

Permalink
[DX] Validate rules no longer exists on $rectorConfig->skip() (#4728)
Browse files Browse the repository at this point in the history
* [DX] Validate rules no longer exists on $rectorConfig->skip()

* clean up
  • Loading branch information
samsonasik committed Aug 9, 2023
1 parent b98df68 commit 59cf4dd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
53 changes: 53 additions & 0 deletions packages/Config/RectorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,64 @@ public function memoryLimit(string $memoryLimit): void
SimpleParameterProvider::setParameter(Option::MEMORY_LIMIT, $memoryLimit);
}

private function isRuleNoLongerExists(mixed $skipRule): bool
{
return
// only validate string
is_string($skipRule)
// not regex path
&& ! str_contains($skipRule, '*')
// not realpath
&& realpath($skipRule) === false
// a Rector end
&& str_ends_with($skipRule, 'Rector')
// class not exists
&& ! class_exists($skipRule);
}

/**
* @param array<int|string, mixed> $criteria
*/
public function skip(array $criteria): void
{
$notExistsRules = [];
foreach ($criteria as $key => $value) {
/**
* Cover define rule then list of files
*
* $rectorConfig->skip([
* RenameVariableToMatchMethodCallReturnTypeRector::class => [
* __DIR__ . '/packages/Config/RectorConfig.php'
* ],
* ]);
*/
if ($this->isRuleNoLongerExists($key)) {
$notExistsRules[] = $key;
}

if (! is_string($value)) {
continue;
}

/**
* Cover direct value without array list of files, eg:
*
* $rectorConfig->skip([
* StringClassNameToClassConstantRector::class,
* ]);
*/
if ($this->isRuleNoLongerExists($value)) {
$notExistsRules[] = $value;
}
}

if ($notExistsRules !== []) {
throw new ShouldNotHappenException('Following skipped rules on $rectorConfig->skip() are no longer exists or changed to different namespace: ' . implode(
', ',
$notExistsRules
));
}

SimpleParameterProvider::addParameter(Option::SKIP, $criteria);
}

Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ parameters:
# for config class reflection
- src/Bootstrap/ExtensionConfigResolver.php
- config/config.php
- packages/Config/RectorConfig.php

# use of internal phpstan classes
-
Expand Down

0 comments on commit 59cf4dd

Please sign in to comment.