Completed
Pull Request — master (#38)
by Marco
03:32
created

MethodChanged   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 40
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\ApiCompare\Comparator\BackwardsCompatibility\PropertyBased\PropertyBased;
10
use Roave\BetterReflection\Reflection\ReflectionClass;
11
use Roave\BetterReflection\Reflection\ReflectionMethod;
12
13
final class MethodChanged implements ClassBased
14
{
15
    /**
16
     * @var PropertyBased
17
     */
18
    private $checkMethod;
19
20
    public function __construct(MethodBased $checkMethod)
21
    {
22
        $this->checkMethod = $checkMethod;
0 ignored issues
show
Documentation Bug introduced by
It seems like $checkMethod of type Roave\ApiCompare\Compara...MethodBased\MethodBased is incompatible with the declared type Roave\ApiCompare\Compara...ertyBased\PropertyBased of property $checkMethod.

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...
23
    }
24
25
    public function compare(ReflectionClass $fromClass, ReflectionClass $toClass) : Changes
26
    {
27
        $methodsFrom   = $this->methods($fromClass);
28
        $methodsTo     = $this->methods($toClass);
29
        $commonMethods = array_intersect_key($methodsFrom, $methodsTo);
30
31
        return array_reduce(
32
            array_keys($commonMethods),
33
            function (Changes $accumulator, string $methodName) use ($methodsFrom, $methodsTo) : Changes {
34
                return $accumulator->mergeWith($this->checkMethod->compare(
35
                    $methodsFrom[$methodName],
36
                    $methodsTo[$methodName])
37
                );
38
            },
39
            Changes::new()
40
        );
41
    }
42
43
    /** @return ReflectionMethod[] indexed by lower case method name */
44
    private function methods(ReflectionClass $class) : array
45
    {
46
        $methods = $class->getMethods();
47
48
        return array_combine(
49
            array_map(function (ReflectionMethod $method) : string {
50
                return strtolower($method->getName());
51
            }, $methods),
52
            $methods
53
        );
54
    }
55
}
56