Skip to content
Permalink
Browse files

Add support for iterating over SimpleXMLElement

  • Loading branch information...
muglug committed Aug 23, 2019
1 parent fef61e9 commit 80d9b5dc293b1398acfe4ed3fb07d6dee9c0ea9a
Showing with 30 additions and 0 deletions.
  1. +22 −0 src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php
  2. +8 −0 tests/Loop/ForeachTest.php
@@ -629,6 +629,28 @@ public static function handleIterable(
$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 @@ function foo(array $a) : void {
}
}',
],
'simpleXmlIterator' => [
'<?php
function f(SimpleXMLElement $elt): void {
foreach ($elt as $item) {
f($item);
}
}'
],
];
}

0 comments on commit 80d9b5d

Please sign in to comment.
You can’t perform that action at this time.