Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NodeNameResolver] Check regex start delimiter and contains * on NodeNameResolver to use fnmatch() #4964

Merged
merged 5 commits into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
samsonasik marked this conversation as resolved.
Show resolved Hide resolved
}

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';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the configured test is using Nette\*, this is new fixture to test it :)

->ruleWithConfiguration(StringClassNameToClassConstantRector::class, ['Nette\*', 'Error', 'Exception']);

}
}
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\*',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is existing example using fnmatch().

'#^PHPStan\\\\Type#',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is example using regex check.

'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;
}
Comment on lines -183 to -185
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here removed and ensure the new fixture working (skipped) as expected.

}

return false;
Expand Down