CleanArray   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 38
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B filter() 0 24 7
A filterSingle() 0 4 1
1
<?php
2
declare(strict_types=1);
3
namespace Sirius\Filtration\Filter;
4
5
class CleanArray extends AbstractFilter
6
{
7
    const OPTION_NULLIFY = 'nullify';
8
9
    protected $options = [
10
        self::OPTION_NULLIFY => true
11
    ];
12
13 4
    public function filter($value, string $valueIdentifier = null)
14
    {
15 4
        if (! is_array($value)) {
16 1
            return $value;
17
        }
18 3
        $result = [];
19 3
        if ($this->options['nullify']) {
20 3
            $nullifier = new Nullify();
21
        }
22 3
        $arrayIsAssociative = array_keys($value) === range(0, count($value) - 1);
23 3
        foreach ($value as $k => $v) {
24 3
            if (isset($nullifier)) {
25 3
                $v = $nullifier->filter($v);
26
            }
27 3
            if ($v !== null) {
28 2
                if ($arrayIsAssociative) {
29 1
                    $result[] = $v;
30
                } else {
31 1
                    $result[$k] = $v;
32
                }
33
            }
34
        }
35 3
        return $result;
36
    }
37
38 1
    public function filterSingle($value, string $valueIdentifier = null)
39
    {
40 1
        return $this->filter($value, $valueIdentifier);
41
    }
42
}
43