Skip to content

Commit 9f7f3d2

Browse files
authored
Add support for isMatch in phpstan type ext (#26)
* Add support for isMatch in phpstan type ext * Fix example to work around phpstan bug for now
1 parent 37ef71e commit 9f7f3d2

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

src/PHPStan/PregMatchParameterOutTypeExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function isStaticMethodSupported(MethodReflection $methodReflection, Para
3030
{
3131
return
3232
$methodReflection->getDeclaringClass()->getName() === Preg::class
33-
&& $methodReflection->getName() === 'match'
33+
&& in_array($methodReflection->getName(), ['match', 'isMatch'], true)
3434
&& $parameter->getName() === 'matches';
3535
}
3636

src/PHPStan/PregMatchTypeSpecifyingExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function getClass(): string
4343

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

4949
public function specifyTypes(MethodReflection $methodReflection, StaticCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes

tests/PHPStanTests/nsrt/preg-match.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ function doMatch(string $s): void
2727
assertType('array{}', $matches);
2828
}
2929
assertType('array{}|array{0: string, 1?: string|null}', $matches);
30+
31+
if (Preg::isMatch('/Price: (?<currency>£|€)\d+/', $s, $matches)) {
32+
assertType('array{0: string, currency: string|null, 1: string|null}', $matches);
33+
} else {
34+
assertType('array{}', $matches);
35+
}
36+
assertType('array{}|array{0: string, currency: string|null, 1: string|null}', $matches);
3037
}
3138

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

0 commit comments

Comments
 (0)