Skip to content

Commit 9e2825c

Browse files
staabmclxmstaab
andauthored
simplify doctrine query extension (#190)
Co-authored-by: Markus Staab <[email protected]>
1 parent 9bcdb59 commit 9e2825c

File tree

2 files changed

+6
-16
lines changed

2 files changed

+6
-16
lines changed

config/extensions.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
-
3-
class: staabm\PHPStanDba\Extensions\DoctrineConnectionDynamicReturnTypeExtension
3+
class: staabm\PHPStanDba\Extensions\DoctrineConnectionQueryDynamicReturnTypeExtension
44
tags:
55
- phpstan.broker.dynamicMethodReturnTypeExtension
66

src/Extensions/DoctrineConnectionDynamicReturnTypeExtension.php renamed to src/Extensions/DoctrineConnectionQueryDynamicReturnTypeExtension.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Composer\Semver\VersionParser;
99
use Doctrine\DBAL\Connection;
1010
use Doctrine\DBAL\Result;
11-
use Doctrine\DBAL\Statement;
1211
use PhpParser\Node\Expr;
1312
use PhpParser\Node\Expr\MethodCall;
1413
use PHPStan\Analyser\Scope;
@@ -21,24 +20,16 @@
2120
use staabm\PHPStanDba\QueryReflection\QueryReflection;
2221
use staabm\PHPStanDba\QueryReflection\QueryReflector;
2322

24-
final class DoctrineConnectionDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
23+
final class DoctrineConnectionQueryDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
2524
{
26-
/**
27-
* @var array<string, class-string> return types of Connection methods in doctrine 3.x
28-
*/
29-
private $resultMap = [
30-
'query' => Result::class,
31-
//'prepare' => Statement::class,
32-
];
33-
3425
public function getClass(): string
3526
{
3627
return Connection::class;
3728
}
3829

3930
public function isMethodSupported(MethodReflection $methodReflection): bool
4031
{
41-
return \in_array(strtolower($methodReflection->getName()), ['query'/*, 'prepare'*/], true);
32+
return \in_array(strtolower($methodReflection->getName()), ['query'], true);
4233
}
4334

4435
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
@@ -63,26 +54,25 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
6354
return $defaultReturn;
6455
}
6556

66-
$resultType = $this->inferType($methodReflection, $args[0]->value, $scope);
57+
$resultType = $this->inferType($args[0]->value, $scope);
6758
if (null !== $resultType) {
6859
return $resultType;
6960
}
7061

7162
return $defaultReturn;
7263
}
7364

74-
private function inferType(MethodReflection $methodReflection, Expr $queryExpr, Scope $scope): ?Type
65+
private function inferType(Expr $queryExpr, Scope $scope): ?Type
7566
{
7667
$queryReflection = new QueryReflection();
7768
$queryString = $queryReflection->resolveQueryString($queryExpr, $scope);
7869
if (null === $queryString) {
7970
return null;
8071
}
8172

82-
$genericMainType = $this->resultMap[strtolower($methodReflection->getName())];
8373
$resultType = $queryReflection->getResultType($queryString, QueryReflector::FETCH_TYPE_BOTH);
8474
if ($resultType) {
85-
return new GenericObjectType($genericMainType, [$resultType]);
75+
return new GenericObjectType(Result::class, [$resultType]);
8676
}
8777

8878
return null;

0 commit comments

Comments
 (0)