phpstan-symfony icon indicating copy to clipboard operation
phpstan-symfony copied to clipboard

Add Twig template exists rule

Open zacharylund opened this issue 1 year ago • 2 comments

Fixes #257

zacharylund avatar Aug 21 '24 13:08 zacharylund

I would use the Twig environment using a loader, then it auto picks up all namespaces.

@ruudk, I've updated this to use a loader, but I'm not loving it. Because twig is a private service, the loader would need to look something like:

use App\Kernel;

use App\Kernel;
use Symfony\Component\Dotenv\Dotenv;

require __DIR__ . '/../vendor/autoload.php';

(new Dotenv())->bootEnv(__DIR__ . '/../.env');

$kernel = new Kernel('test', (bool) $_SERVER['APP_DEBUG']);
$kernel->boot();

return $kernel->getContainer()->get('test.service_container')->get('twig');

zacharylund avatar Aug 25 '24 20:08 zacharylund

Yes. Downside is that now you are using the test environment.

In TwigStan I came up with this solution: https://github.com/twigstan/twigstan/blob/90e1792a68dc0cd9471ce87a1e1f0145467299f9/twig-loader-symfony.php.dist#L18-L26

ruudk avatar Aug 26 '24 08:08 ruudk