diff --git a/packages/NodeNameResolver/NodeNameResolver.php b/packages/NodeNameResolver/NodeNameResolver.php index d0801226be1..8d287967248 100644 --- a/packages/NodeNameResolver/NodeNameResolver.php +++ b/packages/NodeNameResolver/NodeNameResolver.php @@ -15,7 +15,9 @@ use Rector\CodingStyle\Naming\ClassNaming; use Rector\Core\Exception\ShouldNotHappenException; use Rector\Core\NodeAnalyzer\CallAnalyzer; +use Rector\Core\Util\StringUtils; use Rector\NodeNameResolver\Contract\NodeNameResolverInterface; +use Rector\NodeNameResolver\Regex\RegexPatternDetector; use Rector\NodeTypeResolver\Node\AttributeKey; final class NodeNameResolver @@ -31,6 +33,7 @@ final class NodeNameResolver public function __construct( private readonly ClassNaming $classNaming, private readonly CallAnalyzer $callAnalyzer, + private readonly RegexPatternDetector $regexPatternDetector, private readonly iterable $nodeNameResolvers = [] ) { } @@ -181,7 +184,19 @@ public function isStringName(string $resolvedName, string $desiredName): bool return $desiredName === $resolvedName; } - return strcasecmp($resolvedName, $desiredName) === 0; + if (strcasecmp($resolvedName, $desiredName) === 0) { + return true; + } + + if ($this->regexPatternDetector->isRegexPattern($desiredName)) { + return StringUtils::isMatch($resolvedName, $desiredName); + } + + if (str_contains($desiredName, '*')) { + return fnmatch($desiredName, $resolvedName, FNM_NOESCAPE); + } + + return false; } private function isCallOrIdentifier(Expr|Identifier $node): bool diff --git a/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_nette_class.php.inc b/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_nette_class.php.inc new file mode 100644 index 00000000000..f9f3104afc9 --- /dev/null +++ b/rules-tests/Php55/Rector/String_/StringClassNameToClassConstantRector/Fixture/skip_nette_class.php.inc @@ -0,0 +1,11 @@ +ruleWithConfiguration(StringClassNameToClassConstantRector::class, ['Nette\*', 'Error', 'Exception']); + ->ruleWithConfiguration(StringClassNameToClassConstantRector::class, [ + 'Nette\*', + '#^PHPStan\\\\Type#', + 'Error', + 'Exception' + ]); }; diff --git a/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php b/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php index 4e7ac17bc0a..d87355ae6bc 100644 --- a/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php +++ b/rules/Php55/Rector/String_/StringClassNameToClassConstantRector.php @@ -179,10 +179,6 @@ private function shouldSkip(string $classLikeName): bool if ($this->nodeNameResolver->isStringName($classLikeName, $classToSkip)) { return true; } - - if (fnmatch($classToSkip, $classLikeName, FNM_NOESCAPE)) { - return true; - } } return false;