vimeo / psalm

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

Unused function call for array_map unlink #6687

Open kkmuffme opened 2 years ago

kkmuffme commented 2 years ago

https://psalm.dev/r/33a00f8479

While technically unused, PHP throws notices if any of the files in the array don't exist (and therefore cannot be unlinked). Most people would not use the return value here.

Since psalm does not complain about an unused return value of "unlink" it should not complain about unused return value when "unlink" is used in array_map*

*I assume there are other similar functions, for which this should apply too - if psalm does not complain about unused return value if the function is called standalone, it should not complain about unused return value if it's used in array_map

psalm-github-bot[bot] commented 2 years ago

I found these snippets:

https://psalm.dev/r/33a00f8479 ```php
weirdan commented 2 years ago

Even though in this particular case it could be replaced with array_walk() (https://psalm.dev/r/50774895f1) the concept of conditional mutability would be interesting to explore.

psalm-github-bot[bot] commented 2 years ago

I found these snippets:

https://psalm.dev/r/50774895f1 ```php
kkmuffme commented 2 years ago

The code you provided with array_walk causes a PHP notice:


unlink() expects parameter 2 to be resource, int given
#0 [internal function] (): unlink( $filename = 'file.log', $context = 0 )
#1 \file.php (807): array_walk( $input = Array, $funcname = 'unlink' )
`` 
weirdan commented 2 years ago

That's true. It could be easily fixed by wrapping the unlink() into a closure with a single argument, of course: https://psalm.dev/r/8decfdf179

psalm-github-bot[bot] commented 2 years ago

I found these snippets:

https://psalm.dev/r/8decfdf179 ```php unlink($file)); ``` ``` Psalm output (using commit f7a6336): No issues! ```