Alert   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 73
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B init() 0 27 5
1
<?php
2
3
namespace cornernote\dashboard\widgets;
4
5
use \yii\bootstrap\Alert as BootstrapAlert;
6
use \yii\bootstrap\Widget;
7
8
/**
9
 * Alert widget renders a message from session flash for AdminLTE alerts. All flash messages are displayed
10
 * in the sequence they were assigned using setFlash. You can set message as following:
11
 *
12
 * ```php
13
 * \Yii::$app->getSession()->setFlash('error', '<b>Alert!</b> Danger alert preview. This alert is dismissable.');
14
 * ```
15
 *
16
 * Multiple messages could be set as follows:
17
 *
18
 * ```php
19
 * \Yii::$app->getSession()->setFlash('error', ['Error 1', 'Error 2']);
20
 * ```
21
 *
22
 * @author Evgeniy Tkachenko <[email protected]>
23
 */
24
class Alert extends Widget
25
{
26
    /**
27
     * @var array the alert types configuration for the flash messages.
28
     * This array is setup as $key => $value, where:
29
     * - $key is the name of the session flash variable
30
     * - $value is the array:
31
     *       - class of alert type (i.e. danger, success, info, warning)
32
     *       - icon for alert AdminLTE
33
     */
34
    public $alertTypes = [
35
        'error' => [
36
            'class' => 'alert-danger',
37
            'icon' => '<i class="icon fa fa-ban"></i>',
38
        ],
39
        'danger' => [
40
            'class' => 'alert-danger',
41
            'icon' => '<i class="icon fa fa-ban"></i>',
42
        ],
43
        'success' => [
44
            'class' => 'alert-success',
45
            'icon' => '<i class="icon fa fa-check"></i>',
46
        ],
47
        'info' => [
48
            'class' => 'alert-info',
49
            'icon' => '<i class="icon fa fa-info"></i>',
50
        ],
51
        'warning' => [
52
            'class' => 'alert-warning',
53
            'icon' => '<i class="icon fa fa-warning"></i>',
54
        ],
55
    ];
56
57
    /**
58
     * @var array the options for rendering the close button tag.
59
     */
60
    public $closeButton = [
61
        'label' => '<i class="fa fa-times"></i>',
62
    ];
63
64
    /**
65
     * Initializes the widget.
66
     * This method will register the bootstrap asset bundle. If you override this method,
67
     * make sure you call the parent implementation first.
68
     */
69
    public function init()
70
    {
71
        parent::init();
72
73
        $session = \Yii::$app->getSession();
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
74
        $flashes = $session->getAllFlashes();
75
        $appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
76
77
        foreach ($flashes as $type => $data) {
78
            if (isset($this->alertTypes[$type])) {
79
                $data = (array)$data;
80
                foreach ($data as $message) {
81
82
                    $this->options['class'] = $this->alertTypes[$type]['class'] . $appendCss;
83
                    $this->options['id'] = $this->getId() . '-' . $type;
84
85
                    echo BootstrapAlert::widget([
86
                        'body' => $this->alertTypes[$type]['icon'] . ' ' . $message,
87
                        'closeButton' => $this->closeButton,
88
                        'options' => $this->options,
89
                    ]);
90
                }
91
92
                $session->removeFlash($type);
93
            }
94
        }
95
    }
96
}
97