1

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.

6
  • You cannot echo an Array! Could it just be that thats causing the message Commented Nov 17, 2022 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. Commented Nov 17, 2022 at 10:50
  • @RiggsFolly no, that's not the problem Commented Nov 17, 2022 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. Commented Nov 17, 2022 at 11:20
  • @Grodriguez Ok, then try something like /** @var array $config */ at the top of the file, see: github.com/phpstan/phpstan/issues/5815 Commented Nov 17, 2022 at 11:45

1 Answer 1

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

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.