Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/Type/Php/SimpleXMLElementXpathMethodReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
Expand All @@ -31,11 +31,12 @@ public function isMethodSupported(MethodReflection $methodReflection): bool

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
{
if (!isset($methodCall->getArgs()[0])) {
$args = $methodCall->getArgs();
if (!isset($args[0])) {
return null;
}

$argType = $scope->getType($methodCall->getArgs()[0]->value);
$argType = $scope->getType($args[0]->value);

$xmlElement = new SimpleXMLElement('<foo />');

Expand All @@ -53,7 +54,9 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
return null;
}

return new ArrayType(new MixedType(), $scope->getType($methodCall->var));
$variant = ParametersAcceptorSelector::selectFromArgs($scope, $args, $methodReflection->getVariants());

return TypeCombinator::remove($variant->getReturnType(), new ConstantBooleanType(false));
}

}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3144,7 +3144,7 @@ public static function dataBinaryOperations(): array
'$simpleXMLWritingXML',
],
[
'array<SimpleXMLElement>',
'array<SimpleXMLElement>|null',
'$simpleXMLRightXpath',
],
[
Expand Down
3 changes: 1 addition & 2 deletions tests/PHPStan/Analyser/nsrt/bug-9714.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public function exampleFunction(): void
{
$xml = new \SimpleXmlElement('');
$elements = $xml->xpath('//data');
assertType('array<SimpleXmlElement>', $elements);
assertType('array<SimpleXmlElement>|null', $elements);
}
}

Loading