Completed
Push — master ( 1db1ce...04b64d )
by Massimiliano
08:04
created

Application::getHelp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of Bowerphp.
5
 *
6
 * (c) Massimiliano Arione <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bowerphp\Console;
13
14
use Bowerphp\Command;
15
use Bowerphp\Util\ErrorHandler;
16
use Symfony\Component\Console\Application as BaseApplication;
17
use Symfony\Component\Console\Input\ArrayInput;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * The console application that handles the commands
24
 * Inspired by Composer https://github.com/composer/composer
25
 */
26
class Application extends BaseApplication
0 ignored issues
show
Complexity introduced by
The class Application has a coupling between objects value of 20. Consider to reduce the number of dependencies under 13.
Loading history...
27
{
28
    /**
29
     * @var Bowerphp
30
     */
31
    protected $bowerphp;
32
33
    private static $logo = '    ____                                __
34
   / __ )____ _      _____  _________  / /_  ____
35
  / __  / __ \ | /| / / _ \/ ___/ __ \/ __ \/ __ \
36
 / /_/ / /_/ / |/ |/ /  __/ /  / /_/ / / / / /_/ /
37
/_____/\____/|__/|__/\___/_/  / .___/_/ /_/ .___/
38
                             /_/         /_/
39
';
40
41
    /**
42
     * Constructor
43
     */
44
    public function __construct()
45
    {
46
        ErrorHandler::register();
47
        parent::__construct('Bowerphp', '0.3 Powered by BeeLab (bee-lab.net)');
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function doRun(InputInterface $input, OutputInterface $output)
54
    {
55
        if (version_compare(PHP_VERSION, '5.3.2', '<')) {
56
            $output->writeln('<warning>Bowerphp only officially supports PHP 5.3.2 and above, you will most likely encounter problems with your PHP ' . PHP_VERSION . ', upgrading is strongly recommended.</warning>');
57
        }
58
59
        if ($input->hasParameterOption('--profile')) {
60
            $startTime = microtime(true);
61
        }
62
63
        if ($newWorkDir = $this->getNewWorkingDir($input)) {
64
            $oldWorkingDir = getcwd();
65
            chdir($newWorkDir);
66
        }
67
68
        $result = $this->symfonyDoRun($input, $output);
69
70
        if (isset($oldWorkingDir)) {
71
            chdir($oldWorkingDir);
72
        }
73
74
        if (isset($startTime)) {
75
            $output->writeln('<info>Memory usage: ' . round(memory_get_usage() / 1024 / 1024, 2) . 'MB (peak: ' . round(memory_get_peak_usage() / 1024 / 1024, 2) . 'MB), time: ' . round(microtime(true) - $startTime, 2) . 's');
76
        }
77
78
        return $result;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function getHelp()
85
    {
86
        return self::$logo . parent::getHelp();
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    protected function getDefaultCommands()
93
    {
94
        return array(
0 ignored issues
show
Best Practice introduced by
The expression return array(new \Bowerp...nd\UninstallCommand()); seems to be an array, but some of its elements' types (Bowerphp\Command\HomeCom...ommand\UninstallCommand) 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...
95
            new Command\HelpCommand(),
96
            new Command\CommandListCommand(),
97
            new Command\HomeCommand(),
98
            new Command\InfoCommand(),
99
            new Command\InitCommand(),
100
            new Command\InstallCommand(),
101
            new Command\ListCommand(),
102
            new Command\LookupCommand(),
103
            new Command\SearchCommand(),
104
            new Command\UpdateCommand(),
105
            new Command\UninstallCommand(),
106
        );
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    protected function getDefaultInputDefinition()
113
    {
114
        $definition = parent::getDefaultInputDefinition();
115
        $definition->addOption(new InputOption('--profile', null, InputOption::VALUE_NONE, 'Display timing and memory usage information'));
116
        $definition->addOption(new InputOption('--working-dir', '-d', InputOption::VALUE_REQUIRED, 'If specified, use the given directory as working directory.'));
117
118
        return $definition;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    protected function getDefaultHelperSet()
125
    {
126
        $helperSet = parent::getDefaultHelperSet();
127
        if (class_exists('Symfony\Component\Console\Helper\DialogHelper')) {
128
            $helperSet->set(new \Bowerphp\Command\Helper\DialogHelper());
129
        } else {
130
            $helperSet->set(new \Bowerphp\Command\Helper\QuestionHelper());
131
        }
132
133
        return $helperSet;
134
    }
135
136
    /**
137
     * @param  InputInterface    $input
138
     * @throws \RuntimeException
139
     */
140
    private function getNewWorkingDir(InputInterface $input)
141
    {
142
        $workingDir = $input->getParameterOption(array('--working-dir', '-d'));
143
        if (false !== $workingDir && !is_dir($workingDir)) {
144
            throw new \RuntimeException('Invalid working directory specified.');
145
        }
146
147
        return $workingDir;
148
    }
149
150
    /**
151
     * Copy of original Symfony doRun, to allow a default command
152
     *
153
     * @param InputInterface  $input   An Input instance
154
     * @param OutputInterface $output  An Output instance
155
     * @param string          $default Default command to execute
156
     *
157
     * @return int 0 if everything went fine, or an error code
158
     * @codeCoverageIgnore
159
     */
160
    private function symfonyDoRun(InputInterface $input, OutputInterface $output, $default = 'list-commands')
161
    {
162
        if (true === $input->hasParameterOption(array('--version', '-V'))) {
163
            $output->writeln($this->getLongVersion());
164
165
            return 0;
166
        }
167
168
        $name = $this->getCommandName($input);
169
170
        if (true === $input->hasParameterOption(array('--help', '-h'))) {
171
            if (!$name) {
172
                $name = 'help';
173
                $input = new ArrayInput(array('command' => 'help'));
174
            } else {
175
                $this->wantHelps = true;
176
            }
177
        }
178
179
        if (!$name) {
180
            $name = $default;
181
            $input = new ArrayInput(array('command' => $default));
182
        }
183
184
        // the command name MUST be the first element of the input
185
        $command = $this->find($name);
186
187
        $this->runningCommand = $command;
188
        $exitCode = $this->doRunCommand($command, $input, $output);
189
        $this->runningCommand = null;
190
191
        return $exitCode;
192
    }
193
}
194