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

PropertyScopeChangedTest::propertiesToBeTested()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 100
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 100
rs 8.2857
c 0
b 0
f 0
cc 1
eloc 44
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\ApiCompare\Comparator\BackwardsCompatibility\PropertyBased\PropertyScopeChanged;
11
use Roave\BetterReflection\BetterReflection;
12
use Roave\BetterReflection\Reflection\ReflectionProperty;
13
use Roave\BetterReflection\Reflector\ClassReflector;
14
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
15
use function array_map;
16
use function iterator_to_array;
17
18
/**
19
 * @covers \Roave\ApiCompare\Comparator\BackwardsCompatibility\PropertyBased\PropertyScopeChanged
20
 */
21
final class PropertyScopeChangedTest extends TestCase
22
{
23
    /**
24
     * @dataProvider propertiesToBeTested
25
     *
26
     * @param string[] $expectedMessages
27
     */
28
    public function testDiffs(
29
        ReflectionProperty $fromFunction,
30
        ReflectionProperty $toFunction,
31
        array $expectedMessages
32
    ) : void {
33
        $changes = (new PropertyScopeChanged())
34
            ->compare($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[]|ReflectionProperty)[][] */
45
    public function propertiesToBeTested() : array
46
    {
47
        $astLocator = (new BetterReflection())->astLocator();
48
49
        $fromLocator = new StringSourceLocator(
50
            <<<'PHP'
51
<?php
52
53
class TheClass {
54
    public $publicInstanceToStatic;
55
    public static $publicStaticToInstance;
56
    public $publicInstanceToInstance;
57
    public static $publicStaticToStatic;
58
    
59
    protected $protectedInstanceToStatic;
60
    protected static $protectedStaticToInstance;
61
    protected $protectedInstanceToInstance;
62
    protected static $protectedStaticToStatic;
63
    
64
    private $privateInstanceToStatic;
65
    private static $privateStaticToInstance;
66
    private $privateInstanceToInstance;
67
    private static $privateStaticToStatic;
68
}
69
PHP
70
            ,
71
            $astLocator
72
        );
73
74
        $toLocator = new StringSourceLocator(
75
            <<<'PHP'
76
<?php
77
78
class TheClass {
79
    public static $publicInstanceToStatic;
80
    public $publicStaticToInstance;
81
    public $publicInstanceToInstance;
82
    public static $publicStaticToStatic;
83
    
84
    protected static $protectedInstanceToStatic;
85
    protected $protectedStaticToInstance;
86
    protected $protectedInstanceToInstance;
87
    protected static $protectedStaticToStatic;
88
    
89
    private static $privateInstanceToStatic;
90
    private $privateStaticToInstance;
91
    private $privateInstanceToInstance;
92
    private static $privateStaticToStatic;
93
}
94
PHP
95
            ,
96
            $astLocator
97
        );
98
99
        $fromClassReflector = new ClassReflector($fromLocator);
100
        $toClassReflector   = new ClassReflector($toLocator);
101
        $fromClass          = $fromClassReflector->reflect('TheClass');
102
        $toClass            = $toClassReflector->reflect('TheClass');
103
104
        $properties = [
105
            'publicInstanceToStatic'   => [
106
                '[BC] CHANGED: Property $publicInstanceToStatic of TheClass changed scope from instance to static',
107
            ],
108
            'publicStaticToInstance'   => [
109
                '[BC] CHANGED: Property $publicStaticToInstance of TheClass changed scope from static to instance',
110
            ],
111
            'publicInstanceToInstance' => [],
112
            'publicStaticToStatic'     => [],
113
114
            'protectedInstanceToStatic'    => [
115
                '[BC] CHANGED: Property $protectedInstanceToStatic of TheClass changed scope from instance to static',
116
            ],
117
            'protectedStaticToInstance'    => [
118
                '[BC] CHANGED: Property $protectedStaticToInstance of TheClass changed scope from static to instance',
119
            ],
120
            'protectedInstanceToInstance' => [],
121
            'protectedStaticToStatic'      => [],
122
123
            'privateInstanceToStatic'    => [
124
                '[BC] CHANGED: Property $privateInstanceToStatic of TheClass changed scope from instance to static'
125
            ],
126
            'privateStaticToInstance'   => [
127
                '[BC] CHANGED: Property $privateStaticToInstance of TheClass changed scope from static to instance'
128
            ],
129
            'privateInstanceToInstance' => [],
130
            'privateStaticToStatic'     => [],
131
        ];
132
133
        return array_combine(
134
            array_keys($properties),
135
            array_map(
136
                function (string $property, array $errorMessages) use ($fromClass, $toClass) : array {
137
                    return [
138
                        $fromClass->getProperty($property),
139
                        $toClass->getProperty($property),
140
                        $errorMessages,
141
                    ];
142
                },
143
                array_keys($properties),
144
                $properties
145
            )
146
        );
147
    }
148
}
149