Skip to content

Add dynamic return type extensions for getRepository() methods #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ services:
class: PHPStan\Type\Doctrine\EntityManagerFindDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\ObjectManagerGetRepositoryDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: PHPStan\Type\Doctrine\ManagerRegistryGetRepositoryDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: Doctrine\Common\Annotations\AnnotationReader
setup:
- Doctrine\Common\Annotations\AnnotationRegistry::registerLoader( class_exists )
106 changes: 106 additions & 0 deletions src/Type/Doctrine/AbstractGetRepositoryDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Doctrine;

use Doctrine\Common\Annotations\Reader;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping\Entity;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\BrokerAwareClassReflectionExtension;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;

abstract class AbstractGetRepositoryDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension, BrokerAwareClassReflectionExtension
{

/**
* @var \PHPStan\Broker\Broker
*/
private $broker;

/**
* @var \Doctrine\Common\Annotations\Reader
*/
private $annotationReader;

public function __construct(Reader $annotationReader)
{
$this->annotationReader = $annotationReader;
}

public function setBroker(\PHPStan\Broker\Broker $broker)
{
$this->broker = $broker;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return in_array($methodReflection->getName(), [
'getRepository',
], true);
}

public function getTypeFromMethodCall(
MethodReflection $methodReflection,
MethodCall $methodCall,
Scope $scope
): Type
{
if (count($methodCall->args) === 0) {
return $methodReflection->getReturnType();
}
$arg = $methodCall->args[0]->value;
if (!($arg instanceof \PhpParser\Node\Expr\ClassConstFetch)) {
return $methodReflection->getReturnType();
}

$class = $arg->class;
if (!($class instanceof \PhpParser\Node\Name)) {
return $methodReflection->getReturnType();
}

$class = (string) $class;

if ($class === 'static') {
return $methodReflection->getReturnType();
}

if ($class === 'self') {
$class = $scope->getClassReflection()->getName();
}

if (!$this->broker->hasClass($class)) {
return $methodReflection->getReturnType();
}

$classReflection = $this->broker->getClass($class);
$annotations = $this->annotationReader
->getClassAnnotations($classReflection->getNativeReflection());

/** @var \Doctrine\ORM\Mapping\Entity|null $entityAnnotation */
$entityAnnotation = null;
foreach ($annotations as $annotation) {
if ($annotation instanceof Entity) {
$entityAnnotation = $annotation;
}
}

if ($entityAnnotation === null) {
return $methodReflection->getReturnType();
}

$repositoryClass = EntityRepository::class;
if (
$entityAnnotation->repositoryClass &&
$this->broker->hasClass($entityAnnotation->repositoryClass)
) {
$repositoryClass = $entityAnnotation->repositoryClass;
}

return new ObjectType($repositoryClass);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Doctrine;

class ManagerRegistryGetRepositoryDynamicReturnTypeExtension extends AbstractGetRepositoryDynamicReturnTypeExtension
{

public static function getClass(): string
{
return \Doctrine\Common\Persistence\ManagerRegistry::class;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Doctrine;

class ObjectManagerGetRepositoryDynamicReturnTypeExtension extends AbstractGetRepositoryDynamicReturnTypeExtension
{

public static function getClass(): string
{
return \Doctrine\Common\Persistence\ObjectManager::class;
}

}