Passed
Pull Request — master (#50)
by Marco
02:46
created

functionsToBeTested()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 137
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 52
nc 1
nop 0
dl 0
loc 137
rs 8.2857
c 0
b 0
f 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\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\BackwardCompatibility\Change;
9
use Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ParameterTypeContravarianceChanged;
10
use Roave\BackwardCompatibility\DetectChanges\Variance\TypeIsContravariant;
11
use Roave\BetterReflection\BetterReflection;
12
use Roave\BetterReflection\Reflection\ReflectionFunctionAbstract;
13
use Roave\BetterReflection\Reflector\ClassReflector;
14
use Roave\BetterReflection\Reflector\FunctionReflector;
15
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
16
use function array_map;
17
use function iterator_to_array;
18
19
/**
20
 * @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ParameterTypeContravarianceChanged
21
 */
22
final class ParameterTypeContravarianceChangedTest extends TestCase
23
{
24
    /**
25
     * @dataProvider functionsToBeTested
26
     *
27
     * @param string[] $expectedMessages
28
     */
29
    public function testDiffs(
30
        ReflectionFunctionAbstract $fromFunction,
31
        ReflectionFunctionAbstract $toFunction,
32
        array $expectedMessages
33
    ) : void {
34
        $changes = (new ParameterTypeContravarianceChanged(new TypeIsContravariant()))
35
            ->__invoke($fromFunction, $toFunction);
36
37
        self::assertSame(
38
            $expectedMessages,
39
            array_map(function (Change $change) : string {
40
                return $change->__toString();
41
            }, iterator_to_array($changes))
42
        );
43
    }
44
45
    /** @return (string[]|ReflectionFunctionAbstract)[][] */
46
    public function functionsToBeTested() : array
47
    {
48
        $astLocator = (new BetterReflection())->astLocator();
49
50
        $fromLocator = new StringSourceLocator(
51
            <<<'PHP'
52
<?php
53
54
namespace {
55
   function changed(int $a, int $b) {}
56
   function untouched(int $a, int $b) {}
57
}
58
59
namespace N1 {
60
   class A {}
61
   function changed(A $a, A $b) {}
62
   function untouched(A $a, A $b) {}
63
}
64
65
namespace N2 {
66
   class A {}
67
   function changed(A $a, A $b) {}
68
   function untouched(A $a, A $b) {}
69
}
70
71
namespace N3 {
72
   function changed(?int $a, ?int $b) {}
73
   function untouched(?int $a, ?int $b) {}
74
}
75
76
namespace N4 {
77
   class C {
78
       static function changed1($a, $b) {}
79
       function changed2($a, $b) {}
80
   }
81
}
82
PHP
83
            ,
84
            $astLocator
85
        );
86
87
        $toLocator = new StringSourceLocator(
88
            <<<'PHP'
89
<?php
90
91
namespace {
92
   function changed(float $a, float $b) {}
93
   function untouched($a, $b) {}
94
}
95
96
namespace N1 {
97
   class A {}
98
   function changed(\N2\A $a, \N2\A $b) {}
99
   function untouched(A $a, A $b) {}
100
}
101
102
namespace N2 {
103
   class A {}
104
   function changed(\N3\A $b) {}
105
   function untouched(A $a, A $b) {}
106
}
107
108
namespace N3 {
109
   class A {}
110
   function changed(int $d, int $e, int $f) {}
111
   function untouched(?int $a, ?int $b) {}
112
}
113
114
namespace N4 {
115
   class C {
116
       static function changed1(int $a, int $b) {}
117
       function changed2(int $a, int $b) {}
118
   }
119
}
120
PHP
121
            ,
122
            $astLocator
123
        );
124
125
        $fromClassReflector = new ClassReflector($fromLocator);
126
        $toClassReflector   = new ClassReflector($toLocator);
127
        $fromReflector      = new FunctionReflector($fromLocator, $fromClassReflector);
128
        $toReflector        = new FunctionReflector($toLocator, $toClassReflector);
129
130
        $functions = [
131
            'changed'      => [
132
                '[BC] CHANGED: The parameter $a of changed() changed from int to a non-contravariant float',
133
                '[BC] CHANGED: The parameter $b of changed() changed from int to a non-contravariant float',
134
            ],
135
            'untouched'    => [],
136
            'N1\changed'   => [
137
                '[BC] CHANGED: The parameter $a of N1\changed() changed from N1\A to a non-contravariant N2\A',
138
                '[BC] CHANGED: The parameter $b of N1\changed() changed from N1\A to a non-contravariant N2\A',
139
            ],
140
            'N1\untouched' => [],
141
            'N2\changed'   => [
142
                '[BC] CHANGED: The parameter $a of N2\changed() changed from N2\A to a non-contravariant N3\A',
143
            ],
144
            'N2\untouched' => [],
145
            'N3\changed'   => [
146
                '[BC] CHANGED: The parameter $a of N3\changed() changed from ?int to a non-contravariant int',
147
                '[BC] CHANGED: The parameter $b of N3\changed() changed from ?int to a non-contravariant int',
148
            ],
149
            'N3\untouched' => [],
150
        ];
151
152
        return array_merge(
153
            array_combine(
154
                array_keys($functions),
155
                array_map(
156
                    function (string $function, array $errorMessages) use ($fromReflector, $toReflector) : array {
157
                        return [
158
                            $fromReflector->reflect($function),
159
                            $toReflector->reflect($function),
160
                            $errorMessages,
161
                        ];
162
                    },
163
                    array_keys($functions),
164
                    $functions
165
                )
166
            ),
167
            [
168
                'N4\C::changed1' => [
169
                    $fromClassReflector->reflect('N4\C')->getMethod('changed1'),
170
                    $toClassReflector->reflect('N4\C')->getMethod('changed1'),
171
                    [
172
                        '[BC] CHANGED: The parameter $a of N4\C::changed1() changed from no type to a non-contravariant int',
173
                        '[BC] CHANGED: The parameter $b of N4\C::changed1() changed from no type to a non-contravariant int',
174
175
                    ],
176
                ],
177
                'N4\C#changed2'  => [
178
                    $fromClassReflector->reflect('N4\C')->getMethod('changed2'),
179
                    $toClassReflector->reflect('N4\C')->getMethod('changed2'),
180
                    [
181
                        '[BC] CHANGED: The parameter $a of N4\C#changed2() changed from no type to a non-contravariant int',
182
                        '[BC] CHANGED: The parameter $b of N4\C#changed2() changed from no type to a non-contravariant int',
183
                    ]
184
                ],
185
            ]
186
        );
187
    }
188
}
189