Passed
Push — master ( 4d0c94...12c08d )
by
unknown
02:57
created

OptionsHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
dl 0
loc 25
rs 10
c 1
b 0
f 1
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B filterRecursive() 0 23 8
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Tests\Support\Helper;
6
7
use function array_key_exists;
8
use function is_array;
9
10
final class OptionsHelper
11
{
12
    public static function filterRecursive(&$array, array $keptOptionNames)
13
    {
14
        $hasKeptOptionName = false;
15
        foreach ($keptOptionNames as $keptOptionName) {
16
            if (array_key_exists($keptOptionName, $array)) {
17
                $hasKeptOptionName = true;
18
19
                break;
20
            }
21
        }
22
23
        $removedOptionNames = $hasKeptOptionName
24
            ? array_diff(array_keys($array), $keptOptionNames)
25
            : [];
26
        foreach ($removedOptionNames as $removedOptionName) {
27
            if ($removedOptionName !== 0) {
28
                unset($array[$removedOptionName]);
29
            }
30
        }
31
32
        foreach ($array as &$value) {
33
            if (is_array($value)) {
34
                self::filterRecursive($value, $keptOptionNames);
35
            }
36
        }
37
    }
38
}
39