Completed
Push — Rover/albertg ( 3c48fc )
by Albert
01:56
created

Orientation::turn()   C

Complexity

Conditions 17
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 35.496

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 6
cts 10
cp 0.6
rs 5.2166
c 0
b 0
f 0
cc 17
nc 5
nop 1
crap 35.496

How to fix   Complexity   

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
namespace Kata\Rover\Domain\Model;
4
5
class Orientation
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
6
{
7
    private $orientation;
8
9 1
    private function __construct(string $orientation)
10
    {
11 1
        $this->orientation = $orientation;
12 1
    }
13
14 1
    public static function NORTH(): Orientation
15
    {
16 1
        return new self('N');
17
    }
18
19
    public static function SOUTH(): Orientation
20
    {
21
        return new self('S');
22
    }
23
24 1
    public static function EAST(): Orientation
25
    {
26 1
        return new self('E');
27
    }
28
29
    public static function WEST(): Orientation
30
    {
31
        return new self('W');
32
    }
33
34 2
    public function isNorth(): bool
35
    {
36 2
        return $this->orientation === 'N';
37
    }
38
39 2
    public function isSouth(): bool
40
    {
41 2
        return $this->orientation === 'S';
42
    }
43
44 2
    public function isEast(): bool
45
    {
46 2
        return $this->orientation === 'E';
47
    }
48
49 1
    public function isWest(): bool
50
    {
51 1
        return $this->orientation === 'W';
52
    }
53
54 1
    public function turn(Movement $movement): Orientation
55
    {
56 1
        if (($movement->isLeft() && $this->isEast()) || ($movement->isRight() && $this->isWest()))
57
        {
58 1
            return self::NORTH();
59
        }
60
61 1
        if (($movement->isLeft() && $this->isWest()) || ($movement->isRight() && $this->isEast()))
62
        {
63
            return self::SOUTH();
64
        }
65
66 1
        if (($movement->isLeft() && $this->isSouth()) || ($movement->isRight() && $this->isNorth()))
67
        {
68 1
            return self::EAST();
69
        }
70
71
        if (($movement->isLeft() && $this->isNorth()) || ($movement->isRight() && $this->isSouth()))
72
        {
73
            return self::WEST();
74
        }
75
    }
76
}