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

InvalidPropertyAssignmentValue when using class, but not when using interfaces in templated array keys #3907

Closed
MisatoTremor opened this issue Jul 29, 2020 · 4 comments
Labels

Comments

@MisatoTremor
Copy link

ERROR: https://psalm.dev/r/1ef3e94a1a
OK: https://psalm.dev/r/dc6a46bdf2

If using an interface for one and a class for the other only the class one fails: https://psalm.dev/r/f7c663771f

Is this some bug or am I missing something here?

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/1ef3e94a1a
<?php

class Foo {}
class Bar {}

/** @psalm-template O of ?(Foo|Bar) */
class Resolved
{
    /**
     * @var Foo|Bar
     * @psalm-var O
     */
    private $entity = null;

    /**
     * @psalm-param array{foo?: O, bar?: O} $qux
     */
    public function __contruct(array $qux)
    {
        if (isset($qux['foo']) && $qux['foo'] instanceof Foo) {
            $this->entity = $qux['foo'];
        }
        if (isset($qux['bar']) && $qux['bar'] instanceof Bar) {
            $this->entity = $qux['bar'];
        }
    }
}
Psalm output (using commit 38f7481):

ERROR: InvalidPropertyAssignmentValue - 21:29 - $this->entity with declared type 'O:Resolved as Bar|Foo|null' cannot be assigned type 'Foo'

ERROR: InvalidPropertyAssignmentValue - 24:29 - $this->entity with declared type 'O:Resolved as Bar|Foo|null' cannot be assigned type 'Bar'
https://psalm.dev/r/dc6a46bdf2
<?php

interface Foo {}
interface Bar {}

/** @psalm-template O of ?(Foo|Bar) */
class Resolved
{
    /**
     * @var Foo|Bar
     * @psalm-var O
     */
    private $entity = null;

    /**
     * @psalm-param array{foo?: O, bar?: O} $qux
     */
    public function __contruct(array $qux)
    {
        if (isset($qux['foo']) && $qux['foo'] instanceof Foo) {
            $this->entity = $qux['foo'];
        }
        if (isset($qux['bar']) && $qux['bar'] instanceof Bar) {
            $this->entity = $qux['bar'];
        }
    }
}
Psalm output (using commit 38f7481):

No issues!
https://psalm.dev/r/f7c663771f
<?php

class Foo {}
interface Bar {}

/** @psalm-template O of ?(Foo|Bar) */
class Resolved
{
    /**
     * @var Foo|Bar
     * @psalm-var O
     */
    private $entity = null;

    /**
     * @psalm-param array{foo?: O, bar?: O} $qux
     */
    public function __contruct(array $qux)
    {
        if (isset($qux['foo']) && $qux['foo'] instanceof Foo) {
            $this->entity = $qux['foo'];
        }
        if (isset($qux['bar']) && $qux['bar'] instanceof Bar) {
            $this->entity = $qux['bar'];
        }
    }
}
Psalm output (using commit 38f7481):

ERROR: InvalidPropertyAssignmentValue - 21:29 - $this->entity with declared type 'O:Resolved as Bar|Foo|null' cannot be assigned type 'Foo'

@muglug muglug added the bug label Jul 29, 2020
@muglug
Copy link
Collaborator

muglug commented Jul 29, 2020

That's a bug, simplified to https://psalm.dev/r/22d9bce02f

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/22d9bce02f
<?php

class Foo {}
class Bar {}

/** @template FooOrBarOrNull of Foo|Bar|null */
class Resolved
{
    /**
     * @var FooOrBarOrNull
     */
    private $entity = null;

    /**
     * @psalm-param FooOrBarOrNull $qux
     */
    public function __contruct(?object $qux)
    {
        if ($qux instanceof Foo) {
            $this->entity = $qux;
        }
    }
}
Psalm output (using commit 38f7481):

ERROR: InvalidPropertyAssignmentValue - 20:29 - $this->entity with declared type 'FooOrBarOrNull:Resolved as Bar|Foo|null' cannot be assigned type 'Foo'

@MisatoTremor
Copy link
Author

Thank you

@muglug muglug closed this as completed in 1c8f9e9 Jul 29, 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

2 participants