Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properties in SimpleXMLElement may be null #76

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Type/BenevolentUnionType.php
Expand Up @@ -42,4 +42,13 @@ public function isAcceptedBy(Type $acceptingType, bool $strictTypes): TrinaryLog
return TrinaryLogic::createNo();
}

/**
* @param callable(Type $type): TrinaryLogic $getResult
* @return TrinaryLogic
*/
protected function unionResults(callable $getResult): TrinaryLogic
{
return TrinaryLogic::maxMin(...array_map($getResult, $this->getTypes()));
Copy link
Contributor Author

@orklah orklah Jan 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not certain maxMin was the best method to use here, but I wasn't sure what to do with the Maybe case...

}

}
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('SimpleXMLElement'));
return new SimpleXMLElementProperty($classReflection, new BenevolentUnionType([new ObjectType('SimpleXMLElement'), new NullType()]));
}

}
2 changes: 1 addition & 1 deletion src/Type/UnionType.php
Expand Up @@ -620,7 +620,7 @@ public static function __set_state(array $properties): Type
* @param callable(Type $type): TrinaryLogic $getResult
* @return TrinaryLogic
*/
private function unionResults(callable $getResult): TrinaryLogic
protected function unionResults(callable $getResult): TrinaryLogic
{
return TrinaryLogic::extremeIdentity(...array_map($getResult, $this->types));
}
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Classes/data/impossible-instanceof.php
Expand Up @@ -375,3 +375,13 @@ public function doTest($int, $objectWithoutClass, $object, $intOrObject, $string
}
}
}

class InstanceofBenevolentUnionType
{
public function doFoo(\SimpleXMLElement $xml)
{
echo $xml->branch1 instanceof \SimpleXMLElement;
echo $xml->branch2->branch3 instanceof \SimpleXMLElement;
}

}