remove()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt;
5
6
/**
7
 * Remove all provided elements from the collection
8
 * This function uses strict comparison to find and remove elements
9
 *
10
 * @example $set = [1, 1, 2, 3, 4]
11
 * remove($set, 1, 3) will return 3 (int) and $set will contain [2, 4]
12
 *
13
 * @param  array $set         The collection
14
 * @param  mixed ...$elements Elements to be removed
15
 * @return int The number of removed elements
16
 */
17
function remove(array &$set, ...$elements): int
18
{
19
    $removed = 0;
20
    foreach ($elements as $element) {
21
        foreach (array_keys($set, $element, true) as $index) {
22
            unset($set[$index]);
23
            $removed++;
24
        }
25
    }
26
27
    return $removed;
28
}
29