ArrayUniqueCanBeUsed
info on by default quick-fix PHP ≥ 7.2Group: Control flow · PhpStorm name: ArrayUniqueCanBeUsedInspection
Counting occurrences with array_count_values() just to take the keys or the number of keys is an indirect way of deduplicating. Since PHP 7.2 array_unique() is fast and says what is meant: array_values(array_unique($a)) or count(array_unique($a)).
Example
php
<?php
function tally(array $votes, $batch) {
$names = array_keys(array_count_values(['alpha', 'beta']));
$total = \count(\array_count_values([1, 2, 1]));
return [$names, $total];
}php
<?php
function tally(array $votes, $batch) {
$names = array_values(array_unique(['alpha', 'beta']));
$total = \count(\array_unique([1, 2, 1]));
return [$names, $total];
}Reported:
- line 3: Use 'array_values(array_unique(['alpha', 'beta']))' instead (array_unique() is fast since PHP 7.2).
- line 4: Use 'count(array_unique([1, 2, 1]))' instead (array_unique() is fast since PHP 7.2).
Configure
In custos.json:
json
{
"rules": {
"ArrayUniqueCanBeUsed": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file), either of:
php
// @custos-ignore ArrayUniqueCanBeUsed
/**
* @noinspection ArrayUniqueCanBeUsedInspection
*/