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

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