Issues (1313)

widgets/Alert.php (1 issue)

Labels
Severity
1
<?php
2
namespace app\widgets;
3
4
use Yii;
5
6
/**
7
 * Alert widget renders a message from session flash. All flash messages are displayed
8
 * in the sequence they were assigned using setFlash. You can set message as following:
9
 *
10
 * ```php
11
 * Yii::$app->session->setFlash('error', 'This is the message');
12
 * Yii::$app->session->setFlash('success', 'This is the message');
13
 * Yii::$app->session->setFlash('info', 'This is the message');
14
 * ```
15
 *
16
 * Multiple messages could be set as follows:
17
 *
18
 * ```php
19
 * Yii::$app->session->setFlash('error', ['Error 1', 'Error 2']);
20
 * ```
21
 *
22
 * @author Kartik Visweswaran <[email protected]>
23
 * @author Alexander Makarov <[email protected]>
24
 */
25
class Alert extends \yii\bootstrap\Widget
26
{
27
    /**
28
     * @var array the alert types configuration for the flash messages.
29
     * This array is setup as $key => $value, where:
30
     * - key: the name of the session flash variable
31
     * - value: the bootstrap alert type (i.e. danger, success, info, warning)
32
     */
33
    public $alertTypes = [
34
        'error'   => 'alert-danger',
35
        'danger'  => 'alert-danger',
36
        'success' => 'alert-success',
37
        'info'    => 'alert-info',
38
        'warning' => 'alert-warning'
39
    ];
40
    /**
41
     * @var array the options for rendering the close button tag.
42
     * Array will be passed to [[\yii\bootstrap\Alert::closeButton]].
43
     */
44
    public $closeButton = [];
45
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function run()
51
    {
52
        $session = Yii::$app->session;
53
        $flashes = $session->getAllFlashes();
0 ignored issues
show
The method getAllFlashes() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        /** @scrutinizer ignore-call */ 
54
        $flashes = $session->getAllFlashes();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
        $appendClass = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
55
56
        foreach ($flashes as $type => $flash) {
57
            if (!isset($this->alertTypes[$type])) {
58
                continue;
59
            }
60
61
            foreach ((array) $flash as $i => $message) {
62
                echo \yii\bootstrap\Alert::widget([
63
                    'body' => $message,
64
                    'closeButton' => $this->closeButton,
65
                    'options' => array_merge($this->options, [
66
                        'id' => $this->getId() . '-' . $type . '-' . $i,
67
                        'class' => $this->alertTypes[$type] . $appendClass,
68
                    ]),
69
                ]);
70
            }
71
72
            $session->removeFlash($type);
73
        }
74
    }
75
}
76