Skip to content

Using array_key_exists() is fine, but its not when using its alias key_exists() #8091

@Ilyes512

Description

@Ilyes512

Bug report

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

/**
* @param array{foo?: ?string} $data
*/
function foobar(array $data): void
{
	if (key_exists('foo', $data) && !is_null($data['foo'])) {
		var_dump($data['foo']);
	}
}

# key_exists() VS array_key_exists()

/**
* @param array{foo?: ?string} $data
*/
function foobar2(array $data): void
{
	if (array_key_exists('foo', $data) && !is_null($data['foo'])) {
		var_dump($data['foo']);
	}
}

https://phpstan.org/r/611006d0-f84e-4616-bc0b-c80defe6abbd

Expected output

When using array_key_exists() you get no warnings, while using it's alias key_exists() does return an warning:

Offset 'foo' does not exist on array{foo?: string|null}.

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

PHPStan is awesome!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions