Passed
Pull Request — master (#38)
by Marco
02:40
created

ConstantChanged::compare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\ApiCompare\Comparator\BackwardsCompatibility\ClassBased;
6
7
use Roave\ApiCompare\Changes;
8
use Roave\ApiCompare\Comparator\BackwardsCompatibility\ClassConstantBased\ClassConstantBased;
9
use Roave\BetterReflection\Reflection\ReflectionClass;
10
use function array_intersect_key;
11
use function array_keys;
12
use function array_reduce;
13
14
final class ConstantChanged implements ClassBased
15
{
16
    /** @var ClassConstantBased */
17
    private $checkConstant;
18
19
    public function __construct(ClassConstantBased $checkConstant)
20
    {
21
        $this->checkConstant = $checkConstant;
22
    }
23
24
    public function compare(ReflectionClass $fromClass, ReflectionClass $toClass) : Changes
25
    {
26
        $constantsFrom   = $fromClass->getReflectionConstants();
27
        $constantsTo     = $toClass->getReflectionConstants();
28
        $commonConstants = array_intersect_key($constantsFrom, $constantsTo);
29
30
        return array_reduce(
31
            array_keys($commonConstants),
32
            function (Changes $accumulator, string $constantName) use ($constantsFrom, $constantsTo) : Changes {
33
                return $accumulator->mergeWith($this->checkConstant->compare(
34
                    $constantsFrom[$constantName],
35
                    $constantsTo[$constantName]
36
                ));
37
            },
38
            Changes::new()
39
        );
40
    }
41
}
42