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

ConstantChanged   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
dl 0
loc 25
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A compare() 0 15 1
A __construct() 0 3 1
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