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

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