Completed
Push — master ( bf7075...9e5f92 )
by Andrii
06:20
created

Flashes::normalizeMessage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
/*
4
 * Yii2 adapter for PNotify JQuery extension
5
 *
6
 * @link      https://github.com/hiqdev/yii2-pnotify
7
 * @package   yii2-pnotify
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiqdev\pnotify;
13
14
use Yii;
15
use yii\helpers\ArrayHelper;
16
17
/**
18
 * Flashes widget renders messages from session flashes. All flash messages are displayed
19
 * in the sequence they were assigned using setFlash. You can set message as following:.
20
 *
21
 * ```php
22
 * Yii::$app->getSession()->setFlash('error', 'This is the message');
23
 * Yii::$app->getSession()->setFlash('success', 'This is the message');
24
 * Yii::$app->getSession()->setFlash('info', 'This is the message');
25
 * ```
26
 *
27
 * Multiple messages could be set as follows:
28
 *
29
 * ```php
30
 * Yii::$app->getSession()->setFlash('error', ['Error 1', 'Error 2']);
31
 * ```
32
 */
33
class Flashes extends \yii\bootstrap\Widget
34
{
35
    /**
36
     * @var array the alert types configuration for the flash messages.
37
     * This array is setup as $key => $value, where:
38
     * - $key is the name of the session flash variable
39
     * - $value is the bootstrap alert type (i.e. danger, success, info, warning)
40
     */
41
    public $alertTypes = [
42
        'error' => 'alert-danger',
43
        'danger' => 'alert-danger',
44
        'success' => 'alert-success',
45
        'info' => 'alert-info',
46
        'warning' => 'alert-warning',
47
    ];
48
49
    /**
50
     * @var array the options for rendering the close button tag.
51
     */
52
    public $closeButton = [];
53
54
    /**
55
     * @param mixed $message Flash value to be normalized
56
     * @return array
57
     */
58
    public function normalizeMessage($message)
59
    {
60
        $res = [];
61
        if (is_string($message)) {
62
            $res['text'] = $message;
63
        } elseif (is_array($message)) {
64
            $res = $message;
65
        }
66
67
        return $res;
68
    }
69
70
    public function run()
71
    {
72
        $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...
73
        $flashes = $session->getAllFlashes();
74
        $notifications = [];
75
76
        foreach ($flashes as $type => $data) {
77
            if (isset($this->alertTypes[$type])) {
78
                $data = (array) $data;
79
80
                foreach ($data as $message) {
81
                    $message = $this->normalizeMessage($message);
82
                    $notifications[] = ArrayHelper::merge([
83
                        'type' => $type,
84
                    ], $message);
85
                }
86
                $session->removeFlash($type);
87
            }
88
        }
89
90
        echo PNotify::widget([
91
            'notifications' => $notifications,
92
        ]);
93
    }
94
}
95