Completed
Push — master ( 901cc6...ad6830 )
by James
15s queued 11s
created

PropertyScopeChanged   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A scopeAsString() 0 3 2
A compare() 0 19 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
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 compare(ReflectionProperty $fromProperty, ReflectionProperty $toProperty) : Changes
19
    {
20
        $fromScope = $this->scopeAsString($fromProperty);
21
        $toScope   = $this->scopeAsString($toProperty);
22
23
        if ($fromScope === $toScope) {
24
            return Changes::new();
25
        }
26
27
        return Changes::fromArray([
28
            Change::changed(
29
                sprintf(
30
                    'Property $%s of %s changed scope from %s to %s',
31
                    $fromProperty->getName(),
32
                    $fromProperty->getDeclaringClass()->getName(),
33
                    $fromScope,
34
                    $toScope
35
                ),
36
                true
37
            ),
38
        ]);
39
    }
40
41
    private function scopeAsString(ReflectionProperty $property) : string
42
    {
43
        return $property->isStatic() ? 'static' : 'instance';
44
    }
45
}
46