Phpinspectionsea: iterable is valid in foreach

Created on 13 Apr 2017  路  9Comments  路  Source: kalessil/phpinspectionsea

Putting an iterable typehinted item into a foreach (in my code these are \Generator and array) shows a problem where the code works perfectly fine. This inspection doesn't seem quite right in that regard.

Here's an example:

function generate(): \Generator
{
    yield 'this is ';
    yield 'an example';
}

function iterate(iterable $iter)
{
    foreach ($iter as $i) {
        echo $i;
    }
}

iterate(generate());

This executes fine, but the inspection highlights a problem area at $iter.

bug / false-positive fixed

Most helpful comment

Can you please post a code that shows that issue?

All 9 comments

Can you please post a code that shows that issue?

@royallthefourth : let me check if I can reproduce this (we re-worked the inspection in the upcoming release). Or perhaps code fragment would speed up investigation a lot =)

Thank you @royallthefourth, checking this.

Seems to be resolved:

image

To not wait for the release you can download PhpInspectionsEA.jar from master branch and install it manually: File -> Setting -> Plugins, under the plugin list click "Install from disk" and select the downloaded file.

The dev-build is stable: @funivan and me running dev builds.

Can not iterate 'iterable' (must implement one of iterator interfaces)

I get this message over variable which is returned from function with return type iterable
function() : iterable
installed version 2.3.6

@kalessil will it help in this particular case?

Thank you in advance
iterable

@whale001 : thanks for reporting, yes all needed information is here.

EDIT: obviously the fix needs to be extended.

@kalessil Thank you for awesome plugin

You are welcome @whale001 =)

I added support for the iterable type.

Was this page helpful?
0 / 5 - 0 ratings