Completed
Push — master ( 901cc6...ad6830 )
by James
15s queued 11s
created

TraitBecameClassTest::classesToBeTested()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 56
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 9.7251
c 0
b 0
f 0
cc 1
eloc 29
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\TraitBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\ApiCompare\Change;
9
use Roave\ApiCompare\Comparator\BackwardsCompatibility\TraitBased\TraitBecameClass;
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\ApiCompare\Comparator\BackwardsCompatibility\TraitBased\TraitBecameClass
21
 */
22
final class TraitBecameClassTest 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 TraitBecameClass())
35
            ->compare($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
trait TraitToClass {}
54
trait TraitToInterface {}
55
class ClassToTrait {}
56
trait TraitToTrait {}
57
class ClassToClass {}
58
interface InterfaceToTrait {}
59
interface InterfaceToInterface {}
60
PHP
61
            ,
62
            $locator
63
        ));
64
        $toReflector   = new ClassReflector(new StringSourceLocator(
65
            <<<'PHP'
66
<?php
67
68
class TraitToClass {}
69
interface TraitToInterface {}
70
trait ClassToTrait {}
71
trait TraitToTrait {}
72
class ClassToClass {}
73
trait InterfaceToTrait {}
74
interface InterfaceToInterface {}
75
PHP
76
            ,
77
            $locator
78
        ));
79
80
        $classes = [
81
            'TraitToClass'         => ['[BC] CHANGED: Trait TraitToClass became a class'],
82
            'TraitToInterface'     => [],
83
            'ClassToTrait'         => [],
84
            'TraitToTrait'         => [],
85
            'ClassToClass'         => [],
86
            'InterfaceToTrait'     => [],
87
            'InterfaceToInterface' => [],
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