[PHP] "Remove the unused function parameter" on closures/anonymous functions (php:S1172)

735 views
Skip to first unread message

Günter Grodotzki

unread,
May 25, 2016, 10:37:31 AM5/25/16
to SonarQube
With PHP analysis 2.8 (php:S1172) I get the following error:

"Remove the unused function parameter" with the code:

return array_first($this->categories, function ($key, $value) {

The code seems ok - so this would be a false positive?

Elena Vilchik

unread,
May 26, 2016, 4:13:41 AM5/26/16
to SonarQube
Hello,

It's hard to decide whether issue is false positive with this code piece. Could you please provide entire function code and issue message (to get parameter name) ?

Thanks!

Elena VILCHIK | SonarSource
Language Team

Günter Grodotzki

unread,
May 26, 2016, 6:47:12 AM5/26/16
to SonarQube
Thanks for having a look. My bad, this actually a legit issue (e.g. NOT a false positive) - the issue here is that sometimes, especially if the parent function allows the usage of callbacks, you can not really control the number or position of the arguments.

For example: "array_walk", this would always be a major issue if your callback only requires doing stuff with the $key and not the $value.

Would be happy to hear from others what the ideal way is for these kind of issues. At a minimum maybe ignore php-internal callbacks, because those are functions where you can't change the arguments of the callback.

Elena Vilchik

unread,
May 27, 2016, 3:02:19 AM5/27/16
to SonarQube
After quick search I've found these array functions: 
  • array_map — Applies the callback to the elements of the given arrays  (so if you don't need the key)
  • array_keys — Return all the keys or a subset of the keys of an array (if you don't need the value)
Reply all
Reply to author
Forward
0 new messages