Skip to content

Commit

Permalink
Add support for isMatch in phpstan type ext (#26)
Browse files Browse the repository at this point in the history
* Add support for isMatch in phpstan type ext

* Fix example to work around phpstan bug for now
  • Loading branch information
Seldaek authored Jul 11, 2024
1 parent 37ef71e commit 9f7f3d2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
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: (?<currency>£|€)\d+/', $s, $matches)) {
assertType('array{0: string, currency: string|null, 1: string|null}', $matches);
} else {
assertType('array{}', $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
Expand Down

0 comments on commit 9f7f3d2

Please sign in to comment.