proget-hq / phpstan-yii2

Yii2 extension for PHPStan
MIT License
52 stars 18 forks source link

.env variables warnings #53

Open Julielesss opened 1 year ago

Julielesss commented 1 year ago

Hi! I use .env variables in my project and see these warnings when running phpstan: image

I created a bootstrap file with the following code:

$dotenv = new \Symfony\Component\Dotenv\Dotenv();
$dotenv->load(__DIR__.'/.env.test');

and add it to config Screenshot from 2022-12-08 13-14-10

There are lines like this: 'dsn' =>$_ENV['DB_TEST_DSN']

in config/test_db.php, and I require this file ./config/test.php

particleflux commented 1 year ago

The $dotenv->load() exposes the variables at runtime, while phpstan analyses statically.

It's probably better to just set all of those $_ENV values you use to a dummy value in the boostrap.php directly, without relying on dotenv, e.g:

$_ENV['DB_TEST_DSN'] = 'mysql:host=localhost';
$_ENV['DB_TEST_CONNECTION'] = 'foo';