-
Notifications
You must be signed in to change notification settings - Fork 653
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
reset() modifies the array, but Psalm does not detect it #9840
Comments
I found these snippets: https://psalm.dev/r/e9d0177183<?php
final readonly class Foo
{
private array $values;
public function __construct(array $values)
{
$this->values = $values;
}
public function bar(): mixed
{
return reset($this->values);
}
}
$x = new Foo([]);
$x->bar();
https://psalm.dev/r/b37bb7be31<?php
final readonly class Foo
{
private array $values;
public function __construct(array $values)
{
$this->values = $values;
}
public function bar(): mixed
{
return array_shift($this->values);
}
}
$x = new Foo(['1']);
$x->bar();
|
Oh that's a tough one. Internal function is one thing but any reference on a readonly property is dangerous (but not necessarily, there are performance reasons why you would want references, even on a readonly property) Maybe some kind of RiskyReferenceUsage would be needed here. That would warn for everything and it would be easier than to have some difference between references on internal function and reference on userland functions (and I'm pretty sure we would end up finding a perfectly fine case with an internal function that takes a reference but don't crash on readonly properties :p) |
I’d personally be fine with RiskyReferenceUsage for any reference to a readonly property! 👍 I’m not aware of performance improvements when using references as PHP uses copy-on-write, so I don’t think there are many legitimate use cases anyway? |
Really? I was convinced that it made a difference! Good to know! |
The following code:
Yields the following error:
But Psalm does not detect it: https://psalm.dev/r/e9d0177183
Same with
array_shift()
: https://psalm.dev/r/b37bb7be31This probably applies to all functions with a parameter passed by reference.
The text was updated successfully, but these errors were encountered: