Completed
Push — master ( 8184b0...a4b4a5 )
by Douglas
02:13
created

Report   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 3
dl 0
loc 31
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 13 1
A execute() 0 10 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
use Reith\ToyRobot\Messaging\Query\RobotReport;
18
19
class Report extends Command
20
{
21 4
    protected function configure()
22
    {
23
        $this
24 4
            ->setName('REPORT')
25 4
            ->setDescription('Get a report from the robot')
26 4
            ->setHelp(<<<EOT
27 4
The <info>robot:report</info> will get a report from the robot;
28
29
<info>./toyrobot</info> <comment>REPORT</comment>
30
31
EOT
32
        );
33 4
    }
34
35
    /**
36
     * @param InputInterface  $input
37
     * @param OutputInterface $output
38
     */
39 2
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41 2
        $message = new RobotReport();
42
43 2
        $onSuccess = function (string $result) use ($output) {
44 2
            $output->writeln($result);
45 2
        };
46
47 2
        $this->getHelper('bus')->postQuery($message, $onSuccess);
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 postQuery() 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...
48 2
    }
49
}
50