Skip to content

Commit 4ff1cae

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

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

src/Type/Php/SimpleXMLElementXpathMethodReturnTypeExtension.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\DependencyInjection\AutowiredService;
88
use PHPStan\Reflection\MethodReflection;
9+
use PHPStan\Reflection\ParametersAcceptorSelector;
910
use PHPStan\Type\ArrayType;
11+
use PHPStan\Type\Constant\ConstantBooleanType;
1012
use PHPStan\Type\DynamicMethodReturnTypeExtension;
1113
use PHPStan\Type\MixedType;
1214
use PHPStan\Type\NeverType;
@@ -31,11 +33,12 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
3133

3234
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
3335
{
34-
if (!isset($methodCall->getArgs()[0])) {
36+
$args = $methodCall->getArgs();
37+
if (!isset($args[0])) {
3538
return null;
3639
}
3740

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

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

@@ -53,7 +56,9 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
5356
return null;
5457
}
5558

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

5964
}

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)