-
-
Notifications
You must be signed in to change notification settings - Fork 947
preg_match in match expression - variable might be undefined #10418
Copy link
Copy link
Closed
Description
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:
- https://phpstan.org/r/21bf6895-eca2-4ffe-922e-79bc2f4cf8bc - simple if condition, works 👍
- https://phpstan.org/r/7875af98-f3bc-4489-8694-adb9d2f549fb - using switch statement, also works 👍
- https://phpstan.org/r/577b8c37-acaf-46b1-8413-6b1dcf19b150 - and match statement, doesn't work 👎
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. 🙈 😅
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels