Completed
Pull Request — master (#30)
by Marco
01:47
created

MethodRemoved::compare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 9
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\ApiCompare\Comparator\BackwardsCompatibility\ClassBased;
6
7
use Assert\Assert;
8
use Roave\ApiCompare\Change;
9
use Roave\ApiCompare\Changes;
10
use Roave\BetterReflection\Reflection\ReflectionClass;
11
use Roave\BetterReflection\Reflection\ReflectionMethod;
12
13
final class MethodRemoved implements ClassBased
14
{
15
    public function compare(ReflectionClass $fromClass, ReflectionClass $toClass) : Changes
16
    {
17
        Assert::that($fromClass->getName())->same($toClass->getName());
18
19
        $removedMethods = array_diff_key(
20
            array_change_key_case($this->accessibleMethods($fromClass), \CASE_UPPER),
21
            array_change_key_case($this->accessibleMethods($toClass), \CASE_UPPER)
22
        );
23
24
        return Changes::fromArray(array_values(array_map(function (ReflectionMethod $method) use ($fromClass) : Change {
25
            return Change::removed(
26
                sprintf('Method %s#%s() was removed', $fromClass->getName(), $method->getName()),
27
                true
28
            );
29
        }, $removedMethods)));
30
    }
31
32
    /** @return ReflectionMethod[] */
33
    private function accessibleMethods(ReflectionClass $class) : array
34
    {
35
        $methods = array_filter($class->getMethods(), function (ReflectionMethod $method) : bool {
36
            return $method->isPublic() || $method->isProtected();
37
        });
38
39
        return array_combine(
40
            array_map(function (ReflectionMethod $method) : string {
41
                return $method->getName();
42
            }, $methods),
43
            $methods
44
        );
45
    }
46
}