Completed
Push — master ( 9bf7f5...90a8df )
by wen
12:07
created

Select   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 190
Duplicated Lines 16.32 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 24
c 3
b 0
f 0
lcom 1
cbo 3
dl 31
loc 190
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getSize() 0 4 1
A setSize() 0 6 1
A getValue() 0 4 1
A isOptionsModel() 0 4 2
A isRelation() 0 4 1
B getOptions() 11 23 5
A setOptions() 0 6 1
A setOptionsFromModel() 0 22 3
A getOptionsModel() 20 20 3
A getOptionsLabelAttribute() 0 4 1
A setOptionsLabelAttribute() 0 6 1
A getOptionsValueAttribute() 0 4 1
A setOptionsValueAttribute() 0 6 1
A toArray() 0 7 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
namespace Sco\Admin\Form\Elements;
4
5
use Illuminate\Database\Eloquent\Model;
6
use InvalidArgumentException;
7
use Sco\Admin\Contracts\RepositoryInterface;
8
9
class Select extends NamedElement
10
{
11
12
    protected $type = 'select';
13
14
    protected $size = '';
15
16
    protected $options;
17
18
    protected $optionsLabelAttribute;
19
20
    protected $optionsValueAttribute;
21
22
    public function __construct($name, $title, $options)
23
    {
24
        parent::__construct($name, $title);
25
26
        $this->setOptions($options);
27
    }
28
29
    public function getSize()
30
    {
31
        return $this->size;
32
    }
33
34
    public function setSize($value)
35
    {
36
        $this->size = $value;
37
38
        return $this;
39
    }
40
41
    public function getValue()
42
    {
43
        return (string)parent::getValue();
44
    }
45
46
    protected function isOptionsModel()
47
    {
48
        return is_string($this->options) || $this->options instanceof Model;
49
    }
50
51
    protected function isRelation()
52
    {
53
        return method_exists($this->getModel(), $this->getName());
54
    }
55
56
    public function getOptions()
57
    {
58 View Code Duplication
        if ($this->options instanceof \Closure) {
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...
59
            $options = ($this->options)();
60
        } elseif (is_string($this->options) || $this->options instanceof Model) {
61
            $options = $this->setOptionsFromModel();
62
        } elseif (is_array($this->options)) {
63
            $options = $this->options;
64
        } else {
65
            throw new InvalidArgumentException(
66
                "The form select element's options must be return array(key=>value)"
67
            );
68
        }
69
70
        return collect($options)->mapWithKeys(function ($value, $key) {
71
            return [
72
                $key => [
73
                    'label' => $value,
74
                    'value' => (string)$key,
75
                ],
76
            ];
77
        })->values();
78
    }
79
80
    /**
81
     * @param mixed $options
82
     *
83
     * @return $this
84
     */
85
    public function setOptions($options)
86
    {
87
        $this->options = $options;
88
89
        return $this;
90
    }
91
92
    /**
93
     *
94
     * @return array
95
     */
96
    protected function setOptionsFromModel()
97
    {
98
        if (is_null(($label = $this->getOptionsLabelAttribute()))) {
99
            throw new InvalidArgumentException(
100
                sprintf(
101
                    'Form %s element must set label attribute',
102
                    $this->getType()
103
                )
104
            );
105
        }
106
107
        $model = $this->getOptionsModel();
108
109
        $repository = app(RepositoryInterface::class);
110
        $repository->setModel($model);
111
112
        $results = $repository->getQuery()->get();
113
114
        $key = $this->getOptionsValueAttribute() ?: $model->getKeyName();
115
116
        return $results->pluck($label, $key);
117
    }
118
119
    /**
120
     * @return Model
121
     */
122 View Code Duplication
    public function getOptionsModel()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
123
    {
124
        $model = $this->options;
125
126
        if (is_string($model)) {
127
            $model = app($model);
128
        }
129
130
        if (!($model instanceof Model)) {
131
            throw new InvalidArgumentException(
132
                sprintf(
133
                    'Form %s element options class must be instanced of "%s".',
134
                    $this->getType(),
135
                    Model::class
136
                )
137
            );
138
        }
139
140
        return $model;
141
    }
142
143
    /**
144
     * 获取 options 标题字段
145
     *
146
     * @return string
147
     */
148
    public function getOptionsLabelAttribute()
149
    {
150
        return $this->optionsLabelAttribute;
151
    }
152
153
    /**
154
     * 设置 options 标题字段
155
     *
156
     * @param string $value
157
     *
158
     * @return $this
159
     */
160
    public function setOptionsLabelAttribute($value)
161
    {
162
        $this->optionsLabelAttribute = $value;
163
164
        return $this;
165
    }
166
167
    /**
168
     * 获取 options value 字段
169
     *
170
     * @return string
171
     */
172
    public function getOptionsValueAttribute()
173
    {
174
        return $this->optionsValueAttribute;
175
    }
176
177
    /**
178
     * 设置 options value 字段
179
     *
180
     * @param string $value
181
     *
182
     * @return $this
183
     */
184
    public function setOptionsValueAttribute($value)
185
    {
186
        $this->optionsValueAttribute = $value;
187
188
        return $this;
189
    }
190
191
    public function toArray()
192
    {
193
        return parent::toArray() + [
194
                'options' => $this->getOptions(),
195
                'size'    => $this->getSize(),
196
            ];
197
    }
198
}
199