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; |
|
|
|
|
61
|
|
|
} |
62
|
|
|
} |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.