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

Magic instance property is not defined #11098

Open
rarila opened this issue Sep 17, 2024 · 1 comment
Open

Magic instance property is not defined #11098

rarila opened this issue Sep 17, 2024 · 1 comment

Comments

@rarila
Copy link
Contributor

rarila commented Sep 17, 2024

The following code gets no errors up to v5.25.
(The Psalm version on psalm.dev is at time of writing quite old, May 5th, so no problems there)

https://psalm.dev/r/6e53e9fc0c

Starting with v5.26 you get the error Magic instance property FooValue::$foo is not defined on line 56.

I tracked that down to pull request #11081.
Reversing commit 3cf1942 makes things work again, so the problem seems to be there.

Copy link

I found these snippets:

https://psalm.dev/r/6e53e9fc0c
<?php

declare(strict_types=1);

abstract class AbstractValue
{
    public function __get(string $name): mixed
    {
        return 123;
    }
    
    public function __set(string $name, mixed $value): void
    {
    }
}

/** @property int $foo */
final class FooValue extends AbstractValue
{
}

/** @template T of AbstractValue */
abstract class AbstractClass
{
    /** @var T */
    private AbstractValue $value;

    /** @param T $value */
    public function __construct(AbstractValue $value)
    {
        $this->value = $value;
    }

    /** @return T */
    protected function getValue(): AbstractValue
    {
        return $this->value;
    }
}

/** @extends AbstractClass<FooValue> */
final class Test extends AbstractClass
{
    public function __construct()
    {
        parent::__construct(new FooValue());

        // Works:
        echo $this->getValue()->foo;

        // Works:
        $v = $this->getValue();
        $v->foo = 1;

        // Fails with: Magic instance property FooValue::$foo is not defined
        $this->getValue()->foo = 1;
    }
}
Psalm output (using commit 16b24bd):

No issues!

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

1 participant