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

PropertyScopeChanged   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

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

2 Methods

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