Alert   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B init() 0 33 6
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace zacksleo\yii2\backend\widgets;
9
10
/**
11
 * Alert widget renders a message from session flash. All flash messages are displayed
12
 * in the sequence they were assigned using setFlash. You can set message as following:
13
 *
14
 * - \Yii::$app->getSession()->setFlash('error', 'This is the message');
15
 * - \Yii::$app->getSession()->setFlash('success', 'This is the message');
16
 * - \Yii::$app->getSession()->setFlash('info', 'This is the message');
17
 *
18
 * @author Kartik Visweswaran <[email protected]>
19
 * @author Alexander Makarov <[email protected]>
20
 */
21
class Alert extends \yii\bootstrap\Widget
22
{
23
    /**
24
     * @var array the alert types configuration for the flash messages.
25
     * This array is setup as $key => $value, where:
26
     * - $key is the name of the session flash variable
27
     * - $value is the bootstrap alert type (i.e. danger, success, info, warning)
28
     */
29
    public $alertTypes = [
30
        'error' => 'alert-danger',
31
        'danger' => 'alert-danger',
32
        'success' => 'alert-success',
33
        'info' => 'alert-info',
34
        'warning' => 'alert-warning'
35
    ];
36
37
    /**
38
     * @var array the options for rendering the close button tag.
39
     */
40
    public $closeButton = [];
41
42
    public function init()
43
    {
44
        parent::init();
45
46
        $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...
47
        $flashes = $session->getAllFlashes();
48
        $appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
49
50
        foreach ($flashes as $type => $message) {
51
            if (isset($this->alertTypes[$type])) {
52
                /* initialize css class for each alert box */
53
                $this->options['class'] = $this->alertTypes[$type] . $appendCss;
54
                /* assign unique id to each alert box */
55
                $this->options['id'] = $this->getId() . '-' . $type;
56
                if (is_array($message)) {
57
                    foreach ($message as $msg) {
58
                        echo \yii\bootstrap\Alert::widget([
59
                            'body' => $msg,
60
                            'closeButton' => $this->closeButton,
61
                            'options' => $this->options,
62
                        ]);
63
                    }
64
                } else {
65
                    echo \yii\bootstrap\Alert::widget([
66
                        'body' => $message,
67
                        'closeButton' => $this->closeButton,
68
                        'options' => $this->options,
69
                    ]);
70
                }
71
                $session->removeFlash($type);
72
            }
73
        }
74
    }
75
}
76