Completed
Pull Request — master (#29)
by Aleh
03:19
created

AsyncCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 3
dl 0
loc 42
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B run() 0 31 5
A getContainer() 0 4 1
1
<?php
2
3
namespace Padawan\Command;
4
5
use DI\Container;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Exception\ExceptionInterface;
10
11
abstract class AsyncCommand extends Command
12
{
13
    public function run(InputInterface $input, OutputInterface $output)
14
    {
15
        // force the creation of the synopsis before the merge with the app definition
16
        $this->getSynopsis(true);
17
        $this->getSynopsis(false);
18
19
        // add the application arguments and options
20
        $this->mergeApplicationDefinition();
21
22
        // bind the input against the command specific arguments/options
23
        try {
24
            $input->bind($this->getDefinition());
25
        } catch (ExceptionInterface $e) {
26
            if (!$this->ignoreValidationErrors()) {
27
                throw $e;
28
            }
29
        }
30
31
        $this->initialize($input, $output);
32
33
        // The command name argument is often omitted when a command is executed directly with its run() method.
34
        // It would fail the validation if we didn't make sure the command argument is present,
35
        // since it's required by the application.
36
        if ($input->hasArgument('command') && null === $input->getArgument('command')) {
37
            $input->setArgument('command', $this->getName());
38
        }
39
40
        $input->validate();
41
42
        return $this->execute($input, $output);
43
    }
44
45
    /**
46
     * @return Container
47
     */
48
    public function getContainer()
49
    {
50
        return $this->getApplication()->getContainer();
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 getContainer() does only exist in the following sub-classes of Symfony\Component\Console\Application: Padawan\Framework\Application, Padawan\Framework\Application\Cli, Padawan\Framework\Application\Socket, PhpSpec\Console\Application. 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...
51
    }
52
}
53