sirbrillig / phpcs-variable-analysis

Find undefined and unused variables with the PHP Codesniffer static analysis tool.
Other
136 stars 14 forks source link

Incorrect warning about redeclaration of function parameter as static variable #280

Closed mxr576 closed 2 years ago

mxr576 commented 2 years ago

I believe there is still a regression related to static variables even after #272 and #275.

The following code leads to an invalid concern raised by the checker on the last line in the example.

      $developer_team_ids = array_map(static function (GroupInterface $group) {
        return (int) $group->id();
      }, ($this->getTeamsByUserQuery)((int) $account->id()));
Redeclaration of function parameter $account as static variable.
(DrupalPractice.CodeAnalysis.VariableAnalysis.VariableRedeclaration)
sirbrillig commented 2 years ago

😩 You're right. Detecting static declarations is a lot harder than it looks, I guess. Here's a minimal test case I came up with which actually produces a bunch of weird errors:

function function_with_static_closure_inside_array_map($account, $getTeamsByUserQuery) {
  $developer_team_ids = array_map(
    static function (GroupInterface $group) {
      return (int) $group->id();
    },
    ($getTeamsByUserQuery)((int) $account->id())
  );
  return $developer_team_ids;
}
 2 | WARNING | Unused static variable $account.
 2 | WARNING | Unused static variable $getTeamsByUserQuery.
 7 | WARNING | Redeclaration of function parameter $getTeamsByUserQuery as static variable.
 7 | WARNING | Redeclaration of function parameter $account as static variable.
mxr576 commented 2 years ago

Thanks, this was quick 😉