Skip to content

Commit

Permalink
BC: Separated regex matching in NodeNameResolver->isName() (#4951)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Sep 9, 2023
1 parent b82ad4c commit 9a322af
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 25 deletions.
34 changes: 12 additions & 22 deletions packages/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@

final class NodeNameResolver
{
/**
* Used to check if a string might contain a regex or fnmatch pattern
*/
private const REGEX_WILDCARD_CHARS = ['*', '#', '~', '/'];

/**
* @var array<string, NodeNameResolverInterface|null>
*/
Expand Down Expand Up @@ -198,27 +193,22 @@ public function isStringName(string $resolvedName, string $desiredName): bool
return $desiredName === $resolvedName;
}

$containsWildcard = false;
foreach (self::REGEX_WILDCARD_CHARS as $char) {
if (str_contains($desiredName, $char)) {
$containsWildcard = true;
break;
}
}
return strtolower($resolvedName) === strtolower($desiredName);
}

if ($containsWildcard) {
// is probably regex pattern
if ($this->regexPatternDetector->isRegexPattern($desiredName)) {
return StringUtils::isMatch($resolvedName, $desiredName);
}
public function matchesStringName(string|Identifier $resolvedName, string $desiredNamePattern): bool
{
if ($resolvedName instanceof Identifier) {
$resolvedName = $resolvedName->toString();
}

// is probably fnmatch
if (\str_contains($desiredName, '*')) {
return fnmatch($desiredName, $resolvedName, FNM_NOESCAPE);
}
// is probably regex pattern
if ($this->regexPatternDetector->isRegexPattern($desiredNamePattern)) {
return StringUtils::isMatch($resolvedName, $desiredNamePattern);
}

return strtolower($resolvedName) === strtolower($desiredName);
// is probably fnmatch
return fnmatch($desiredNamePattern, $resolvedName, FNM_NOESCAPE);
}

private function isCallOrIdentifier(Expr|Identifier $node): bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ private function hasParentClassController(Class_ $class): bool
return false;
}

return $this->nodeNameResolver->isName($class->extends, '#(Controller|Presenter)$#');
return $this->nodeNameResolver->matchesStringName($class->extends->toString(), '#(Controller|Presenter)$#');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ private function shouldSkip(string $classLikeName): bool
if ($this->nodeNameResolver->isStringName($classLikeName, $classToSkip)) {
return true;
}

if ($this->nodeNameResolver->matchesStringName($classLikeName, $classToSkip)) {
return true;
}
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node

private function shouldSkipClassMethod(ClassMethod $classMethod): bool
{
if ($this->isName($classMethod, 'createComponent*')) {
if ($this->nodeNameResolver->matchesStringName($classMethod->name->toString(), 'createComponent*')) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ private function shouldSkip(Class_ $class): bool
if ($this->transformOnNamespaces !== []) {
$className = (string) $this->nodeNameResolver->getName($class);
foreach ($this->transformOnNamespaces as $transformOnNamespace) {
if (! $this->nodeNameResolver->isStringName($className, $transformOnNamespace)) {
if ($this->nodeNameResolver->isStringName($className, $transformOnNamespace)) {
continue;
}

if (! $this->nodeNameResolver->matchesStringName($className, $transformOnNamespace)) {
return true;
}
}
Expand Down

0 comments on commit 9a322af

Please sign in to comment.