From 8383436c9b0448f8a57d171ecbd18a7bc0d4798f Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 11 Jul 2024 13:57:38 +0200 Subject: [PATCH 1/2] 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 From 92f975f539a7a9736f51f2f22b11aa140b7ea3cb Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 11 Jul 2024 15:26:26 +0200 Subject: [PATCH 2/2] Fix example to work around phpstan bug for now --- tests/PHPStanTests/nsrt/preg-match.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/PHPStanTests/nsrt/preg-match.php b/tests/PHPStanTests/nsrt/preg-match.php index 441d203..e9f7caf 100644 --- a/tests/PHPStanTests/nsrt/preg-match.php +++ b/tests/PHPStanTests/nsrt/preg-match.php @@ -28,12 +28,12 @@ function doMatch(string $s): void } 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); + if (Preg::isMatch('/Price: (?£|€)\d+/', $s, $matches)) { + assertType('array{0: string, currency: string|null, 1: string|null}', $matches); } else { assertType('array{}', $matches); } - assertType('array{}|array{0: string, 1?: string|null}', $matches); + assertType('array{}|array{0: string, currency: string|null, 1: string|null}', $matches); } // disabled until https://github.com/phpstan/phpstan-src/pull/3185 can be resolved