4

Static code analyzers like (in this specific case) PHPMD bemoan an error in the following piece of PHP code:

foreach ($aSomething as $key => $value) {
    ... do something with the $key only
}

The error:

Avoid unused local variables such as '$value'.

Now, I am not aware of any way to create a foreach loop with only the keys. What would be the "analyzer safe" solution to phrase these lines?

I am solving this at the moment via a call to array_keys and then foreach-ing over this one but it feels like overkill. Another solution is always to silence the analyzer for this loop.

What is the "right" way to keep in line with code quality and "understandability" of code requirements?

Patrick
  • 691
  • 8
  • 30
  • 5
    just use `array_keys` if you want just keys .. that is the point of the function.. to get keys ... – treyBake Apr 16 '19 at 14:17
  • why don't you use a for loop then? – Lelio Faieta Apr 16 '19 at 14:19
  • OP already mentioned that he uses `array_keys`. But getting keys is a redundant operation in this case. – u_mulder Apr 16 '19 at 14:20
  • 1
    @LelioFaieta because keys are not always 0-indexed? – u_mulder Apr 16 '19 at 14:20
  • 2
    '*What is the "right" way to keep in line with code quality and "understandability" of code requirements?*' - That is opinionated depending on the person. There is no "right" way to do it. – Script47 Apr 16 '19 at 14:21
  • @LelioFaieta: and not always numeric. – AbraCadaver Apr 16 '19 at 14:23
  • @Script47 that was more or less what I wanted to know. those analyzers are sometimes quite intimidating with their "opinions" ;) – Patrick Apr 16 '19 at 14:24
  • @u_mulder yes, this specific line has strings as keys. – Patrick Apr 16 '19 at 14:25
  • 1
    Suppress this inspection for that particular line of code with an appropriate comment; if your analyser supports such a thing. Essentially tell the analyser *I know what I'm doing in this particular case.* – deceze Apr 16 '19 at 14:28
  • 1
    Also as I see from docs there's a `allow-unused-foreach-variables` property for rule in phpmd. – u_mulder Apr 16 '19 at 14:30
  • 1
    And this github thread also https://github.com/phpmd/phpmd/pull/329 – u_mulder Apr 16 '19 at 14:32
  • @u_mulder do you care to add an answer with the link and a sample? It's probably the most forward way. This might be useful for people searching for this specific case. (funny, the 'fugly hack" they are mentioning, like unset($value), was what I was doing before. But it seemed soo wrong and to much additional unused work for PHP :]) - I am not sure if this clashes with the whole "opinionated" rule of StackOverflow, but I see use in this answer for people searching for how to have analyser-prone code... – Patrick Apr 16 '19 at 14:48
  • In many languages it's an informal standard to use `$_` (or equivalent) as name for variables which are necessary but not used; perhaps that's enough to satisfy PHPMD (I don't know though). – deceze Apr 16 '19 at 14:52
  • @Patrick done, have no experience in configuring phpmd, so feel free to edit my answer with configuration file if needed. – u_mulder Apr 16 '19 at 15:04

1 Answers1

3

As I read from some phpmd docs there's a allow-unused-foreach-variables property for rule UnusedLocalVariable, read more here:

https://phpmd.org/rules/unusedcode.html

Also, according to github thread here https://github.com/phpmd/phpmd/pull/329, there should be an option to

whitelist variables in the UnusedLocalVariable rule

As for using variable like $_ which means "value not needed" or "throw it away", there's another git thread https://github.com/phpmd/phpmd/issues/326, which in the end sends you to previous one with opportunity to "whitelist variables in the UnusedLocalVariable rule".

So, there're two options - allow unused variables, which I don't think a good idea. Second option is to whitelist variables which will be ignored (the above metioned $_ for example) and use them when you don't need data in these variables.

Although I don't know how to configure phpmd, I suppose someone will be able to edit my answer with correct configuration for abovesaid options.

u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • Specifically, PHPMD is complaining about `$_` as not being named in camelCase, but adding `* @SuppressWarnings(PHPMD.UnusedLocalVariable)` to the doc-block of the method (without using $_ as a parameter in foreach) will silence the error. – Patrick Apr 16 '19 at 15:24