vscode-intelephense
vscode-intelephense copied to clipboard
Array - Type inference inconsistency
Description:
There is an inconsistency in type inference when accessing an array value directly using a string key versus using a variable containing the same string key. The first method correctly infers the type of the value as a string, whereas the second method infers the type as mixed.
Example:
<?php declare (strict_types = 1);
function returnArray(): array
{
return [
'string' => 'abcd',
];
}
$array = returnArray();
if (isset($array['string']) && is_string($array['string'])) {
$value = $array['string']; // @var string $value 👍👍
}
$key = 'string';
if (isset($array[$key]) && is_string($array[$key])) {
$value = $array[$key]; // @var mixed $value
}