Skip to content

Variables declared by phpdoc are marked as undefined in arrow functions #339

@fpierrat

Description

@fpierrat

A variable defined "somewhere else"(*)
and explicited through a /**@var string $myvar*/
triggers an "undefined..." warning when this variable is used in an arrow function.

(*) e.g. :

  • variables defined in a separate file included with include()
  • or associative array extracted into variables using extract()

Notice: this issue is similar to #316

Describe the eclipse environment
Eclipse IDE for PHP Developers 2026-03 (4.39.0) / Eclipse PDT 8.4.0.202512011310

Describe your system

  • OS: win11
  • PHP Version: 8.3

To Reproduce
See capture for eclipse warnings.

    $arr = ['foo' => 'bar', 'baz' => "doo"];
    extract($arr); // $foo and $baz are now defined
    
    $test_1 = $foo . "1";  // PDT is not aware of $foo
    
    /**@var string $foo*/
    /**@var string $baz*/
    // PDT is now aware of $foo and baz
    
    $test_2 = $foo . "2"; // PDT is aware of $foo, no warning
    
    $test_3 = array_map(fn($it)=>'test'.$foo.$baz.$it , ['3','4','5']); // PDT is not aware of baz in arrow func
    //(but strangely, it IS aware of foo)
    
    echo "<pre>$test_1\n$test_2\n";
    print_r($test_3);

Expected behavior
As stated by PHP doc "Arrow functions support the same features as anonymous functions, except that using variables from the parent scope is always automatic." (https://www.php.net/manual/en/functions.arrow.php)
PDT "knows" of the $myvar variable thanks to the /**@var...*/ block, so it should not consider it undeclared inside of the arrow function.

Screenshots

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions