| 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\KeyLighter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Symfony\Component\Console\Application as SymfonyApplication; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Symfony\Component\Console\Formatter\OutputFormatterStyle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Symfony\Component\Console\Output\ConsoleOutput; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | class Application extends SymfonyApplication | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public $explicit = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected function getCommandName(InputInterface $input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $command = $input->getFirstArgument(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         if(!$command && !$input->hasParameterOption('--help')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             return 'list'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } elseif ($this->has($command)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             return $command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $this->explicit = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             return 'highlight'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     protected function getDefaultCommands() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return array_merge(parent::getDefaultCommands(), [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             new HighlightCommand(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             new LanguagesCommand(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             new FormattersCommand(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             new Dev\GenerateTableCommand(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             new Dev\GenerateAliasesCommand(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     protected function getDefaultInputDefinition() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $input = parent::getDefaultInputDefinition(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $input->setOptions(array_filter($input->getOptions(), function (InputOption $option) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             return $option->getShortcut() != 'q'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         })); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $input->addOption(new InputOption('no-output', 's', InputOption::VALUE_NONE, 'Disables output, useful for debug')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return $input; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         parent::__construct('KeyLighter', KeyLighter::VERSION); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $this->setDefaultCommand('highlight'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     public function run(InputInterface $input = null, OutputInterface $output = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         $output = $output ?: new ConsoleOutput(); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |         $output->getFormatter()->setStyle('command', $output->getFormatter()->getStyle('info')); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         $output->getFormatter()->setStyle('language', new OutputFormatterStyle('magenta')); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         $output->getFormatter()->setStyle('path', new OutputFormatterStyle('blue')); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         $output->getFormatter()->setStyle('formatter', new OutputFormatterStyle('yellow')); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         return parent::run($input, $output); | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  | 
            
                        
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.