From 8383436c9b0448f8a57d171ecbd18a7bc0d4798f Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 11 Jul 2024 13:57:38 +0200 Subject: [PATCH] Add support for isMatch in phpstan type ext --- src/PHPStan/PregMatchParameterOutTypeExtension.php | 2 +- src/PHPStan/PregMatchTypeSpecifyingExtension.php | 2 +- tests/PHPStanTests/nsrt/preg-match.php | 7 +++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/PHPStan/PregMatchParameterOutTypeExtension.php b/src/PHPStan/PregMatchParameterOutTypeExtension.php index 7793844..186f549 100644 --- a/src/PHPStan/PregMatchParameterOutTypeExtension.php +++ b/src/PHPStan/PregMatchParameterOutTypeExtension.php @@ -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'; } diff --git a/src/PHPStan/PregMatchTypeSpecifyingExtension.php b/src/PHPStan/PregMatchTypeSpecifyingExtension.php index 63559cc..00d1de5 100644 --- a/src/PHPStan/PregMatchTypeSpecifyingExtension.php +++ b/src/PHPStan/PregMatchTypeSpecifyingExtension.php @@ -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 diff --git a/tests/PHPStanTests/nsrt/preg-match.php b/tests/PHPStanTests/nsrt/preg-match.php index a217b57..441d203 100644 --- a/tests/PHPStanTests/nsrt/preg-match.php +++ b/tests/PHPStanTests/nsrt/preg-match.php @@ -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£|€)\d+/', $s, $matches)) { + 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