Completed
Push — master ( c7f0ae...412313 )
by James
9s
created

Comparator::examineClass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 3
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Roave\ApiCompare;
5
6
use Roave\BetterReflection\Reflection\ReflectionClass;
7
use Roave\BetterReflection\Reflection\ReflectionMethod;
8
use Roave\BetterReflection\Reflection\ReflectionParameter;
9
use Roave\BetterReflection\Reflector\ClassReflector;
10
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
11
12
final class Comparator
13
{
14
    public function compare(ClassReflector $oldApi, ClassReflector $newApi): array
15
    {
16
        $changelog = [];
17
18
        foreach ($oldApi->getAllClasses() as $oldClass) {
19
            $changelog = $this->examineClass($changelog, $oldClass, $newApi);
20
        }
21
22
        return $changelog;
23
    }
24
25
    private function examineClass(array $changelog, ReflectionClass $oldClass, ClassReflector $newApi): array
26
    {
27
        try {
28
            $newClass = $newApi->reflect($oldClass->getName());
29
        } catch (IdentifierNotFound $exception) {
30
            $changelog[] = sprintf('[BC] Class %s has been deleted', $oldClass->getName());
31
            return $changelog;
32
        }
33
34
        foreach ($oldClass->getMethods() as $oldMethod) {
35
            $changelog = $this->examineMethod($changelog, $oldClass, $oldMethod, $newClass);
36
        }
37
38
        return $changelog;
39
    }
40
41
    private function examineMethod(
42
        array $changelog,
43
        ReflectionClass $oldClass,
44
        ReflectionMethod $oldMethod,
45
        ReflectionClass $newClass
46
    ): array {
47
        if ($oldMethod->isPrivate()) {
48
            return $changelog;
49
        }
50
51
        try {
52
            $newMethod = $newClass->getMethod($oldMethod->getName());
53
        } catch (\OutOfBoundsException $exception) {
54
            $changelog[] = sprintf(
55
                '[BC] Method %s in class %s has been deleted',
56
                $oldMethod->getName(),
57
                $oldClass->getName()
58
            );
59
            return $changelog;
60
        }
61
62
        foreach ($oldMethod->getParameters() as $parameterPosition => $oldParameter) {
63
            $changelog = $this->examineParameter(
64
                $changelog,
65
                $parameterPosition,
66
                $oldClass,
67
                $oldMethod,
68
                $oldParameter,
69
                $newMethod
70
            );
71
        }
72
73
        return $changelog;
74
    }
75
76
    private function examineParameter(
77
        array $changelog,
78
        int $parameterPosition,
79
        ReflectionClass $oldClass,
80
        ReflectionMethod $oldMethod,
81
        ReflectionParameter $oldParameter,
82
        ReflectionMethod $newMethod
83
    ): array {
84
        $newParameters = $newMethod->getParameters();
85
        if (!array_key_exists($parameterPosition, $newParameters)) {
86
            $changelog[] = sprintf(
87
                '[BC] Parameter %s (position %d) in %s%s%s has been deleted',
88
                $oldParameter->getName(),
89
                $parameterPosition,
90
                $oldClass->getName(),
91
                $oldMethod->isStatic() ? '#' : '::',
92
                $oldMethod->getName()
93
            );
94
            return $changelog;
95
        }
96
97
        $newParameter = $newParameters[$parameterPosition];
0 ignored issues
show
Unused Code introduced by
The assignment to $newParameter is dead and can be removed.
Loading history...
98
99
        // @todo check if types changed, or becoming default
100
        // @todo check if a new param (without a default) was added
101
102
        return $changelog;
103
    }
104
}
105