toyrobotTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 149
Duplicated Lines 30.2 %

Coupling/Cohesion

Components 1
Dependencies 18

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 18
dl 45
loc 149
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 45 1
A testToyrobotApp() 0 11 1
A testPlaceRobot() 0 6 1
A testRobotReport() 0 6 1
A testPlacingAndReporting() 0 24 1
A testTurningLeft() 15 15 1
A testTurningRight() 15 15 1
A testMovingForward() 15 15 1

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
 * (c) 2018 Douglas Reith.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
use PHPUnit\Framework\TestCase;
11
use Psr\Log\LoggerInterface;
12
use Symfony\Component\Console\Application;
13
use Symfony\Component\Console\Tester\ApplicationTester;
14
use Reith\ToyRobot\Domain\Space\Table;
15
16
// Persistence
17
use Reith\ToyRobot\Infrastructure\Persistence\InMemoryRobotStore;
18
use Reith\ToyRobot\Infrastructure\Persistence\RobotRepository;
19
20
// Console tasks
21
use Reith\ToyRobot\Console\Place;
22
use Reith\ToyRobot\Console\Report;
23
use Reith\ToyRobot\Console\Left;
24
use Reith\ToyRobot\Console\Right;
25
use Reith\ToyRobot\Console\Move;
26
use Reith\ToyRobot\Console\BusHelper;
27
28
// Buses
29
use Reith\ToyRobot\Infrastructure\Bus\CommandBus;
30
use Reith\ToyRobot\Infrastructure\Bus\QueryBus;
31
32
// Command and query handlers
33
use Reith\ToyRobot\CommandHandler\RobotPlacer;
34
use Reith\ToyRobot\CommandHandler\RobotMover;
35
use Reith\ToyRobot\QueryHandler\RobotReporter;
36
37
class toyrobotTest extends TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
38
{
39
    private $testApp;
40
41
    protected function setUp()
42
    {
43
        $mockLogger = self::createMock(LoggerInterface::class);
44
45
        $store = InMemoryRobotStore::getStore($mockLogger);
0 ignored issues
show
Documentation introduced by
$mockLogger is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Log\LoggerInterface>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
46
47
        // Pass to the repository
48
        $repository = new RobotRepository($store);
49
        $table = Table::create(5);
50
51
        // Create the command and query handlers
52
        $robotPlacer = new RobotPlacer($table, $repository, $mockLogger);
0 ignored issues
show
Documentation introduced by
$mockLogger is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Log\LoggerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
        $robotMover = new RobotMover($repository, $mockLogger);
0 ignored issues
show
Documentation introduced by
$mockLogger is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Log\LoggerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54
        $robotReporter = new RobotReporter($repository, $mockLogger);
0 ignored issues
show
Documentation introduced by
$mockLogger is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Log\LoggerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
55
56
        // Create the busses and register the handlers
57
        $commandBus = (new CommandBus())
58
            ->registerHandler($robotPlacer)
59
            ->registerHandler($robotMover)
60
        ;
61
62
        $queryBus = (new QueryBus())
63
            ->registerHandler($robotReporter)
64
        ;
65
66
        // Set up the console
67
        $application = new Application();
68
        $application->setAutoExit(false);
69
        $application->addCommands([
70
            new Place(),
71
            new Report(),
72
            new Left(),
73
            new Right(),
74
            new Move(),
75
        ]);
76
77
        $busHelper = (new BusHelper())
78
            ->setCommandBus($commandBus)
79
            ->setQueryBus($queryBus)
80
        ;
81
82
        $application->getHelperSet()->set($busHelper);
83
84
        $this->testApp = new ApplicationTester($application);
85
    }
86
87
    public function testToyrobotApp()
88
    {
89
        $this->testApp->run([]);
90
91
        self::assertContains(
92
            'Place the robot on the table',
93
            $this->testApp->getDisplay()
94
        );
95
96
        self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay());
97
    }
98
99
    public function testPlaceRobot()
100
    {
101
        $this->testApp->run(['place']);
102
103
        self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay());
104
    }
105
106
    public function testRobotReport()
107
    {
108
        $this->testApp->run(['report']);
109
110
        self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay());
111
    }
112
113
    public function testPlacingAndReporting()
114
    {
115
        $instruction = '2,2,S';
116
117
        $this->testApp->run(['command' => 'place', 'X,Y,F' => $instruction]);
118
119
        self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay());
120
121
        $this->testApp->run(['report']);
122
123
        self::assertContains($instruction, $this->testApp->getDisplay());
124
125
        // 
126
        // An incorrect placement won't move the robot, it'll report
127
        // the same position
128
        //
129
        $this->testApp->run(['command' => 'place', 'X,Y,F' => '12,4,W']);
130
131
        self::assertNotEquals(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay());
132
133
        $this->testApp->run(['report']);
134
135
        self::assertContains($instruction, $this->testApp->getDisplay());
136
    }
137
138 View Code Duplication
    public function testTurningLeft()
0 ignored issues
show
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...
139
    {
140
        $instruction = '3,2,W';
141
142
        $this->testApp->run(['command' => 'place', 'X,Y,F' => $instruction]);
143
144
        self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay());
145
146
        $this->testApp->run(['left']);
147
148
        self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay());
149
150
        // Now face south
151
        self::assertContains('3,2,S', $this->testApp->getDisplay());
152
    }
153
154 View Code Duplication
    public function testTurningRight()
0 ignored issues
show
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...
155
    {
156
        $instruction = '1,2,E';
157
158
        $this->testApp->run(['command' => 'place', 'X,Y,F' => $instruction]);
159
160
        self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay());
161
162
        $this->testApp->run(['right']);
163
164
        self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay());
165
166
        // Now face south
167
        self::assertContains('1,2,S', $this->testApp->getDisplay());
168
    }
169
170 View Code Duplication
    public function testMovingForward()
0 ignored issues
show
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...
171
    {
172
        $instruction = '3,3,W';
173
174
        $this->testApp->run(['command' => 'place', 'X,Y,F' => $instruction]);
175
176
        self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay());
177
178
        $this->testApp->run(['move']);
179
180
        self::assertSame(0, $this->testApp->getStatusCode(), $this->testApp->getDisplay());
181
182
        // Now x has decreased
183
        self::assertContains('2,3,W', $this->testApp->getDisplay());
184
    }
185
}
186