diff --git a/src/Type/Php/SimpleXMLElementClassPropertyReflectionExtension.php b/src/Type/Php/SimpleXMLElementClassPropertyReflectionExtension.php index 9646898c00..503e998fd3 100644 --- a/src/Type/Php/SimpleXMLElementClassPropertyReflectionExtension.php +++ b/src/Type/Php/SimpleXMLElementClassPropertyReflectionExtension.php @@ -6,6 +6,8 @@ use PHPStan\Reflection\Php\SimpleXMLElementProperty; use PHPStan\Reflection\PropertiesClassReflectionExtension; use PHPStan\Reflection\PropertyReflection; +use PHPStan\Type\BenevolentUnionType; +use PHPStan\Type\NullType; use PHPStan\Type\ObjectType; class SimpleXMLElementClassPropertyReflectionExtension implements PropertiesClassReflectionExtension @@ -19,7 +21,7 @@ public function hasProperty(ClassReflection $classReflection, string $propertyNa public function getProperty(ClassReflection $classReflection, string $propertyName): PropertyReflection { - return new SimpleXMLElementProperty($classReflection, new ObjectType($classReflection->getName())); + return new SimpleXMLElementProperty($classReflection, new BenevolentUnionType([new ObjectType($classReflection->getName()), new NullType()])); } } diff --git a/tests/PHPStan/Rules/Classes/data/impossible-instanceof.php b/tests/PHPStan/Rules/Classes/data/impossible-instanceof.php index 106781de88..a11d9a1c4e 100644 --- a/tests/PHPStan/Rules/Classes/data/impossible-instanceof.php +++ b/tests/PHPStan/Rules/Classes/data/impossible-instanceof.php @@ -441,3 +441,13 @@ function test(\DateTimeInterface $a, \DateTimeInterface $b): void { if ($b instanceof $a) return; } } + +class InstanceofBenevolentUnionType +{ + public function doFoo(\SimpleXMLElement $xml) + { + echo $xml->branch1 instanceof \SimpleXMLElement; + echo $xml->branch2->branch3 instanceof \SimpleXMLElement; + } + +}