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

RegularExpressionPatternRule: support extended Nette\Utils\Strings #165

Merged
merged 3 commits into from
Nov 15, 2024
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
5 changes: 3 additions & 2 deletions src/Rule/Nette/RegularExpressionPatternRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ObjectType;
use function in_array;
use function sprintf;
use function strtolower;
Expand Down Expand Up @@ -51,8 +52,8 @@ private function extractPatterns(StaticCall $staticCall, Scope $scope): array
if (!$staticCall->class instanceof Node\Name || !$staticCall->name instanceof Node\Identifier) {
return [];
}
$className = $scope->resolveName($staticCall->class);
if ($className !== Strings::class) {
$caller = $scope->resolveTypeByName($staticCall->class);
if (!(new ObjectType(Strings::class))->isSuperTypeOf($caller)->yes()) {
return [];
}
$methodName = strtolower((string) $staticCall->name);
Expand Down
4 changes: 4 additions & 0 deletions tests/Rule/Nette/RegularExpressionPatternRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public function testValidRegexPatternAfter73(): void
'Regex pattern is invalid: Compilation failed: missing closing parenthesis at offset 1 in pattern: ~(~',
26,
],
[
sprintf('Regex pattern is invalid: Delimiter must not be %s in pattern: nok', $messagePart),
36,
],
],
);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/Rule/Nette/data/valid-regex-pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@
'~(~' => function () {},
]
);

class MyStrings extends \Nette\Utils\Strings {}
MyStrings::replace('', 'nok', '');