SumValue::merge()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 2
crap 3
1
<?php
2
3
namespace Graze\ArrayMerger\ValueMerger;
4
5
/**
6
 * Add together the contents of the values.
7
 *
8
 * Only works if they are both numeric values. If they are not, a backup merger is used (defaults to lastValue)
9
 *
10
 * ### Examples:
11
 *
12
 * ```
13
 * 1, 1 -> 2
14
 * 1.5, 7.2 -> 8.7
15
 * left, 5 => 5
16
 * ```
17
 */
18
class SumValue implements ValueMergerInterface
19
{
20
    use InvokeMergeTrait;
21
22
    /** @var ValueMergerInterface */
23
    private $backup;
24
25
    /**
26
     * SumValueMerger constructor.
27
     *
28
     * @param ValueMergerInterface|null $backup
29
     */
30 2
    public function __construct(ValueMergerInterface $backup = null)
31
    {
32 2
        $this->backup = $backup ?: new LastValue();
33 2
    }
34
35
    /**
36
     * Always take the last result
37
     *
38
     * @param mixed $value1
39
     * @param mixed $value2
40
     *
41
     * @return mixed
42
     */
43 2
    public function merge($value1, $value2)
44
    {
45 2
        if (is_numeric($value1) && is_numeric($value2)) {
46 2
            return $value1 + $value2;
47
        }
48 2
        return $this->backup->merge($value1, $value2);
49
    }
50
}
51