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

PropertyScopeChanged::compare()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 2
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