Completed
Push — master ( df77c7...554437 )
by Alexey
03:31
created

Alert::renderFlashAlerts()   D

Complexity

Conditions 10
Paths 42

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 20
cts 20
cp 1
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 20
nc 42
nop 0
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 4
    public function init()
52
    {
53 4
        parent::init();
54 4
        $this->registerAssets();
55 4
    }
56
57
    /**
58
     * @return string|void
59
     */
60 3
    public function run()
61
    {
62 3
        if ($this->useSessionFlash) {
63 2
            $this->renderFlashAlerts();
64
        } else {
65 1
            $this->registerSwal($this->getOptions(), $this->callback);
66
        }
67 3
    }
68
69
    /**
70
     * Renders alerts from session flash settings.
71
     * @see [[\yii\web\Session::getAllFlashes()]]
72
     */
73 2
    public function renderFlashAlerts()
74
    {
75 2
        $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...
76 2
        $flashes = $session->getAllFlashes();
77 2
        $steps = [];
78 2
        foreach ($flashes as $type => $data) {
79 2
            $data = (array)$data;
80 2
            foreach ($data as $message) {
81 2
                array_push($steps, ['type' => $type, 'text' => $message]);
82
            }
83 2
            $session->removeFlash($type);
84
        }
85 2
        if (!empty($steps)) {
86 2
            if (!is_array($steps[0]['text'])) {
87 1
                $this->registerSwalQueue($steps);
88
            } else {
89 1
                $steps[0]['text']['type'] = isset($steps[0]['text']['type']) ? $steps[0]['text']['type'] : $steps[0]['type'];
90 1
                if (isset($steps[0]['text']['animation']) && $steps[0]['text']['animation'] == false) {
91 1
                    if (isset($steps[0]['text']['customClass'])) {
92 1
                        $this->registerAnimate();
93
                    }
94
                }
95 1
                $this->options = $steps[0]['text'];
96 1
                $this->callback = isset($steps[1]['text']['callback']) ? $steps[1]['text']['callback'] : $this->callback;
97 1
                $this->registerSwal($this->getOptions(), $this->callback);
98
            }
99
        }
100 2
    }
101
102
    /**
103
     * Get widget options
104
     *
105
     * @return string
106
     */
107 3
    public function getOptions()
108
    {
109 3
        if (isset($this->options['id']))
110 2
            unset($this->options['id']);
111
112 3
        if (ArrayHelper::isIndexed($this->options)) {
113 1
            $str = '';
114 1
            foreach ($this->options as $value) {
115 1
                $str .= '"' . $value . '",';
116
            }
117 1
            return chop($str, ' ,');
118
        }
119 2
        return Json::encode($this->options);
120
    }
121
122
    /**
123
     * @param array $steps
124
     */
125 1
    protected function registerSwalQueue($steps = [])
126
    {
127 1
        $view = $this->getView();
128 1
        $js = "swal.queue(" . Json::encode($steps) . ");";
129 1
        $this->view->registerJs($js, $view::POS_END);
130 1
    }
131
132
    /**
133
     * @param string $options
134
     * @param string $callback
135
     */
136 2
    protected function registerSwal($options = '', $callback = '')
137
    {
138 2
        $view = $this->getView();
139 2
        $js = "swal({$options}).then({$callback}).catch(swal.noop);";
140 2
        $this->view->registerJs($js, $view::POS_END);
141 2
    }
142
143
    /**
144
     * Register Animate Assets
145
     */
146 2
    protected function registerAnimate()
147
    {
148 2
        AnimateCssAsset::register($this->view);
149 2
    }
150
151
    /**
152
     * Register client assets
153
     */
154 4
    protected function registerAssets()
155
    {
156 4
        SweetAlert2Asset::register($this->view);
157 4
        if (isset($this->options['animation']) && $this->options['animation'] == false) {
158 1
            if (isset($this->options['customClass'])) {
159 1
                $this->registerAnimate();
160
            }
161
        }
162 4
    }
163
}
164