Place::execute()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 14
cts 14
cp 1
rs 9.504
c 0
b 0
f 0
cc 4
nc 6
nop 2
crap 4
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
use Reith\ToyRobot\Messaging\Command\PlaceRobot;
18
19
class Place extends Command
20
{
21
    private const ARG = 'X,Y,F';
22
23
    private const DEFAULT_INSTRUCTION = '0,0,E';
24
25 14
    protected function configure()
26
    {
27 14
        $argMsg = 'The optional placement instructions. [X,Y] is the position. [F] is the direction: [N|E|S|W]';
28
29
        $this
30 14
            ->setName('PLACE')
31 14
            ->setDescription('Place the robot on the table')
32 14
            ->addArgument(self::ARG, InputArgument::OPTIONAL, $argMsg)
33 14
            ->setHelp(<<<EOT
34 14
The <info>PLACE</info> instruction will set a robot on the table.
35
36
<info>./toyrobot</info> <comment>PLACE X,Y,F</comment>
37
38
<comment>[X,Y]</comment> is the coordinates of the starting position Robot, default is [0,0]
39
<comment>[F]</comment> is the direction the robot is facing, default is [E]ast
40
41
e.g.
42
    `./toyrobot PLACE`          to place a robot at [0,0] facing East.
43
    `./toyrobot PLACE 1,1`      to place a robot at [1,1] facing East.
44
    `./toyrobot PLACE 2,3,S`    to place a robot at [2,3] facing South.
45
46
EOT
47
        );
48 14
    }
49
50 12
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52 12
        $instruction = $input->getArgument(self::ARG) ?: self::DEFAULT_INSTRUCTION;
53
54 12
        $validInstructionRegEx = '/^[0-4],[0-4](,[NESW])?$/';
55
56 12
        if (1 !== preg_match($validInstructionRegEx, $instruction)) {
57 7
            throw new \RuntimeException(
58 7
                sprintf('Instruction [%s] is not valid', $instruction)
59
            );
60
        }
61
62 6
        $parts = explode(',', $instruction);
63
64 6
        [$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...
65
66 6
        $direction = 3 === count($parts) ? $parts[2] : 'E';
67
68 6
        $output->writeln(
69 6
            sprintf('Using instruction [%d,%d,%s]', $x, $y, $direction)
70
        );
71
72 6
        $message = new PlaceRobot([$x, $y], $direction);
73
74 6
        $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...
75 6
    }
76
}
77