vimeo / psalm

A static analysis tool for finding errors in PHP applications
https://psalm.dev
MIT License
5.49k stars 655 forks source link

Inconsistent immutability assumptions causing incorrect type inference #8334

Open AndrolGenhald opened 1 year ago

AndrolGenhald commented 1 year ago

https://psalm.dev/r/a8e457deb1

One assumption we often make about immutability is that an immutable object's methods will always return the same value when called with the same arguments, and that's a great thing to have, but with current immutability rules, referenced objects aren't forced to be immutable, so they could change.

See also #7438, #7629, #6881, etc. I think at some point we need to overhaul our immutability implementation and provide better support for different levels of immutability.

psalm-github-bot[bot] commented 1 year ago

I found these snippets:

https://psalm.dev/r/a8e457deb1 ```php baz?->str; } } class Baz { public string $str = ""; } $baz = new Baz(); $foobar = new Foobar($baz); assert($foobar->getStr() === ""); $baz->str = "baz"; /** @psalm-trace $b */ $b = $foobar->getStr(); // $b is actually "baz" ``` ``` Psalm output (using commit 7c4228f): INFO: Trace - 27:1 - $b: '' ```