Skip to content

Commit

Permalink
return a BenovolentUnionType for properties in SimpleXMLElement and i…
Browse files Browse the repository at this point in the history
…mplement behaviour for calls on BenevolentUnionTypes by overriding UnionType::unionResults
  • Loading branch information
orklah authored and ondrejmirtes committed Feb 8, 2022
1 parent c8a3ab4 commit fcfcbd5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
Expand Up @@ -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
Expand All @@ -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()]));
}

}
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Classes/data/impossible-instanceof.php
Expand Up @@ -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;
}

}

0 comments on commit fcfcbd5

Please sign in to comment.