Description
First of all, @shish thanks for you amazing work and adding phpstan's port for special-case support for preg_match
. That's brilliant.
However, I've found a small inconvenience in using that vs. how it's handled by native preg_match
. Example:
use function Safe\preg_match;
class a {
public function test(): void
{
$safeResult = preg_match('/^id-(?P<id>\d+)^/', 'id-123', $safeMatch);
assert(1 === $safeResult);
\PHPStan\dumpType($safeResult); // Dumped type: 1
\PHPStan\dumpType($safeMatch); // Dumped type: array{0?: string, id?: numeric-string, 1?: numeric-string}
$nativeResult = \preg_match('/^id-(?P<id>\d+)^/', 'id-123', $nativeMatch);
assert(1 === $nativeResult);
\PHPStan\dumpType($nativeResult); // Dumped type: 1
\PHPStan\dumpType($nativeMatch); // Dumped type: array{0: string, id: numeric-string, 1: numeric-string}
}
}
As you can see phpstan with using native preg_match
(after asserting its result that is 1
which mean match was found) is recognizing matched values are required/existing for sure in $match
argument (array contains id
key for sure).
With using safe version of the function it recognize the same match output, however its keys are optional (array may contain id id?
).
Maybe it's not so crucial, however this missing part prevents me from using safe version for now - that would require additional assertions that $match
variable contains expected keys for sure (what is not an intention why I chose safe library 😉).