SpammerApplication::getDefaultCommands()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace EndelWar\Spammer\Application;
4
5
use EndelWar\Spammer\Command;
6
use Symfony\Component\Console\Application;
7
use Symfony\Component\Console\Input\InputInterface;
8
9
class SpammerApplication extends Application
10
{
11
    /**
12
     * Gets the name of the command based on input.
13
     *
14
     * @param InputInterface $input The input interface
15
     *
16
     * @return string The command name
17
     */
18 32
    protected function getCommandName(InputInterface $input)
19
    {
20 32
        return 'spammer';
21
    }
22
23
    /**
24
     * Gets the default commands that should always be available.
25
     *
26
     * @return array An array of default Command instances
27
     * @throws \Symfony\Component\Console\Exception\LogicException
28
     */
29 32
    protected function getDefaultCommands()
30
    {
31
        // Keep the core default commands to have the HelpCommand
32
        // which is used when using the --help option
33 32
        $defaultCommands = parent::getDefaultCommands();
34
35 32
        $defaultCommands[] = new Command\SpammerCommand();
36
37 32
        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 (EndelWar\Spammer\Command\SpammerCommand) 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...
38
    }
39
40
    /**
41
     * Overridden so that the application doesn't expect the command
42
     * name to be the first argument.
43
     */
44 32
    public function getDefinition()
45
    {
46 32
        $inputDefinition = parent::getDefinition();
47
        // clear out the normal first argument, which is the command name
48 32
        $inputDefinition->setArguments();
49
50 32
        return $inputDefinition;
51
    }
52
}
53