These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace NerdsAndCompany\Schematic\Console; |
||
4 | |||
5 | use Craft\Craft; |
||
6 | use Craft\ConsoleCommandRunner; |
||
7 | use Craft\StringHelper; |
||
8 | use Craft\IOHelper; |
||
9 | |||
10 | /** |
||
11 | * Schematic Console Command Runner. |
||
12 | * |
||
13 | * Sync Craft Setups. |
||
14 | * |
||
15 | * @author Nerds & Company |
||
16 | * @copyright Copyright (c) 2015, Nerds & Company |
||
17 | * @license MIT |
||
18 | * |
||
19 | * @link http://www.nerds.company |
||
20 | */ |
||
21 | class CommandRunner extends ConsoleCommandRunner |
||
22 | { |
||
23 | // Public Methods |
||
24 | // ========================================================================= |
||
25 | |||
26 | /** |
||
27 | * @param string $name command name (case-insensitive) |
||
28 | * |
||
29 | * @return \CConsoleCommand The command object. Null if the name is invalid. |
||
30 | */ |
||
31 | public function createCommand($name) |
||
32 | { |
||
33 | $name = StringHelper::toLowerCase($name); |
||
34 | |||
35 | $command = null; |
||
36 | |||
37 | if (isset($this->commands[$name])) { |
||
38 | $command = $this->commands[$name]; |
||
39 | } else { |
||
40 | $commands = array_change_key_case($this->commands); |
||
41 | |||
42 | if (isset($commands[$name])) { |
||
43 | $command = $commands[$name]; |
||
44 | } |
||
45 | } |
||
46 | |||
47 | if ($command !== null) { |
||
48 | if (is_string($command)) { |
||
49 | $className = 'NerdsAndCompany\Schematic\ConsoleCommands\\'.IOHelper::getFileName($command, false); |
||
50 | |||
51 | return new $className($name, $this); |
||
52 | } else { |
||
53 | // an array configuration |
||
54 | |||
55 | return Craft::createComponent($command, $name, $this); |
||
56 | } |
||
57 | } elseif ($name === 'help') { |
||
58 | return new \CHelpCommand('help', $this); |
||
0 ignored issues
–
show
|
|||
59 | } else { |
||
60 | return; |
||
61 | } |
||
62 | } |
||
63 | } |
||
64 |
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.