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

Variable type change inside loop using break is erroneous

Open momala454 opened this issue 3 years ago • 4 comments

Describe the bug $a variable is of type null instead of object

To Reproduce

$a = null;
$check = false;
foreach ([['hello' => 'world']] as $item) {
    if ($item['hello'] === 'world') {
        $a = (object)$item;
        $check = true;
        break;
    }
}
if ($check) {
    echo $a->hello;
}

Expected behavior No error. Running the script display "world". Removing "break" prevent the error on intelephense. Removing the if also remove the error. It shows an error even if the condition is "if (1)"

Screenshots image

Platform and version windows 10 intelephense 1.8.1

is maybe related to https://github.com/bmewburn/vscode-intelephense/issues/2138

momala454 avatar Feb 15 '22 16:02 momala454

There's two parts to this. One is that the break type of $a is being lost, the other is that it is an indirect type guard for $a using the $checked variable. The first is easy enough to solve, the latter is more difficult.

bmewburn avatar Feb 15 '22 21:02 bmewburn

thanks, at least intelephense could set type to null|object if the "check" variable condition is difficult to handle

by the way, did you saw my response there ? https://github.com/bmewburn/vscode-intelephense/issues/1967

momala454 avatar Feb 15 '22 21:02 momala454

still valid in 1.10.0

momala454 avatar Nov 06 '23 08:11 momala454

Happens to me as well in 1.10.2.

    foreach ([''] as $val) {
        if (true) {
            $variable = '';
            break;
        }
    }

    echo $variable;

In VSCode looks like this: image

Criko0 avatar Feb 16 '24 08:02 Criko0

Closing this. The break variable type should now be fixed in 1.11. Tracking dependent types in #2966

bmewburn avatar Jul 30 '24 04:07 bmewburn