Skip to content
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

preg_match's referenced matches not considered updated #5365

Closed
ondrejmirtes opened this issue Jul 22, 2021 Discussed in #5361 · 3 comments
Closed

preg_match's referenced matches not considered updated #5365

ondrejmirtes opened this issue Jul 22, 2021 Discussed in #5361 · 3 comments
Labels
Milestone

Comments

@ondrejmirtes
Copy link
Member

Discussed in #5361

Originally posted by hollodotme July 22, 2021

Code

<?php declare(strict_types = 1);

$matches = [];
$pattern = '#^C\s+(?<productId>\d+)$#i';
$subject = 'C 1234567890';

$found = (bool)preg_match( $pattern, $subject, $matches ) && isset( $matches['productId'] );

PHPStan Error

LINE   ERROR
-------------------------------------
7      Result of && is always false.

See: https://phpstan.org/r/dc55b6b9-5285-4b3c-895f-bb036dd1e2d5

IMO this is a false positive as the second condition isset($matches['productId']) is true after evaluation of the preg_match function.

The $matches array was changed by preg_match() via reference.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Jul 22, 2021
@ondrejmirtes
Copy link
Member Author

Looks like PHPStan is a bit confused about already existing $matches variable, without it it works: https://phpstan.org/r/ae249e12-2326-494c-968c-316e503e9768

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-No errors
+8: Result of && is always false.
Full report
Line Error
8 Result of && is always false.

@github-actions
Copy link

github-actions bot commented Aug 1, 2023

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants