0

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.

Grodriguez
  • 21,501
  • 10
  • 63
  • 107
  • You cannot echo an Array! Could it just be that thats causing the message – RiggsFolly Nov 17 '22 at 10:48
  • Read the comment under/in your linked question that starts with "just a config/bootstrap file". Thats explains it good. And "might not" is not "not" defined. – Foobar Nov 17 '22 at 10:50
  • @RiggsFolly no, that's not the problem – Grodriguez Nov 17 '22 at 11:19
  • @Foobar No, the linked question does not explicitly `require` or `include` anything, it just relies on the variables being defined elsewhere. PHPStan cannot know that. This case is different. – Grodriguez Nov 17 '22 at 11:20
  • @Grodriguez Ok, then try something like `/** @var array $config */` at the top of the file, see: https://github.com/phpstan/phpstan/issues/5815 – Foobar Nov 17 '22 at 11:45
  • @Grodriguez Also i have seen your Bug Report, may you have found something that needs to be fixed. But i thing PHPStan works best on OOP style projects. – Foobar Nov 17 '22 at 11:49

1 Answers1

2

One Solution:

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

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

Grodriguez
  • 21,501
  • 10
  • 63
  • 107
Foobar
  • 769
  • 1
  • 4