baao
baao

Reputation: 73231

Prevent error reporting for variable variable in PhpStorm

I have this code and get an error reported for the two "undefined" variables

$tables = [
            'foo',
            'bar',
            'baz'
        ];
foreach ($tables as $table) {
    $$table = $this->setUpTables($table, $prefix);
}
$all = $this->getBaz($foo,$bar); // those two are reported as undefined

Is it possible to tell PhpStorm to not report this "error"?

EDIT:

/** @var foo $foo */
/** @var bar $bar */
$all = $this->getBaz($foo,$bar);

Upvotes: 2

Views: 350

Answers (1)

erisco
erisco

Reputation: 14329

Using simpler language features wins in this case, I think. PhpStorm should also have no trouble figuring out which variables are in scope.

$products        = $this->setUpTables('products', $prefix);
$excludeRules    = $this->setUpTables('excludeRules', $prefix);
$excludedSellers = $this->setUpTables('excludedSellers', $prefix);
$livePricing     = $this->setUpTables('livePricing', $prefix);

$all = $this->getProducts($products, $livePricing);

If PhpStorm thinks a variable is out of scope when it is not, you can add this declaration within the scope.

/** @var variableName */

Upvotes: 4

Related Questions