vscode-intelephense
vscode-intelephense copied to clipboard
Variable type change inside loop using break is erroneous
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
Platform and version windows 10 intelephense 1.8.1
is maybe related to https://github.com/bmewburn/vscode-intelephense/issues/2138
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.
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
still valid in 1.10.0
Happens to me as well in 1.10.2.
foreach ([''] as $val) {
if (true) {
$variable = '';
break;
}
}
echo $variable;
In VSCode looks like this:
Closing this. The break variable type should now be fixed in 1.11. Tracking dependent types in #2966