MethodAddedTest::interfacesToBeTested()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 87
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 34
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 87
rs 9.376

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\BackwardCompatibility\DetectChanges\BCBreak\InterfaceBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\BackwardCompatibility\Change;
9
use Roave\BackwardCompatibility\DetectChanges\BCBreak\InterfaceBased\MethodAdded;
10
use Roave\BetterReflection\BetterReflection;
11
use Roave\BetterReflection\Reflection\ReflectionClass;
12
use Roave\BetterReflection\Reflector\ClassReflector;
13
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
14
use RoaveTest\BackwardCompatibility\TypeRestriction;
15
use function array_combine;
16
use function array_keys;
17
use function array_map;
18
use function iterator_to_array;
19
20
final class MethodAddedTest extends TestCase
21
{
22
    /**
23
     * @param string[] $expectedMessages
24
     *
25
     * @dataProvider interfacesToBeTested
26
     */
27
    public function testDiffs(
28
        ReflectionClass $fromInterface,
29
        ReflectionClass $toInterface,
30
        array $expectedMessages
31
    ) : void {
32
        $changes = (new MethodAdded())
33
            ->__invoke($fromInterface, $toInterface);
34
35
        self::assertSame(
36
            $expectedMessages,
37
            array_map(static function (Change $change) : string {
38
                return $change->__toString();
39
            }, iterator_to_array($changes))
40
        );
41
    }
42
43
    /**
44
     * @return array<string, array<int, ReflectionClass|array<int, string>>>
45
     *
46
     * @psalm-return array<string, array{0: ReflectionClass, 1: ReflectionClass, 2: list<string>}>
47
     */
48
    public function interfacesToBeTested() : array
49
    {
50
        $astLocator = (new BetterReflection())->astLocator();
51
52
        $fromLocator = new StringSourceLocator(
53
            <<<'PHP'
54
<?php
55
56
interface A {}
57
interface B {
58
    function removed() {}
59
}
60
interface C {
61
    function kept() {}
62
}
63
interface D {
64
    function casingChanged() {}
65
}
66
interface E {}
67
interface F {
68
    public function a() {}
69
    public function c() {}
70
}
71
PHP
72
            ,
73
            $astLocator
74
        );
75
76
        $toLocator = new StringSourceLocator(
77
            <<<'PHP'
78
<?php
79
80
interface A {
81
    function added() {}
82
}
83
interface B {}
84
interface C {
85
    function kept() {}
86
}
87
interface D {
88
    function casingchanged() {}
89
}
90
interface E {
91
    function added1() {}
92
    function added2() {}
93
    function ADDED3() {}
94
}
95
interface F {
96
    public function a() {}
97
    public function b() {}
98
    public function c() {}
99
}
100
PHP
101
            ,
102
            $astLocator
103
        );
104
105
        $fromClassReflector = new ClassReflector($fromLocator);
106
        $toClassReflector   = new ClassReflector($toLocator);
107
108
        $properties = [
109
            'A' => ['[BC] ADDED: Method added() was added to interface A'],
110
            'B' => [],
111
            'C' => [],
112
            'D' => [],
113
            'E' => [
114
                '[BC] ADDED: Method added1() was added to interface E',
115
                '[BC] ADDED: Method added2() was added to interface E',
116
                '[BC] ADDED: Method ADDED3() was added to interface E',
117
            ],
118
            'F' => ['[BC] ADDED: Method b() was added to interface F'],
119
        ];
120
121
        return TypeRestriction::array(array_combine(
122
            array_keys($properties),
123
            array_map(
124
                /** @psalm-param list<string> $errorMessages https://github.com/vimeo/psalm/issues/2772 */
125
                static function (string $className, array $errorMessages) use ($fromClassReflector, $toClassReflector
126
                ) : array {
127
                    return [
128
                        $fromClassReflector->reflect($className),
129
                        $toClassReflector->reflect($className),
130
                        $errorMessages,
131
                    ];
132
                },
133
                array_keys($properties),
134
                $properties
135
            )
136
        ));
137
    }
138
}
139