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 does not detect immutable annotation and then complains about it missing #8604

Closed
addiks opened this issue Oct 19, 2022 · 3 comments · Fixed by #9304
Closed

Psalm does not detect immutable annotation and then complains about it missing #8604

addiks opened this issue Oct 19, 2022 · 3 comments · Fixed by #9304
Labels
bug easy problems Issues that can be fixed without background knowledge of Psalm

Comments

@addiks
Copy link
Contributor

addiks commented Oct 19, 2022

https://psalm.dev/r/94190bc824

ReflectionProperty::getType() is marked as immutable internally, so a method that is overwriting (overloading?) this internal method would need the @psalm-immutable annotation. I have added that annotation, but psalm does not detect it and then complains about it missing:

...
final class Foo extends ReflectionProperty
{
    /** @psalm-immutable */
    public function getType(): ?ReflectionType
...

ERROR: [MissingImmutableAnnotation](https://psalm.dev/213) - 9:5 - ReflectionProperty::getType is marked @psalm-immutable, but Foo::getType is not marked @psalm-immutable

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/94190bc824
<?php

use ReflectionType;
use ReflectionProperty;

final class Foo extends ReflectionProperty
{
    /** @psalm-immutable */
    public function getType(): ?ReflectionType
    {
        return null;
    }
}
Psalm output (using commit 212281d):

ERROR: MissingImmutableAnnotation - 9:5 - ReflectionProperty::getType is marked @psalm-immutable, but Foo::getType is not marked @psalm-immutable

@danog
Copy link
Collaborator

danog commented Oct 19, 2022

This is a case of a slightly incorrect error message, the actually required annotation for instance methods is psalm-external-mutation-free or psalm-mutation-free: https://psalm.dev/r/2411650486

@psalm-github-bot
Copy link

I found these snippets:

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

use ReflectionType;
use ReflectionProperty;

final class Foo extends ReflectionProperty
{
    /** @psalm-external-mutation-free */
    public function getType(): ?ReflectionType
    {
        return null;
    }
}
Psalm output (using commit 212281d):

No issues!

@weirdan weirdan added bug easy problems Issues that can be fixed without background knowledge of Psalm labels Nov 19, 2022
weirdan added a commit to weirdan/psalm that referenced this issue Feb 15, 2023
weirdan added a commit to weirdan/psalm that referenced this issue Feb 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug easy problems Issues that can be fixed without background knowledge of Psalm
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants