GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Alert::init()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.1448
c 0
b 0
f 0
cc 5
nc 8
nop 0
1
<?php
2
3
namespace app\widgets;
4
5
/**
6
 * Alert widget renders a message from session flash. All flash messages are displayed
7
 * in the sequence they were assigned using setFlash. You can set message as following:
8
 *
9
 * ```php
10
 * \Yii::$app->getSession()->setFlash('error', 'This is the message');
11
 * \Yii::$app->getSession()->setFlash('success', 'This is the message');
12
 * \Yii::$app->getSession()->setFlash('info', 'This is the message');
13
 * ```
14
 *
15
 * Multiple messages could be set as follows:
16
 *
17
 * ```php
18
 * \Yii::$app->getSession()->setFlash('error', ['Error 1', 'Error 2']);
19
 * ```
20
 *
21
 * @author Kartik Visweswaran <[email protected]>
22
 * @author Alexander Makarov <[email protected]>
23
 */
24
class Alert extends \yii\bootstrap\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 bootstrap alert type (i.e. danger, success, info, warning)
31
     */
32
    public $alertTypes = [
33
        'error'   => 'alert-danger',
34
        'danger'  => 'alert-danger',
35
        'success' => 'alert-success',
36
        'info'    => 'alert-info',
37
        'warning' => 'alert-warning'
38
    ];
39
40
    /**
41
     * @var array the options for rendering the close button tag.
42
     */
43
    public $closeButton = [];
44
45
    public function init()
46
    {
47
        parent::init();
48
49
        $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...
50
        $flashes = $session->getAllFlashes();
51
        $appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
52
53
        foreach ($flashes as $type => $data) {
54
            if (isset($this->alertTypes[$type])) {
55
                $data = (array) $data;
56
                foreach ($data as $message) {
57
                    /* initialize css class for each alert box */
58
                    $this->options['class'] = $this->alertTypes[$type] . $appendCss;
59
60
                    /* assign unique id to each alert box */
61
                    $this->options['id'] = $this->getId() . '-' . $type;
62
63
                    echo \yii\bootstrap\Alert::widget([
64
                        'body' => $message,
65
                        'closeButton' => $this->closeButton,
66
                        'options' => $this->options,
67
                    ]);
68
                }
69
70
                $session->removeFlash($type);
71
            }
72
        }
73
    }
74
}
75