Handling Explicit Mixed with SimpleXMLElement #5586
-
|
I like to enable But I’m wondering the best way to handle using SimpleXMLElement. When iterating through its elements, each item is mixed by default. But it seems overkill to check the type of each item. Here is code demonstrating the issue: <?php
$xml = <<<'EOT'
<?xml version="1.0"?>
<items>
<item id="1">foo</item>
<item id="2">bar</item>
</items>
EOT;
$xml = simplexml_load_string($xml);
assert($xml instanceof SimpleXMLElement);
foreach ($xml->item as $item) {
var_dump((string) $item['id']);
}I get this error:
Not sure if this is a bug, or if there is a different way I should handle this. Can iterating through a SimpleXMLElement even ever return anything other than SimpleXMLElement? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
|
Hi, I'd like you to do the research - what are all the possible types coming from SimpleXMLElement and if it's even possible to type it more strongly. One possible outcome from the research is that it might be better to wrap working with XML in a better and easier typed API instead of working directly with SimpleXMLElement. That is - if you want to use |
Beta Was this translation helpful? Give feedback.
-
|
I think the issue is the /**
* @implements Traversable<string, SimpleXMLElement>
* @implements ArrayAccess<int, SimpleXMLElement>
* @implements Iterator<string, SimpleXMLElement>
* @implements RecursiveIterator<string, SimpleXMLElement>
*/
class SimpleXMLElement implements Traversable, ArrayAccess, Iterator, RecursiveIterator
{
}Then this code: //…
foreach ($xml->item as $i => $item) {
\PHPStan\dumpType($i);
\PHPStan\dumpType($item);
}Results in: What are your thoughts on this @ondrejmirtes? Edit: I think |
Beta Was this translation helpful? Give feedback.
Hi, I'd like you to do the research - what are all the possible types coming from SimpleXMLElement and if it's even possible to type it more strongly.
One possible outcome from the research is that it might be better to wrap working with XML in a better and easier typed API instead of working directly with SimpleXMLElement. That is - if you want to use
checkExplicitMixed: true.