Skip to content

Fix wrong inference on preg_match != false #4223

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

Merged
merged 1 commit into from
Aug 9, 2025

Conversation

VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Aug 8, 2025

@@ -30,7 +30,7 @@ public function sayHello3(string $s): void
public function sayHello4(string $s): void
{
if (preg_match('/data-(\d{6})\.json$/', $s, $matches) <= 0) {
assertType('array{}', $matches);
assertType('list{0?: string, 1?: non-falsy-string&numeric-string}', $matches);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be considered as a "regression" but:

BTW array{} is already technically not true since if preg_match return false, $matches is null.

@staabm
Copy link
Contributor

staabm commented Aug 9, 2025

Ondrey wanted this problem solved via a more general approach - see #3881

@ondrejmirtes ondrejmirtes merged commit 745ae5a into phpstan:2.1.x Aug 9, 2025
448 of 453 checks passed
@ondrejmirtes
Copy link
Member

Thank you! I'd like improvements to TypeSpecifierContext but we already have many hardcoded fixes in TypeSpecifier so this is just one more. It can be refactored later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

preg_match + if: false positive nullCoalesce.offset and isset.offset report an error on an array offset that can be null
3 participants