Issues (33)

widgets/Alert.php (2 issues)

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

57
            /** @scrutinizer ignore-call */ 
58
            $flash = $session->getFlash($type);

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...
58
59
            foreach ((array) $flash as $i => $message) {
60
                echo \yii\bootstrap5\Alert::widget([
61
                    'body' => $message,
62
                    'closeButton' => $this->closeButton,
63
                    'options' => array_merge($this->options, [
64
                        'id' => $this->getId() . '-' . $type . '-' . $i,
65
                        'class' => $this->alertTypes[$type] . $appendClass,
66
                    ]),
67
                ]);
68
            }
69
70
            $session->removeFlash($type);
71
        }
72
    }
73
}
74