Passed
Pull Request — master (#38)
by Marco
02:19
created

MethodScopeChanged::compare()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 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
        $scopeFrom = $this->methodScope($fromMethod);
21
        $scopeTo   = $this->methodScope($toMethod);
22
23
        if ($scopeFrom === $scopeTo) {
24
            return Changes::new();
25
        }
26
27
        return Changes::fromArray([Change::changed(
28
            sprintf(
29
                'Method %s() of class %s changed scope from %s to %s',
30
                $fromMethod->getName(),
31
                $fromMethod->getDeclaringClass()->getName(),
32
                $scopeFrom,
33
                $scopeTo
34
            ),
35
            true
36
        ),
37
        ]);
38
    }
39
40
    private function methodScope(ReflectionMethod $method) : string
41
    {
42
        return $method->isStatic() ? 'static' : 'instance';
43
    }
44
}
45