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

propertiesToBeTested()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 80
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 8.8387
c 0
b 0
f 0
cc 1
eloc 38
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace RoaveTest\ApiCompare\Comparator\BackwardsCompatibility\MethodBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\ApiCompare\Change;
9
use Roave\ApiCompare\Comparator\BackwardsCompatibility\MethodBased\MethodVisibilityReduced;
10
use Roave\BetterReflection\BetterReflection;
11
use Roave\BetterReflection\Reflection\ReflectionMethod;
12
use Roave\BetterReflection\Reflector\ClassReflector;
13
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
14
use function array_map;
15
use function iterator_to_array;
16
17
/**
18
 * @covers \Roave\ApiCompare\Comparator\BackwardsCompatibility\MethodBased\MethodVisibilityReduced
19
 */
20
final class MethodVisibilityReducedTest extends TestCase
21
{
22
    /**
23
     * @dataProvider propertiesToBeTested
24
     *
25
     * @param string[] $expectedMessages
26
     */
27
    public function testDiffs(
28
        ReflectionMethod $fromMethod,
29
        ReflectionMethod $toMethod,
30
        array $expectedMessages
31
    ) : void {
32
        $changes = (new MethodVisibilityReduced())
33
            ->compare($fromMethod, $toMethod);
34
35
        self::assertSame(
36
            $expectedMessages,
37
            array_map(function (Change $change) : string {
38
                return $change->__toString();
39
            }, iterator_to_array($changes))
40
        );
41
    }
42
43
    /** @return (string[]|ReflectionMethod)[][] */
44
    public function propertiesToBeTested() : array
45
    {
46
        $astLocator = (new BetterReflection())->astLocator();
47
48
        $fromLocator = new StringSourceLocator(
49
            <<<'PHP'
50
<?php
51
52
class TheClass {
53
    public function publicMaintainedPublic() {}
54
    public function publicReducedToProtected() {}
55
    public function publicReducedToPrivate() {}
56
    protected function protectedMaintainedProtected() {}
57
    protected function protectedReducedToPrivate() {}
58
    protected function protectedIncreasedToPublic() {}
59
    private function privateMaintainedPrivate() {}
60
    private function privateIncreasedToProtected() {}
61
    private function privateIncreasedToPublic() {}
62
}
63
PHP
64
            ,
65
            $astLocator
66
        );
67
68
        $toLocator = new StringSourceLocator(
69
            <<<'PHP'
70
<?php
71
72
class TheClass {
73
    public function publicMaintainedPublic() {}
74
    protected function publicReducedToProtected() {}
75
    private function publicReducedToPrivate() {}
76
    protected function protectedMaintainedProtected() {}
77
    private function protectedReducedToPrivate() {}
78
    public function protectedIncreasedToPublic() {}
79
    private function privateMaintainedPrivate() {}
80
    protected function privateIncreasedToProtected() {}
81
    public function privateIncreasedToPublic() {}
82
}
83
PHP
84
            ,
85
            $astLocator
86
        );
87
88
        $fromClassReflector = new ClassReflector($fromLocator);
89
        $toClassReflector   = new ClassReflector($toLocator);
90
        $fromClass          = $fromClassReflector->reflect('TheClass');
91
        $toClass            = $toClassReflector->reflect('TheClass');
92
93
        $properties = [
94
95
            'publicMaintainedPublic'       => [],
96
            'publicReducedToProtected'     => [
97
                '[BC] CHANGED: Method publicReducedToProtected() of class TheClass visibility reduced from public to protected',
98
            ],
99
            'publicReducedToPrivate'       => [
100
                '[BC] CHANGED: Method publicReducedToPrivate() of class TheClass visibility reduced from public to private',
101
            ],
102
            'protectedMaintainedProtected' => [],
103
            'protectedReducedToPrivate'    => [
104
                '[BC] CHANGED: Method protectedReducedToPrivate() of class TheClass visibility reduced from protected to private',
105
            ],
106
            'protectedIncreasedToPublic'   => [],
107
            'privateMaintainedPrivate'     => [],
108
            'privateIncreasedToProtected'  => [],
109
            'privateIncreasedToPublic'     => [],
110
        ];
111
112
        return array_combine(
113
            array_keys($properties),
114
            array_map(
115
                function (string $method, array $errorMessages) use ($fromClass, $toClass) : array {
116
                    return [
117
                        $fromClass->getMethod($method),
118
                        $toClass->getMethod($method),
119
                        $errorMessages,
120
                    ];
121
                },
122
                array_keys($properties),
123
                $properties
124
            )
125
        );
126
    }
127
}
128