Home > Mobile >  PHPStan reports possibly undefined variable in PHPStan, but it's defined in included script
PHPStan reports possibly undefined variable in PHPStan, but it's defined in included script

Time:11-17

I have an application where I define some variables in a file (a.php), then include this from another file (b.php). PHPStan is complaining about possibly undefined variables.

Simplified example:

a.php:

$config['foo'] = 'bar';

b.php:

<?php

require 'a.php';

//new SlimApp($config)->run();
echo $config;

PHPStan says:

------ ---------------------------------------- 
  Line   b.php                                   
 ------ ---------------------------------------- 
  :6     Variable $config might not be defined.  
 ------ ---------------------------------------- 

Also: I don't know why but if I remove the <?php line at the beginning of b.php, the warning goes away.

How can I have PHPStan realize that $config is actually defined?

Btw I am aware of this question. It is not the same problem as I am explicitly including the file where the variable is defined.

CodePudding user response:

One Solution:

Yo can prevent the notice with /** @var array $config */ at the top of the file.

See for more information here: github.com/phpstan/phpstan/issues/5815

  • Related