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

MethodVisibilityReduced   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 methodVisibility() 0 11 3
A compare() 0 18 2
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