Completed
Push — master ( bce2bd...fc52ee )
by Douglas
01:38
created

Place::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 8
cts 8
cp 1
rs 8.9713
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
crap 1
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\Console;
12
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Console\Input\InputArgument;
17
18
use Reith\ToyRobot\Messaging\Command\PlaceRobot;
19
20
class Place extends Command
21
{
22
    private const ARG = 'X,Y,F';
23
24
    private const DEFAULT_INSTRUCTION = '0,0,E';
25
26 7
    protected function configure()
27
    {
28 7
        $argMsg = 'The optional placement instructions. [X,Y] is the position. [F] is the direction, NESW';
29
30
        $this
31 7
            ->setName('PLACE')
32 7
            ->setDescription('Place the robot on the table')
33 7
            ->addArgument(self::ARG, InputArgument::OPTIONAL, $argMsg)
34 7
            ->setHelp(<<<EOT
35 7
The <info>robot:place</info> will set a robot on the table.
36
37
<info>./toyrobot</info> <comment>PLACE X,Y,F</comment>
38
39
<comment>[X,Y]</comment> is the coordinates of the starting position Robot, default is [0,0]
40
<comment>[F]</comment> is the direction the robot is facing, default is [E]ast
41
42
e.g.
43
    `./toyrobot PLACE`          to place a robot at [0,0] facing East.
44
    `./toyrobot PLACE 1,1`      to place a robot at [1,1] facing East.
45
    `./toyrobot PLACE 2,3,S`    to place a robot at [2,3] facing South.
46
47
EOT
48
        );
49 7
    }
50
51 7
    protected function execute(InputInterface $input, OutputInterface $output)
52
    {
53 7
        $instruction = $input->getArgument(self::ARG) ?: self::DEFAULT_INSTRUCTION;
54
55 7
        $validInstructionRegEx = '/^[0-4],[0-4](,[NESW])?$/';
56
57 7
        if (1 !== preg_match($validInstructionRegEx, $instruction)) {
58 6
            throw new \RuntimeException(
59 6
                sprintf('Instruction [%s] is not valid', $instruction)
60
            );
61
        }
62
63 1
        $parts = explode(',', $instruction);
64
65 1
        [$x, $y] = $parts;
0 ignored issues
show
Bug introduced by
The variable $x does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $y does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
66
67 1
        $direction = 3 === count($parts) ? $parts[2] : 'E';
68
69 1
        $output->writeln(
70 1
            sprintf('Using instruction [%d,%d,%s]', $x, $y, $direction)
71
        );
72
73 1
        $message = new PlaceRobot([$x, $y], $direction);
74
75 1
        $this->getHelper('bus')->postCommand($message);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method postCommand() does only exist in the following implementations of said interface: Reith\ToyRobot\Console\BusHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
76 1
    }
77
}
78