-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dynamic return type extension for EntityRepository methods via a gene…
…ric type created by EM::getRepository()
- Loading branch information
1 parent
a27368f
commit 5fa20b8
Showing
4 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/Type/Doctrine/EntityManagerGetRepositoryDynamicReturnTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine; | ||
|
||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\Type; | ||
|
||
class EntityManagerGetRepositoryDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension | ||
{ | ||
|
||
public static function getClass(): string | ||
{ | ||
return \Doctrine\ORM\EntityManager::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'getRepository'; | ||
} | ||
|
||
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(); | ||
} | ||
|
||
return new EntityRepositoryType($class); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/Type/Doctrine/EntityRepositoryDynamicReturnTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine; | ||
|
||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\MixedType; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
|
||
class EntityRepositoryDynamicReturnTypeExtension implements \PHPStan\Type\DynamicMethodReturnTypeExtension | ||
{ | ||
|
||
public static function getClass(): string | ||
{ | ||
return \Doctrine\ORM\EntityRepository::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
$methodName = $methodReflection->getName(); | ||
return strpos($methodName, 'findBy') === 0 | ||
|| strpos($methodName, 'findOneBy') === 0 | ||
|| $methodName === 'findAll' | ||
|| $methodName === 'find'; | ||
} | ||
|
||
public function getTypeFromMethodCall( | ||
MethodReflection $methodReflection, | ||
MethodCall $methodCall, | ||
Scope $scope | ||
): Type | ||
{ | ||
$calledOnType = $scope->getType($methodCall->var); | ||
if (!$calledOnType instanceof EntityRepositoryType) { | ||
return new MixedType(); | ||
} | ||
$methodName = $methodReflection->getName(); | ||
$entityType = new ObjectType($calledOnType->getEntityClass()); | ||
|
||
if ($methodName === 'find' || strpos($methodName, 'findOneBy') === 0) { | ||
return TypeCombinator::addNull($entityType); | ||
} | ||
|
||
return new ArrayType($entityType); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine; | ||
|
||
use PHPStan\Type\ObjectType; | ||
|
||
class EntityRepositoryType extends ObjectType | ||
{ | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $entityClass; | ||
|
||
public function __construct(string $entityClass) | ||
{ | ||
parent::__construct(\Doctrine\ORM\EntityRepository::class); | ||
$this->entityClass = $entityClass; | ||
} | ||
|
||
public function getEntityClass(): string | ||
{ | ||
return $this->entityClass; | ||
} | ||
|
||
public function describe(): string | ||
{ | ||
return sprintf('%s<%s>', parent::describe(), $this->entityClass); | ||
} | ||
|
||
} |