Field   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 321
Duplicated Lines 3.12 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 5
dl 10
loc 321
rs 10
c 0
b 0
f 0

22 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A make() 0 13 3
A label() 0 6 1
A name() 0 6 1
A data() 0 6 1
A type() 0 6 1
A modelOptions() 0 6 1
A tableOptions() 0 6 1
A separator() 0 6 1
A format() 0 6 1
A default() 0 6 1
A message() 0 6 1
A fieldInfo() 0 6 1
A labelInfo() 0 6 1
A entityDecode() 0 6 1
A multiEditable() 0 6 1
A id() 0 6 1
A submit() 0 6 1
A compare() 0 6 1
A opts() 0 6 1
A attr() 0 6 1
A options() 10 10 2

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 Yajra\DataTables\Html\Editor\Fields;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Fluent;
7
use Illuminate\Contracts\Support\Arrayable;
8
use Yajra\DataTables\Html\HasAuthorizations;
9
10
/**
11
 * @see https://editor.datatables.net/reference/option/
12
 */
13
class Field extends Fluent
14
{
15
    use HasAuthorizations;
16
17
    /**
18
     * Field type.
19
     *
20
     * @var string
21
     */
22
    protected $type = 'text';
23
24
    /**
25
     * Password constructor.
26
     *
27
     * @param array $attributes
28
     */
29
    public function __construct($attributes = [])
30
    {
31
        $attributes['type'] = $attributes['type'] ?? $this->type;
32
33
        parent::__construct($attributes);
34
    }
35
36
    /**
37
     * Make a new instance of a field.
38
     *
39
     * @param string $name
40
     * @param string $label
41
     * @return static|\Yajra\DataTables\Html\Editor\Fields\Field
42
     */
43
    public static function make($name, $label = '')
44
    {
45
        if (is_array($name)) {
46
            return new static($name);
47
        }
48
49
        $data = [
50
            'name'  => $name,
51
            'label' => $label ?: Str::title(str_replace('_', ' ', $name)),
52
        ];
53
54
        return new static($data);
55
    }
56
57
    /**
58
     * @param string $label
59
     * @return $this
60
     * @see https://editor.datatables.net/reference/option/fields.label
61
     */
62
    public function label($label)
63
    {
64
        $this->attributes['label'] = $label;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param string $name
71
     * @return $this
72
     * @see https://editor.datatables.net/reference/option/fields.name
73
     */
74
    public function name($name)
75
    {
76
        $this->attributes['name'] = $name;
77
78
        return $this;
79
    }
80
81
    /**
82
     * @param string $data
83
     * @return $this
84
     * @see https://editor.datatables.net/reference/option/fields.data
85
     */
86
    public function data($data)
87
    {
88
        $this->attributes['data'] = $data;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param string $type
95
     * @return $this
96
     * @see https://editor.datatables.net/reference/option/fields.type
97
     */
98
    public function type($type)
99
    {
100
        $this->attributes['type'] = $type;
101
102
        return $this;
103
    }
104
105
    /**
106
     * Get options from a model.
107
     *
108
     * @param mixed $model
109
     * @param string $value
110
     * @param string $key
111
     * @return $this
112
     */
113
    public function modelOptions($model, $value, $key = 'id')
114
    {
115
        return $this->options(
116
            Options::model($model, $value, $key)
117
        );
118
    }
119
120
    /**
121
     * Set select options.
122
     *
123
     * @param array|mixed $options
124
     * @return $this
125
     */
126 View Code Duplication
    public function options($options)
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...
127
    {
128
        if ($options instanceof Arrayable) {
129
            $options = $options->toArray();
130
        }
131
132
        $this->attributes['options'] = $options;
133
134
        return $this;
135
    }
136
137
    /**
138
     * Get options from a table.
139
     *
140
     * @param mixed $table
141
     * @param string $value
142
     * @param string $key
143
     * @param \Closure $whereCallback
144
     * @param string|null $key
145
     * @return $this
146
     */
147
    public function tableOptions($table, $value, $key = 'id', \Closure $whereCallback = null, $connection = null)
148
    {
149
        return $this->options(
150
            Options::table($table, $value, $key, $whereCallback, $connection)
151
        );
152
    }
153
154
    /**
155
     * Set checkbox separator.
156
     *
157
     * @param string $separator
158
     * @return $this
159
     */
160
    public function separator($separator = ',')
161
    {
162
        $this->attributes['separator'] = $separator;
163
164
        return $this;
165
    }
166
167
    /**
168
     * Set dateTime format.
169
     *
170
     * @param string $format
171
     * @return $this
172
     * @see https://editor.datatables.net/reference/field/datetime
173
     */
174
    public function format($format)
175
    {
176
        $this->attributes['format'] = $format;
177
178
        return $this;
179
    }
180
181
    /**
182
     * Set field default value.
183
     *
184
     * @param mixed $value
185
     * @return $this
186
     * @see https://editor.datatables.net/reference/option/fields.def
187
     */
188
    public function default($value)
189
    {
190
        $this->attributes['def'] = $value;
191
192
        return $this;
193
    }
194
195
    /**
196
     * Set field message value.
197
     *
198
     * @param string $value
199
     * @return $this
200
     * @see https://editor.datatables.net/reference/option/fields.message
201
     */
202
    public function message($value)
203
    {
204
        $this->attributes['message'] = $value;
205
206
        return $this;
207
    }
208
209
    /**
210
     * Set field fieldInfo value.
211
     *
212
     * @param string $value
213
     * @return $this
214
     * @see https://editor.datatables.net/reference/option/fields.fieldInfo
215
     */
216
    public function fieldInfo($value)
217
    {
218
        $this->attributes['fieldInfo'] = $value;
219
220
        return $this;
221
    }
222
223
    /**
224
     * Set field labelInfo value.
225
     *
226
     * @param string $value
227
     * @return $this
228
     * @see https://editor.datatables.net/reference/option/fields.labelInfo
229
     */
230
    public function labelInfo($value)
231
    {
232
        $this->attributes['labelInfo'] = $value;
233
234
        return $this;
235
    }
236
237
    /**
238
     * Set field entityDecode value.
239
     *
240
     * @param mixed|bool $value
241
     * @return $this
242
     * @see https://editor.datatables.net/reference/option/fields.entityDecode
243
     */
244
    public function entityDecode($value)
245
    {
246
        $this->attributes['entityDecode'] = $value;
247
248
        return $this;
249
    }
250
251
    /**
252
     * Set field multiEditable value.
253
     *
254
     * @param mixed|bool $value
255
     * @return $this
256
     * @see https://editor.datatables.net/reference/option/fields.multiEditable
257
     */
258
    public function multiEditable($value)
259
    {
260
        $this->attributes['multiEditable'] = $value;
261
262
        return $this;
263
    }
264
265
    /**
266
     * Set field id value.
267
     *
268
     * @param string $value
269
     * @return $this
270
     * @see https://editor.datatables.net/reference/option/fields.id
271
     */
272
    public function id($value)
273
    {
274
        $this->attributes['id'] = $value;
275
276
        return $this;
277
    }
278
279
    /**
280
     * Set field submit value.
281
     *
282
     * @param bool $value
283
     * @return $this
284
     * @see https://editor.datatables.net/reference/option/fields.submit
285
     */
286
    public function submit($value)
287
    {
288
        $this->attributes['submit'] = $value;
289
290
        return $this;
291
    }
292
293
    /**
294
     * Set field compare value.
295
     *
296
     * @param bool $value
297
     * @return $this
298
     * @see https://editor.datatables.net/reference/option/fields.compare
299
     */
300
    public function compare($value)
301
    {
302
        $this->attributes['compare'] = $value;
303
304
        return $this;
305
    }
306
307
    /**
308
     * Set field opts value.
309
     *
310
     * @param bool $value
311
     * @return $this
312
     */
313
    public function opts(array $value)
314
    {
315
        $this->attributes['opts'] = $value;
316
317
        return $this;
318
    }
319
320
    /**
321
     * Set field attr option.
322
     *
323
     * @param string $attribute
324
     * @param string $value
325
     * @return $this
326
     */
327
    public function attr($attribute, $value)
328
    {
329
        $this->attributes['attr'][$attribute] = $value;
330
331
        return $this;
332
    }
333
}
334