-
Notifications
You must be signed in to change notification settings - Fork 23
The preg_match always return with option array key when condition is === 1
#57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
cc @staabm who has some idea how this part works, I am even more confused by this than the last thread along these lines 😅 |
@shish this looks very similar. Was the other fix released already? @kayw-geek which version of the safe/* packages do you use? |
Yup, checked that a testcase fails with the current phpstan-safe-rule master branch + Safe 3.0.2 (and verified that |
Ok thanks. Will have a look tomorrow :-) |
@staabm The |
I have tested with this reproducer (as the origin report does not provide a full runnable example): <?php declare(strict_types = 1);
function doFoo(string $string) {
$pattern = '/\s/';
$result = preg_match($pattern, $string, $matches);
if ($result === 1){
\PHPStan\dumpType($matches);
}
if (preg_match($pattern, $string, $matches) === 1) {
\PHPStan\dumpType( $matches); // line 24
}
} this case works with \preg_match() because PHPStan has a built-in hack for it in I have a open todo on phpstan-src which I guess will fix this problem. In case this is a high priority issue for you, please consider sponsoring my open source work. |
Hi @shish, Thank you for adding more features to Safe.
Currently, I found an issue where the return value of the \Safe\preg_match() method must be assigned to a variable for PHPStan to correctly infer it; otherwise, it assumes that the array key might not exist.
The text was updated successfully, but these errors were encountered: