11
11
use PHPStan \PhpDoc \TypeNodeResolver ;
12
12
use PHPStan \Rules \IdentifierRuleError ;
13
13
use PHPStan \Rules \RuleErrorBuilder ;
14
- use PHPStan \TrinaryLogic ;
15
14
use PHPStan \Type \ArrayType ;
16
15
use PHPStan \Type \Generic \GenericObjectType ;
17
16
use PHPStan \Type \MixedType ;
@@ -141,15 +140,15 @@ private function shouldVarTagTypeBeReported(Node\Expr $expr, Type $type, Type $v
141
140
$ type = new ArrayType (new MixedType (), new MixedType ());
142
141
}
143
142
144
- return !$ this ->isSuperTypeOfVarType ($ type , $ varTagType, false );
143
+ return !$ this ->isAtLeastMaybeSuperTypeOfVarType ($ type , $ varTagType );
145
144
}
146
145
147
146
if ($ expr instanceof Expr \ConstFetch) {
148
- return !$ this ->isSuperTypeOfVarType ($ type , $ varTagType, false );
147
+ return !$ this ->isAtLeastMaybeSuperTypeOfVarType ($ type , $ varTagType );
149
148
}
150
149
151
150
if ($ expr instanceof Node \Scalar) {
152
- return !$ this ->isSuperTypeOfVarType ($ type , $ varTagType, false );
151
+ return !$ this ->isAtLeastMaybeSuperTypeOfVarType ($ type , $ varTagType );
153
152
}
154
153
155
154
if ($ expr instanceof Expr \New_) {
@@ -164,50 +163,58 @@ private function shouldVarTagTypeBeReported(Node\Expr $expr, Type $type, Type $v
164
163
private function checkType (Type $ type , Type $ varTagType , int $ depth = 0 ): bool
165
164
{
166
165
if ($ this ->strictWideningCheck ) {
167
- return !$ this ->isSuperTypeOfVarType ($ type , $ varTagType, true );
166
+ return !$ this ->isSuperTypeOfVarType ($ type , $ varTagType );
168
167
}
169
168
170
169
if ($ type ->isConstantArray ()->yes ()) {
171
170
if ($ type ->isIterableAtLeastOnce ()->no ()) {
172
171
$ type = new ArrayType (new MixedType (), new MixedType ());
173
- return !$ this ->isSuperTypeOfVarType ($ type , $ varTagType, false );
172
+ return !$ this ->isAtLeastMaybeSuperTypeOfVarType ($ type , $ varTagType );
174
173
}
175
174
}
176
175
177
176
if ($ type ->isIterable ()->yes () && $ varTagType ->isIterable ()->yes ()) {
178
- if (!$ this ->isSuperTypeOfVarType ($ type , $ varTagType, false )) {
177
+ if (!$ this ->isAtLeastMaybeSuperTypeOfVarType ($ type , $ varTagType )) {
179
178
return true ;
180
179
}
181
180
182
181
$ innerType = $ type ->getIterableValueType ();
183
182
$ innerVarTagType = $ varTagType ->getIterableValueType ();
184
183
185
184
if ($ type ->equals ($ innerType ) || $ varTagType ->equals ($ innerVarTagType )) {
186
- return !$ this ->isSuperTypeOfVarType ($ innerType , $ innerVarTagType, true );
185
+ return !$ this ->isSuperTypeOfVarType ($ innerType , $ innerVarTagType );
187
186
}
188
187
189
188
return $ this ->checkType ($ innerType , $ innerVarTagType , $ depth + 1 );
190
189
}
191
190
192
191
if ($ depth === 0 && $ type ->isConstantValue ()->yes ()) {
193
- return !$ this ->isSuperTypeOfVarType ($ type , $ varTagType, false );
192
+ return !$ this ->isAtLeastMaybeSuperTypeOfVarType ($ type , $ varTagType );
194
193
}
195
194
196
- return !$ this ->isSuperTypeOfVarType ($ type , $ varTagType, true );
195
+ return !$ this ->isSuperTypeOfVarType ($ type , $ varTagType );
197
196
}
198
197
199
- private function isSuperTypeOfVarType (Type $ type , Type $ varTagType, bool $ strict ): bool
198
+ private function isSuperTypeOfVarType (Type $ type , Type $ varTagType ): bool
200
199
{
201
- $ validationCallable = static fn (TrinaryLogic $ trinaryLogic ): bool => $ strict ? $ trinaryLogic ->yes () : !$ trinaryLogic ->no ();
200
+ if ($ type ->isSuperTypeOf ($ varTagType )->yes ()) {
201
+ return true ;
202
+ }
203
+
204
+ $ type = $ this ->typeNodeResolver ->resolve ($ type ->toPhpDocNode (), new NameScope (null , []));
202
205
203
- $ result = $ type ->isSuperTypeOf ($ varTagType );
204
- if ($ validationCallable ($ result )) {
206
+ return $ type ->isSuperTypeOf ($ varTagType )->yes ();
207
+ }
208
+
209
+ private function isAtLeastMaybeSuperTypeOfVarType (Type $ type , Type $ varTagType ): bool
210
+ {
211
+ if (!$ type ->isSuperTypeOf ($ varTagType )->no ()) {
205
212
return true ;
206
213
}
207
214
208
215
$ type = $ this ->typeNodeResolver ->resolve ($ type ->toPhpDocNode (), new NameScope (null , []));
209
216
210
- return $ validationCallable ( $ type ->isSuperTypeOf ($ varTagType ));
217
+ return ! $ type ->isSuperTypeOf ($ varTagType )-> no ( );
211
218
}
212
219
213
220
}
0 commit comments