ParameterByReferenceChangedTest::testDiffs()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 3
dl 0
loc 13
rs 10
c 0
b 0
f 0
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_combine;
16
use function array_map;
17
use function iterator_to_array;
18
19
/**
20
 * @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\FunctionBased\ParameterByReferenceChanged
21
 */
22
final class ParameterByReferenceChangedTest 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 ParameterByReferenceChanged())
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
    /**
46
     * @return array<string, array<int, ReflectionFunctionAbstract|array<int, string>>>
47
     *
48
     * @psalm-return array<string, array{0: ReflectionFunctionAbstract, 1: ReflectionFunctionAbstract, 2: list<string>}>
49
     */
50
    public function functionsToBeTested() : array
51
    {
52
        $astLocator = (new BetterReflection())->astLocator();
53
54
        $fromLocator = new StringSourceLocator(
55
            <<<'PHP'
56
<?php
57
58
namespace {
59
    function valueToReference($a) {}
60
    function referenceToValue(& $a) {}
61
    function valueToValue($a) {}
62
    function referenceToReference(& $a) {}
63
    function referenceOnRemovedParameter($a, & $b) {}
64
    function referenceToValueOnRenamedParameter(& $a, & $b) {}
65
    function addedParameter(& $a, & $b) {}
66
}
67
namespace N1 {
68
    class C {
69
        static function changed1($a) {}
70
        function changed2($a) {}
71
    }
72
}
73
PHP
74
            ,
75
            $astLocator
76
        );
77
78
        $toLocator = new StringSourceLocator(
79
            <<<'PHP'
80
<?php
81
82
namespace {
83
    function valueToReference(& $a) {}
84
    function referenceToValue($a) {}
85
    function valueToValue($a) {}
86
    function referenceToReference(& $a) {}
87
    function referenceOnRemovedParameter($a) {}
88
    function referenceToValueOnRenamedParameter(& $b, $a) {}
89
    function addedParameter(& $a, & $b, $c) {}
90
}
91
namespace N1 {
92
    class C {
93
        static function changed1(& $a) {}
94
        function changed2(& $a) {}
95
    }
96
}
97
PHP
98
            ,
99
            $astLocator
100
        );
101
102
        $fromClassReflector = new ClassReflector($fromLocator);
103
        $toClassReflector   = new ClassReflector($toLocator);
104
        $fromReflector      = new FunctionReflector($fromLocator, $fromClassReflector);
105
        $toReflector        = new FunctionReflector($toLocator, $toClassReflector);
106
107
        $functions = [
108
            'valueToReference'                   => [
109
                '[BC] CHANGED: The parameter $a of valueToReference() changed from by-value to by-reference',
110
            ],
111
            'referenceToValue'                   => [
112
                '[BC] CHANGED: The parameter $a of referenceToValue() changed from by-reference to by-value',
113
            ],
114
            'valueToValue'                       => [],
115
            'referenceToReference'               => [],
116
            'referenceOnRemovedParameter'        => [],
117
            'referenceToValueOnRenamedParameter' => [
118
                '[BC] CHANGED: The parameter $b of referenceToValueOnRenamedParameter() changed from by-reference to by-value',
119
            ],
120
            'addedParameter'                     => [],
121
        ];
122
123
        return array_merge(
124
            array_combine(
0 ignored issues
show
Bug introduced by
It seems like array_combine(array_keys...unctions), $functions)) can also be of type false; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

124
            /** @scrutinizer ignore-type */ array_combine(
Loading history...
125
                array_keys($functions),
126
                array_map(
127
                    /** @psalm-param list<string> $errorMessages https://github.com/vimeo/psalm/issues/2772 */
128
                    static function (string $function, array $errorMessages) use ($fromReflector, $toReflector) : array {
129
                        return [
130
                            $fromReflector->reflect($function),
131
                            $toReflector->reflect($function),
132
                            $errorMessages,
133
                        ];
134
                    },
135
                    array_keys($functions),
136
                    $functions
137
                )
138
            ),
139
            [
140
                'N1\C::changed1' => [
141
                    $fromClassReflector->reflect('N1\C')->getMethod('changed1'),
142
                    $toClassReflector->reflect('N1\C')->getMethod('changed1'),
143
                    [
144
                        '[BC] CHANGED: The parameter $a of N1\C::changed1() changed from by-value to by-reference',
145
146
                    ],
147
                ],
148
                'N1\C#changed2'  => [
149
                    $fromClassReflector->reflect('N1\C')->getMethod('changed2'),
150
                    $toClassReflector->reflect('N1\C')->getMethod('changed2'),
151
                    [
152
                        '[BC] CHANGED: The parameter $a of N1\C#changed2() changed from by-value to by-reference',
153
                    ],
154
                ],
155
            ]
156
        );
157
    }
158
}
159