DoctrineBundle: phpstan failure on LazyGhostTrait with Sym5.4.*

phpstan level 3 analysis now failing with

Fatal error: Trait “Symfony\Component\VarExporter\LazyGhostTrait” not found in /my-app/vendor/doctrine/doctrine-bundle/Repository/LazyServiceEntityRepository.php on line 31

This is on upgrade of doctrine/DoctrineBundle from 2.7.2 to 2.8.2

I am using Symfony 5.4.18

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

This problem can happen pretty easily:

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

use Doctrine\Bundle\DoctrineBundle\Repository\LazyServiceEntityRepository;

class_exists(LazyServiceEntityRepository::class);

This is enough to make this bundle crash without PHPStan being involved at all.

The problem is that you put a class in your autoloader, but the class is broken unless another dependency is required. That’s why PHPStan considers this class to be safely autoloadable but actually isn’t.

I’m gonna fix it on phpstan-doctrine’s side, but it’s something to keep in mind when designing your classes.

Glad I could help!

I don’t really know, personally I’d avoid “optional” dependencies altogether because they’re messy. “LazyServiceEntityRepository” should be defined in a package that has both DoctrineBundle and VarExporter in require