Skip to content

preg_match and switch/case: variable $match might not be defined #5326

@paulbalandan

Description

@paulbalandan

Bug report

Using this script, PHPStan 0.12.92 complains that the $match variable might be undefined although it would have been defined already when it satisfies the case arm:

<?php declare(strict_types = 1);

function normalize(string $value): string
{
	switch (true) {
		case strpos($value, 'get_') === 0 && preg_match('/get_(\S+)/m', $value, $match) === 1:
			return $match[1]; // Variable $match might not be defined.
		default:
			return $value;
	}
}

Code snippet that reproduces the problem

Reproducer:
https://phpstan.org/r/8dbc1625-dadd-40a5-b0ee-baff854ded3d

Proof that it works:
https://3v4l.org/XKsrG

Expected output

No errors.

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

Yes, as always. 😁

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions