Completed
Pull Request — master (#38)
by Marco
03:08
created

propertiesToBeTested()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 116
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 116
rs 8.2857
c 0
b 0
f 0
cc 1
eloc 50
nc 1
nop 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\ApiCompare\Comparator\BackwardsCompatibility\PropertyBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\ApiCompare\Change;
9
use Roave\ApiCompare\Comparator\BackwardsCompatibility\PropertyBased\PropertyDefaultValueChanged;
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_map;
15
use function iterator_to_array;
16
17
/**
18
 * @covers \Roave\ApiCompare\Comparator\BackwardsCompatibility\PropertyBased\PropertyDefaultValueChanged
19
 */
20
final class PropertyDefaultValueChangedTest extends TestCase
21
{
22
    /**
23
     * @dataProvider propertiesToBeTested
24
     *
25
     * @param string[] $expectedMessages
26
     */
27
    public function testDiffs(
28
        ReflectionProperty $fromFunction,
29
        ReflectionProperty $toFunction,
30
        array $expectedMessages
31
    ) : void {
32
        $changes = (new PropertyDefaultValueChanged())
33
            ->compare($fromFunction, $toFunction);
34
35
        self::assertSame(
36
            $expectedMessages,
37
            array_map(function (Change $change) : string {
38
                return $change->__toString();
39
            }, iterator_to_array($changes))
40
        );
41
    }
42
43
    /** @return (string[]|ReflectionProperty)[][] */
44
    public function propertiesToBeTested() : array
45
    {
46
        $astLocator = (new BetterReflection())->astLocator();
47
48
        $fromLocator = new StringSourceLocator(
49
            <<<'PHP'
50
<?php
51
52
class TheClass {
53
    public $publicNothingToNothing;
54
    public $publicNothingToNull;
55
    public $publicNullToNull = null;
56
    public $publicValueChanged = 1;
57
    public $publicValueToSimilarValue = '1';
58
    public $publicExpressionToExpressionValue = 101 + 5;
59
    
60
    protected $protectedNothingToNothing;
61
    protected $protectedNothingToNull;
62
    protected $protectedNullToNull = null;
63
    protected $protectedValueChanged = 1;
64
    protected $protectedValueToSimilarValue = '1';
65
    protected $protectedExpressionToExpressionValue = 101 + 5;
66
    
67
    private $privateNothingToNothing;
68
    private $privateNothingToNull;
69
    private $privateNullToNull = null;
70
    private $privateValueChanged = 1;
71
    private $privateValueToSimilarValue = '1';
72
    private $privateExpressionToExpressionValue = 101 + 5;
73
}
74
PHP
75
            ,
76
            $astLocator
77
        );
78
79
        $toLocator = new StringSourceLocator(
80
            <<<'PHP'
81
<?php
82
83
class TheClass {
84
    public $publicNothingToNothing;
85
    public $publicNothingToNull = null;
86
    public $publicNullToNull = null;
87
    public $publicValueChanged = 2;
88
    public $publicValueToSimilarValue = 1;
89
    public $publicExpressionToExpressionValue = 106;
90
    
91
    protected $protectedNothingToNothing;
92
    protected $protectedNothingToNull = null;
93
    protected $protectedNullToNull = null;
94
    protected $protectedValueChanged = 2;
95
    protected $protectedValueToSimilarValue = 1;
96
    protected $protectedExpressionToExpressionValue = 106;
97
    
98
    private $privateNothingToNothing;
99
    private $privateNothingToNull = null;
100
    private $privateNullToNull = null;
101
    private $privateValueChanged = 2;
102
    private $privateValueToSimilarValue = 1;
103
    private $privateExpressionToExpressionValue = 106;
104
}
105
PHP
106
            ,
107
            $astLocator
108
        );
109
110
        $fromClassReflector = new ClassReflector($fromLocator);
111
        $toClassReflector   = new ClassReflector($toLocator);
112
        $fromClass          = $fromClassReflector->reflect('TheClass');
113
        $toClass            = $toClassReflector->reflect('TheClass');
114
115
        $properties = [
116
            'publicNothingToNothing'               => [],
117
            'publicNothingToNull'                  => [],
118
            'publicNullToNull'                     => [],
119
            'publicValueChanged'                   => [
120
                '[BC] CHANGED: Property TheClass#$publicValueChanged changed default value from 1 to 2',
121
            ],
122
            'publicValueToSimilarValue'            => [
123
                '[BC] CHANGED: Property TheClass#$publicValueToSimilarValue changed default value from \'1\' to 1',
124
            ],
125
            'publicExpressionToExpressionValue'    => [],
126
            'protectedNothingToNothing'            => [],
127
            'protectedNothingToNull'               => [],
128
            'protectedNullToNull'                  => [],
129
            'protectedValueChanged'                => [
130
                '[BC] CHANGED: Property TheClass#$protectedValueChanged changed default value from 1 to 2',
131
            ],
132
            'protectedValueToSimilarValue'         => [
133
                '[BC] CHANGED: Property TheClass#$protectedValueToSimilarValue changed default value from \'1\' to 1',
134
            ],
135
            'protectedExpressionToExpressionValue' => [],
136
            'privateNothingToNothing'              => [],
137
            'privateNothingToNull'                 => [],
138
            'privateNullToNull'                    => [],
139
            'privateValueChanged'                  => [
140
                '[BC] CHANGED: Property TheClass#$privateValueChanged changed default value from 1 to 2',
141
            ],
142
            'privateValueToSimilarValue'           => [
143
                '[BC] CHANGED: Property TheClass#$privateValueToSimilarValue changed default value from \'1\' to 1',
144
            ],
145
            'privateExpressionToExpressionValue'   => [],
146
        ];
147
148
        return array_combine(
149
            array_keys($properties),
150
            array_map(
151
                function (string $property, array $errorMessages) use ($fromClass, $toClass) : array {
152
                    return [
153
                        $fromClass->getProperty($property),
154
                        $toClass->getProperty($property),
155
                        $errorMessages,
156
                    ];
157
                },
158
                array_keys($properties),
159
                $properties
160
            )
161
        );
162
    }
163
}
164