1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Add support for iterating over SimpleXMLElement

This commit is contained in:
Brown 2019-08-23 14:15:20 -04:00
parent fef61e996e
commit 80d9b5dc29
2 changed files with 30 additions and 0 deletions

View File

@ -629,6 +629,28 @@ class ForeachAnalyzer
$has_valid_iterator = true;
if ($iterator_atomic_type instanceof Type\Atomic\TNamedObject
&& strtolower($iterator_atomic_type->value) === 'simplexmlelement'
) {
if ($value_type) {
$value_type = Type::combineUnionTypes(
$value_type,
new Type\Union([clone $iterator_atomic_type])
);
} else {
$value_type = new Type\Union([clone $iterator_atomic_type]);
}
if ($key_type) {
$key_type = Type::combineUnionTypes(
$key_type,
Type::getString()
);
} else {
$key_type = Type::getString();
}
}
if ($iterator_atomic_type instanceof Type\Atomic\TIterable
|| (strtolower($iterator_atomic_type->value) === 'traversable'
|| $codebase->classImplements(

View File

@ -961,6 +961,14 @@ class ForeachTest extends \Psalm\Tests\TestCase
}
}',
],
'simpleXmlIterator' => [
'<?php
function f(SimpleXMLElement $elt): void {
foreach ($elt as $item) {
f($item);
}
}'
],
];
}