8
8
use PHPStan \Reflection \MethodReflection ;
9
9
use PHPStan \Reflection \ParametersAcceptorSelector ;
10
10
use PHPStan \ShouldNotHappenException ;
11
+ use PHPStan \Type \Accessory \AccessoryArrayListType ;
11
12
use PHPStan \Type \ArrayType ;
12
13
use PHPStan \Type \Constant \ConstantIntegerType ;
13
14
use PHPStan \Type \DynamicMethodReturnTypeExtension ;
@@ -71,10 +72,12 @@ public function getTypeFromMethodCall(
71
72
72
73
$ queryType = $ scope ->getType ($ methodCall ->var );
73
74
$ queryResultType = $ this ->getQueryResultType ($ queryType );
75
+ $ queryKeyType = $ this ->getQueryKeyType ($ queryType );
74
76
75
77
return $ this ->getMethodReturnTypeForHydrationMode (
76
78
$ methodReflection ,
77
79
$ hydrationMode ,
80
+ $ queryKeyType ,
78
81
$ queryResultType
79
82
);
80
83
}
@@ -93,9 +96,24 @@ private function getQueryResultType(Type $queryType): Type
93
96
return $ resultType ;
94
97
}
95
98
99
+ private function getQueryKeyType (Type $ queryType ): Type
100
+ {
101
+ if (!$ queryType instanceof TypeWithClassName) {
102
+ return new MixedType ();
103
+ }
104
+
105
+ $ resultType = GenericTypeVariableResolver::getType ($ queryType , AbstractQuery::class, 'TKey ' );
106
+ if ($ resultType === null ) {
107
+ return new MixedType ();
108
+ }
109
+
110
+ return $ resultType ;
111
+ }
112
+
96
113
private function getMethodReturnTypeForHydrationMode (
97
114
MethodReflection $ methodReflection ,
98
115
Type $ hydrationMode ,
116
+ Type $ queryKeyType ,
99
117
Type $ queryResultType
100
118
): Type
101
119
{
@@ -126,12 +144,18 @@ private function getMethodReturnTypeForHydrationMode(
126
144
return TypeCombinator::addNull ($ queryResultType );
127
145
case 'toIterable ' :
128
146
return new IterableType (
129
- new MixedType () ,
147
+ $ queryKeyType instanceof NullType ? new IntegerType () : $ queryKeyType ,
130
148
$ queryResultType
131
149
);
132
150
default :
151
+ if ($ queryKeyType instanceof NullType) {
152
+ return AccessoryArrayListType::intersectWith (new ArrayType (
153
+ new IntegerType (),
154
+ $ queryResultType
155
+ ));
156
+ }
133
157
return new ArrayType (
134
- new MixedType () ,
158
+ $ queryKeyType ,
135
159
$ queryResultType
136
160
);
137
161
}
0 commit comments