XEditableTrait   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 125
Duplicated Lines 14.4 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 5
dl 18
loc 125
ccs 0
cts 94
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A registerAssets() 0 11 3
A init() 0 5 1
A registerMyJs() 0 11 3
A prepareValue() 0 21 5
A prepareDataValue() 9 14 3
A prepareDisplayValue() 9 22 5
A prepareHtml() 0 17 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * X-editable extension for Yii2
4
 *
5
 * @link      https://github.com/hiqdev/yii2-x-editable
6
 * @package   yii2-x-editable
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\xeditable\traits;
12
13
use hipanel\helpers\ArrayHelper;
14
use hiqdev\xeditable\assets\XEditableAsset;
15
use vova07\select2\Select2Asset;
16
use Yii;
17
use yii\helpers\Html;
18
use yii\helpers\Json;
19
use yii\helpers\Url;
20
21
/**
22
 * Class XEditableTrait.
23
 */
24
trait XEditableTrait
25
{
26
    /**
27
     * @var array
28
     */
29
    public $pluginOptions = [];
30
31
    /**
32
     * @var string library to be used: bootstrap, jqueryui, plain jquery
33
     */
34
    public $library;
35
36
    /**
37
     * @var array will be passed to [[Html::a]] method as options
38
     */
39
    public $linkOptions = [];
40
41
    public function registerAssets()
42
    {
43
        $asset = new XEditableAsset();
44
        if ($this->library) {
45
            $asset->library = $this->library;
46
        }
47
        $asset->register(Yii::$app->view);
48
        if ($this->pluginOptions['type'] === 'select2') {
49
            Select2Asset::register(Yii::$app->view);
50
        }
51
    }
52
53
    public function init()
54
    {
55
        parent::init();
56
        $this->registerAssets();
57
    }
58
59
    public function registerMyJs($data)
60
    {
61
        if (!$this->pluginOptions['title']) {
62
            $this->pluginOptions['title'] = $data['model']->getAttributeLabel($data['attribute']);
63
        }
64
        if ($this->pluginOptions['url']) {
65
            $this->pluginOptions['url'] = Url::to($this->pluginOptions['url']);
66
        }
67
        $selector = ArrayHelper::remove($this->pluginOptions, 'selector', ".editable[data-pk={$data['model']->primaryKey}][data-name={$data['attribute']}]");
68
        Yii::$app->view->registerJs('$("' . $selector . '").editable(' . Json::htmlEncode($this->pluginOptions) . ');');
69
    }
70
71
    public function prepareValue($data)
72
    {
73
        if ($data['value'] !== null) {
74
            if ($data['value'] instanceof \Closure) {
75
                $value = call_user_func($data['value'], $this->model, $this);
0 ignored issues
show
Bug introduced by
The property model 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...
76
            } else {
77
                $value = $data['value'];
78
            }
79
        } else {
80
            $value = $data['model']->getAttribute($data['attribute']);
81
            if (is_array($value)) {
82
                if (ArrayHelper::isAssociative($value)) {
83
                    $value = array_keys($value);
84
                }
85
            } else {
86
                $value = [$value];
87
            }
88
        }
89
90
        $this->pluginOptions['value'] = $value;
91
    }
92
93
    protected function prepareDataValue($data)
94
    {
95 View Code Duplication
        if (isset($this->pluginOptions['data-value'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
            if ($this->pluginOptions['data-value'] instanceof \Closure) {
97
                $result = call_user_func($this->pluginOptions['data-value'], $this, $data);
98
            } else {
99
                $result = $this->pluginOptions['data-value'];
100
            }
101
        } else {
102
            $result = $this->pluginOptions['value'];
103
        }
104
105
        $this->pluginOptions['data-value'] = $result;
106
    }
107
108
    protected function prepareDisplayValue($data)
109
    {
110 View Code Duplication
        if (isset($this->pluginOptions['data-display-value'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
            if ($this->pluginOptions['data-display-value'] instanceof \Closure) {
112
                $result = call_user_func($this->pluginOptions['data-display-value'], $this, $data);
113
            } else {
114
                $result = $this->pluginOptions['data-display-value'];
115
            }
116
        } else {
117
            $result = $this->pluginOptions['value'];
118
        }
119
120
        if (is_array($result)) {
121
            if ($this->pluginOptions['source']) {
122
                $result = ArrayHelper::getValues($this->pluginOptions['source'], $result);
123
            }
124
125
            $result = implode('<br />', $result);
126
        }
127
128
        $this->pluginOptions['data-display-value'] = $result;
129
    }
130
131
    public function prepareHtml($data)
132
    {
133
        $this->prepareValue($data);
134
        $this->prepareDataValue($data);
135
        $this->prepareDisplayValue($data);
136
        $this->registerMyJs($data);
137
138
        $params = ArrayHelper::merge([
139
            'id' => $this->getId(),
0 ignored issues
show
Bug introduced by
It seems like getId() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
140
            'class' => 'editable',
141
            'data-pk' => $data['model']->primaryKey,
142
            'data-name' => $data['attribute'],
143
            'data-value' => $this->pluginOptions['data-value'],
144
        ], $this->linkOptions);
145
146
        return Html::a($this->pluginOptions['data-display-value'], '#', $params);
147
    }
148
}
149