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

functionsToBeTested()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 102
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

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