BootstrapFileInput   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 52
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 9 2
A registerClientScript() 0 19 4
1
<?php
2
/**
3
 * @link https://github.com/2amigos/yii2-file-input-widget
4
 *
5
 * @copyright Copyright (c) 2013-2015 2amigOS! Consulting Group LLC
6
 * @license http://opensource.org/licenses/BSD-3-Clause
7
 */
8
9
namespace dosamigos\fileinput;
10
11
use yii\helpers\Html;
12
use yii\helpers\Json;
13
use yii\widgets\InputWidget;
14
15
/**
16
 * BootstrapFileInput widget renders the improved and amazing plugin version from Krajee. It supports multiple file
17
 * preview with both images and/or text types.
18
 *
19
 * @author Antonio Ramirez <[email protected]>
20
 *
21
 * @link http://www.ramirezcobos.com/
22
 * @link http://www.2amigos.us/
23
 */
24
class BootstrapFileInput extends InputWidget
25
{
26
    /**
27
     * @var array the options for the Bootstrap File Input plugin. Default options have exporting enabled.
28
     * Please refer to the Bootstrap File Input plugin Web page for possible options.
29
     *
30
     * @see http://plugins.krajee.com/file-input#options
31
     */
32
    public $clientOptions = [];
33
    /**
34
     * @var array the event handlers for the underlying Jasny file input JS plugin.
35
     * Please refer to the [Bootstrap File Input](http://plugins.krajee.com/file-input#events) plugin
36
     * Web page for possible events.
37
     */
38
    public $clientEvents = [];
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 3
    public function run()
44
    {
45 3
        if ($this->hasModel()) {
46 3
            echo Html::activeFileInput($this->model, $this->attribute, $this->options);
47 3
        } else {
48 3
            echo Html::fileInput($this->name, $this->value, $this->options);
49
        }
50 3
        $this->registerClientScript();
51 3
    }
52
53
    /**
54
     * Registers Bootstrap File Input plugin.
55
     */
56 3
    public function registerClientScript()
57
    {
58 3
        $view = $this->getView();
59
60 3
        BootstrapFileInputAsset::register($view);
61
62 3
        $id = $this->options['id'];
63
64 3
        $options = !empty($this->clientOptions) ? Json::encode($this->clientOptions) : '';
65
66 3
        $js[] = ";jQuery('#$id').fileinput({$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...
67
68 3
        if (!empty($this->clientEvents)) {
69 3
            foreach ($this->clientEvents as $event => $handler) {
70 3
                $js[] = ";jQuery('#$id').on('$event', $handler);";
71 3
            }
72 3
        }
73 3
        $view->registerJs(implode("\n", $js));
74 3
    }
75
}
76