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(), [ |
|
|
|
|
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
|
|
|
|
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.