Completed
Push — master ( a4b4a5...98881f )
by Douglas
02:14
created

RobotMover   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 24.39 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 20
loc 82
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A handleTurningLeft() 10 10 1
A handleTurningRight() 0 10 1
A handleMoveForward() 10 10 1
A getRobot() 0 12 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * (c) 2018 Douglas Reith.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
declare(strict_types=1);
10
11
namespace Reith\ToyRobot\CommandHandler;
12
13
use MathPHP\LinearAlgebra\Vector;
14
use Psr\Log\LoggerInterface;
15
use Reith\ToyRobot\Domain\Robot\RobotRepositoryInterface;
16
use Reith\ToyRobot\Domain\Robot\Robot;
17
18
use Reith\ToyRobot\Messaging\Annotation\Subscribe;
19
use Reith\ToyRobot\Messaging\Command\TurnLeft;
20
use Reith\ToyRobot\Messaging\Command\TurnRight;
21
use Reith\ToyRobot\Messaging\Command\MoveForward;
22
23
final class RobotMover
24
{
25
    private $repository;
26
27
    private $logger;
28
29
    /**
30
     * @param RobotRepositoryInterface $repository
31
     * @param LoggerInterface          $logger
32
     */
33 7
    public function __construct(
34
        RobotRepositoryInterface $repository,
35
        LoggerInterface $logger
36
    ) {
37 7
        $this->repository = $repository;
38 7
        $this->logger = $logger;
39 7
    }
40
41
    /**
42
     * @Subscribe
43
     *
44
     * @param TurnLeft $command
45
     */
46 1 View Code Duplication
    public function handleTurningLeft(TurnLeft $command): void
0 ignored issues
show
Unused Code introduced by
The parameter $command is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48 1
        $robot = $this->getRobot();
49
50 1
        $robot->left();
51
52 1
        $this->repository->save($robot);
53
54 1
        $this->logger->info('Robot turned left');
55 1
    }
56
57
    /**
58
     * @Subscribe
59
     *
60
     * @param TurnRight $command
61
     */
62 1
    public function handleTurningRight(TurnRight $command): void
0 ignored issues
show
Unused Code introduced by
The parameter $command is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64 1
        $robot = $this->getRobot();
65
66 1
        $robot->right();
67
68 1
        $this->repository->save($robot);
69
70 1
        $this->logger->info('Robot turned right');
71 1
    }
72
73
    /**
74
     * @Subscribe
75
     *
76
     * @param MoveForward $command
77
     */
78 1 View Code Duplication
    public function handleMoveForward(MoveForward $command): void
0 ignored issues
show
Unused Code introduced by
The parameter $command is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80 1
        $robot = $this->getRobot();
81
82 1
        $robot->move();
83
84 1
        $this->repository->save($robot);
85
86 1
        $this->logger->info('Robot moved forward');
87 1
    }
88
89
    /**
90
     * @return Robot
91
     */
92 3
    private function getRobot(): Robot
93
    {
94 3
        $robot = $this->repository->load();
95
96 3
        if (!($robot instanceof Robot)) {
97
            throw new \RuntimeException(
98
                'Robot cannot be found. Please PLACE the robot first.'
99
            );
100
        }
101
102 3
        return $robot;
103
    }
104
}
105