Filter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A getFilteredData() 0 3 1
A getContaminatedData() 0 3 1
A filter() 0 14 4
1
<?php
2
3
namespace PhpcsDiff\Filter;
4
5
use PhpcsDiff\Filter\Exception\FilterException;
6
use PhpcsDiff\Filter\Exception\InvalidRuleException;
7
use PhpcsDiff\Filter\Rule\Exception\RuleException;
8
use PhpcsDiff\Filter\Rule\RuleInterface;
9
use PhpcsDiff\Validator\Exception\ValidatorException;
10
use PhpcsDiff\Validator\RuleValidator;
11
12
class Filter
13
{
14
    /**
15
     * @var RuleInterface[]
16
     */
17
    protected $rules;
18
19
    /**
20
     * @var array
21
     */
22
    protected $unfilteredData;
23
24
    /**
25
     * @var array
26
     */
27
    protected $filteredData = [];
28
29
    /**
30
     * @var array
31
     */
32
    protected $contaminatedData = [];
33
34
    /**
35
     * @param array $rules
36
     * @param array $unfilteredData
37
     * @throws FilterException
38
     */
39
    public function __construct(array $rules, array $unfilteredData)
40
    {
41
        try {
42
            (new RuleValidator($rules))->validate();
43
        } catch (ValidatorException $exception) {
44
            throw new InvalidRuleException('', 0, $exception);
45
        }
46
47
        $this->rules = $rules;
48
        $this->unfilteredData = $unfilteredData;
49
    }
50
51
    /**
52
     * @return $this
53
     */
54
    public function filter(): Filter
55
    {
56
        foreach ($this->unfilteredData as $key => $item) {
57
            foreach ($this->rules as $rule) {
58
                try {
59
                    $rule($item);
60
                    $this->filteredData[$key] = $item;
61
                } catch (RuleException $exception) {
62
                    $this->contaminatedData[$key] = $item;
63
                }
64
            }
65
        }
66
67
        return $this;
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public function getFilteredData(): array
74
    {
75
        return $this->filteredData;
76
    }
77
78
    /**
79
     * @return array
80
     */
81
    public function getContaminatedData(): array
82
    {
83
        return $this->contaminatedData;
84
    }
85
}
86