Skip to content

Commit 4be03a5

Browse files
authored
Remove custom isSuperTypeOf reasons from ObjectType
1 parent d057cd1 commit 4be03a5

22 files changed

Lines changed: 9 additions & 162 deletions

‎src/Type/ObjectType.php‎

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -599,14 +599,7 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
599599
}
600600

601601
if ($thisClassReflection->isTrait() || $thatClassReflection->isTrait()) {
602-
$traitReflection = $thisClassReflection->isTrait() ? $thisClassReflection : $thatClassReflection;
603-
// Deliberately not stored in self::$superTypes: the reason string is specific to this
604-
// class pair, so caching it would retain one distinct string per compared pair for the
605-
// whole process. On large codebases that adds up to hundreds of MB. The verdict is cheap
606-
// to recompute and the reason is only ever read when an error is emitted.
607-
return IsSuperTypeOfResult::createNo([
608-
sprintf('%s is a trait, which is never a value type.', $traitReflection->getDisplayName()),
609-
]);
602+
return self::$superTypes[$thisDescription][$description] = IsSuperTypeOfResult::createNo();
610603
}
611604

612605
if ($thisClassReflection->getName() === $thatClassReflection->getName()) {
@@ -629,40 +622,7 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
629622
return self::$superTypes[$thisDescription][$description] = IsSuperTypeOfResult::createMaybe();
630623
}
631624

632-
// Deliberately not stored in self::$superTypes — see the trait branch above; the per-pair
633-
// reason string would otherwise accumulate in the static cache for the whole process.
634-
return IsSuperTypeOfResult::createNo(
635-
$this->describeUnrelatedClassesReasons($thisClassReflection, $thatClassReflection),
636-
);
637-
}
638-
639-
/**
640-
* Explains why two distinct, non-trait class reflections that are not in an
641-
* inheritance relationship can never share an instance. Reached only from the
642-
* final `createNo()` of isSuperTypeOf(): one side being an interface implies
643-
* the other is a final class that does not implement it; otherwise both are
644-
* classes and PHP's single inheritance rules them out.
645-
*
646-
* @return list<string>
647-
*/
648-
private function describeUnrelatedClassesReasons(ClassReflection $a, ClassReflection $b): array
649-
{
650-
if ($a->isInterface() !== $b->isInterface()) {
651-
$interfaceReflection = $a->isInterface() ? $a : $b;
652-
$classReflection = $a->isInterface() ? $b : $a;
653-
654-
return [sprintf(
655-
'Final class %s does not implement interface %s.',
656-
$classReflection->getDisplayName(),
657-
$interfaceReflection->getDisplayName(),
658-
)];
659-
}
660-
661-
return [sprintf(
662-
'Classes %s and %s are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
663-
$a->getDisplayName(),
664-
$b->getDisplayName(),
665-
)];
625+
return self::$superTypes[$thisDescription][$description] = IsSuperTypeOfResult::createNo();
666626
}
667627

668628
public function equals(Type $type): bool

‎tests/PHPStan/Rules/Classes/ClassAttributesRuleTest.php‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ public function testBug7171(): void
152152
[
153153
'Parameter $repositoryClass of attribute class Bug7171\Entity constructor expects class-string<Bug7171\EntityRepository<T of object>>|null, \'stdClass\' given.',
154154
66,
155-
'Type #1 from the union: Classes Bug7171\EntityRepository<T of object> and stdClass are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
156155
],
157156
]);
158157
}

‎tests/PHPStan/Rules/Classes/ImpossibleInstanceOfRuleTest.php‎

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ public function testInstanceof(): void
7878
[
7979
'Instanceof between ImpossibleInstanceOf\Dolor and ImpossibleInstanceOf\Lorem will always evaluate to false.',
8080
71,
81-
'Classes ImpossibleInstanceOf\Lorem and ImpossibleInstanceOf\Dolor are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
8281
],
8382
[
8483
'Instanceof between ImpossibleInstanceOf\FooImpl and ImpossibleInstanceOf\Foo will always evaluate to true.',
@@ -103,7 +102,6 @@ public function testInstanceof(): void
103102
[
104103
'Instanceof between ImpossibleInstanceOf\Test|null and ImpossibleInstanceOf\Lorem will always evaluate to false.',
105104
119,
106-
'Classes ImpossibleInstanceOf\Lorem and ImpossibleInstanceOf\Test are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
107105
],
108106
[
109107
'Instanceof between ImpossibleInstanceOf\Test and ImpossibleInstanceOf\Test will always evaluate to true.',
@@ -112,7 +110,6 @@ public function testInstanceof(): void
112110
[
113111
'Instanceof between ImpossibleInstanceOf\Test|null and ImpossibleInstanceOf\Lorem will always evaluate to false.',
114112
137,
115-
'Classes ImpossibleInstanceOf\Lorem and ImpossibleInstanceOf\Test are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
116113
],
117114
[
118115
'Instanceof between ImpossibleInstanceOf\Test and ImpossibleInstanceOf\Test will always evaluate to true.',
@@ -121,7 +118,6 @@ public function testInstanceof(): void
121118
[
122119
'Instanceof between ImpossibleInstanceOf\Test|null and ImpossibleInstanceOf\Lorem will always evaluate to false.',
123120
155,
124-
'Classes ImpossibleInstanceOf\Lorem and ImpossibleInstanceOf\Test are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
125121
],
126122
[
127123
'Instanceof between ImpossibleInstanceOf\Test and ImpossibleInstanceOf\Test will always evaluate to true.',
@@ -228,7 +224,6 @@ public function testDoNotReportTypesFromPhpDocs(): void
228224
[
229225
'Instanceof between stdClass and Exception will always evaluate to false.',
230226
15,
231-
'Classes Exception and stdClass are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
232227
],
233228
[
234229
'Instanceof between DateTimeInterface and DateTimeInterface will always evaluate to true.',
@@ -237,7 +232,6 @@ public function testDoNotReportTypesFromPhpDocs(): void
237232
[
238233
'Instanceof between DateTimeInterface and ImpossibleInstanceofNotPhpDoc\SomeFinalClass will always evaluate to false.',
239234
30,
240-
'Final class ImpossibleInstanceofNotPhpDoc\SomeFinalClass does not implement interface DateTimeInterface.',
241235
],
242236
]);
243237
}
@@ -253,7 +247,6 @@ public function testReportTypesFromPhpDocs(): void
253247
[
254248
'Instanceof between stdClass and Exception will always evaluate to false.',
255249
15,
256-
'Classes Exception and stdClass are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
257250
],
258251
[
259252
'Instanceof between DateTimeImmutable and DateTimeInterface will always evaluate to true.',
@@ -262,7 +255,6 @@ public function testReportTypesFromPhpDocs(): void
262255
[
263256
'Instanceof between DateTimeImmutable and ImpossibleInstanceofNotPhpDoc\SomeFinalClass will always evaluate to false.',
264257
30,
265-
'Classes ImpossibleInstanceofNotPhpDoc\SomeFinalClass and DateTimeImmutable are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
266258
],
267259
[
268260
'Instanceof between DateTimeImmutable and DateTimeImmutable will always evaluate to true.',
@@ -272,7 +264,6 @@ public function testReportTypesFromPhpDocs(): void
272264
[
273265
'Instanceof between DateTimeImmutable and DateTime will always evaluate to false.',
274266
36,
275-
'Classes DateTime and DateTimeImmutable are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
276267
//'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
277268
],
278269
]);
@@ -578,37 +569,30 @@ public function testNewIsAlwaysFinalClass(): void
578569
[
579570
'Instanceof between ImpossibleInstanceofNewIsAlwaysFinal\Bar and ImpossibleInstanceofNewIsAlwaysFinal\Foo will always evaluate to false.',
580571
17,
581-
'Final class ImpossibleInstanceofNewIsAlwaysFinal\Bar does not implement interface ImpossibleInstanceofNewIsAlwaysFinal\Foo.',
582572
],
583573
[
584574
'Instanceof between ImpossibleInstanceofNewIsAlwaysFinal\Bar and ImpossibleInstanceofNewIsAlwaysFinal\Foo will always evaluate to false.',
585575
33,
586-
'Final class ImpossibleInstanceofNewIsAlwaysFinal\Bar does not implement interface ImpossibleInstanceofNewIsAlwaysFinal\Foo.',
587576
],
588577
[
589578
'Instanceof between ImpossibleInstanceofNewIsAlwaysFinal\Bar and ImpossibleInstanceofNewIsAlwaysFinal\Foo will always evaluate to false.',
590579
43,
591-
'Final class ImpossibleInstanceofNewIsAlwaysFinal\Bar does not implement interface ImpossibleInstanceofNewIsAlwaysFinal\Foo.',
592580
],
593581
[
594582
'Instanceof between ImpossibleInstanceofNewIsAlwaysFinal\Bar and ImpossibleInstanceofNewIsAlwaysFinal\Foo will always evaluate to false.',
595583
53,
596-
'Final class ImpossibleInstanceofNewIsAlwaysFinal\Bar does not implement interface ImpossibleInstanceofNewIsAlwaysFinal\Foo.',
597584
],
598585
[
599586
'Instanceof between ImpossibleInstanceofNewIsAlwaysFinal\Bar and ImpossibleInstanceofNewIsAlwaysFinal\Foo will always evaluate to false.',
600587
63,
601-
'Final class ImpossibleInstanceofNewIsAlwaysFinal\Bar does not implement interface ImpossibleInstanceofNewIsAlwaysFinal\Foo.',
602588
],
603589
[
604590
'Instanceof between ImpossibleInstanceofNewIsAlwaysFinal\Bar|null and ImpossibleInstanceofNewIsAlwaysFinal\Foo will always evaluate to false.',
605591
73,
606-
'Final class ImpossibleInstanceofNewIsAlwaysFinal\Bar does not implement interface ImpossibleInstanceofNewIsAlwaysFinal\Foo.',
607592
],
608593
[
609594
'Instanceof between ImpossibleInstanceofNewIsAlwaysFinal\Bar|null and ImpossibleInstanceofNewIsAlwaysFinal\Baz will always evaluate to false.',
610595
88,
611-
'Classes ImpossibleInstanceofNewIsAlwaysFinal\Baz and ImpossibleInstanceofNewIsAlwaysFinal\Bar are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
612596
],
613597
]);
614598
}
@@ -620,7 +604,6 @@ public function testBug13469(): void
620604
[
621605
sprintf('Instanceof between Bug13469\Foo and Stringable will always evaluate to %s.', PHP_VERSION_ID >= 80000 ? 'true' : 'false'),
622606
23,
623-
PHP_VERSION_ID < 80000 ? 'Final class Bug13469\Foo does not implement interface Stringable.' : null,
624607
],
625608
]);
626609
}
@@ -661,16 +644,17 @@ public function testPossiblyImpureTip(): void
661644
public function testInTrait(): void
662645
{
663646
$this->treatPhpDocTypesAsCertain = true;
647+
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
664648
$this->analyse([__DIR__ . '/data/impossible-instanceof-in-trait.php'], [
665649
[
666650
'Instanceof between ImpossibleInstanceofInTrait\Cat and stdClass will always evaluate to false.',
667651
25,
668-
'Classes stdClass and ImpossibleInstanceofInTrait\Cat are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
652+
$tipText,
669653
],
670654
[
671655
'Instanceof between ImpossibleInstanceofInTrait\Dog and stdClass will always evaluate to false.',
672656
25,
673-
'Classes stdClass and ImpossibleInstanceofInTrait\Dog are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
657+
$tipText,
674658
],
675659
]);
676660
}

‎tests/PHPStan/Rules/Classes/MixinRuleTest.php‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public function testRule(): void
5252
[
5353
'PHPDoc tag @mixin contains unresolvable type.',
5454
24,
55-
'Classes Exception and SplFileInfo are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
5655
],
5756
[
5857
'PHPDoc tag @mixin contains generic type Exception<MixinRule\Foo> but class Exception is not generic.',

‎tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -294,23 +294,6 @@ public function testBug7898(): void
294294
$this->analyse([__DIR__ . '/data/bug-7898.php'], []);
295295
}
296296

297-
public function testReasons(): void
298-
{
299-
$this->treatPhpDocTypesAsCertain = true;
300-
$this->analyse([__DIR__ . '/data/check-type-function-call-reasons.php'], [
301-
[
302-
'Call to function is_a() with CheckTypeFunctionCallReasons\Foo and \'CheckTypeFunctionCallReasons\\\\Bar\' will always evaluate to false.',
303-
23,
304-
'Final class CheckTypeFunctionCallReasons\Foo does not implement interface CheckTypeFunctionCallReasons\Bar.',
305-
],
306-
[
307-
'Call to function is_a() with CheckTypeFunctionCallReasons\Baz and \'CheckTypeFunctionCallReasons\\\\Foo\' will always evaluate to false.',
308-
28,
309-
'Classes CheckTypeFunctionCallReasons\Foo and CheckTypeFunctionCallReasons\Baz are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
310-
],
311-
]);
312-
}
313-
314297
#[RequiresPhp('>= 8.1.0')]
315298
public function testStructExists(): void
316299
{

‎tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ public function testBug3633(): void
581581
[
582582
'Strict comparison using === between class-string<$this(Bug3633\HelloWorld)> and \'Bug3633\\\OtherClass\' will always evaluate to false.',
583583
37,
584-
'Classes Bug3633\HelloWorld and Bug3633\OtherClass are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
584+
$tipText,
585585
],
586586
[
587587
'Strict comparison using === between \'Bug3633\\\HelloWorld\' and \'Bug3633\\\HelloWorld\' will always evaluate to true.',
@@ -595,7 +595,7 @@ public function testBug3633(): void
595595
[
596596
'Strict comparison using === between class-string<$this(Bug3633\OtherClass)> and \'Bug3633\\\HelloWorld\' will always evaluate to false.',
597597
64,
598-
'Classes Bug3633\OtherClass and Bug3633\HelloWorld are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
598+
$tipText,
599599
],
600600
[
601601
'Strict comparison using === between \'Bug3633\\\OtherClass\' and \'Bug3633\\\HelloWorld\' will always evaluate to false.',
@@ -610,12 +610,12 @@ public function testBug3633(): void
610610
[
611611
'Strict comparison using === between class-string<$this(Bug3633\FinalClass)> and \'Bug3633\\\HelloWorld\' will always evaluate to false.',
612612
93,
613-
'Classes Bug3633\FinalClass and Bug3633\HelloWorld are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
613+
$tipText,
614614
],
615615
[
616616
'Strict comparison using === between class-string<$this(Bug3633\FinalClass)> and \'Bug3633\\\OtherClass\' will always evaluate to false.',
617617
96,
618-
'Classes Bug3633\FinalClass and Bug3633\OtherClass are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
618+
$tipText,
619619
],
620620
[
621621
'Strict comparison using === between \'Bug3633\\\FinalClass\' and \'Bug3633\\\FinalClass\' will always evaluate to true.',

‎tests/PHPStan/Rules/Comparison/data/check-type-function-call-reasons.php‎

Lines changed: 0 additions & 31 deletions
This file was deleted.

‎tests/PHPStan/Rules/Functions/ExistingClassesInArrowFunctionTypehintsRuleTest.php‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,10 @@ public static function dataIntersectionTypes(): array
262262
[
263263
'Parameter $a of anonymous function has unresolvable native type.',
264264
27,
265-
'Classes ArrowFunctionIntersectionTypes\Ipsum and ArrowFunctionIntersectionTypes\Lorem are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
266265
],
267266
[
268267
'Anonymous function has unresolvable native return type.',
269268
27,
270-
'Classes ArrowFunctionIntersectionTypes\Ipsum and ArrowFunctionIntersectionTypes\Lorem are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
271269
],
272270
[
273271
'Parameter $a of anonymous function has unresolvable native type.',

‎tests/PHPStan/Rules/Functions/ExistingClassesInClosureTypehintsRuleTest.php‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,10 @@ public static function dataIntersectionTypes(): array
321321
[
322322
'Parameter $a of anonymous function has unresolvable native type.',
323323
30,
324-
'Classes ClosureIntersectionTypes\Ipsum and ClosureIntersectionTypes\Lorem are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
325324
],
326325
[
327326
'Anonymous function has unresolvable native return type.',
328327
30,
329-
'Classes ClosureIntersectionTypes\Ipsum and ClosureIntersectionTypes\Lorem are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
330328
],
331329
[
332330
'Parameter $a of anonymous function has unresolvable native type.',

‎tests/PHPStan/Rules/Functions/ExistingClassesInTypehintsRuleTest.php‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,10 @@ public static function dataIntersectionTypes(): array
391391
[
392392
'Parameter $a of function FunctionIntersectionTypes\doBar() has unresolvable native type.',
393393
30,
394-
'Classes FunctionIntersectionTypes\Ipsum and FunctionIntersectionTypes\Lorem are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
395394
],
396395
[
397396
'Function FunctionIntersectionTypes\doBar() has unresolvable native return type.',
398397
30,
399-
'Classes FunctionIntersectionTypes\Ipsum and FunctionIntersectionTypes\Lorem are not in an inheritance relationship and because of single inheritance no object can be an instance of both.',
400398
],
401399
[
402400
'Parameter $a of function FunctionIntersectionTypes\doBaz() has unresolvable native type.',

0 commit comments

Comments
 (0)