forked from phpstan/phpstan-doctrine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a DynamicMethodReturnTypeExtension for the DBAL QueryBuilde…
…r execute method fixes phpstan#117
- Loading branch information
1 parent
723ed14
commit ebaa9eb
Showing
6 changed files
with
174 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
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
57 changes: 57 additions & 0 deletions
57
src/Type/Doctrine/DBAL/QueryBuilder/QueryBuilderExecuteMethodExtension.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,57 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Doctrine\DBAL\QueryBuilder; | ||
|
||
use Doctrine\DBAL\Driver\ResultStatement; | ||
use Doctrine\DBAL\Query\QueryBuilder; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Identifier; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\DynamicMethodReturnTypeExtension; | ||
use PHPStan\Type\ObjectType; | ||
use PHPStan\Type\Type; | ||
|
||
class QueryBuilderExecuteMethodExtension implements DynamicMethodReturnTypeExtension | ||
{ | ||
|
||
public function getClass(): string | ||
{ | ||
return QueryBuilder::class; | ||
} | ||
|
||
public function isMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'execute'; | ||
} | ||
|
||
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type | ||
{ | ||
$defaultReturnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType(); | ||
|
||
$queryBuilderType = new ObjectType(QueryBuilder::class); | ||
$var = $methodCall->var; | ||
while ($var instanceof MethodCall) { | ||
$varType = $scope->getType($var->var); | ||
if (!$queryBuilderType->isSuperTypeOf($varType)->yes()) { | ||
return $defaultReturnType; | ||
} | ||
|
||
$nameObject = $var->name; | ||
if (!($nameObject instanceof Identifier)) { | ||
return $defaultReturnType; | ||
} | ||
|
||
$name = $nameObject->toString(); | ||
if ($name === 'select' || $name === 'addSelect') { | ||
return new ObjectType(ResultStatement::class); | ||
} | ||
|
||
$var = $var->var; | ||
} | ||
|
||
return $defaultReturnType; | ||
} | ||
|
||
} |
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
22 changes: 22 additions & 0 deletions
22
tests/DoctrineIntegration/ORM/data/dbalQueryBuilderExecuteDynamicReturn-7.json
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,22 @@ | ||
[ | ||
{ | ||
"message": "Cannot call method fetchAll() on Doctrine\\DBAL\\Driver\\ResultStatement|int.", | ||
"line": 43, | ||
"ignorable": true | ||
}, | ||
{ | ||
"message": "Cannot call method fetchAll() on Doctrine\\DBAL\\Driver\\ResultStatement|int.", | ||
"line": 60, | ||
"ignorable": true | ||
}, | ||
{ | ||
"message": "Cannot call method fetchAll() on Doctrine\\DBAL\\Driver\\ResultStatement|int.", | ||
"line": 76, | ||
"ignorable": true | ||
}, | ||
{ | ||
"message": "Cannot call method fetchAll() on Doctrine\\DBAL\\Driver\\ResultStatement|int.", | ||
"line": 87, | ||
"ignorable": true | ||
} | ||
] |
89 changes: 89 additions & 0 deletions
89
tests/DoctrineIntegration/ORM/data/dbalQueryBuilderExecuteDynamicReturn.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,89 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PHPStan\DoctrineIntegration\ORM\DbalQueryBuilderExecuteDynamicReturn; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Query\QueryBuilder; | ||
|
||
class Example | ||
{ | ||
/** @var Connection */ | ||
private $connection; | ||
|
||
public function __construct(Connection $connection) | ||
{ | ||
$this->connection = $connection; | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
public function testCaseOne(int $userId): array | ||
{ | ||
return $this->connection->createQueryBuilder() | ||
->select('*') | ||
->from('user') | ||
->where('user.id = :id') | ||
->setParameter('id', $userId) | ||
->execute() | ||
->fetchAll(); | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
public function testCaseTwo(int $userId): array | ||
{ | ||
$qb = $this->connection->createQueryBuilder(); | ||
$qb->select('*'); | ||
$qb->from('user'); | ||
$qb->where('user.id = :id'); | ||
$qb->setParameter('id', $userId); | ||
|
||
return $qb->execute()->fetchAll(); | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
public function testCaseThree(?int $userId = null): array | ||
{ | ||
$qb = $this->connection->createQueryBuilder(); | ||
$qb->select('*'); | ||
$qb->from('user'); | ||
|
||
if ($userId !== null) { | ||
$qb->where('user.id = :id'); | ||
$qb->setParameter('id', $userId); | ||
} | ||
|
||
return $qb->execute()->fetchAll(); | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
public function testCaseFourA(?int $userId = null): array | ||
{ | ||
$qb = $this->connection->createQueryBuilder(); | ||
$qb->select('*'); | ||
$qb->from('user'); | ||
|
||
if ($userId !== null) { | ||
return $this->testCaseFourB($qb, $userId); | ||
} | ||
|
||
return $qb->execute()->fetchAll(); | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
private function testCaseFourB(QueryBuilder $qb, int $userId): array | ||
{ | ||
$qb->where('user.id = :id'); | ||
$qb->setParameter('id', $userId); | ||
|
||
return $qb->execute()->fetchAll(); | ||
} | ||
} |