bmewburn / vscode-intelephense

PHP intellisense for Visual Studio Code
https://intelephense.com
Other
1.6k stars 93 forks source link

Array - Type inference inconsistency #2971

Open runelanghelle opened 1 month ago

runelanghelle commented 1 month ago

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 
}
runelanghelle commented 1 month ago

Version details