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

propertiesToBeTested()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 141
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 36
nc 1
nop 0
dl 0
loc 141
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\PropertyBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\BackwardCompatibility\Change;
9
use Roave\BackwardCompatibility\DetectChanges\BCBreak\PropertyBased\PropertyDocumentedTypeChanged;
10
use Roave\BetterReflection\BetterReflection;
11
use Roave\BetterReflection\Reflection\ReflectionProperty;
12
use Roave\BetterReflection\Reflector\ClassReflector;
13
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
14
use function array_combine;
15
use function array_keys;
16
use function array_map;
17
use function iterator_to_array;
18
19
/**
20
 * @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\PropertyBased\PropertyDocumentedTypeChanged
21
 */
22
final class PropertyDocumentedTypeChangedTest extends TestCase
23
{
24
    /**
25
     * @dataProvider propertiesToBeTested
26
     *
27
     * @param string[] $expectedMessages
28
     */
29
    public function testDiffs(
30
        ReflectionProperty $fromProperty,
31
        ReflectionProperty $toProperty,
32
        array $expectedMessages
33
    ) : void {
34
        $changes = (new PropertyDocumentedTypeChanged())
35
            ->__invoke($fromProperty, $toProperty);
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[]|ReflectionProperty)[][] */
46
    public function propertiesToBeTested() : array
47
    {
48
        $astLocator = (new BetterReflection())->astLocator();
49
50
        $fromLocator = new StringSourceLocator(
51
            <<<'PHP'
52
<?php
53
54
class TheClass {
55
    public $publicNoDocblockToNoDocblock;
56
    public $publicNoDocblockToDocblock;
57
    
58
    /**
59
     * Hi
60
     */
61
    public $publicNoTypeDocblockToDocblock;
62
    
63
    /**
64
     * @var int
65
     */
66
    public $publicDocblockToSameDocblock;
67
    
68
    /**
69
     * @var int
70
     */
71
    public $publicDocblockToDifferentDocblock;
72
    
73
    /**
74
     * @var int
75
     */
76
    public $publicDocblockToNoDocblock;
77
    
78
    /**
79
     * @var int|float
80
     */
81
    public $publicCompositeTypeDocblockToSameTypeDocblock;
82
    
83
    /**
84
     * @var int|float
85
     */
86
    public $publicCompositeTypeDocblockToSameTypeDocblockWithDifferentSorting;
87
    
88
    /**
89
     * @var int|float
90
     */
91
    public $publicCompositeTypeDocblockToDifferentCompositeTypeDocblock;
92
    
93
    /**
94
     * @var int
95
     */
96
    private $privateDocblockToDifferentDocblock;
97
}
98
PHP
99
            ,
100
            $astLocator
101
        );
102
103
        $toLocator = new StringSourceLocator(
104
            <<<'PHP'
105
<?php
106
107
class TheClass {
108
    public $publicNoDocblockToNoDocblock;
109
    
110
    /**
111
     * @var int
112
     */
113
    public $publicNoDocblockToDocblock;
114
    
115
    /**
116
     * @var int
117
     */
118
    public $publicNoTypeDocblockToDocblock;
119
    
120
    /**
121
     * @var int
122
     */
123
    public $publicDocblockToSameDocblock;
124
    
125
    /**
126
     * @var float
127
     */
128
    public $publicDocblockToDifferentDocblock;
129
    
130
    public $publicDocblockToNoDocblock;
131
    
132
    /**
133
     * @var int|float
134
     */
135
    public $publicCompositeTypeDocblockToSameTypeDocblock;
136
    
137
    /**
138
     * @var float|int
139
     */
140
    public $publicCompositeTypeDocblockToSameTypeDocblockWithDifferentSorting;
141
    
142
    /**
143
     * @var int|float|string
144
     */
145
    public $publicCompositeTypeDocblockToDifferentCompositeTypeDocblock;
146
    
147
    /**
148
     * @var float
149
     */
150
    private $privateDocblockToDifferentDocblock;
151
}
152
PHP
153
            ,
154
            $astLocator
155
        );
156
157
        $fromClassReflector = new ClassReflector($fromLocator);
158
        $toClassReflector   = new ClassReflector($toLocator);
159
        $fromClass          = $fromClassReflector->reflect('TheClass');
160
        $toClass            = $toClassReflector->reflect('TheClass');
161
162
        $properties = [
163
            'publicNoDocblockToNoDocblock'                                      => [],
164
            'publicNoDocblockToDocblock'                                        => [],
165
            'publicNoTypeDocblockToDocblock'                                    => ['[BC] CHANGED: Type documentation for property TheClass#$publicNoTypeDocblockToDocblock changed from having no type to int'],
166
            'publicDocblockToSameDocblock'                                      => [],
167
            'publicDocblockToDifferentDocblock'                                 => ['[BC] CHANGED: Type documentation for property TheClass#$publicDocblockToDifferentDocblock changed from int to float'],
168
            'publicDocblockToNoDocblock'                                        => ['[BC] CHANGED: Type documentation for property TheClass#$publicDocblockToNoDocblock changed from int to having no type'],
169
            'publicCompositeTypeDocblockToSameTypeDocblock'                     => [],
170
            'publicCompositeTypeDocblockToSameTypeDocblockWithDifferentSorting' => [],
171
            'publicCompositeTypeDocblockToDifferentCompositeTypeDocblock'       => ['[BC] CHANGED: Type documentation for property TheClass#$publicCompositeTypeDocblockToDifferentCompositeTypeDocblock changed from float|int to float|int|string'],
172
            'privateDocblockToDifferentDocblock'                                => ['[BC] CHANGED: Type documentation for property TheClass#$privateDocblockToDifferentDocblock changed from int to float'],
173
        ];
174
175
        return array_combine(
176
            array_keys($properties),
177
            array_map(
178
                function (string $property, array $errorMessages) use ($fromClass, $toClass) : array {
179
                    return [
180
                        $fromClass->getProperty($property),
181
                        $toClass->getProperty($property),
182
                        $errorMessages,
183
                    ];
184
                },
185
                array_keys($properties),
186
                $properties
187
            )
188
        );
189
    }
190
}
191