Skip to content

Commit 64f507f

Browse files
committed
Move getQuery method return type to own extension
1 parent 6150d44 commit 64f507f

File tree

3 files changed

+64
-26
lines changed

3 files changed

+64
-26
lines changed

extension.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ services:
297297
-
298298
class: mglaman\PHPStanDrupal\Type\EntityStorage\EntityStorageDynamicReturnTypeExtension
299299
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
300+
-
301+
class: mglaman\PHPStanDrupal\Type\EntityStorage\GetQueryReturnTypeExtension
302+
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]
300303
-
301304
class: mglaman\PHPStanDrupal\Type\ContainerDynamicReturnTypeExtension
302305
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

src/Type/EntityStorage/EntityStorageDynamicReturnTypeExtension.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@
44

55
use Drupal\Core\Entity\EntityStorageInterface;
66
use mglaman\PHPStanDrupal\Drupal\EntityDataRepository;
7-
use mglaman\PHPStanDrupal\Type\EntityQuery\ConfigEntityQueryType;
8-
use mglaman\PHPStanDrupal\Type\EntityQuery\ContentEntityQueryType;
97
use PhpParser\Node\Expr\MethodCall;
108
use PHPStan\Analyser\Scope;
119
use PHPStan\Reflection\MethodReflection;
1210
use PHPStan\Reflection\ParametersAcceptorSelector;
13-
use PHPStan\ShouldNotHappenException;
1411
use PHPStan\Type\ArrayType;
1512
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1613
use PHPStan\Type\IntegerType;
17-
use PHPStan\Type\ObjectType;
1814
use PHPStan\Type\StringType;
1915
use PHPStan\Type\TypeCombinator;
2016

@@ -46,7 +42,6 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
4642
'loadMultiple',
4743
'loadByProperties',
4844
'loadUnchanged',
49-
'getQuery',
5045
],
5146
true
5247
);
@@ -78,27 +73,6 @@ public function getTypeFromMethodCall(
7873

7974
return new ArrayType(new IntegerType(), $type);
8075
}
81-
if ($methodReflection->getName() === 'getQuery') {
82-
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
83-
if (!$returnType instanceof ObjectType) {
84-
return $returnType;
85-
}
86-
if ($callerType instanceof ContentEntityStorageType) {
87-
return new ContentEntityQueryType(
88-
$returnType->getClassName(),
89-
$returnType->getSubtractedType(),
90-
$returnType->getClassReflection()
91-
);
92-
}
93-
if ($callerType instanceof ConfigEntityStorageType) {
94-
return new ConfigEntityQueryType(
95-
$returnType->getClassName(),
96-
$returnType->getSubtractedType(),
97-
$returnType->getClassReflection()
98-
);
99-
}
100-
return $returnType;
101-
}
10276

10377
return $type;
10478
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace mglaman\PHPStanDrupal\Type\EntityStorage;
4+
5+
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
6+
use Drupal\Core\Entity\ContentEntityStorageInterface;
7+
use Drupal\Core\Entity\EntityStorageInterface;
8+
use mglaman\PHPStanDrupal\Type\EntityQuery\ConfigEntityQueryType;
9+
use mglaman\PHPStanDrupal\Type\EntityQuery\ContentEntityQueryType;
10+
use PhpParser\Node\Expr\MethodCall;
11+
use PHPStan\Analyser\Scope;
12+
use PHPStan\Reflection\MethodReflection;
13+
use PHPStan\Reflection\ParametersAcceptorSelector;
14+
use PHPStan\Type\DynamicMethodReturnTypeExtension;
15+
use PHPStan\Type\ObjectType;
16+
17+
final class GetQueryReturnTypeExtension implements DynamicMethodReturnTypeExtension
18+
{
19+
20+
public function getClass(): string
21+
{
22+
return EntityStorageInterface::class;
23+
}
24+
25+
public function isMethodSupported(MethodReflection $methodReflection): bool
26+
{
27+
return $methodReflection->getName() === 'getQuery';
28+
}
29+
30+
public function getTypeFromMethodCall(
31+
MethodReflection $methodReflection,
32+
MethodCall $methodCall,
33+
Scope $scope
34+
): \PHPStan\Type\Type {
35+
$returnType = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
36+
if (!$returnType instanceof ObjectType) {
37+
return $returnType;
38+
}
39+
40+
$callerType = $scope->getType($methodCall->var);
41+
if (!$callerType instanceof ObjectType) {
42+
return $returnType;
43+
}
44+
45+
if ((new ObjectType(ContentEntityStorageInterface::class))->isSuperTypeOf($callerType)->yes()) {
46+
return new ContentEntityQueryType(
47+
$returnType->getClassName(),
48+
$returnType->getSubtractedType(),
49+
$returnType->getClassReflection()
50+
);
51+
}
52+
if ((new ObjectType(ConfigEntityStorageInterface::class))->isSuperTypeOf($callerType)->yes()) {
53+
return new ConfigEntityQueryType(
54+
$returnType->getClassName(),
55+
$returnType->getSubtractedType(),
56+
$returnType->getClassReflection()
57+
);
58+
}
59+
return $returnType;
60+
}
61+
}

0 commit comments

Comments
 (0)