Passed
Pull Request — master (#109)
by Marco
05:58 queued 03:28
created

AncestorRemovedTest::classesToBeTested()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 95
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 42
nc 1
nop 0
dl 0
loc 95
rs 9.248
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\ClassBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\BackwardCompatibility\Change;
9
use Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassBased\AncestorRemoved;
10
use Roave\BetterReflection\BetterReflection;
11
use Roave\BetterReflection\Reflection\ReflectionClass;
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\ClassBased\AncestorRemoved
21
 */
22
final class AncestorRemovedTest extends TestCase
23
{
24
    /**
25
     * @dataProvider classesToBeTested
26
     *
27
     * @param string[] $expectedMessages
28
     */
29
    public function testDiffs(
30
        ReflectionClass $fromClass,
31
        ReflectionClass $toClass,
32
        array $expectedMessages
33
    ) : void {
34
        $changes = (new AncestorRemoved())
35
            ->__invoke($fromClass, $toClass);
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[]|ReflectionClass)[][] */
46
    public function classesToBeTested() : array
47
    {
48
        $locator       = (new BetterReflection())->astLocator();
49
        $fromReflector = new ClassReflector(new StringSourceLocator(
50
            <<<'PHP'
51
<?php
52
53
class A {}
54
class B extends A {}
55
class C extends A {}
56
class D extends C {}
57
interface IA {}
58
interface IB extends IA {}
59
interface IC extends IA {}
60
interface ID extends IB, IE {}
61
interface IE {}
62
interface IG {}
63
class ClassWithNoAncestors {}
64
class ClassWithAddedAncestors {}
65
class ClassWithAddedInterface {}
66
class ClassWithRemovedAncestor extends A {}
67
class ClassWithRemovedIndirectAncestor extends B {}
68
class ClassWithRemovedVeryIndirectAncestor extends D {}
69
class ClassWithRemovedInterface implements IA {}
70
class ClassWithRemovedIndirectInterface implements IB {}
71
class ClassWithRemovedVeryIndirectInterface implements ID {}
72
class ClassWithInvertedInterfaceNames implements IE, IG {}
73
PHP
74
            ,
75
            $locator
76
        ));
77
        $toReflector   = new ClassReflector(new StringSourceLocator(
78
            <<<'PHP'
79
<?php
80
81
class A {}
82
class B {}
83
class C {}
84
class D extends C {}
85
interface IA {}
86
interface IB {}
87
interface IC extends IA {}
88
interface ID extends IB, IE {}
89
interface IE {}
90
interface IG {}
91
class ClassWithNoAncestors {}
92
class ClassWithAddedAncestors extends A {}
93
class ClassWithAddedInterface implements IA {}
94
class ClassWithRemovedAncestor {}
95
class ClassWithRemovedIndirectAncestor extends B {}
96
class ClassWithRemovedVeryIndirectAncestor extends D {}
97
class ClassWithRemovedInterface {}
98
class ClassWithRemovedIndirectInterface implements IB {}
99
class ClassWithRemovedVeryIndirectInterface implements ID {}
100
class ClassWithInvertedInterfaceNames implements IG, IE {}
101
PHP
102
            ,
103
            $locator
104
        ));
105
106
        $classes = [
107
            'A' => [],
108
            'B' => ['[BC] REMOVED: These ancestors of B have been removed: ["A"]'],
109
            'C' => ['[BC] REMOVED: These ancestors of C have been removed: ["A"]'],
110
            'D' => ['[BC] REMOVED: These ancestors of D have been removed: ["A"]'],
111
            'IA' => [],
112
            'IB' => ['[BC] REMOVED: These ancestors of IB have been removed: ["IA"]'],
113
            'IC' => [],
114
            'ID' => ['[BC] REMOVED: These ancestors of ID have been removed: ["IA"]'],
115
            'IE' => [],
116
            'IG' => [],
117
            'ClassWithNoAncestors' => [],
118
            'ClassWithAddedAncestors' => [],
119
            'ClassWithAddedInterface' => [],
120
            'ClassWithRemovedAncestor' => ['[BC] REMOVED: These ancestors of ClassWithRemovedAncestor have been removed: ["A"]'],
121
            'ClassWithRemovedIndirectAncestor' => ['[BC] REMOVED: These ancestors of ClassWithRemovedIndirectAncestor have been removed: ["A"]'],
122
            'ClassWithRemovedVeryIndirectAncestor' => ['[BC] REMOVED: These ancestors of ClassWithRemovedVeryIndirectAncestor have been removed: ["A"]'],
123
            'ClassWithRemovedInterface' => ['[BC] REMOVED: These ancestors of ClassWithRemovedInterface have been removed: ["IA"]'],
124
            'ClassWithRemovedIndirectInterface' => ['[BC] REMOVED: These ancestors of ClassWithRemovedIndirectInterface have been removed: ["IA"]'],
125
            'ClassWithRemovedVeryIndirectInterface' => ['[BC] REMOVED: These ancestors of ClassWithRemovedVeryIndirectInterface have been removed: ["IA"]'],
126
            'ClassWithInvertedInterfaceNames' => [],
127
        ];
128
129
        return array_combine(
130
            array_keys($classes),
131
            array_map(
132
                function (string $className, array $errors) use ($fromReflector, $toReflector) : array {
133
                    return [
134
                        $fromReflector->reflect($className),
135
                        $toReflector->reflect($className),
136
                        $errors,
137
                    ];
138
                },
139
                array_keys($classes),
140
                $classes
141
            )
142
        );
143
    }
144
}
145