Completed
Push — master ( 1e7cdd...2a3f6b )
by Taosikai
55:23 queued 40:24
created

Application::getHelp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * CRM library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\Crm;
7
8
use Symfony\Component\Console\Application as BaseApplication;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class Application extends BaseApplication
13
{
14
    /**
15
     * Application name
16
     * @var string
17
     */
18
    const NAME = 'Composer Registry Manager';
19
20
    protected static $logo = <<<EOT
21
 _____   _____        ___  ___  
22
/  ___| |  _  \      /   |/   | 
23
| |     | |_| |     / /|   /| | 
24
| |     |  _  /    / / |__/ | | 
25
| |___  | | \ \   / /       | | 
26
\_____| |_|  \_\ /_/        |_| 
27
28
29
EOT;
30
31
    /**
32
     * @var RegistryManager
33
     */
34
    protected $manager;
35
36
    public function __construct(RegistryManager $manager = null)
37
    {
38
        $this->manager = $manager ?: new RegistryManager();
39
        parent::__construct(static::NAME);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     * @codeCoverageIgnore
45
     */
46
    public function run(InputInterface $input = null, OutputInterface $output = null)
47
    {
48
        $this->initializeConfigFile();
49
        return parent::run($input, $output);
50
    }
51
52
    /**
53
     * @codeCoverageIgnore
54
     */
55
    protected function initializeConfigFile()
56
    {
57
        if (!file_exists(ConfigPath::getUserConfigFile())) {
58
            Utils::getFilesystem()->copy(ConfigPath::getDefaultConfigFile(), ConfigPath::getUserConfigFile());
59
        }
60
        $this->manager->readRegistriesFromFile(ConfigPath::getUserConfigFile());
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getHelp()
67
    {
68
        return static::$logo . parent::getHelp();
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getDefaultCommands()
75
    {
76
        return array_merge([
0 ignored issues
show
Best Practice introduced by
The expression return array_merge(array...:getDefaultCommands()); seems to be an array, but some of its elements' types (Slince\Crm\Command\ListC...rm\Command\ResetCommand) are incompatible with the return type of the parent method Symfony\Component\Consol...ion::getDefaultCommands of type array<Symfony\Component\...le\Command\ListCommand>.

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...
77
            new Command\ListCommand($this->manager),
78
            new Command\UseCommand($this->manager),
79
            new Command\AddCommand($this->manager),
80
            new Command\RemoveCommand($this->manager),
81
            new Command\ResetCommand($this->manager),
82
        ], parent::getDefaultCommands());
83
    }
84
}
85