DataModifierGroup::addModifier()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Thruster\Component\DataModifier;
4
5
/**
6
 * Class DataModifierGroup
7
 *
8
 * @package Thruster\Component\DataModifier
9
 * @author  Aurimas Niekis <[email protected]>
10
 */
11
class DataModifierGroup
12
{
13
    /**
14
     * @var DataModifierInterface[]
15
     */
16
    protected $dataModifiers;
17
    /**
18
     * @var DataModifierInterface[]
19
     */
20
    protected $sortedDataModifiers;
21
22 4
    public function __construct()
23
    {
24 4
        $this->dataModifiers       = [];
25 4
        $this->sortedDataModifiers = [];
26 4
    }
27
28
    /**
29
     * @param DataModifierInterface $dataModifier
30
     * @param int                   $priority
31
     *
32
     * @return $this
33
     */
34 2
    public function addModifier(DataModifierInterface $dataModifier, int $priority = 0) : self
35
    {
36 2
        $this->dataModifiers[$priority][] = $dataModifier;
37 2
        $this->sortedDataModifiers = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array<integer,object<Thr...DataModifierInterface>> of property $sortedDataModifiers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
39 2
        return $this;
40
    }
41
42
    /**
43
     * @return DataModifierInterface[]
44
     */
45 2
    public function getModifiers() : array
46
    {
47 2
        if (null !== $this->sortedDataModifiers) {
48 1
            return $this->sortedDataModifiers;
49
        }
50
51 2
        krsort($this->dataModifiers);
52 2
        $this->sortedDataModifiers = call_user_func_array('array_merge', $this->dataModifiers);
0 ignored issues
show
Documentation Bug introduced by
It seems like call_user_func_array('ar..., $this->dataModifiers) of type * is incompatible with the declared type array<integer,object<Thr...DataModifierInterface>> of property $sortedDataModifiers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53
54 2
        return $this->sortedDataModifiers;
55
    }
56
57
    /**
58
     * @param mixed $input
59
     *
60
     * @return mixed
61
     */
62 1
    public function modify($input)
63
    {
64 1
        foreach ($this->getModifiers() as $modifier) {
65 1
            $input = $modifier->modify($input);
66
        }
67
68 1
        return $input;
69
    }
70
}
71