Passed
Pull Request — master (#1)
by James
01:31
created

Comparator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
C compare() 0 46 8
1
<?php
2
declare(strict_types=1);
3
4
namespace Roave\ApiCompare;
5
6
use Roave\BetterReflection\Reflector\ClassReflector;
7
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
8
9
final class Comparator
10
{
11
    public function compare(ClassReflector $oldApi, ClassReflector $newApi): array
12
    {
13
        $changelog = [];
14
15
        foreach ($oldApi->getAllClasses() as $oldClass) {
16
            try {
17
                $newClass = $newApi->reflect($oldClass->getName());
18
            } catch (IdentifierNotFound $exception) {
19
                $changelog[] = sprintf('[BC] Class %s has been deleted', $oldClass->getName());
20
                continue;
21
            }
22
23
            foreach ($oldClass->getMethods() as $oldMethod) {
24
                // @todo ignore private methods
25
                try {
26
                    $newMethod = $newClass->getMethod($oldMethod->getName());
0 ignored issues
show
Bug introduced by
The method getMethod() does not exist on Roave\BetterReflection\Reflection\Reflection. It seems like you code against a sub-type of Roave\BetterReflection\Reflection\Reflection such as Roave\BetterReflection\Reflection\ReflectionClass. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
                    /** @scrutinizer ignore-call */ 
27
                    $newMethod = $newClass->getMethod($oldMethod->getName());
Loading history...
27
                } catch (\OutOfBoundsException $exception) {
28
                    $changelog[] = sprintf(
29
                        '[BC] Method %s in class %s has been deleted',
30
                        $oldMethod->getName(),
31
                        $oldClass->getName()
32
                    );
33
                    continue;
34
                }
35
36
                foreach ($oldMethod->getParameters() as $oldParameter) {
37
                    $newParameter = $newMethod->getParameter($oldParameter->getName());
38
39
                    if (null === $newParameter) {
40
                        $changelog[] = sprintf(
41
                            '[BC] Parameter %s in %s%s%s has been deleted',
42
                            $oldParameter->getName(),
43
                            $oldClass->getName(),
44
                            $oldMethod->isStatic() ? '#' : '::',
45
                            $oldMethod->getName()
46
                        );
47
                        continue;
48
                    }
49
50
                    // @todo check if types changed, or becoming default
51
                    // @todo check if a new param (without a default) was added
52
                }
53
            }
54
        }
55
56
        return $changelog;
57
    }
58
}
59