2

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);
5
  • 2
    Use inline PHPDoc to declare them. No other way (except, maybe, suppressing warning for that line, which is just wrong approach overall). Another alternative requires editing your code to use real variables. Commented Jul 30, 2015 at 18:02
  • Do you mean like I edited in? @LazyOne, seems like the error is gone now. Commented Jul 30, 2015 at 18:05
  • Yes -- exactly what I meant. Commented Jul 30, 2015 at 18:05
  • Great, thank you! @LazyOne Commented Jul 30, 2015 at 18:06
  • Not sure how PHPstorm will see it, but an array? $result[$table] = $this->setUpTables($table, $prefix); Commented Jul 30, 2015 at 18:14

1 Answer 1

4

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 */
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.