-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
140 additions
and
1 deletion.
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
69 changes: 69 additions & 0 deletions
69
src/Type/Doctrine/QueryBuilder/Expr/BaseExpressionDynamicReturnTypeExtension.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,69 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine\QueryBuilder\Expr; | ||
|
||
use PhpParser\Node\Expr\MethodCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Rules\Doctrine\ORM\DynamicQueryBuilderArgumentException; | ||
use PHPStan\Type\Doctrine\ArgumentsProcessor; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\Type; | ||
use function get_class; | ||
use function is_object; | ||
use function method_exists; | ||
|
||
class BaseExpressionDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
|
||
/** @var ArgumentsProcessor */ | ||
private $argumentsProcessor; | ||
|
||
public function __construct( | ||
ArgumentsProcessor $argumentsProcessor | ||
) | ||
{ | ||
$this->argumentsProcessor = $argumentsProcessor; | ||
} | ||
|
||
public function getClass(): string | ||
{ | ||
return 'Doctrine\ORM\Query\Expr\Base'; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type | ||
{ | ||
$defaultReturnType = ParametersAcceptorSelector::selectFromArgs($scope, $methodCall->getArgs(), $methodReflection->getVariants())->getReturnType(); | ||
|
||
try { | ||
$args = $this->argumentsProcessor->processArgs($scope, $methodReflection->getName(), $methodCall->getArgs()); | ||
} catch (DynamicQueryBuilderArgumentException $e) { | ||
return $defaultReturnType; | ||
} | ||
|
||
$calledOnType = $scope->getType($methodCall->var); | ||
if (!$calledOnType instanceof ExprType) { | ||
return $defaultReturnType; | ||
} | ||
|
||
$expr = $calledOnType->getExprObject(); | ||
|
||
if (!method_exists($expr, $methodReflection->getName())) { | ||
return $defaultReturnType; | ||
} | ||
|
||
$exprValue = $expr->{$methodReflection->getName()}(...$args); | ||
if (is_object($exprValue)) { | ||
return new ExprType(get_class($exprValue), $exprValue); | ||
} | ||
|
||
return $scope->getTypeFromValue($exprValue); | ||
} | ||
|
||
} |
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
47 changes: 47 additions & 0 deletions
47
tests/Rules/Doctrine/ORM/data/query-builder-dql-and-or-variants.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,47 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Doctrine\ORM; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
|
||
class TestAndxWithConsequentMethodCall | ||
{ | ||
|
||
/** @var EntityManager */ | ||
private $entityManager; | ||
|
||
public function __construct(EntityManager $entityManager) | ||
{ | ||
$this->entityManager = $entityManager; | ||
} | ||
|
||
public function emptyConstructorConsequentCall(): void | ||
{ | ||
$expr = (new \Doctrine\ORM\Query\Expr\Andx())->add('1 = 1'); | ||
$queryBuilder = $this->entityManager->createQueryBuilder(); | ||
$queryBuilder->select('e') | ||
->from(MyEntity::class, 'e') | ||
->andWhere($expr); | ||
$queryBuilder->getQuery(); | ||
} | ||
|
||
public function usedConstructor(): void | ||
{ | ||
$expr = new \Doctrine\ORM\Query\Expr\Andx('1 = 1'); | ||
$queryBuilder = $this->entityManager->createQueryBuilder(); | ||
$queryBuilder->select('e') | ||
->from(MyEntity::class, 'e') | ||
->andWhere($expr); | ||
$queryBuilder->getQuery(); | ||
} | ||
|
||
public function createdFromQueryBuilder(): void | ||
{ | ||
$queryBuilder = $this->entityManager->createQueryBuilder(); | ||
$queryBuilder->select('e') | ||
->from(MyEntity::class, 'e') | ||
->andWhere($queryBuilder->expr()->andX('1 = 1')); | ||
$queryBuilder->getQuery(); | ||
} | ||
|
||
} |
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