Completed
Push — rover-kata ( cc14c5...e33e35 )
by Marcos
02:36
created

Direction   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 68
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

8 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
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 14
    private function __construct(string $direction)
34
    {
35 14
        $this->direction = $direction;
36 14
    }
37
38 14
    public static function north(): Direction
39
    {
40 14
        return new self(Direction::NORTH);
41
    }
42
43 3
    public static function south(): Direction
44
    {
45 3
        return new self(Direction::SOUTH);
46
    }
47
48 4
    public static function east(): Direction
49
    {
50 4
        return new self(Direction::EAST);
51
    }
52
53 4
    public static function west(): Direction
54
    {
55 4
        return new self(Direction::WEST);
56
    }
57
58 5
    public function turnRight(): Direction
59
    {
60 5
        return new Direction(self::DIRECTIONS[$this->direction]['right']);
61
    }
62
63 5
    public function turnLeft(): Direction
64
    {
65 5
        return new Direction(self::DIRECTIONS[$this->direction]['left']);
66
    }
67
68 1
    public function direction(): string
69
    {
70 1
        return $this->direction;
71
    }
72
}
73