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

testWillDetectChangesInMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 58
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 9.639
c 0
b 0
f 0
cc 1
eloc 27
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\ClassBased;
6
7
use PHPUnit\Framework\TestCase;
8
use Roave\ApiCompare\Change;
9
use Roave\ApiCompare\Changes;
10
use Roave\ApiCompare\Comparator\BackwardsCompatibility\ClassBased\MethodChanged;
11
use Roave\ApiCompare\Comparator\BackwardsCompatibility\MethodBased\MethodBased;
12
use Roave\BetterReflection\BetterReflection;
13
use Roave\BetterReflection\Reflection\ReflectionMethod;
14
use Roave\BetterReflection\Reflector\ClassReflector;
15
use Roave\BetterReflection\SourceLocator\Type\StringSourceLocator;
16
use function strtolower;
17
18
/**
19
 * @covers \Roave\ApiCompare\Comparator\BackwardsCompatibility\ClassBased\MethodChanged
20
 */
21
final class MethodChangedTest extends TestCase
22
{
23
    public function testWillDetectChangesInMethods() : void
24
    {
25
        $astLocator = (new BetterReflection())->astLocator();
26
27
        $fromLocator = new StringSourceLocator(
28
            <<<'PHP'
29
<?php
30
31
class TheClass {
32
    public function a() {}
33
    protected function b() {}
34
    private function c() {}
35
    private static function d() {}
36
    public function G() {}
37
}
38
PHP
39
            ,
40
            $astLocator
41
        );
42
43
        $toLocator = new StringSourceLocator(
44
            <<<'PHP'
45
<?php
46
47
class TheClass {
48
    protected function b() {}
49
    private static function d() {}
50
    public function e() {}
51
    public function f() {}
52
    public function g() {}
53
}
54
PHP
55
            ,
56
            $astLocator
57
        );
58
59
        $comparator = $this->createMock(MethodBased::class);
60
61
        $comparator
62
            ->expects(self::exactly(3))
63
            ->method('compare')
64
            ->willReturnCallback(function (ReflectionMethod $from, ReflectionMethod $to) : Changes {
65
                $methodName = $from->getName();
66
67
                self::assertSame(strtolower($methodName), strtolower($to->getName()));
68
69
                return Changes::fromArray([Change::added($methodName, true)]);
70
            });
71
72
        self::assertEquals(
73
            Changes::fromArray([
74
                Change::added('b', true),
75
                Change::added('d', true),
76
                Change::added('G', true),
77
            ]),
78
            (new MethodChanged($comparator))->compare(
79
                (new ClassReflector($fromLocator))->reflect('TheClass'),
80
                (new ClassReflector($toLocator))->reflect('TheClass')
81
            )
82
        );
83
    }
84
}
85