Passed
Pull Request — master (#38)
by Marco
02:17
created

PropertyVisibilityReduced   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A propertyVisibility() 0 11 3
A compare() 0 18 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
final class PropertyVisibilityReduced implements PropertyBased
13
{
14
    private const VISIBILITY_PRIVATE = 'private';
15
16
    private const VISIBILITY_PROTECTED = 'protected';
17
18
    private const VISIBILITY_PUBLIC = 'public';
19
20
    public function compare(ReflectionProperty $fromProperty, ReflectionProperty $toProperty) : Changes
21
    {
22
        $visibilityFrom = $this->propertyVisibility($fromProperty);
23
        $visibilityTo   = $this->propertyVisibility($toProperty);
24
25
        if ($visibilityFrom <= $visibilityTo) {
26
            return Changes::new();
27
        }
28
29
        return Changes::fromArray([Change::changed(
30
            sprintf(
31
                'Property %s#$%s visibility reduced from %s to %s',
32
                $fromProperty->getDeclaringClass()->getName(),
33
                $fromProperty->getName(),
34
                $visibilityFrom,
35
                $visibilityTo
36
            ),
37
            true
38
        )]);
39
    }
40
41
    private function propertyVisibility(ReflectionProperty $property) : string
42
    {
43
        if ($property->isPublic()) {
44
            return self::VISIBILITY_PUBLIC;
45
        }
46
47
        if ($property->isProtected()) {
48
            return self::VISIBILITY_PROTECTED;
49
        }
50
51
        return self::VISIBILITY_PRIVATE;
52
    }
53
}
54