Skip to content

Commit 4d597bd

Browse files
Fix SimpleXMLElement::xpath return type
1 parent 5ab9acc commit 4d597bd

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

src/Type/Php/SimpleXMLElementXpathMethodReturnTypeExtension.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\DependencyInjection\AutowiredService;
88
use PHPStan\Reflection\MethodReflection;
9-
use PHPStan\Type\ArrayType;
9+
use PHPStan\Reflection\ParametersAcceptorSelector;
10+
use PHPStan\Type\Constant\ConstantBooleanType;
1011
use PHPStan\Type\DynamicMethodReturnTypeExtension;
11-
use PHPStan\Type\MixedType;
1212
use PHPStan\Type\NeverType;
1313
use PHPStan\Type\Type;
1414
use PHPStan\Type\TypeCombinator;
@@ -31,11 +31,12 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
3131

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

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

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

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

56-
return new ArrayType(new MixedType(), $scope->getType($methodCall->var));
57+
$variant = ParametersAcceptorSelector::selectFromArgs($scope, $args, $methodReflection->getVariants());
58+
59+
return TypeCombinator::remove($variant->getReturnType(), new ConstantBooleanType(false));
5760
}
5861

5962
}

tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3144,7 +3144,7 @@ public static function dataBinaryOperations(): array
31443144
'$simpleXMLWritingXML',
31453145
],
31463146
[
3147-
'array<SimpleXMLElement>',
3147+
'array<SimpleXMLElement>|null',
31483148
'$simpleXMLRightXpath',
31493149
],
31503150
[

tests/PHPStan/Analyser/nsrt/bug-9714.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public function exampleFunction(): void
1010
{
1111
$xml = new \SimpleXmlElement('');
1212
$elements = $xml->xpath('//data');
13-
assertType('array<SimpleXmlElement>', $elements);
13+
assertType('array<SimpleXmlElement>|null', $elements);
1414
}
1515
}
16-

0 commit comments

Comments
 (0)