CommandManager   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 7
dl 0
loc 81
ccs 0
cts 46
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A command() 0 4 1
A getCommand() 0 4 1
A add() 0 17 4
A usage() 0 9 1
A shortUsage() 0 8 1
1
<?php
2
3
namespace Buttress\Concrete\Console\Command\Manager;
4
5
use Buttress\Concrete\Console\Command\Argument\Parser;
6
use Buttress\Concrete\Console\Command\Argument\Summary;
7
use Buttress\Concrete\Exception\RuntimeException;
8
use League\CLImate\Argument\Argument;
9
use League\CLImate\Argument\Filter;
10
use League\CLImate\Argument\Manager;
11
use League\CLImate\CLImate;
12
13
class CommandManager extends Manager
14
{
15
    /** @var string */
16
    protected $name;
17
18
    public function __construct($command = '')
19
    {
20
        $this->command($command);
21
22
        $this->filter  = new Filter();
23
        $this->summary = new Summary();
24
        $this->parser  = new Parser();
25
    }
26
27
    /**
28
     * Set the name of this command
29
     * @param string $name
30
     */
31
    public function command($name)
32
    {
33
        $this->name = $name;
34
    }
35
36
    public function getCommand()
37
    {
38
        return $this->name;
39
    }
40
41
    /**
42
     * Add an argument.
43
     *
44
     * @throws \Exception if $argument isn't an array or Argument object.
45
     * @param Argument|string|array $argument
46
     * @param $options
47
     */
48
    public function add($argument, array $options = [])
49
    {
50
        if (is_array($argument)) {
51
            $this->addMany($argument);
52
            return;
53
        }
54
55
        if (is_string($argument)) {
56
            $argument = Argument::createFromArray($argument, $options);
57
        }
58
59
        if (!($argument instanceof Argument)) {
60
            throw new RuntimeException('Please provide an argument name or object.');
61
        }
62
63
        $this->arguments[$argument->name()] = $argument;
64
    }
65
66
    /**
67
     * Output a script's usage statement.
68
     *
69
     * @param CLImate $climate
70
     * @param array $argv
71
     */
72
    public function usage(CLImate $climate, array $argv = null)
73
    {
74
        $this->summary
75
            ->setClimate($climate)
76
            ->setDescription($this->description)
77
            ->setCommand($this->name)
78
            ->setFilter($this->filter, $this->all())
79
            ->output();
80
    }
81
82
    /**
83
     * Get a script's short statement.
84
     */
85
    public function shortUsage()
86
    {
87
        return $this->summary
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class League\CLImate\Argument\Summary as the method summarize() does only exist in the following sub-classes of League\CLImate\Argument\Summary: Buttress\Concrete\Console\Command\Argument\Summary. 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...
88
            ->setDescription($this->description)
89
            ->setCommand($this->name)
90
            ->setFilter($this->filter, $this->all())
91
            ->summarize();
92
    }
93
}
94