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:
parent
fef61e996e
commit
80d9b5dc29
@ -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(
|
||||
|
@ -961,6 +961,14 @@ class ForeachTest extends \Psalm\Tests\TestCase
|
||||
}
|
||||
}',
|
||||
],
|
||||
'simpleXmlIterator' => [
|
||||
'<?php
|
||||
function f(SimpleXMLElement $elt): void {
|
||||
foreach ($elt as $item) {
|
||||
f($item);
|
||||
}
|
||||
}'
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user