Skip to content

Commit

Permalink
Fix stubs in regard to LazyServiceEntityRepository again
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 12, 2023
1 parent ed1a207 commit 6812cf4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"require": {
"php": "^7.2 || ^8.0",
"phpstan/phpstan": "^1.9.7"
"phpstan/phpstan": "^1.9.10"
},
"conflict": {
"doctrine/collections": "<1.0",
Expand Down
27 changes: 21 additions & 6 deletions src/Stubs/Doctrine/StubFilesExtensionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@

namespace PHPStan\Stubs\Doctrine;

use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound;
use PHPStan\BetterReflection\Reflector\Reflector;
use PHPStan\PhpDoc\StubFilesExtension;
use function class_exists;
use function dirname;
use function trait_exists;

class StubFilesExtensionLoader implements StubFilesExtension
{

/** @var Reflector */
private $reflector;

/** @var bool */
private $bleedingEdge;

public function __construct(
Reflector $reflector,
bool $bleedingEdge
)
{
$this->reflector = $reflector;
$this->bleedingEdge = $bleedingEdge;
}

Expand All @@ -34,10 +39,20 @@ public function getFiles(): array
$path . '/EntityRepository.stub',
];

if (
trait_exists('Symfony\Component\VarExporter\LazyGhostTrait')
&& class_exists('Doctrine\Bundle\DoctrineBundle\Repository\LazyServiceEntityRepository')
) {
$hasLazyServiceEntityRepositoryAsParent = false;

try {
$serviceEntityRepository = $this->reflector->reflectClass('Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository');
if ($serviceEntityRepository->getParentClass() !== null) {
/** @var class-string $lazyServiceEntityRepositoryName */
$lazyServiceEntityRepositoryName = 'Doctrine\Bundle\DoctrineBundle\Repository\LazyServiceEntityRepository';
$hasLazyServiceEntityRepositoryAsParent = $serviceEntityRepository->getParentClass()->getName() === $lazyServiceEntityRepositoryName;
}
} catch (IdentifierNotFound $e) {
// pass
}

if ($hasLazyServiceEntityRepositoryAsParent) {
$files[] = $stubsDir . '/LazyServiceEntityRepository.stub';
} else {
$files[] = $stubsDir . '/ServiceEntityRepository.stub';
Expand Down

0 comments on commit 6812cf4

Please sign in to comment.