vscode-intelephense icon indicating copy to clipboard operation
vscode-intelephense copied to clipboard

Array - Type inference inconsistency

Open runelanghelle opened this issue 7 months ago • 1 comments

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 avatar Aug 01 '24 02:08 runelanghelle