Passed
Push — master ( 81aad7...eebd04 )
by Kacper
04:31
created

bin/Application.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Highlighter\bin;
17
18
19
use Kadet\Highlighter\bin\Commands\FormattersCommand;
20
use Kadet\Highlighter\bin\Commands\HighlightCommand;
21
use Kadet\Highlighter\bin\Commands\LanguagesCommand;
22
use Kadet\Highlighter\bin\Commands\Dev;
23
use Kadet\Highlighter\bin\Commands\Benchmark;
24
use Kadet\Highlighter\KeyLighter;
25
use Symfony\Component\Console\Application as SymfonyApplication;
26
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Output\ConsoleOutput;
30
use Symfony\Component\Console\Output\OutputInterface;
31
32
class Application extends SymfonyApplication
33
{
34
    public $explicit = true;
35
36
    protected function getCommandName(InputInterface $input)
37
    {
38
        $command = $input->getFirstArgument();
39
        if(!$command && !$input->hasParameterOption('--help')) {
40
            return 'list';
41
        } elseif ($this->has($command)) {
42
            return $command;
43
        } else {
44
            $this->explicit = false;
45
            return 'highlight';
46
        }
47
    }
48
49
    protected function getDefaultCommands()
50
    {
51
        return array_merge(parent::getDefaultCommands(), [
0 ignored issues
show
The expression return array_merge(paren...\RegenerateCommand())); seems to be an array, but some of its elements' types (Kadet\Highlighter\bin\Co...\Test\RegenerateCommand) 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...
52
            new HighlightCommand(),
53
            new LanguagesCommand(),
54
            new FormattersCommand(),
55
            new Dev\GenerateTableCommand(),
56
            new Dev\GenerateMetadataCommand(),
57
            new Benchmark\RunCommand(),
58
//            new Benchmark\ReportCommand(),
59
            new Benchmark\AnalyzeCommand(),
60
            new Commands\Test\RegenerateCommand()
61
        ]);
62
    }
63
64
    protected function getDefaultInputDefinition()
65
    {
66
        $input = parent::getDefaultInputDefinition();
67
        $input->setOptions(array_filter($input->getOptions(), function (InputOption $option) {
68
            return $option->getShortcut() != 'q';
69
        }));
70
        $input->addOption(new InputOption('no-output', 's', InputOption::VALUE_NONE, 'Disables output, useful for debug'));
71
72
        return $input;
73
    }
74
75
76
    public function __construct()
77
    {
78
        parent::__construct('KeyLighter', KeyLighter::VERSION);
79
        $this->setDefaultCommand('highlight');
80
    }
81
82
    public function run(InputInterface $input = null, OutputInterface $output = null)
83
    {
84
        $output = $output ?: new ConsoleOutput();
85
86
        $output->getFormatter()->setStyle('command', $output->getFormatter()->getStyle('info'));
87
        $output->getFormatter()->setStyle('language', new OutputFormatterStyle('magenta'));
88
        $output->getFormatter()->setStyle('path', new OutputFormatterStyle('blue'));
89
        $output->getFormatter()->setStyle('formatter', new OutputFormatterStyle('yellow'));
90
91
        return parent::run($input, $output);
92
    }
93
94
95
}
96