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-mutation-free
only works *once* inside if
#8133
Comments
I found these snippets: https://psalm.dev/r/fb594bf0e4<?php
class Foo
{
private ?string $bar = null;
/**
* @psalm-mutation-free
*/
public function getBar(): ?string
{
return $this->bar;
}
}
class Cat
{
private ?string $bar = null;
public function setBar(string $bar): void
{
$this->bar = $bar;
}
}
$foo = new Foo();
$cat = new Cat();
if (is_string($foo->getBar())) {
$cat->setBar($foo->getBar());
echo $foo->getBar() . $foo->getBar();
}
|
Psalm cannot know what happens inside the |
Hm. But it's $cat->setBar(). How could this modify the value of $foo->getBar()? |
Psalm isn't smart enough to tell that |
and most importantly, Psalm doesn't check method name like setXXX or getXXX, so the method name could as well be called |
I see, indeed, it could even be So the exact rule is that there must not be a call to any function (more precisely: to any function without This is somewhat surprising (at least to me) - what about adding more explanation to https://psalm.dev/docs/running_psalm/issues/PossiblyNullArgument/ ? |
Mainly the latter, contributions are very welcome :) |
See vimeo#8133 (comment) Don't know if this is the best way to explain this, but it's a start :-) Is there a better way to add the link to https://psalm.dev/docs/annotating_code/supported_annotations/#psalm-mutation-free ? I also removed the `<?php` tag from the code block.
https://psalm.dev/r/fb594bf0e4
This line is not raising an error, due to
@psalm-mutation-free
andif (is_string())
:However, the next line does give an error - so it looks like
@psalm-mutation-free
isn't "working" here anymore:If I delete the first line, the second line does not raise the error. This makes me think that
@psalm-mutation-free
is only working at the first call.The text was updated successfully, but these errors were encountered: