Passed
Pull Request — master (#38)
by Marco
02:55
created

ClassBecameInterfaceTest::classesToBeTested()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 62
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

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

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\ClassBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\ApiCompare\Change;
9
use Roave\ApiCompare\Comparator\BackwardsCompatibility\ClassBased\ClassBecameAbstract;
10
use Roave\ApiCompare\Comparator\BackwardsCompatibility\ClassBased\ClassBecameInterface;
11
use Roave\BetterReflection\BetterReflection;
12
use Roave\BetterReflection\Reflection\ReflectionClass;
13
use Roave\BetterReflection\Reflector\ClassReflector;
14
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
15
16
final class ClassBecameInterfaceTest extends TestCase
17
{
18
    /**
19
     * @dataProvider classesToBeTested
20
     *
21
     * @param string[] $expectedMessages
22
     */
23
    public function testDiffs(
24
        ReflectionClass $fromClass,
25
        ReflectionClass $toClass,
26
        array $expectedMessages
27
    ) : void {
28
        $changes = (new ClassBecameInterface())
29
            ->compare($fromClass, $toClass);
30
31
        self::assertSame(
32
            $expectedMessages,
33
            array_map(function (Change $change) : string {
34
                return $change->__toString();
35
            }, iterator_to_array($changes))
36
        );
37
    }
38
39
    /** @return (string[]|ReflectionClass)[][] */
40
    public function classesToBeTested() : array
41
    {
42
        $locator       = (new BetterReflection())->astLocator();
43
        $fromReflector = new ClassReflector(new StringSourceLocator(
44
            <<<'PHP'
45
<?php
46
47
class ConcreteToAbstract {}
48
abstract class AbstractToConcrete {}
49
class ConcreteToConcrete {}
50
abstract class AbstractToAbstract {}
51
class ConcreteToInterface {}
52
interface InterfaceToConcrete {}
53
interface InterfaceToInterface {}
54
interface InterfaceToAbstract {}
55
abstract class AbstractToInterface {}
56
PHP
57
            ,
58
            $locator
59
        ));
60
        $toReflector   = new ClassReflector(new StringSourceLocator(
61
            <<<'PHP'
62
<?php
63
64
abstract class ConcreteToAbstract {}
65
class AbstractToConcrete {}
66
class ConcreteToConcrete {}
67
abstract class AbstractToAbstract {}
68
interface ConcreteToInterface {}
69
class InterfaceToConcrete {}
70
interface InterfaceToInterface {}
71
abstract class InterfaceToAbstract {}
72
interface AbstractToInterface {}
73
PHP
74
            ,
75
            $locator
76
        ));
77
78
        $classes = [
79
            'ConcreteToAbstract'   => [],
80
            'AbstractToConcrete'   => [],
81
            'ConcreteToConcrete'   => [],
82
            'AbstractToAbstract'   => [],
83
            'ConcreteToInterface'  => ['[BC] CHANGED: Class ConcreteToInterface became an interface'],
84
            'InterfaceToConcrete'  => [],
85
            'InterfaceToInterface' => [],
86
            'InterfaceToAbstract'  => [],
87
            'AbstractToInterface'  => ['[BC] CHANGED: Class AbstractToInterface became an interface'],
88
        ];
89
90
        return array_combine(
91
            array_keys($classes),
92
            array_map(
93
                function (string $className, array $errors) use ($fromReflector, $toReflector) : array {
94
                    return [
95
                        $fromReflector->reflect($className),
96
                        $toReflector->reflect($className),
97
                        $errors,
98
                    ];
99
                },
100
                array_keys($classes),
101
                $classes
102
            )
103
        );
104
    }
105
}
106