ConfigPanel::getPanel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Webino™ (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino for the canonical source repository
6
 * @copyright   Copyright (c) 2015-2017 Webino, s.r.o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoAppLib\Debugger\Bar;
12
13
use WebinoAppLib\Service\DebuggerInterface;
14
use Zend\Config\Config;
15
16
/**
17
 * Class ConfigPanel
18
 */
19
class ConfigPanel extends AbstractPanel
20
{
21
    /**
22
     * Config panel name
23
     */
24
    const NAME = 'Webino:config';
25
26
    /**
27
     * @var Config
28
     */
29
    private $config;
30
31
    /**
32
     * @var DebuggerInterface
33
     */
34
    private $debugger;
35
36
    /**
37
     * @return string
38
     */
39
    protected function getTitle()
40
    {
41
        return 'Application config';
42
    }
43
44
    /**
45
     * @param object|Config $config
46
     * @param object|DebuggerInterface $debugger
47
     */
48
    public function __construct(Config $config, DebuggerInterface $debugger)
49
    {
50
        $this->config   = $config;
51
        $this->debugger = $debugger;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getTab()
58
    {
59
        return $this->createIcon('config',[ 'margin-top' => '5px']) . parent::getTab();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createIcon...')) . parent::getTab(); (string) is incompatible with the return type of the parent method WebinoAppLib\Debugger\Bar\AbstractPanel::getTab of type WebinoHtmlLib\Html\InlineText.

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...
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getPanel()
66
    {
67
        $this->setContent($this->debugger->dump($this->config->toArray(), true));
68
        return $this->renderTemplate('config');
69
    }
70
}
71