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

ConstantVisibilityReduced::compare()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\ApiCompare\Comparator\BackwardsCompatibility\ClassConstantBased;
6
7
use Roave\ApiCompare\Change;
8
use Roave\ApiCompare\Changes;
9
use Roave\BetterReflection\Reflection\ReflectionClassConstant;
10
11
final class ConstantVisibilityReduced implements ConstantBased
12
{
13
    private const VISIBILITY_PRIVATE = 'private';
14
15
    private const VISIBILITY_PROTECTED = 'protected';
16
17
    private const VISIBILITY_PUBLIC = 'public';
18
19
    public function compare(ReflectionClassConstant $fromConstant, ReflectionClassConstant $toConstant) : Changes
20
    {
21
        $visibilityFrom = $this->propertyVisibility($fromConstant);
22
        $visibilityTo   = $this->propertyVisibility($toConstant);
23
24
        // Works because private, protected and public are sortable:
25
        if ($visibilityFrom <= $visibilityTo) {
26
            return Changes::new();
27
        }
28
29
        return Changes::fromArray([Change::changed(
30
            sprintf(
31
                'Constant %s::%s visibility reduced from %s to %s',
32
                $fromConstant->getDeclaringClass()->getName(),
33
                $fromConstant->getName(),
34
                $visibilityFrom,
35
                $visibilityTo
36
            ),
37
            true
38
        )]);
39
    }
40
41
    private function propertyVisibility(ReflectionClassConstant $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