Skip to content

Commit

Permalink
[NodeNameResolver] Check regex start delimiter and contains * on Node…
Browse files Browse the repository at this point in the history
…NameResolver to use fnmatch() (#4964)

* [NodeNameResolver] Check starts or ends with * on NodeNameResolver to use fnmatch()

* [NodeNameResolver] Add fixture

* clean up

* Add back regex support

* example use regex
  • Loading branch information
samsonasik authored Sep 10, 2023
1 parent 0717306 commit ef6c8bc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
17 changes: 16 additions & 1 deletion packages/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = []
) {
}
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Fixture;

final class SkipNetteClass
{
public function run()
{
return 'Nette\Utils\FileSystem';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\Php55\Rector\String_\StringClassNameToClassConstantRector\Fixture;

final class SkipPHPStanClass
{
public function run()
{
return 'Nette\Utils\FileSystem';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@

return static function (RectorConfig $rectorConfig): void {
$rectorConfig
->ruleWithConfiguration(StringClassNameToClassConstantRector::class, ['Nette\*', 'Error', 'Exception']);
->ruleWithConfiguration(StringClassNameToClassConstantRector::class, [
'Nette\*',
'#^PHPStan\\\\Type#',
'Error',
'Exception'
]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ef6c8bc

Please sign in to comment.