Passed
Pull Request — master (#38)
by Marco
03:01
created

MethodScopeChanged   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 30
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A compare() 0 22 3
A methodScope() 0 3 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\ApiCompare\Comparator\BackwardsCompatibility\MethodBased;
6
7
use Roave\ApiCompare\Change;
8
use Roave\ApiCompare\Changes;
9
use Roave\BetterReflection\Reflection\ReflectionMethod;
10
use function sprintf;
11
12
/**
13
 * A method that changes from instance to static or the opposite has to be called differently,
14
 * so any of such changes are to be considered BC breaks
15
 */
16
final class MethodScopeChanged implements MethodBased
17
{
18
    public function compare(ReflectionMethod $fromMethod, ReflectionMethod $toMethod) : Changes
19
    {
20
        if ($fromMethod->isPrivate()) {
21
            return Changes::new();
22
        }
23
24
        $scopeFrom = $this->methodScope($fromMethod);
25
        $scopeTo   = $this->methodScope($toMethod);
26
27
        if ($scopeFrom === $scopeTo) {
28
            return Changes::new();
29
        }
30
31
        return Changes::fromArray([Change::changed(
32
            sprintf(
33
                'Method %s() of class %s changed scope from %s to %s',
34
                $fromMethod->getName(),
35
                $fromMethod->getDeclaringClass()->getName(),
36
                $scopeFrom,
37
                $scopeTo
38
            ),
39
            true
40
        )]);
41
    }
42
43
    private function methodScope(ReflectionMethod $method) : string
44
    {
45
        return $method->isStatic() ? 'static' : 'instance';
46
    }
47
}
48