WhichCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 2
1
<?php
2
namespace Nubs\Which\Application;
3
4
use Symfony\Component\Console\Command\Command as SymfonyCommand;
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
class WhichCommand extends SymfonyCommand
10
{
11
    /**
12
     * Configures the command's options.
13
     *
14
     * @return void
15
     */
16
    protected function configure()
17
    {
18
        $this->setName('which')
19
            ->setDescription('Locate a command in the PATH environment')
20
            ->addArgument('commands', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The commands to locate');
21
    }
22
23
    /**
24
     * Locates the commands given as arguments.
25
     *
26
     * @param \Symfony\Component\Console\Input\InputInterface $input The command
27
     *     input.
28
     * @param \Symfony\Component\Console\Output\OutputInterface $output The
29
     *     command output.
30
     * @return int The exit status of the command.
31
     */
32
    protected function execute(InputInterface $input, OutputInterface $output)
33
    {
34
        $locator = $this->getApplication()->getLocator();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Application as the method getLocator() does only exist in the following sub-classes of Symfony\Component\Console\Application: Nubs\Which\Application\WhichApplication. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

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

class MyUser extends 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 sub-classes 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 parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
35
        $exitStatus = 0;
36
37
        foreach ($input->getArgument('commands') as $command) {
38
            $location = $locator->locate($command);
39
40
            if ($location === null) {
41
                $output->writeln("<error>{$command} not found</error>");
42
                $exitStatus = 1;
43
            } else {
44
                $output->writeln($location);
45
            }
46
        }
47
48
        return $exitStatus;
49
    }
50
}
51