Skip to content

Commit 4f7ac53

Browse files
VincentLangletondrejmirtes
authored andcommitted
Avoid false positif for namespaced Xpath
1 parent b7d6080 commit 4f7ac53

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

src/Type/Php/SimpleXMLElementXpathMethodReturnTypeExtension.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPStan\Reflection\MethodReflection;
88
use PHPStan\Reflection\ParametersAcceptorSelector;
99
use PHPStan\Type\ArrayType;
10-
use PHPStan\Type\Constant\ConstantBooleanType;
1110
use PHPStan\Type\MixedType;
1211
use PHPStan\Type\NeverType;
1312
use PHPStan\Type\Type;
@@ -37,26 +36,20 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
3736

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

40-
$result = null;
4139
foreach (TypeUtils::getConstantStrings($argType) as $constantString) {
42-
$newResult = @$xmlElement->xpath($constantString->getValue());
43-
44-
if ($result !== null && gettype($result) !== gettype($newResult)) {
40+
$result = @$xmlElement->xpath($constantString->getValue());
41+
if ($result === false) {
42+
// We can't be sure since it's maybe a namespaced xpath
4543
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
4644
}
4745

48-
$result = $newResult;
4946
$argType = TypeCombinator::remove($argType, $constantString);
5047
}
5148

52-
if ($result === null || !$argType instanceof NeverType) {
49+
if (!$argType instanceof NeverType) {
5350
return ParametersAcceptorSelector::selectSingle($methodReflection->getVariants())->getReturnType();
5451
}
5552

56-
if ($result === false) {
57-
return new ConstantBooleanType(false);
58-
}
59-
6053
return new ArrayType(new MixedType(), $scope->getType($methodCall->var));
6154
}
6255

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3231,13 +3231,17 @@ public function dataBinaryOperations(): array
32313231
'$simpleXMLRightXpath',
32323232
],
32333233
[
3234-
'false',
3234+
'array<SimpleXMLElement>|false',
32353235
'$simpleXMLWrongXpath',
32363236
],
32373237
[
32383238
'array<SimpleXMLElement>|false',
32393239
'$simpleXMLUnknownXpath',
32403240
],
3241+
[
3242+
'array<SimpleXMLElement>|false',
3243+
'$namespacedXpath',
3244+
],
32413245
];
32423246
}
32433247

tests/PHPStan/Analyser/data/binary.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ public function doFoo(array $generalArray)
177177
$simpleXMLWrongXpath = $simpleXML->xpath('[foo]');
178178
$simpleXMLUnknownXpath = $simpleXML->xpath($stringForXpath);
179179

180+
$namespacedXML = new \SimpleXMLElement('<a><b><c/></b></a>');
181+
$namespacedXML->registerXPathNamespace('ns', 'namespace');
182+
$namespacedXpath = $namespacedXML->xpath('/ns:node');
183+
180184
if (rand(0, 1)) {
181185
$maybeDefinedVariable = 'foo';
182186
}

0 commit comments

Comments
 (0)