Alert   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

1 Method

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