diff --git a/src/Type/Traits/LateResolvableTypeTrait.php b/src/Type/Traits/LateResolvableTypeTrait.php index d35ee72461..4c8bfd4c53 100644 --- a/src/Type/Traits/LateResolvableTypeTrait.php +++ b/src/Type/Traits/LateResolvableTypeTrait.php @@ -18,6 +18,7 @@ use PHPStan\Type\LateResolvableType; use PHPStan\Type\NeverType; use PHPStan\Type\Type; +use PHPStan\Type\UnionType; trait LateResolvableTypeTrait { @@ -65,6 +66,18 @@ private function isSuperTypeOfDefault(Type $type): IsSuperTypeOfResult return IsSuperTypeOfResult::createYes(); } + if ($this->equals($type)) { + return IsSuperTypeOfResult::createYes(); + } + + if ($type instanceof UnionType) { + foreach ($type->getTypes() as $innerType) { + if ($this->equals($innerType)) { + return IsSuperTypeOfResult::createYes(); + } + } + } + if ($type instanceof LateResolvableType) { $type = $type->resolve(); } @@ -570,6 +583,18 @@ public function tryRemove(Type $typeToRemove): ?Type public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult { + if ($this->equals($otherType)) { + return IsSuperTypeOfResult::createYes(); + } + + if ($otherType instanceof UnionType) { + foreach ($otherType->getTypes() as $innerType) { + if ($this->equals($innerType)) { + return IsSuperTypeOfResult::createYes(); + } + } + } + $result = $this->resolve(); if ($result instanceof CompoundType) { diff --git a/tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php b/tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php index faa0a3080b..0af1b0e953 100644 --- a/tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php +++ b/tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php @@ -550,6 +550,13 @@ public function testBug10488(): void $this->analyse([__DIR__ . '/data/bug-10488.php'], []); } + public function testBug10942(): void + { + $this->reportMaybes = true; + $this->reportStatic = true; + $this->analyse([__DIR__ . '/data/bug-10942.php'], []); + } + #[RequiresPhp('>= 8.0')] public function testBug12073(): void { diff --git a/tests/PHPStan/Rules/Methods/data/bug-10942.php b/tests/PHPStan/Rules/Methods/data/bug-10942.php new file mode 100644 index 0000000000..d4a6d5824f --- /dev/null +++ b/tests/PHPStan/Rules/Methods/data/bug-10942.php @@ -0,0 +1,27 @@ + + */ + private static function provideCases(): array + { + return [ + 'conditional vs same conditional' => [ + self::createConditional(), + self::createConditional(), + TrinaryLogic::createYes(), + ], + 'conditional vs union containing it' => [ + self::createConditional(), + new UnionType([new StringType(), self::createConditional()]), + TrinaryLogic::createYes(), + ], + ]; + } + + #[DataProvider('dataIsSuperTypeOf')] + public function testIsSuperTypeOf(Type $left, Type $right, TrinaryLogic $expected): void + { + $actual = $left->isSuperTypeOf($right); + $this->assertSame($expected->describe(), $actual->describe()); + } + + #[DataProvider('dataIsSubTypeOf')] + public function testIsSubTypeOf(Type $left, Type $right, TrinaryLogic $expected): void + { + $actual = $left->isSubTypeOf($right); + $this->assertSame($expected->describe(), $actual->describe()); + } + +}