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 in match expression - variable might be undefined #10418

Closed
uuf6429 opened this issue Jan 10, 2024 · 2 comments
Closed

preg_match in match expression - variable might be undefined #10418

uuf6429 opened this issue Jan 10, 2024 · 2 comments

Comments

@uuf6429
Copy link

uuf6429 commented Jan 10, 2024

Bug report

match expressions in PHP can make up as a decent pattern matching engine. Unfortunately it seems to confuse phpstan a bit.

Here are three similar cases, for side-by side comparison:

Might be related to #7705, not sure.

Code snippet that reproduces the problem

$subject = 'hello world';
$something = match (1) {
    preg_match('/(\d+)/', $subject, $match) => 'it is a number: ' . $match[1],
    preg_match('/(\w+)/', $subject, $match) => 'it is a word: ' . $match[1],
    default => 'it is...something',
};

Actual output

Variable $match might not be defined.

Expected output

phpstan warning was not expected

Did PHPStan help you today? Did it make you happy in any way?

Help - yes - it did catch quite a few non-trivial mistakes.
Happy, not so much - means I need to review my own stuff more often. 🙈 😅

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@2b74aa8

Copy link

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 Feb 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants