Skip to content

array_column with array of objects evaluates incorrectly in PHP 8.2 #9204

@daniel-calderini

Description

@daniel-calderini

Bug report

Starting with PHP 8.2, PHPStan reports an error when using array_column with an array of objects having __get and __isset methods.

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

$goodObject = (new class() {
	/**
	 * @var array<string, mixed>
	 */
	private array $properties;

	/**
	 * @param array<string, mixed> $properties
	 */
	public function __construct(array $properties = [])
	{
	    $this->properties = $properties;
	}
	public function __isset(string $offset): bool
	{
		return isset($this->properties[$offset]);
	}
	public function __get(string $offset): mixed
	{
		return $this->properties[$offset] ?? null;
	}
});
$objects = [
	new $goodObject(['id' => 42]),
	new $goodObject(['id' => 1337]),
];

$columns = array_column($objects, 'id');

if ([42, 1337] === $columns) {
	echo 'Arrays are the same';
} else {
	echo 'Arrays are different';
}

When running the code above on 3v4l.org, the result is Arrays are the same in PHP versions 7.2 - 8.2.

However, PHPStan reports the following error:

Strict comparison using === between array{42, 1337} and array{} will always evaluate to false.

https://phpstan.org/r/69c4b8bb-042b-4992-b430-a686bd577f63

Expected output

No errors should be reported here. Running PHPStan with PHP 8.1 worked correctly. Since no changes were done to array_column in PHP 8.2, I guess the result should be the same in this version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions