Completed
Push — master ( 147afb...10619b )
by Alexander
04:15
created

Application::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 11
cp 0
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of the Console-Kit library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/console-kit
9
 */
10
11
namespace ConsoleHelpers\ConsoleKit;
12
13
14
use Stecman\Component\Symfony\Console\BashCompletion\CompletionCommand;
15
use Symfony\Component\Console\Application as BaseApplication;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class Application extends BaseApplication
20
{
21
22
	/**
23
	 * Dependency injection container.
24
	 *
25
	 * @var Container
26
	 */
27
	protected $dic;
28
29
	/**
30
	 * Creates application.
31
	 *
32
	 * @param Container $container Container.
33
	 */
34
	public function __construct(Container $container)
35
	{
36
		$this->dic = $container;
37
38
		parent::__construct($this->dic['app_name'], $this->dic['app_version']);
39
40
		$helper_set = $this->getHelperSet();
41
		$helper_set->set($this->dic['container_helper']);
42
43
		$that = $this;
44
		$this->dic['helper_set'] = function () use ($that) {
45
			return $that->getHelperSet();
46
		};
47
	}
48
49
	/**
50
	 * {@inheritdoc}
51
	 */
52
	protected function getDefaultCommands()
53
	{
54
		$default_commands = parent::getDefaultCommands();
55
		$default_commands[] = new CompletionCommand();
56
57
		return $default_commands;
0 ignored issues
show
Best Practice introduced by
The expression return $default_commands; seems to be an array, but some of its elements' types (Stecman\Component\Symfon...etion\CompletionCommand) 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...
58
	}
59
60
	/**
61
	 * {@inheritdoc}
62
	 */
63
	public function run(InputInterface $input = null, OutputInterface $output = null)
64
	{
65
		if ( !isset($input) ) {
66
			$input = $this->dic['input'];
67
		}
68
69
		if ( !isset($output) ) {
70
			$output = $this->dic['output'];
71
		}
72
73
		return parent::run($input, $output);
74
	}
75
76
	/**
77
	 * Detects, when we're inside PHAR file.
78
	 *
79
	 * @return boolean
80
	 */
81
	protected function isPharFile()
82
	{
83
		return strpos(__DIR__, 'phar://') === 0;
84
	}
85
86
}
87