Completed
Push — master ( 901cc6...ad6830 )
by James
15s queued 11s
created

MethodChanged   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 38
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A compare() 0 15 1
A methods() 0 9 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\MethodBased\MethodBased;
9
use Roave\BetterReflection\Reflection\ReflectionClass;
10
use Roave\BetterReflection\Reflection\ReflectionMethod;
11
use function array_combine;
12
use function array_intersect_key;
13
use function array_keys;
14
use function array_map;
15
use function array_reduce;
16
use function strtolower;
17
18
final class MethodChanged implements ClassBased
19
{
20
    /** @var MethodBased */
21
    private $checkMethod;
22
23
    public function __construct(MethodBased $checkMethod)
24
    {
25
        $this->checkMethod = $checkMethod;
26
    }
27
28
    public function compare(ReflectionClass $fromClass, ReflectionClass $toClass) : Changes
29
    {
30
        $methodsFrom   = $this->methods($fromClass);
31
        $methodsTo     = $this->methods($toClass);
32
        $commonMethods = array_intersect_key($methodsFrom, $methodsTo);
33
34
        return array_reduce(
35
            array_keys($commonMethods),
36
            function (Changes $accumulator, string $methodName) use ($methodsFrom, $methodsTo) : Changes {
37
                return $accumulator->mergeWith($this->checkMethod->compare(
38
                    $methodsFrom[$methodName],
39
                    $methodsTo[$methodName]
40
                ));
41
            },
42
            Changes::new()
43
        );
44
    }
45
46
    /** @return ReflectionMethod[] indexed by lower case method name */
47
    private function methods(ReflectionClass $class) : array
48
    {
49
        $methods = $class->getMethods();
50
51
        return array_combine(
52
            array_map(function (ReflectionMethod $method) : string {
53
                return strtolower($method->getName());
54
            }, $methods),
55
            $methods
56
        );
57
    }
58
}
59