CroppieWidget   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 67
ccs 22
cts 28
cp 0.7856
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 2
A run() 0 6 1
B registerClientScript() 0 25 6
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-croppie-widget project.
5
 * (c) 2amigOS! <http://2amigos.us/>
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace dosamigos\croppie;
11
12
use yii\base\Widget;
13
use yii\helpers\Html;
14
use yii\helpers\Json;
15
16
class CroppieWidget extends Widget
17
{
18
    /**
19
     * @var array the displayed tag option
20
     */
21
    public $options = [];
22
    /**
23
     * @var array the plugin options
24
     * @see http://foliotek.github.io/Croppie/
25
     */
26
    public $clientOptions = [];
27
    /**
28
     * @var array the plugin events.
29
     * @see http://foliotek.github.io/Croppie/
30
     */
31
    public $clientEvents = [];
32
33
    /**
34
     * @inheritdoc
35
     */
36 12
    public function init()
37
    {
38 12
        if (!isset($this->options['id'])) {
39 9
            $this->options['id'] = $this->getId();
40 3
        }
41 12
        parent::init();
42 12
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 9
    public function run()
48
    {
49 9
        echo Html::tag('div', '', $this->options);
50
51 9
        $this->registerClientScript();
52 9
    }
53
54
    /**
55
     * Registers required script for the plugin to work
56
     */
57 9
    public function registerClientScript()
58
    {
59 9
        $view = $this->getView();
60
61 9
        CroppieAsset::register($view);
62
63 9
        if (isset($this->clientOptions['enableExif']) && $this->clientOptions['enableExif'] === true) {
64
            ExifJsAsset::register($view);
65
        }
66
67 9
        $options = !empty($this->clientOptions)
68 5
            ? Json::encode($this->clientOptions)
69 9
            : '';
70
71 9
        $id = $this->getId();
72
73 9
        $js[] = ";jQuery('#$id').croppie($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...
74 9
        if (!empty($this->clientEvents)) {
75
            foreach ($this->clientEvents as $event => $handler) {
76
                $js[] = "jQuery('#$id').on('$event', $handler);";
77
            }
78
        }
79
80 9
        $view->registerJs(implode("\n", $js));
81 9
    }
82
}
83