Completed
Push — master ( 256116...bf8903 )
by personal
04:47
created

PhpMetricsApplication::getDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/*
4
 * (c) Jean-François Lépine <https://twitter.com/Halleck45>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Hal\Application\Console;
11
12
use Hal\Application\Command\RunMetricsCommand;
13
use Hal\Application\Command\SelfUpdateCommand;
14
use Symfony\Component\Console\Application;
15
use Symfony\Component\Console\Input\InputInterface;
16
17
/**
18
 * Application
19
 *
20
 * @author Jean-François Lépine <https://twitter.com/Halleck45>
21
 */
22
class PhpMetricsApplication extends Application
23
{
24
25
    /**
26
     * Gets the name of the command based on input.
27
     *
28
     * @param InputInterface $input The input interface
29
     *
30
     * @return string The command name
31
     */
32
    protected function getCommandName(InputInterface $input)
33
    {
34
        $available = ['metrics', 'self-update'];
35
        $arg = $input->getFirstArgument();
36
        if(!in_array($arg, $available) ||'metrics' === $arg) {
37
            // default argument : we don't want to provide the name of the command by default
38
            $inputDefinition = $this->getDefinition();
39
            $inputDefinition->setArguments();
40
            $this->setDefinition($inputDefinition);
41
            return 'metrics';
42
        }
43
        return $arg;
44
    }
45
46
    /**
47
     * Gets the default commands that should always be available.
48
     *
49
     * @return Command[] An array of default Command instances
50
     */
51
    protected function getDefaultCommands()
52
    {
53
        // Keep the core default commands to have the HelpCommand
54
        // which is used when using the --help option
55
        $defaultCommands = parent::getDefaultCommands();
56
57
        $defaultCommands[] = new RunMetricsCommand();
58
        $defaultCommands[] = new SelfUpdateCommand();
59
60
        return $defaultCommands;
0 ignored issues
show
Best Practice introduced by
The expression return $defaultCommands; seems to be an array, but some of its elements' types (Hal\Application\Command\...mmand\SelfUpdateCommand) are incompatible with the return type of the parent method Symfony\Component\Consol...ion::getDefaultCommands of type array<Symfony\Component\...le\Command\ListCommand>.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
61
    }
62
}