Hi guys, can't figure out how come the $leadApi is undefined here… Maybe you could shade some light?

Looks like a bug from that screenshot. Can you post a minimal reproducible example to help debug this?
I can only reproduce this in anonymous functions:
If I put this in a new file, It reports $key and $val as undefined variable.
<?php
$f = function() {
$arr = [
'test' => 'vars in foreach loops',
];
foreach ($arr as $key => $val) {
var_dump($key); var_dump($val);
echo "$key: $val";
}
};
In global scope, normal functions and class methods it works as expected.
Looks like a bug from that screenshot. Can you post a minimal reproducible example to help debug this?
Like @TiSiE said, it happens inside an anonymous function. Sorry, it wasn't visible in my screenshot.
I can only reproduce this in anonymous functions:
If I put this in a new file, It reports $key and $val as undefined variable.
<?php $f = function() { $arr = [ 'test' => 'vars in foreach loops', ]; foreach ($arr as $key => $val) { var_dump($key); var_dump($val); echo "$key: $val"; } };In global scope, normal functions and class methods it works as expected.
I can confirm I get this issue in class methods when using a foreach.
I can also confirm that this is happening in anonymous methods only. Intelephense 1.0.14 in VS Code 1.33.1
Yup can confirm this is happening for me in anonymous functions as well.
https://i.imgur.com/Q9pqQzl.png
Quite annoying actually.
Intelephense 1.0.14
VSCode 1.33.1
I've just stumbled upon the same issue, glad to see I'm not insane (yet)!
The following hypothetical snippet raises a vscode warning Undefined variable '$test'.intelephense(10001):
$array1 = $array2 = [];
$tmp = array_map(function($element) use($array1) {
foreach ($array1 as $test) {
echo $element[$test] . PHP_EOL;
}
}, $array2);
Intelephense 1.0.14
VSCode 1.34.0
I want to report the same problem

Same here with version 1.0.14

I have to add a DocBlock in order to avoid the warning.

Also getting this in switch statements without the closure.
switch ($foo) {
switch ($bar) {
case '1':
foreach ($x as $i) { echo $i; } // $i - Undefined variable. intelephense(10001)
break;
}
}
I have similar problem on VSCode;

any fix for this bug?
Most helpful comment
I can only reproduce this in anonymous functions:
If I put this in a new file, It reports $key and $val as undefined variable.
In global scope, normal functions and class methods it works as expected.