Completed
Push — master ( b4d6a5...352f6d )
by Basil
02:42
created

Application::coreCommands()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace luya\console;
4
5
use luya\traits\ApplicationTrait;
6
7
/**
8
 * LUYA Console/CLI Application.
9
 *
10
 * @property \luya\console\ErrorHandler $errorHandler The error handler component.
11
 *
12
 * @author Basil Suter <[email protected]>
13
 * @since 1.0.0
14
 */
15
class Application extends \yii\console\Application
16
{
17
    use ApplicationTrait;
18
19
    /**
20
     * @var bool Mute the Applications ouput, this is used to make application
21
     * cli tests with no output. The `luya\console\Controller` output/print methods are listening
22
     * to this property.
23
     */
24
    public $mute = false;
25
26
    /**
27
     * Provides an array with all LUYA core commands.
28
     *
29
     * Instead of overriding the core command application and merged the value we directly copied them.
30
     *
31
     * @see \yii\console\Application::coreCommands()
32
     */
33
    public function coreCommands()
34
    {
35
        return [
0 ignored issues
show
Best Practice introduced by
The expression return array('asset' => ...ds\\HealthController'); seems to be an array, but some of its elements' types (array<string,string>) are incompatible with the return type of the parent method yii\console\Application::coreCommands of type array<string,string>.

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 new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return '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...
36
            // yii default commands
37
            'asset' => 'yii\console\controllers\AssetController',
38
            'cache' => 'yii\console\controllers\CacheController',
39
            'fixture' => 'yii\console\controllers\FixtureController',
40
            'help' => 'yii\console\controllers\HelpController',
41
            'message' => 'yii\console\controllers\MessageController',
42
            'serve' => [
43
                'class' => 'yii\console\controllers\ServeController',
44
                'docroot' => '@app/public_html',
45
            ],
46
            // luya default commands
47
            'migrate' => 'luya\console\commands\MigrateController',
48
            'module' => 'luya\console\commands\ModuleController',
49
            'theme' => 'luya\console\commands\ThemeController',
50
            'import' => 'luya\console\commands\ImportController',
51
            'health' => 'luya\console\commands\HealthController',
52
        ];
53
    }
54
    
55
    /**
56
     * @inheritdoc
57
     */
58
    public function coreComponents()
59
    {
60
        return array_merge($this->luyaCoreComponents(), [
61
            'errorHandler' => ['class' => 'luya\console\ErrorHandler'],
62
        ]);
63
    }
64
}
65