Failed Conditions
Pull Request — master (#26)
by Bob Olde
07:33
created

src/console/CommandRunner.php (1 issue)

Upgrade to new PHP Analysis Engine

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
Bug Best Practice introduced by
The return type of return new \CHelpCommand('help', $this); (CHelpCommand) is incompatible with the return type documented by NerdsAndCompany\Schemati...ndRunner::createCommand of type CConsoleCommand.

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 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('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...
59
        } else {
60
            return;
61
        }
62
    }
63
}
64