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

psalm-if-this-is enhancements #7258

Closed
klimick opened this issue Dec 31, 2021 · 1 comment · Fixed by #7259
Closed

psalm-if-this-is enhancements #7258

klimick opened this issue Dec 31, 2021 · 1 comment · Fixed by #7259

Comments

@klimick
Copy link
Contributor

klimick commented Dec 31, 2021

This example raises many issues: https://psalm.dev/r/8f7f7985a6

All issues can be fixed if:

  1. $this variables will be inferred from psalm-if-this-is
  2. template params will be resolved from psalm-if-this-is
@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/8f7f7985a6
<?php

/**
 * @template T
 */
final class Option
{
    /**
     * @return T|null
     */
    public function unwrap()
    {
        throw new RuntimeException('???');
    }
}

/**
 * @template L
 * @template R
 */
final class Either
{
    /**
     * @return R|null
     */
    public function unwrap()
    {
        throw new RuntimeException('???');
    }
}

/**
 * @template T
 */
final class ArrayList
{
    /** @var list<T> */
    private $items;

    /**
     * @param list<T> $items
     */
    public function __construct(array $items)
    {
        $this->items = $items;
    }

    /**
     * @template A
     * @template B
     * @template TOption of Option<A>
     * @template TEither of Either<mixed, B>
     * @psalm-if-this-is ArrayList<TOption|TEither>
     * @return ArrayList<A|B>
     */
    public function compact(): ArrayList
    {
        $values = [];

        foreach ($this->items as $item) {
            $value = $item->unwrap();

            if (null !== $value) {
                $values[] = $value;
            }
        }

        return new self($values);
    }
}

/** @var ArrayList<Either<Exception, int>|Option<int>> $list */
$list = new ArrayList([]);

/** @psalm-trace $numbers */
$numbers = $list->compact();
Psalm output (using commit 51ba96c):

ERROR: IfThisIsMismatch - 76:19 - Class type must be ArrayList<(TEither:fn-arraylist::compact as Either<mixed, B:fn-arraylist::compact as mixed>)|(TOption:fn-arraylist::compact as Option<A:fn-arraylist::compact as mixed>)> current type ArrayList<Either<Exception, int>|Option<int>>

INFO: Trace - 76:1 - $numbers: ArrayList<empty>

INFO: MixedMethodCall - 61:29 - Cannot determine the type of $item when calling method unwrap

INFO: MixedAssignment - 61:13 - Unable to determine the type that $value is being assigned to

INFO: MixedAssignment - 64:17 - Unable to determine the type of this assignment

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

Successfully merging a pull request may close this issue.

1 participant