-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Infer QueryBuilderType for any method returning QueryBuilder
- Loading branch information
Showing
13 changed files
with
265 additions
and
118 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
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
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
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
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
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
103 changes: 103 additions & 0 deletions
103
src/Type/Doctrine/QueryBuilder/ReturnQueryBuilderExpressionTypeResolverExtension.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,103 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine\QueryBuilder; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\EntityRepository; | ||
use Doctrine\ORM\QueryBuilder; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\CallLike; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Identifier; | ||
use PhpParser\Node\Name; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\ExpressionTypeResolverExtension; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use function count; | ||
|
||
class ReturnQueryBuilderExpressionTypeResolverExtension implements ExpressionTypeResolverExtension | ||
{ | ||
|
||
/** @var OtherMethodQueryBuilderParser */ | ||
private $otherMethodQueryBuilderParser; | ||
|
||
public function __construct( | ||
OtherMethodQueryBuilderParser $otherMethodQueryBuilderParser | ||
) | ||
{ | ||
$this->otherMethodQueryBuilderParser = $otherMethodQueryBuilderParser; | ||
} | ||
|
||
public function getType(Expr $expr, Scope $scope): ?Type | ||
{ | ||
if (!$expr instanceof MethodCall && !$expr instanceof StaticCall) { | ||
return null; | ||
} | ||
|
||
if ($expr->isFirstClassCallable()) { | ||
return null; | ||
} | ||
|
||
$methodReflection = $this->getMethodReflection($expr, $scope); | ||
|
||
if ($methodReflection === null) { | ||
return null; | ||
} | ||
|
||
$returnType = ParametersAcceptorSelector::selectFromArgs($scope, $expr->getArgs(), $methodReflection->getVariants())->getReturnType(); | ||
|
||
$returnsQueryBuilder = (new ObjectType(QueryBuilder::class))->isSuperTypeOf($returnType)->yes(); | ||
|
||
if (!$returnsQueryBuilder) { | ||
return null; | ||
} | ||
|
||
$queryBuilderTypes = $this->otherMethodQueryBuilderParser->findQueryBuilderTypesInCalledMethod($scope, $methodReflection); | ||
if (count($queryBuilderTypes) === 0) { | ||
return null; | ||
} | ||
|
||
return TypeCombinator::union(...$queryBuilderTypes); | ||
} | ||
|
||
/** | ||
* @param StaticCall|MethodCall $call | ||
*/ | ||
private function getMethodReflection(CallLike $call, Scope $scope): ?MethodReflection | ||
{ | ||
if (!$call->name instanceof Identifier) { | ||
return null; | ||
} | ||
|
||
if ($call instanceof MethodCall) { | ||
$callerType = $scope->getType($call->var); | ||
} else { | ||
if (!$call->class instanceof Name) { | ||
return null; | ||
} | ||
$callerType = $scope->resolveTypeByName($call->class); | ||
} | ||
|
||
$methodName = $call->name->name; | ||
|
||
foreach ($callerType->getObjectClassReflections() as $callerClassReflection) { | ||
if ($callerClassReflection->is(QueryBuilder::class)) { | ||
return null; // covered by QueryBuilderMethodDynamicReturnTypeExtension | ||
} | ||
if ($callerClassReflection->is(EntityRepository::class) && $methodName === 'createQueryBuilder') { | ||
return null; // covered by EntityRepositoryCreateQueryBuilderDynamicReturnTypeExtension | ||
} | ||
if ($callerClassReflection->is(EntityManagerInterface::class) && $methodName === 'createQueryBuilder') { | ||
return null; // no need to dive there | ||
} | ||
} | ||
|
||
return $scope->getMethodReflection($callerType, $methodName); | ||
} | ||
|
||
} |
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
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
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
Oops, something went wrong.