AmbiguousMethodsCallsInArrayMapping
warning on by defaultGroup: Performance · PhpStorm name: AmbiguousMethodsCallsInArrayMappingInspection
When a loop builds a map like $map[key($x)] = key($x);, the same call is evaluated twice per iteration (once for the key, once for the value). Compute it once into a local variable.
Example
php
<?php
foreach ($users as $user) {
$byEmail[$user->email()] = $user->email();
$byName[strtolower($user)] = strtolower($user);
$byName[(strtolower($user))] .= strtolower( $user );
$tagged['u:' . md5($user)] = $cache[md5($user)];
$groups[Role::of($user)][] = Role::of($user);
$byName[strtolower($user)] = true;
$plain['x'] = strtolower($user);
$other[md5($user)] = md5($guest);
if ($user) {
$skip[md5($user)] = md5($user);
}
}
for ($i = 0; $i < 9; $i++) {
$sq[abs($i)] = abs($i);
}
while ($row = next($rows)) {
$ids[key($row)] = key($row);
}
foreach ($streams as $h) {
$lines[fgets($h)] = fgets($h);
$pairs[next($h)] = next($h);
}Reported:
- line 3: This call is repeated in the key; store its result in a local variable.
- line 4: This call is repeated in the key; store its result in a local variable.
- line 5: This call is repeated in the key; store its result in a local variable.
- line 6: This call is repeated in the key; store its result in a local variable.
- line 7: This call is repeated in the key; store its result in a local variable.
- line 18: This call is repeated in the key; store its result in a local variable.
Configure
In custos.json:
json
{
"rules": {
"AmbiguousMethodsCallsInArrayMapping": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore AmbiguousMethodsCallsInArrayMapping
/**
* @noinspection AmbiguousMethodsCallsInArrayMappingInspection
*/