Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting a PossiblyInvalidMethodCall error complaining about a method call on a possibly other variable #8669

Closed
ollieread opened this issue Nov 5, 2022 · 6 comments

Comments

@ollieread
Copy link

ollieread commented Nov 5, 2022

I have a value storing the following property

/**
 * @var \Smpl\Utils\Contracts\Supplier<\Smpl\Collections\Contracts\Collection<array-key, E>>|iterable<E>
 * @noinspection PhpDocFieldTypeMismatchInspection
 */
private Supplier|iterable $elementSupplier;

Then later on I have the following method.

/**
 * @return iterable<E>
 *
 * @throws \Smpl\Collections\Exceptions\InvalidArgumentException
 */
protected function getElements(): iterable
{
    $elements = $this->elementSupplier;

    if ($elements instanceof Supplier) {
        $elements = $elements->get();
    }

    if (! is_iterable($elements)) {
        throw InvalidArgumentException::invalidElementSupplier(static::class);
    }

    return $elements;
}

The $elements = $elements->get() line is causing the following error, even though it's wrapped in an instanceof check.

ERROR: PossiblyInvalidMethodCall - src/Concerns/SuppliesElementsForOperation.php:40:36 - Cannot call method on possible iterable<mixed, E:Smpl\Collections\Operations\DifferenceOperation as mixed> variable $elements (see https://psalm.dev/113)
            $elements = $elements->get();
@orklah
Copy link
Collaborator

orklah commented Nov 5, 2022

That's weird indeed, did you try reproducing on psalm.dev?

Is it a new issue since Psalm 5 or did you already had this on Psalm 4?

@ollieread
Copy link
Author

That's weird indeed, did you try reproducing on psalm.dev?

Will try and recreate now @orklah.

Is it a new issue since Psalm 5 or did you already had this on Psalm 4?

No, I actually had 0 errors on 4, and now I'm down to 95 XD

@ollieread
Copy link
Author

That's weird indeed, did you try reproducing on psalm.dev?

Here you go, recreated https://psalm.dev/r/937ba0306a

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/937ba0306a
<?php
interface Supplier {
	public function get(): iterable;
}

class SomeClass {
    protected Supplier|iterable $prop;
    
    public function __construct(Supplier|iterable $value) {
        $this->prop = $value;
    }
    
    public function do(): void {
        $var = $this->prop;
        
        if ($var instanceof Supplier) {
            $var->get();
        }
    }
}
Psalm output (using commit 2ae819e):

ERROR: PossiblyInvalidMethodCall - 17:19 - Cannot call method on possible iterable<mixed, mixed> variable $var

danog added a commit to danog/psalm that referenced this issue Nov 5, 2022
orklah added a commit that referenced this issue Nov 5, 2022
@danog danog closed this as completed Nov 5, 2022
@ollieread
Copy link
Author

ollieread commented Nov 5, 2022

Thanks all, will give you a break for a bit 🤣

@orklah
Copy link
Collaborator

orklah commented Nov 5, 2022

😄 Thanks for those reports , really! Everything that can be fixed before release is a lot of pain we won't have to go through the day after !

If you have more, please post them!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants