Skip to content

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

@ollieread

Description

@ollieread

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();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions