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

Add support for isMatch in phpstan type ext #26

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/PHPStan/PregMatchParameterOutTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function isStaticMethodSupported(MethodReflection $methodReflection, Para
{
return
$methodReflection->getDeclaringClass()->getName() === Preg::class
&& $methodReflection->getName() === 'match'
&& in_array($methodReflection->getName(), ['match', 'isMatch'], true)
&& $parameter->getName() === 'matches';
}

Expand Down
2 changes: 1 addition & 1 deletion src/PHPStan/PregMatchTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function getClass(): string

public function isStaticMethodSupported(MethodReflection $methodReflection, StaticCall $node, TypeSpecifierContext $context): bool
{
return $methodReflection->getName() === 'match' && !$context->null();
return in_array($methodReflection->getName(), ['match', 'isMatch'], true) && !$context->null();
}

public function specifyTypes(MethodReflection $methodReflection, StaticCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStanTests/nsrt/preg-match.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ function doMatch(string $s): void
assertType('array{}', $matches);
}
assertType('array{}|array{0: string, 1?: string|null}', $matches);

if (Preg::isMatch('/Price: (?P<currency>£|€)\d+/', $s, $matches)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

something is wrong with Hoa parsing the AST for the pattern.

it works as expected with a pattern like

Suggested change
if (Preg::isMatch('/Price: (?P<currency>£|€)\d+/', $s, $matches)) {
if (Preg::isMatch('/Price: (£|€)\d+/', $s, $matches)) {

Copy link
Contributor

Choose a reason for hiding this comment

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

opened a upstream bug report regarding the bogus-pattern:

phpstan/phpstan#11323

assertType('array{0: string, 1: string|null, currency: string|null}', $matches);
} else {
assertType('array{}', $matches);
}
assertType('array{}|array{0: string, 1?: string|null}', $matches);
}

// disabled until https://github.com/phpstan/phpstan-src/pull/3185 can be resolved
Expand Down
Loading