16
16
use PHPStan \Type \IntegerType ;
17
17
use PHPStan \Type \ObjectType ;
18
18
use PHPStan \Type \StringType ;
19
+ use PHPStan \Type \Type ;
19
20
use PHPStan \Type \TypeCombinator ;
20
21
21
22
class EntityStorageDynamicReturnTypeExtension implements DynamicMethodReturnTypeExtension
@@ -60,6 +61,18 @@ public function getTypeFromMethodCall(
60
61
$ callerType = $ scope ->getType ($ methodCall ->var );
61
62
62
63
if (!$ callerType instanceof EntityStorageType) {
64
+ if (!$ callerType instanceof ObjectType) {
65
+ return ParametersAcceptorSelector::selectSingle ($ methodReflection ->getVariants ())->getReturnType ();
66
+ }
67
+
68
+ // Workaround because cannot figure out why the caller type is not an EntityStorageType
69
+ // when it has been type hinted.
70
+ // Instead, we try to infer the type, i.e. ContentEntityStorageType or ConfigEntityStorageType, here.
71
+ // @todo: we should definitively look for other cases that getQuery.
72
+ if ($ methodReflection ->getName () === 'getQuery ' ) {
73
+ return $ this ->getReturnTypeForGetQueryMethod ($ methodReflection , EntityStorageHelper::getTypeFromStorageObjectType ($ callerType ));
74
+ }
75
+
63
76
return ParametersAcceptorSelector::selectSingle ($ methodReflection ->getVariants ())->getReturnType ();
64
77
}
65
78
@@ -79,27 +92,32 @@ public function getTypeFromMethodCall(
79
92
return new ArrayType (new IntegerType (), $ type );
80
93
}
81
94
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 ;
95
+ return $ this ->getReturnTypeForGetQueryMethod ($ methodReflection , $ callerType );
101
96
}
102
97
103
98
return $ type ;
104
99
}
100
+
101
+ private function getReturnTypeForGetQueryMethod (MethodReflection $ methodReflection , ?Type $ entityStorageType ): Type
102
+ {
103
+ $ returnType = ParametersAcceptorSelector::selectSingle ($ methodReflection ->getVariants ())->getReturnType ();
104
+ if (!$ returnType instanceof ObjectType) {
105
+ return $ returnType ;
106
+ }
107
+ if ($ entityStorageType instanceof ContentEntityStorageType) {
108
+ return new ContentEntityQueryType (
109
+ $ returnType ->getClassName (),
110
+ $ returnType ->getSubtractedType (),
111
+ $ returnType ->getClassReflection ()
112
+ );
113
+ }
114
+ if ($ entityStorageType instanceof ConfigEntityStorageType) {
115
+ return new ConfigEntityQueryType (
116
+ $ returnType ->getClassName (),
117
+ $ returnType ->getSubtractedType (),
118
+ $ returnType ->getClassReflection ()
119
+ );
120
+ }
121
+ return $ returnType ;
122
+ }
105
123
}
0 commit comments