Alert::init()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
c 0
b 0
f 0
rs 8.439
cc 5
eloc 16
nc 8
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Cjt Terabyte LLC [yii2-extension].
5
 *
6
 * (c) Cjt Terabyte LLC [yii2-extension] <http://github.com/cjtterabytesoft>.
7
 * For the full copyright and license information, please view the LICENSE.md.
8
 * file that was distributed with this source code.
9
 *
10
 * @link http://www.cjtterabyte.com.
11
 * @author Wilmer Arámbula <[email protected]>.
12
 * @copyright (c) 2015 Cjt Terabyte LLC.
13
 * @Extension: [yii2-adminlte-advanced].
14
 * @Widget [Alert].
15
 * @since 1.0
16
 */
17
18
/**
19
 * @link http://www.yiiframework.com/
20
 * @copyright Copyright (c) 2008 Yii Software LLC
21
 * @license http://www.yiiframework.com/license/
22
 */
23
24
namespace cjtterabytesoft\adminlte\advanced\widgets;
25
26
/**
27
 * Alert widget renders a message from session flash. All flash messages are displayed
28
 * in the sequence they were assigned using setFlash. You can set message as following:
29
 *
30
 * ```php
31
 * \Yii::$app->getSession()->setFlash('error', 'This is the message');
32
 * \Yii::$app->getSession()->setFlash('success', 'This is the message');
33
 * \Yii::$app->getSession()->setFlash('info', 'This is the message');
34
 * ```
35
 *
36
 * Multiple messages could be set as follows:
37
 *
38
 * ```php
39
 * \Yii::$app->getSession()->setFlash('error', ['Error 1', 'Error 2']);
40
 * ```
41
 *
42
 * @author Kartik Visweswaran <[email protected]>
43
 * @author Alexander Makarov <[email protected]>
44
 */
45
class Alert extends \yii\bootstrap\Widget
46
{
47
    /**
48
     * @var array the alert types configuration for the flash messages.
49
     * This array is setup as $key => $value, where:
50
     * - $key is the name of the session flash variable
51
     * - $value is the bootstrap alert type (i.e. danger, success, info, warning)
52
     */
53
    public $alertTypes = [
54
        'error'   => 'alert-danger',
55
        'danger'  => 'alert-danger',
56
        'success' => 'alert-success',
57
        'info'    => 'alert-info',
58
        'warning' => 'alert-warning'
59
    ];
60
61
    /**
62
     * @var array the options for rendering the close button tag.
63
     */
64
    public $closeButton = [];
65
66
    public function init()
67
    {
68
        parent::init();
69
70
        $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...
71
        $flashes = $session->getAllFlashes();
72
        $appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
73
74
        foreach ($flashes as $type => $data) {
75
            if (isset($this->alertTypes[$type])) {
76
                $data = (array) $data;
77
                foreach ($data as $message) {
78
                    /* initialize css class for each alert box */
79
                    $this->options['class'] = $this->alertTypes[$type] . $appendCss;
80
81
                    /* assign unique id to each alert box */
82
                    $this->options['id'] = $this->getId() . '-' . $type;
83
84
                    echo \yii\bootstrap\Alert::widget([
85
                        'body' => $message,
86
                        'closeButton' => $this->closeButton,
87
                        'options' => $this->options,
88
                    ]);
89
                }
90
91
                $session->removeFlash($type);
92
            }
93
        }
94
    }
95
}
96