diff --git a/src/Rules/Comparison/ImpossibleCheckTypeHelper.php b/src/Rules/Comparison/ImpossibleCheckTypeHelper.php index 25c70705792..859a51dcebc 100644 --- a/src/Rules/Comparison/ImpossibleCheckTypeHelper.php +++ b/src/Rules/Comparison/ImpossibleCheckTypeHelper.php @@ -241,7 +241,10 @@ public function findSpecifiedType( && $genericType->hasMethod($methodType->getValue())->no()) { return false; } + + return null; } + } } } diff --git a/src/Type/Accessory/HasMethodType.php b/src/Type/Accessory/HasMethodType.php index 2201e0e5bef..e6c4b0ced3b 100644 --- a/src/Type/Accessory/HasMethodType.php +++ b/src/Type/Accessory/HasMethodType.php @@ -82,6 +82,13 @@ public function accepts(Type $type, bool $strictTypes): AcceptsResult public function isSuperTypeOf(Type $type): IsSuperTypeOfResult { + if ($type instanceof CompoundType) { + $result = $type->isSubTypeOf($this); + if (!$result->no()) { + return $result; + } + } + return new IsSuperTypeOfResult($type->hasMethod($this->methodName), []); } diff --git a/src/Type/Accessory/HasPropertyType.php b/src/Type/Accessory/HasPropertyType.php index 9c471e44cd7..78bea8b1c3f 100644 --- a/src/Type/Accessory/HasPropertyType.php +++ b/src/Type/Accessory/HasPropertyType.php @@ -79,6 +79,13 @@ public function accepts(Type $type, bool $strictTypes): AcceptsResult public function isSuperTypeOf(Type $type): IsSuperTypeOfResult { + if ($type instanceof CompoundType) { + $result = $type->isSubTypeOf($this); + if (!$result->no()) { + return $result; + } + } + return new IsSuperTypeOfResult( $type->hasInstanceProperty($this->propertyName)->or($type->hasStaticProperty($this->propertyName)), [], diff --git a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php index 2e50b4c511c..17b6d29c7b5 100644 --- a/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/ImpossibleCheckTypeFunctionCallRuleTest.php @@ -1224,4 +1224,10 @@ public function testBug12063(): void $this->analyse([__DIR__ . '/data/bug-12063.php'], []); } + public function testBug8217(): void + { + $this->treatPhpDocTypesAsCertain = true; + $this->analyse([__DIR__ . '/data/bug-8217.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Comparison/data/bug-8217.php b/tests/PHPStan/Rules/Comparison/data/bug-8217.php new file mode 100644 index 00000000000..7da3fe99468 --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/bug-8217.php @@ -0,0 +1,58 @@ +