ClassBecameAbstractTest::classesToBeTested()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 63
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 31
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 63
rs 9.424

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\ClassBecameAbstract;
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 RoaveTest\BackwardCompatibility\TypeRestriction;
15
use function array_combine;
16
use function array_keys;
17
use function array_map;
18
use function iterator_to_array;
19
20
/**
21
 * @covers \Roave\BackwardCompatibility\DetectChanges\BCBreak\ClassBased\ClassBecameAbstract
22
 */
23
final class ClassBecameAbstractTest extends TestCase
24
{
25
    /**
26
     * @param string[] $expectedMessages
27
     *
28
     * @dataProvider classesToBeTested
29
     */
30
    public function testDiffs(
31
        ReflectionClass $fromClass,
32
        ReflectionClass $toClass,
33
        array $expectedMessages
34
    ) : void {
35
        $changes = (new ClassBecameAbstract())
36
            ->__invoke($fromClass, $toClass);
37
38
        self::assertSame(
39
            $expectedMessages,
40
            array_map(static function (Change $change) : string {
41
                return $change->__toString();
42
            }, iterator_to_array($changes))
43
        );
44
    }
45
46
    /**
47
     * @return array<string, array<int, ReflectionClass|array<int, string>>>
48
     *
49
     * @psalm-return array<string, array{0: ReflectionClass, 1: ReflectionClass, 2: list<string>}>
50
     */
51
    public function classesToBeTested() : array
52
    {
53
        $locator       = (new BetterReflection())->astLocator();
54
        $fromReflector = new ClassReflector(new StringSourceLocator(
55
            <<<'PHP'
56
<?php
57
58
class ConcreteToAbstract {}
59
abstract class AbstractToConcrete {}
60
class ConcreteToConcrete {}
61
abstract class AbstractToAbstract {}
62
class ConcreteToInterface {}
63
interface InterfaceToConcrete {}
64
interface InterfaceToInterface {}
65
interface InterfaceToAbstract {}
66
abstract class AbstractToInterface {}
67
PHP
68
            ,
69
            $locator
70
        ));
71
        $toReflector   = new ClassReflector(new StringSourceLocator(
72
            <<<'PHP'
73
<?php
74
75
abstract class ConcreteToAbstract {}
76
class AbstractToConcrete {}
77
class ConcreteToConcrete {}
78
abstract class AbstractToAbstract {}
79
interface ConcreteToInterface {}
80
class InterfaceToConcrete {}
81
interface InterfaceToInterface {}
82
abstract class InterfaceToAbstract {}
83
interface AbstractToInterface {}
84
PHP
85
            ,
86
            $locator
87
        ));
88
89
        $classes = [
90
            'ConcreteToAbstract'   => ['[BC] CHANGED: Class ConcreteToAbstract became abstract'],
91
            'AbstractToConcrete'   => [],
92
            'ConcreteToConcrete'   => [],
93
            'AbstractToAbstract'   => [],
94
            'ConcreteToInterface'  => [],
95
            'InterfaceToConcrete'  => [],
96
            'InterfaceToInterface' => [],
97
            'InterfaceToAbstract'  => [],
98
            'AbstractToInterface'  => [],
99
        ];
100
101
        return TypeRestriction::array(array_combine(
102
            array_keys($classes),
103
            array_map(
104
                /** @psalm-param list<string> $errors https://github.com/vimeo/psalm/issues/2772 */
105
                static function (string $className, array $errors) use ($fromReflector, $toReflector) : array {
106
                    return [
107
                        $fromReflector->reflect($className),
108
                        $toReflector->reflect($className),
109
                        $errors,
110
                    ];
111
                },
112
                array_keys($classes),
113
                $classes
114
            )
115
        ));
116
    }
117
}
118