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_shift() regression with some arrays #7968

Closed
cs278 opened this issue Sep 8, 2022 · 2 comments · Fixed by phpstan/phpstan-src#1712
Closed

array_shift() regression with some arrays #7968

cs278 opened this issue Sep 8, 2022 · 2 comments · Fixed by phpstan/phpstan-src#1712

Comments

@cs278
Copy link
Contributor

cs278 commented Sep 8, 2022

Bug report

PHPStan 1.8.2 didn't raise an error on this code but starting from 1.8.3 an error is raised, the rules for array_shift()/array_pop() no longer seem to understand that the array can be empty.

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

class HelloWorld
{
	/**
	 * @param array{name: string, age: int} $a
	 * @param array{name: string, age: int} $b
	 */
	public function compare(array $a, array $b): int
	{
		$sort = [
			$a['name'] <=> $b['name'],
			$a['age'] <=> $b['age'],
		];
		
		$sort = array_filter($sort, function (int $value): bool {
			return $value !== 0;
		});
		
		return array_shift($sort) ?? 0; // Error: Expression on left side of ?? is not nullable.
	}
}

https://phpstan.org/r/1dfa2673-c745-4d1b-9492-e50a6ab54eaa

Expected output

No error should be raised.

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

Helps me write working code first time every day.

@herndlm
Copy link
Contributor

herndlm commented Sep 8, 2022

I'm on it, this should be a similar thing as phpstan/phpstan-src#1696
also: I broke that, sorry 😅

@github-actions
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 Oct 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants