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

Rover::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Kata\Rover\Domain\Model;
4
5
class Rover
6
{
7
    private $map;
8
    private $position;
9
    private $orientation;
10
    private $collissions;
11
12 2
    public function __construct(Map $a_map, Position $a_position, Orientation $an_orientation)
13
    {
14 2
        self::validate();
0 ignored issues
show
Unused Code introduced by
The call to the method Kata\Rover\Domain\Model\Rover::validate() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
15
16 2
        $this->map         = $a_map;
17 2
        $this->position    = $a_position;
18 2
        $this->orientation = $an_orientation;
19 2
    }
20
21 2
    public function commands(string ...$movements): void
22
    {
23 2
        foreach ($movements as $movement)
24
        {
25 2
            $this->move($movement);
26
        }
27 2
    }
28
29 2
    private function move(string $movement_string): void
30
    {
31 2
        $movement = Movement::fromString($movement_string);
32 2
        if ($movement->isTurn())
33
        {
34 1
            $this->orientation = $this->orientation->turn($movement);
35
36 1
            return;
37
        }
38
39 2
        if ($movement->isForward())
40
        {
41 2
            $new_position = $this->position->moveForward($this->orientation);
42
        }
43
        else
44
        {
45
            $new_position = $this->position->moveBackward($this->orientation);
46
        }
47
48 2
        $normalized_position = $this->map->normalizePosition($new_position);
49
50 2
        if ($this->map->collideWithAnyObstacle($normalized_position))
51
        {
52 1
            $this->collissions[] = $new_position;
53
54 1
            return;
55
        }
56
57 2
        $this->position = $new_position;
58 2
    }
59
60 2
    private static function validate(): void
61
    {
62 2
        return;
63
    }
64
65 2
    public function position(): Position
66
    {
67 2
        return $this->position;
68
    }
69
70
    /** @return Position[] */
71
    public function detectedCollisions(): array
72
    {
73
        if (null === $this->collissions)
74
        {
75
            return [];
76
        }
77
78
        return $this->collissions;
79
    }
80
}