Skip to content

array_shift() regression with some arrays #7968

@cs278

Description

@cs278

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions