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

functionsToBeTested()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 103
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 44
nc 1
nop 0
dl 0
loc 103
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\RequiredParameterAmountIncreased;
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\RequiredParameterAmountIncreased
20
 */
21
final class RequiredParameterAmountIncreasedTest 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 RequiredParameterAmountIncreased())
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 parametersIncreased($a, $b, $c) {}
55
   function parametersReduced($a, $b, $c) {}
56
   function parameterNamesChanged($a, $b, $c) {}
57
   function optionalParameterAdded($a, $b, $c) {}
58
   function noParametersToOneParameter() {}
59
   function variadicParameterAdded($a, $b) {}
60
   function variadicParameterMoved($a, ...$b) {}
61
   function optionalParameterAddedInBetween($a, $b, $c) {}
62
   function untouched($a, $b, $c) {}
63
}
64
65
namespace N1 {
66
   class C {
67
       static function changed1($a, $b, $c) {}
68
       function changed2($a, $b, $c) {}
69
   }
70
}
71
PHP
72
            ,
73
            $astLocator
74
        );
75
76
        $toLocator = new StringSourceLocator(
77
            <<<'PHP'
78
<?php
79
80
namespace {
81
   function parametersIncreased($a, $b, $c, $d) {}
82
   function parametersReduced($a, $b) {}
83
   function parameterNamesChanged($d, $e, $f) {}
84
   function optionalParameterAdded($a, $b, $c, $d = null) {}
85
   function noParametersToOneParameter($a) {}
86
   function variadicParameterAdded($a, $b, ...$c) {}
87
   function variadicParameterMoved($a, $b, ...$b) {}
88
   function optionalParameterAddedInBetween($a, $b = null, $c, $d) {}
89
   function untouched($a, $b, $c) {}
90
}
91
92
namespace N1 {
93
   class C {
94
       static function changed1($a, $b, $c, $d) {}
95
       function changed2($a, $b, $c, $d) {}
96
   }
97
}
98
PHP
99
            ,
100
            $astLocator
101
        );
102
103
        $fromClassReflector = new ClassReflector($fromLocator);
104
        $toClassReflector   = new ClassReflector($toLocator);
105
        $fromReflector      = new FunctionReflector($fromLocator, $fromClassReflector);
106
        $toReflector        = new FunctionReflector($toLocator, $toClassReflector);
107
108
        $functions = [
109
            'parametersIncreased'             => ['[BC] CHANGED: The number of required arguments for parametersIncreased() increased from 3 to 4'],
110
            'parametersReduced'               => [],
111
            'parameterNamesChanged'           => [],
112
            'optionalParameterAdded'          => [],
113
            'noParametersToOneParameter'      => ['[BC] CHANGED: The number of required arguments for noParametersToOneParameter() increased from 0 to 1'],
114
            'variadicParameterAdded'          => [],
115
            'variadicParameterMoved'          => ['[BC] CHANGED: The number of required arguments for variadicParameterMoved() increased from 1 to 2'],
116
            'optionalParameterAddedInBetween' => ['[BC] CHANGED: The number of required arguments for optionalParameterAddedInBetween() increased from 3 to 4'],
117
            'untouched'                       => [],
118
        ];
119
120
        return array_merge(
121
            array_combine(
122
                array_keys($functions),
123
                array_map(
124
                    function (string $function, array $errorMessages) use ($fromReflector, $toReflector) : array {
125
                        return [
126
                            $fromReflector->reflect($function),
127
                            $toReflector->reflect($function),
128
                            $errorMessages,
129
                        ];
130
                    },
131
                    array_keys($functions),
132
                    $functions
133
                )
134
            ),
135
            [
136
                'N1\C::changed1' => [
137
                    $fromClassReflector->reflect('N1\C')->getMethod('changed1'),
138
                    $toClassReflector->reflect('N1\C')->getMethod('changed1'),
139
                    [
140
                        '[BC] CHANGED: The number of required arguments for N1\C::changed1() increased from 3 to 4',
141
                    ],
142
                ],
143
                'N1\C#changed2'  => [
144
                    $fromClassReflector->reflect('N1\C')->getMethod('changed2'),
145
                    $toClassReflector->reflect('N1\C')->getMethod('changed2'),
146
                    [
147
                        '[BC] CHANGED: The number of required arguments for N1\C#changed2() increased from 3 to 4',
148
                    ],
149
                ],
150
            ]
151
        );
152
    }
153
}
154