diff --git a/src/PHPStan/PregMatchParameterOutTypeExtension.php b/src/PHPStan/PregMatchParameterOutTypeExtension.php index 186f549..4cd9d05 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 - && in_array($methodReflection->getName(), ['match', 'isMatch'], true) + && in_array($methodReflection->getName(), ['match', 'isMatch', 'matchStrictGroups', 'isMatchStrictGroups'], true) && $parameter->getName() === 'matches'; } diff --git a/src/PHPStan/PregMatchTypeSpecifyingExtension.php b/src/PHPStan/PregMatchTypeSpecifyingExtension.php index 00d1de5..b83ed12 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 in_array($methodReflection->getName(), ['match', 'isMatch'], true) && !$context->null(); + return in_array($methodReflection->getName(), ['match', 'isMatch', 'matchStrictGroups', 'isMatchStrictGroups'], 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 e9f7caf..fa3ad0a 100644 --- a/tests/PHPStanTests/nsrt/preg-match.php +++ b/tests/PHPStanTests/nsrt/preg-match.php @@ -36,6 +36,30 @@ function doMatch(string $s): void assertType('array{}|array{0: string, currency: string|null, 1: string|null}', $matches); } +function doMatchStrictGroups(string $s): void +{ + if (Preg::matchStrictGroups('/Price: /i', $s, $matches)) { + assertType('array{string}', $matches); + } else { + assertType('array{}', $matches); + } + assertType('array{}|array{string}', $matches); + + if (Preg::matchStrictGroups('/Price: (£|€)\d+/', $s, $matches)) { + assertType('array{string, string}', $matches); + } else { + assertType('array{}', $matches); + } + assertType('array{}|array{string, string}', $matches); + + if (Preg::isMatchStrictGroups('/Price: (£|€)?\d+/', $s, $matches)) { + assertType('array{string, string}', $matches); + } else { + assertType('array{}', $matches); + } + assertType('array{}|array{string, string}', $matches); +} + // disabled until https://github.com/phpstan/phpstan-src/pull/3185 can be resolved // //function identicalMatch(string $s): void