Passed
Pull Request — master (#50)
by Marco
02:36
created

PropertyScopeChanged   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A scopeAsString() 0 3 2
A __invoke() 0 18 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\BackwardCompatibility\DetectChanges\BCBreak\PropertyBased;
6
7
use Roave\BackwardCompatibility\Change;
8
use Roave\BackwardCompatibility\Changes;
9
use Roave\BetterReflection\Reflection\ReflectionProperty;
10
use function sprintf;
11
12
/**
13
 * A property that changes from instance to static or the opposite has to be accessed differently,
14
 * so any of such changes are to be considered BC breaks
15
 */
16
final class PropertyScopeChanged implements PropertyBased
17
{
18
    public function __invoke(ReflectionProperty $fromProperty, ReflectionProperty $toProperty) : Changes
19
    {
20
        $fromScope = $this->scopeAsString($fromProperty);
21
        $toScope   = $this->scopeAsString($toProperty);
22
23
        if ($fromScope === $toScope) {
24
            return Changes::empty();
25
        }
26
27
        return Changes::fromList(Change::changed(
28
            sprintf(
29
                'Property $%s of %s changed scope from %s to %s',
30
                $fromProperty->getName(),
31
                $fromProperty->getDeclaringClass()->getName(),
32
                $fromScope,
33
                $toScope
34
            ),
35
            true
36
        ));
37
    }
38
39
    private function scopeAsString(ReflectionProperty $property) : string
40
    {
41
        return $property->isStatic() ? 'static' : 'instance';
42
    }
43
}
44