vimeo / psalm

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

Is it possible to disable automatic detection of simple pure methods? #9665

Open janopae opened 1 year ago

janopae commented 1 year ago

This is possible currently: https://psalm.dev/r/b2a9011f65

However, the library might change the following way, making it break: https://psalm.dev/r/7e645e6b69

This could be solved by making Psalm require to always explicitly define pureness as part of the method signature. At least, it would be sane to have a warning on missing pure annotations.

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

I found these snippets:

https://psalm.dev/r/b2a9011f65 ```php p; } } } namespace MyProject { /** * @psalm-pure */ function pure(\LibraryThatMightChangeItsInternalsAnytime\A $a): string { return $a->getP(); } } ``` ``` Psalm output (using commit e2957a1): No issues! ```
https://psalm.dev/r/7e645e6b69 ```php p; } } } namespace MyProject { /** * @psalm-pure */ function pure(\LibraryThatMightChangeItsInternalsAnytime\A $a): string { return $a->getP(); } } ``` ``` Psalm output (using commit e2957a1): INFO: UnusedParam - 4:25 - Param s is never referenced in this method ERROR: ImpureMethodCall - 27:20 - Cannot call a non-mutation-free method LibraryThatMightChangeItsInternalsAnytime\A::getP from a pure context ```
weirdan commented 1 year ago

I don't think we have that yet. If you want to try your hand at it, it happens here:

https://github.com/vimeo/psalm/blob/e2957a163190c1f5bd057f4ecbaab4b96098a94a/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php#L247-L270

ygottschalk commented 1 year ago

Can psalter add those annotations? Would be nice...

orklah commented 1 year ago

Seems undocumented but yes it can. --issues=MissingPureAnnotation will do the trick