Completed
Push — master ( c69ec8...812866 )
by Alexey
05:10
created

Alert.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace dominus77\sweetalert2;
4
5
use Yii;
6
use yii\bootstrap\Widget;
7
use yii\helpers\Json;
8
use yii\helpers\ArrayHelper;
9
use dominus77\sweetalert2\assets\SweetAlert2Asset;
10
use dominus77\sweetalert2\assets\AnimateCssAsset;
11
12
/**
13
 * Alert widget renders a message from session flash or custom messages.
14
 * @package dominus77\sweetalert2
15
 */
16
class Alert extends Widget
17
{
18
    //modal type
19
    const TYPE_INFO = 'info';
20
    const TYPE_ERROR = 'error';
21
    const TYPE_SUCCESS = 'success';
22
    const TYPE_WARNING = 'warning';
23
    const TYPE_QUESTION = 'question';
24
    //input type
25
    const INPUT_TYPE_TEXT = 'text';
26
    const INPUT_TYPE_EMAIL = 'email';
27
    const INPUT_TYPE_PASSWORD = 'password';
28
    const INPUT_TYPE_NUMBER = 'number';
29
    const INPUT_TYPE_RANGE = 'range';
30
    const INPUT_TYPE_TEXTAREA = 'textarea';
31
    const INPUT_TYPE_SELECT = 'select';
32
    const INPUT_TYPE_RADIO = 'radio';
33
    const INPUT_TYPE_CHECKBOX = 'checkbox';
34
    const INPUT_TYPE_FILE = 'file';
35
36
    /**
37
     * All the flash messages stored for the session are displayed and removed from the session
38
     * Defaults to false.
39
     * @var bool
40
     */
41
    public $useSessionFlash = false;
42
43
    /**
44
     * @var string alert callback
45
     */
46
    public $callback = 'function() {}';
47
48
    /**
49
     * Initializes the widget
50
     */
51
    public function init()
52
    {
53
        parent::init();
54
        $this->registerAssets();
55
    }
56
57
    /**
58
     * @return string|void
59
     */
60
    public function run()
61
    {
62
        if ($this->useSessionFlash) {
63
            $session = Yii::$app->getSession();
0 ignored issues
show
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...
64
            $flashes = $session->getAllFlashes();
65
            $steps = [];
66
            foreach ($flashes as $type => $data) {
67
                $data = (array)$data;
68
                foreach ($data as $message) {
69
                    array_push($steps, ['type' => $type, 'text' => $message]);
70
                }
71
                $session->removeFlash($type);
72
            }
73
            if (!empty($steps)) {
74
                if (!is_array($steps[0]['text'])) {
75
                    $this->registerSwalQueue($steps);
76
                } else {
77
                    $steps[0]['text']['type'] = isset($steps[0]['text']['type']) ? $steps[0]['text']['type'] : $steps[0]['type'];
78
                    if (isset($steps[0]['text']['animation']) && $steps[0]['text']['animation'] == false) {
79
                        if (isset($steps[0]['text']['customClass'])) {
80
                            $this->registerAnimate();
81
                        }
82
                    }
83
                    $this->options = $steps[0]['text'];
84
                    $this->callback = isset($steps[1]['text']['callback']) ? $steps[1]['text']['callback'] : $this->callback;
85
                    $this->registerSwal($this->getOptions(), $this->callback);
86
                }
87
            }
88
        } else {
89
            $this->registerSwal($this->getOptions(), $this->callback);
90
        }
91
    }
92
93
    /**
94
     * Get widget options
95
     *
96
     * @return string
97
     */
98
    public function getOptions()
99
    {
100
        if (isset($this->options['id']))
101
            unset($this->options['id']);
102
103
        if (ArrayHelper::isIndexed($this->options)) {
104
            $str = '';
105
            foreach ($this->options as $value) {
106
                $str .= '"' . $value . '",';
107
            }
108
            return chop($str, ' ,');
109
        }
110
        return Json::encode($this->options);
111
    }
112
113
    /**
114
     * @param array $steps
115
     */
116
    protected function registerSwalQueue($steps = [])
117
    {
118
        $view = $this->getView();
119
        $js = "swal.queue(" . Json::encode($steps) . ");";
120
        $this->view->registerJs($js, $view::POS_END);
121
    }
122
123
    /**
124
     * @param string $options
125
     * @param string $callback
126
     */
127
    protected function registerSwal($options = '', $callback = '')
128
    {
129
        $view = $this->getView();
130
        $js = "swal({$options}).then({$callback}).catch(swal.noop);";
131
        $this->view->registerJs($js, $view::POS_END);
132
    }
133
134
    /**
135
     * Register Animate Assets
136
     */
137
    protected function registerAnimate()
138
    {
139
        AnimateCssAsset::register($this->view);
140
    }
141
142
    /**
143
     * Register client assets
144
     */
145
    protected function registerAssets()
146
    {
147
        SweetAlert2Asset::register($this->view);
148
        if (isset($this->options['animation']) && $this->options['animation'] == false) {
149
            if (isset($this->options['customClass'])) {
150
                $this->registerAnimate();
151
            }
152
        }
153
    }
154
}
155