Vscode-intelephense: Undefined variable in foreach loop

Created on 2 May 2019  Â·  11Comments  Â·  Source: bmewburn/vscode-intelephense

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

image

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.

<?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.

All 11 comments

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

Code_jI77HiX71Z

Same here with version 1.0.14
imagen

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

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;
Screen Shot 2019-07-04 at 9 42 25 PM
any fix for this bug?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zlianon picture zlianon  Â·  4Comments

Ilyes512 picture Ilyes512  Â·  3Comments

zlianon picture zlianon  Â·  3Comments

superadmini picture superadmini  Â·  4Comments

nn-hh picture nn-hh  Â·  3Comments