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

ArgumentTypeCoercion for static in 3.11.0 #3127

Closed
vudaltsov opened this issue Apr 12, 2020 · 4 comments
Closed

ArgumentTypeCoercion for static in 3.11.0 #3127

vudaltsov opened this issue Apr 12, 2020 · 4 comments
Labels

Comments

@vudaltsov
Copy link
Contributor

https://psalm.dev/r/cc4ea0abe5
I think this is the correct behavior of Psalm, just want to make sure I understand the edge case.

If I have abstract class AbstractId extends Id and then Ids<AbstractId>, the Ids<AbstractId>::contains method will not work correctly, right?

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/cc4ea0abe5
<?php

/**
 * @psalm-immutable
 */
abstract class Id
{
    protected string $id;

    final protected function __construct(string $id)
    {
        $this->id = $id;
    }
    
    /**
     * @param static $id
     */
    final public function equals(self $id): bool
    {
        return $this->id === $id->id;
    }
}

/**
 * @template T of Id
 */
final class Ids
{
    /**
     * @psalm-var list<T>
     */
    private array $ids;

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

    /**
     * @psalm-param T $id
     */
    public function contains(Id $id): bool
    {
        foreach ($this->ids as $oneId) {
            if ($oneId->equals($id)) {
                return true;
            }
        }

        return false;
    }
}
Psalm output (using commit 5bc9b09):

ERROR: ArgumentTypeCoercion - 48:32 - Argument 1 of Id::equals expects Id&static, parent type T:Ids as Id provided

@weirdan
Copy link
Collaborator

weirdan commented Apr 12, 2020

I think this is the correct behavior of Psalm,

Why do you think it's correct?

@vudaltsov
Copy link
Contributor Author

@weirdan , as I said, if I have abstract class AbstractId extends Id and then Ids<AbstractId>, the Ids<AbstractId>::contains method will not be able to check that Id::equals is called against static.

@muglug muglug added the bug label Apr 12, 2020
@vudaltsov
Copy link
Contributor Author

Okay, maybe I am overthinking this and it's just a bug :)

@muglug muglug closed this as completed in 15df39f Apr 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants