AbstractJsAlert::getMessages()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Fwlib\Html\Generator\Element;
3
4
use Fwlib\Html\Generator\AbstractElement;
5
use Fwlib\Html\Generator\Helper\GetJsLoadHtmlTrait;
6
7
/**
8
 * Alert box with pure js
9
 *
10
 * @copyright   Copyright 2014-2015 Fwolf
11
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
12
 */
13
abstract class AbstractJsAlert extends AbstractElement
14
{
15
    use GetJsLoadHtmlTrait;
16
17
18
    /**
19
     * {@inheritdoc}
20
     *
21
     * Configs
22
     * - messages: Messages to alert, string[], auto number index
23
     * - showBackground: boolean, default true
24
     * - showClose: boolean, default true
25
     */
26
    protected function getDefaultConfigs()
27
    {
28
        return array_merge(parent::getDefaultConfigs(), [
0 ignored issues
show
Best Practice introduced by
The expression return array_merge(paren...owCloseLink' => true)); seems to be an array, but some of its elements' types (array|boolean) are incompatible with the return type of the parent method Fwlib\Html\Generator\Abs...ment::getDefaultConfigs of type array<string,null|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...
29
            'messages'       => [],
30
            'showBackground' => true,
31
            'showCloseLink'  => true,
32
        ]);
33
    }
34
35
36
    /**
37
     * @return string[]
38
     */
39
    protected function getMessages()
40
    {
41
        return $this->getConfig('messages');
42
    }
43
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function getOutputForShowMode()
49
    {
50
        $messages = $this->getMessages();
51
        if (empty($messages)) {
52
            return '';
53
        }
54
55
        $title = $this->getTitle();
56
        $idStr = $this->getId();
57
58
        $showCloseLink = $this->isShowCloseLink()
59
            ? 'true' : 'false';
60
        $showBackground = $this->isShowBackground()
61
            ? 'true' : 'false';
62
63
        $output = $this->getJsLoadHtml() .
64
            "<script type='text/javascript'>
65
<!--
66
(function () {
67
  JsAlert(
68
    ['" . implode("', '", $messages) . "'],
69
    '$title',
70
    '$idStr',
71
    $showCloseLink,
72
    $showBackground
73
  );
74
}) ();
75
-->
76
</script>";
77
78
        return $output;
79
    }
80
81
82
    /**
83
     * @return  bool
84
     */
85
    protected function isShowBackground()
86
    {
87
        return $this->getConfig('showBackground');
88
    }
89
90
91
    /**
92
     * @return  bool
93
     */
94
    protected function isShowCloseLink()
95
    {
96
        return $this->getConfig('showCloseLink');
97
    }
98
}
99