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

MethodVisibilityReduced::methodVisibility()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Roave\ApiCompare\Comparator\BackwardsCompatibility\MethodBased;
6
7
use Roave\ApiCompare\Change;
8
use Roave\ApiCompare\Changes;
9
use Roave\BetterReflection\Reflection\ReflectionClassConstant;
10
use Roave\BetterReflection\Reflection\ReflectionMethod;
11
12
final class MethodVisibilityReduced implements MethodBased
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(ReflectionMethod $fromMethod, ReflectionMethod $toMethod) : Changes
21
    {
22
        $visibilityFrom = $this->methodVisibility($fromMethod);
23
        $visibilityTo   = $this->methodVisibility($toMethod);
24
25
        if ($visibilityFrom <= $visibilityTo) {
26
            return Changes::new();
27
        }
28
29
        return Changes::fromArray([Change::changed(
30
            sprintf(
31
                'Method %s() of class %s visibility reduced from %s to %s',
32
                $fromMethod->getName(),
33
                $fromMethod->getDeclaringClass()->getName(),
34
                $visibilityFrom,
35
                $visibilityTo
36
            ),
37
            true
38
        )]);
39
    }
40
41
    private function methodVisibility(ReflectionMethod $method) : string
42
    {
43
        if ($method->isPublic()) {
44
            return self::VISIBILITY_PUBLIC;
45
        }
46
47
        if ($method->isProtected()) {
48
            return self::VISIBILITY_PROTECTED;
49
        }
50
51
        return self::VISIBILITY_PRIVATE;
52
    }
53
}
54