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

Array count in match results in incorrect offset error in lower branch of match #10952

Closed
Grldk opened this issue Apr 30, 2024 · 4 comments
Closed
Labels
Milestone

Comments

@Grldk
Copy link

Grldk commented Apr 30, 2024

Bug report

I have some code similar to this:

		$return = match (true) {
			count($array) > 1 => $array[1],
			default => $array[0] ?? 'empty',			
		};

Phpstan reports an offset error on $array[0].

Code snippet that reproduces the problem

https://phpstan.org/r/3f2b24c5-b3b3-4dcb-b46c-685973482e53

Expected output

No errors should be given I think.

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

Phpstan really helps us maintaining our code quality to a reasonable level, thanks a lot!

@ondrejmirtes
Copy link
Member

Yeah, this is probably a bug.

It works correctly with list type which you use should anyway: https://phpstan.org/r/221e19ac-7484-436b-8338-91e4a7f0b170

In non-list array, even when count($array) is 1, you don't have a guarantee that $array[0] exists.

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Apr 30, 2024
@Grldk
Copy link
Author

Grldk commented Apr 30, 2024

Thanks for the fix!

@ondrejmirtes
Copy link
Member

Fixed phpstan/phpstan-src#3037

Copy link

github-actions bot commented Jun 4, 2024

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

No branches or pull requests

2 participants