Completed
Pull Request — master (#38)
by Marco
03:08
created

PropertyScopeChanged::scopeAsString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
eloc 1
nc 2
nop 1
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
        $fromScope = $this->scopeAsString($fromProperty);
20
        $toScope   = $this->scopeAsString($toProperty);
21
22
        if ($fromScope === $toScope) {
23
            return Changes::new();
24
        }
25
26
        return Changes::fromArray([
27
            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
40
    private function scopeAsString(ReflectionProperty $property) : string
41
    {
42
        return $property->isStatic() ? 'static' : 'instance';
43
    }
44
}
45