MethodScopeChanged   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 26
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A methodScope() 0 3 2
A __invoke() 0 18 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\BackwardCompatibility\DetectChanges\BCBreak\MethodBased;
6
7
use Roave\BackwardCompatibility\Change;
8
use Roave\BackwardCompatibility\Changes;
9
use Roave\BetterReflection\Reflection\ReflectionMethod;
10
use function Safe\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 __invoke(ReflectionMethod $fromMethod, ReflectionMethod $toMethod) : Changes
19
    {
20
        $scopeFrom = $this->methodScope($fromMethod);
21
        $scopeTo   = $this->methodScope($toMethod);
22
23
        if ($scopeFrom === $scopeTo) {
24
            return Changes::empty();
25
        }
26
27
        return Changes::fromList(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
    private function methodScope(ReflectionMethod $method) : string
40
    {
41
        return $method->isStatic() ? 'static' : 'instance';
42
    }
43
}
44