SwitchTrait   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 44
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A registerClientScript() 0 16 3
1
<?php
2
/**
3
 * @link https://github.com/2amigos/yii2-switch-widget
4
 * @copyright Copyright (c) 2013-2017 2amigOS! Consulting Group LLC
5
 * @license http://opensource.org/licenses/BSD-3-Clause
6
 */
7
8
namespace dosamigos\switchinput;
9
10
use yii\helpers\ArrayHelper;
11
use yii\helpers\Json;
12
13
/**
14
 * SwitchTrait holds common attributes and methods for both [[SwitchBox]] and [[SwitchRadio]]
15
 *
16
 * @author Antonio Ramirez <[email protected]>
17
 * @link http://www.ramirezcobos.com/
18
 * @link http://www.2amigos.us/
19
 * @package dosamigos\yii2-switch-widget
20
 */
21
trait SwitchTrait
22
{
23
    /**
24
     * @var bool specifies whether the checkbox should be checked or unchecked, when not used with a model. If [[items]],
25
     * [[$checked]] will specify the value to select.
26
     */
27
    public $checked = false;
28
    /**
29
     * @var array the options for the Bootstrap Switch 3 plugin.
30
     * Please refer to the Bootstrap Switch 3 plugin Web page for possible options.
31
     * @see http://www.bootstrap-switch.org/
32
     */
33
    public $clientOptions = [];
34
    /**
35
     * @var array the event handlers for the underlying Bootstrap Switch 3 input JS plugin.
36
     * Please refer to the [Bootstrap Switch 3](http://www.bootstrap-switch.org/) plugin
37
     * Web page for possible events.
38
     */
39
    public $clientEvents = [];
40
    /**
41
     * @var string the DOM element selector
42
     */
43
    protected $selector;
44
45
    /**
46
     * Registers Bootstrap Switch plugin and related events
47
     */
48 6
    public function registerClientScript()
49
    {
50 6
        $view = $this->view;
0 ignored issues
show
Bug introduced by
The property view does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
51 6
        SwitchAsset::register($view);
52
53 6
        $this->clientOptions['animate'] = ArrayHelper::getValue($this->clientOptions, 'animate', true);
54 6
        $options = Json::encode($this->clientOptions);
55
56 6
        $js[] = ";jQuery('$this->selector').bootstrapSwitch($options);";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$js was never initialized. Although not strictly required by PHP, it is generally a good practice to add $js = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
57 6
        if (!empty($this->clientEvents)) {
58 6
            foreach ($this->clientEvents as $event => $handler) {
59 6
                $js[] = "jQuery('$this->selector').on('$event', $handler);";
60 2
            }
61 2
        }
62 6
        $view->registerJs(implode("\n", $js));
63 6
    }
64
}
65