Completed
Push — rover-kata ( e33e35...c1a929 )
by Marcos
02:43
created

Direction   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 93
ccs 30
cts 31
cp 0.9677
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A north() 0 4 1
A south() 0 4 1
A east() 0 4 1
A west() 0 4 1
A turnRight() 0 4 1
A turnLeft() 0 4 1
A direction() 0 4 1
A inverseDirection() 0 15 5
A equals() 0 8 2
1
<?php
2
3
namespace Kata\Rover;
4
5
final class Direction
6
{
7
    public const NORTH = 'n';
8
    public const SOUTH = 's';
9
    public const EAST = 'e';
10
    public const WEST = 'w';
11
12
    public const DIRECTIONS = [
13
        self::NORTH => [
14
            'right' => self::EAST,
15
            'left' => self::WEST
16
        ],
17
        self::EAST => [
18
            'right' => self::SOUTH,
19
            'left' => self::NORTH
20
        ],
21
        self::SOUTH => [
22
            'right' => self::WEST,
23
            'left' => self::EAST
24
        ],
25
        self::WEST => [
26
            'right' => self::NORTH,
27
            'left' => self::SOUTH
28
        ]
29
    ];
30
31
    private $direction;
32
33 21
    private function __construct(string $direction)
34
    {
35 21
        $this->direction = $direction;
36 21
    }
37
38 21
    public static function north(): Direction
39
    {
40 21
        return new self(self::NORTH);
41
    }
42
43 8
    public static function south(): Direction
44
    {
45 8
        return new self(self::SOUTH);
46
    }
47
48 7
    public static function east(): Direction
49
    {
50 7
        return new self(self::EAST);
51
    }
52
53 6
    public static function west(): Direction
54
    {
55 6
        return new self(self::WEST);
56
    }
57
58 11
    public function turnRight(): Direction
59
    {
60 11
        return new Direction(self::DIRECTIONS[$this->direction]['right']);
61
    }
62
63 7
    public function turnLeft(): Direction
64
    {
65 7
        return new Direction(self::DIRECTIONS[$this->direction]['left']);
66
    }
67
68 10
    public function direction(): string
69
    {
70 10
        return $this->direction;
71
    }
72
73 6
    public function inverseDirection(): Direction
74
    {
75 6
        if ($this->equals(self::north())) {
76 3
            return self::south();
77
        }
78 3
        if ($this->equals(self::east())) {
79 1
            return self::west();
80
        }
81 2
        if ($this->equals(self::south())) {
82 1
            return self::north();
83
        }
84 1
        if ($this->equals(self::west())) {
85 1
            return self::east();
86
        }
87
    }
88
89 6
    private function equals(Direction $a_direction): bool
90
    {
91 6
        if ($this->direction === $a_direction->direction()) {
92 6
            return true;
93
        }
94
95 3
        return false;
96
    }
97
}
98