Psalm: Wrong type inference for iteration over SimpleXmlElement

Created on 22 Aug 2019  路  3Comments  路  Source: vimeo/psalm

Hey, I think I found a false type inflection when dealing with SimpleXmlElements and it's children() method:

https://travis-ci.org/neos/flow-development-collection/jobs/575537058#L867

ERROR: PossiblyInvalidMethodCall
 - Packages/Framework/Neos.Flow/Classes/I18n/Xliff/V12/XliffParser.php:98:55
 - Cannot call method on possible non-empty-array<array-key, mixed> variable $translationElement
                        foreach ($translationElement->children() as $translationPluralForm) {
ERROR: PossiblyUndefinedMethod
 - Packages/Framework/Neos.Flow/Classes/I18n/Xliff/V12/XliffParser.php:98:55
 - Method ArrayAccess::children does not exist
                        foreach ($translationElement->children() as $translationPluralForm) {

The code questioned is https://github.com/neos/flow-development-collection/blob/master/Neos.Flow/Classes/I18n/Xliff/V12/XliffParser.php#L98

\SimpleXmlElement::children() always returns a new \SimpleXmlElement, hence $translationElement is of that type. See https://www.php.net/manual/en/simplexmlelement.children.php

I guess the error comes up, due to the isset($translationElement['restype']) check beforehand, that makes psalm think that $translationElement suddenly is of type ArrayAccess (only?), while it still can only be SimpleXmlElement, as no new assignment has happened.

If any further information is needed, feel free to ask.

Thanks a lot for the awesome tool! It has already helped our project so much, even though we only started using it like a week ago.

bug

Most helpful comment

Fixed in 80d9b5d

All 3 comments

It's likely caused by Psalm being unable to figure out the type of iteration variable when iterating over SimpleXMLElement: https://psalm.dev/r/b17591c0f9

<?php
  function f(\SimpleXMLElement $elt): void {
    foreach ($elt as $item) {
      f($item);
    }
  }

Expected: no issues, as $item is SimpleXMLElement
Actual:

Psalm output (using commit 0860727): 

INFO: MixedAssignment - 3:22 - Cannot assign $item to a mixed type

INFO: MixedArgument - 4:9 - Argument 1 of f cannot be mixed, expecting SimpleXMLElement

That sounds more reasonable than my guess! Thanks for the verification!

Fixed in 80d9b5d

Was this page helpful?
0 / 5 - 0 ratings

Related issues

muglug picture muglug  路  3Comments

orklah picture orklah  路  3Comments

vudaltsov picture vudaltsov  路  3Comments

roukmoute picture roukmoute  路  3Comments

vudaltsov picture vudaltsov  路  3Comments