1
votes

I've got some problems with PHP Semantic Analysis in Eclipse PDT. I'm getting "undefined variable" warning in cases like this:

a.php:

<?php
$a = "var";
?>

b.php:

<?php
/* @var $a String */
/* @var $var String */
include a.php;
echo $a;              // Eclipse says $a is undefined

$arr = ["var" => "val"];
extract($arr);
echo $var;            // Eclipse says $var is undefined
?>

I've tried to notify Eclipse about those variables by PhpDoc syntax but it ignores it.

Is there any way to get rid of the warning not turning it off?

2
That's expected behaviour. Eclipse has no way of knowing that $arr holds a key that will become $var when extracted, and determining that $a was defined in another file isn't trivial either. - GordonM
I'm voting to close this question as off-topic because it's about a development tool, not a programming question - GordonM
I not expect Eclipse to analyze $arr for keys become extracted. But I expect it to get this information from PhpDoc comments. That's how it works in PhpStorm.. - izbushka
Eclipse isn't PHPStorm. If you want something that behaves like PHPStorm, then use PHPStorm. - GordonM

2 Answers

2
votes

This is fixed. You can write in b.php

/**
 * @var string  $a
 * @var MyClass $otherVar
 */
echo $a;    // No warning should be shown as if $a is undefined.
$otherVar-> // Autocomplete will show you MyClass methods and properties.
2
votes

Semantic Analysis is not part of Eclipse PDT. This feature is part of PEX Core-Plugin https://github.com/pdt-eg/Core-Plugin

Reading variable information from comments is still on my todo list: https://github.com/pdt-eg/Core-Plugin/issues/103

EDIT:

PDT Since 5.0 have own built-in semantic validation. Since 5.3 you can configure problem severity (Ingore/Info/Warning/Error). Since 6.0 PEX Variable Validator (unused / undefined) was adopted. We also add API for validation extensions (Symfony and Doctrine plugin already use it).