Skip to content

Regression: regex matches after checking non-empty array #14270

@ondrejmirtes

Description

@ondrejmirtes

Bug report

What do you think about this? /cc @staabm

public function getDuration(string $path): void {
		preg_match('~^([a-z]+)\\:\\/\\/(.+)~', $path, $matches);
		$scheme = null;
		if ($matches !== []) {
			[, $scheme, $path] = $matches;
		}
	}

I think if the array is non-empty then the keys will exist, right? But PHPStan reports:

Offset 1 might not exist on non-empty-list{0?: string, 1?: non-empty-string, 2?: non-empty-string}.
Offset 2 might not exist on non-empty-list{0?: string, 1?: non-empty-string, 2?: non-empty-string}.

Code snippet that reproduces the problem

https://phpstan.org/r/9344ca07-6178-44d2-844b-b2b3903d2a2b

Expected output

No errors

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

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions