Completed
Push — master ( bd55a1...f98c01 )
by Arjay
01:18
created

Column   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 460
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 52
lcom 1
cbo 4
dl 0
loc 460
rs 7.44
c 0
b 0
f 0

31 Methods

Rating   Name   Duplication   Size   Complexity  
A content() 0 6 1
A titleFormat() 0 4 1
A computed() 0 8 2
A searchable() 0 6 1
A orderable() 0 6 1
A title() 0 6 1
A make() 0 9 2
A checkbox() 0 9 1
A className() 0 6 1
A visible() 0 6 1
A addClass() 0 10 2
A exportable() 0 6 1
A printable() 0 6 1
A width() 0 6 1
A data() 0 6 1
A name() 0 6 1
A editField() 0 6 1
A orderData() 0 6 1
A orderDataType() 0 6 1
A orderSequence() 0 6 1
A cellType() 0 6 1
A type() 0 6 1
A contentPadding() 0 6 1
A createdCell() 0 6 1
A render() 0 6 1
F __construct() 0 24 13
A footer() 0 6 1
B parseRender() 0 21 6
A isBuiltInRenderFunction() 0 8 2
A parseRenderAsString() 0 4 1
A toArray() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Column often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Column, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Yajra\DataTables\Html;
4
5
use Illuminate\Support\Fluent;
6
use Illuminate\Support\Str;
7
8
/**
9
 * @property string data
10
 * @property string name
11
 * @property string orderable
12
 * @property string searchable
13
 * @property string printable
14
 * @property string exportable
15
 * @property string footer
16
 * @property array attributes
17
 * @see https://datatables.net/reference/option/#columns
18
 */
19
class Column extends Fluent
20
{
21
    /**
22
     * @param array $attributes
23
     */
24
    public function __construct($attributes = [])
25
    {
26
        $attributes['title'] = isset($attributes['title']) ? $attributes['title'] : self::titleFormat($attributes['data']);
27
        $attributes['orderable'] = isset($attributes['orderable']) ? $attributes['orderable'] : true;
28
        $attributes['searchable'] = isset($attributes['searchable']) ? $attributes['searchable'] : true;
29
        $attributes['exportable'] = isset($attributes['exportable']) ? $attributes['exportable'] : true;
30
        $attributes['printable'] = isset($attributes['printable']) ? $attributes['printable'] : true;
31
        $attributes['footer'] = isset($attributes['footer']) ? $attributes['footer'] : '';
32
        $attributes['attributes'] = isset($attributes['attributes']) ? $attributes['attributes'] : [];
33
34
        // Allow methods override attribute value
35
        foreach ($attributes as $attribute => $value) {
36
            $method = 'parse' . ucfirst(strtolower($attribute));
37
            if (! is_null($value) && method_exists($this, $method)) {
38
                $attributes[$attribute] = $this->$method($value);
39
            }
40
        }
41
42
        if (! isset($attributes['name']) && isset($attributes['data'])) {
43
            $attributes['name'] = $attributes['data'];
44
        }
45
46
        parent::__construct($attributes);
47
    }
48
49
    /**
50
     * Format string to title case.
51
     *
52
     * @param string $value
53
     * @return string
54
     */
55
    public static function titleFormat($value)
56
    {
57
        return Str::title(str_replace('_', ' ', $value));
58
    }
59
60
    /**
61
     * Create a computed column that is not searchable/orderable.
62
     *
63
     * @param string $data
64
     * @param string|null $title
65
     * @return Column
66
     */
67
    public static function computed($data, $title = null)
68
    {
69
        if (is_null($title)) {
70
            $title = self::titleFormat($data);
71
        }
72
73
        return static::make($data)->title($title)->orderable(false)->searchable(false);
74
    }
75
76
    /**
77
     * Set column searchable flag.
78
     *
79
     * @param bool $flag
80
     * @return $this
81
     * @see https://datatables.net/reference/option/columns.searchable
82
     */
83
    public function searchable(bool $flag = true)
84
    {
85
        $this->attributes['searchable'] = $flag;
86
87
        return $this;
88
    }
89
90
    /**
91
     * Set column orderable flag.
92
     *
93
     * @param bool $flag
94
     * @return $this
95
     * @see https://datatables.net/reference/option/columns.orderable
96
     */
97
    public function orderable(bool $flag = true)
98
    {
99
        $this->attributes['orderable'] = $flag;
100
101
        return $this;
102
    }
103
104
    /**
105
     * Set column title.
106
     *
107
     * @param string $value
108
     * @return $this
109
     * @see https://datatables.net/reference/option/columns.title
110
     */
111
    public function title($value)
112
    {
113
        $this->attributes['title'] = $value;
114
115
        return $this;
116
    }
117
118
    /**
119
     * Make a new column instance.
120
     *
121
     * @param string $data
122
     * @param string $name
123
     * @return Column
124
     */
125
    public static function make($data, $name = '')
126
    {
127
        $attr = [
128
            'data' => $data,
129
            'name' => $name ?: $data,
130
        ];
131
132
        return new static($attr);
133
    }
134
135
    /**
136
     * Create a checkbox column.
137
     *
138
     * @param string $title
139
     * @return Column
140
     */
141
    public static function checkbox($title = '')
142
    {
143
        return static::make('')
144
                     ->content('')
145
                     ->title($title)
146
                     ->className('select-checkbox')
147
                     ->orderable(false)
148
                     ->searchable(false);
149
    }
150
151
    /**
152
     * Set column class name.
153
     *
154
     * @param string $class
155
     * @return $this
156
     * @see https://datatables.net/reference/option/columns.className
157
     */
158
    public function className($class)
159
    {
160
        $this->attributes['className'] = $class;
161
162
        return $this;
163
    }
164
165
    /**
166
     * Set column default content.
167
     *
168
     * @param string $value
169
     * @return $this
170
     * @see https://datatables.net/reference/option/columns.defaultContent
171
     */
172
    public function content($value)
173
    {
174
        $this->attributes['defaultContent'] = $value;
175
176
        return $this;
177
    }
178
179
    /**
180
     * Set column visible flag.
181
     *
182
     * @param bool $flag
183
     * @return $this
184
     * @see https://datatables.net/reference/option/columns.visible
185
     */
186
    public function visible(bool $flag = true)
187
    {
188
        $this->attributes['visible'] = $flag;
189
190
        return $this;
191
    }
192
193
    /**
194
     * Append a class name to field.
195
     *
196
     * @param string $class
197
     * @return $this
198
     */
199
    public function addClass($class)
200
    {
201
        if (! isset($this->attributes['className'])) {
202
            $this->attributes['className'] = $class;
203
        } else {
204
            $this->attributes['className'] .= " $class";
205
        }
206
207
        return $this;
208
    }
209
210
    /**
211
     * Set column exportable flag.
212
     *
213
     * @param bool $flag
214
     * @return $this
215
     */
216
    public function exportable(bool $flag = true)
217
    {
218
        $this->attributes['exportable'] = $flag;
219
220
        return $this;
221
    }
222
223
    /**
224
     * Set column printable flag.
225
     *
226
     * @param bool $flag
227
     * @return $this
228
     */
229
    public function printable(bool $flag = true)
230
    {
231
        $this->attributes['printable'] = $flag;
232
233
        return $this;
234
    }
235
236
    /**
237
     * Set column width value.
238
     *
239
     * @param int|string $value
240
     * @return $this
241
     * @see https://datatables.net/reference/option/columns.width
242
     */
243
    public function width($value)
244
    {
245
        $this->attributes['width'] = $value;
246
247
        return $this;
248
    }
249
250
    /**
251
     * Set column data option value.
252
     *
253
     * @param string $value
254
     * @return $this
255
     * @see https://datatables.net/reference/option/columns.data
256
     */
257
    public function data($value)
258
    {
259
        $this->attributes['data'] = $value;
260
261
        return $this;
262
    }
263
264
    /**
265
     * Set column name option value.
266
     *
267
     * @param string $value
268
     * @return $this
269
     * @see https://datatables.net/reference/option/columns.name
270
     */
271
    public function name($value)
272
    {
273
        $this->attributes['name'] = $value;
274
275
        return $this;
276
    }
277
278
    /**
279
     * Set column edit field option value.
280
     *
281
     * @param string $value
282
     * @return $this
283
     * @see https://datatables.net/reference/option/columns.editField
284
     */
285
    public function editField($value)
286
    {
287
        $this->attributes['editField'] = $value;
288
289
        return $this;
290
    }
291
292
    /**
293
     * Set column orderData option value.
294
     *
295
     * @param mixed $value
296
     * @return $this
297
     * @see https://datatables.net/reference/option/columns.orderData
298
     */
299
    public function orderData($value)
300
    {
301
        $this->attributes['orderData'] = $value;
302
303
        return $this;
304
    }
305
306
    /**
307
     * Set column orderDataType option value.
308
     *
309
     * @param mixed $value
310
     * @return $this
311
     * @see https://datatables.net/reference/option/columns.orderDataType
312
     */
313
    public function orderDataType($value)
314
    {
315
        $this->attributes['orderDataType'] = $value;
316
317
        return $this;
318
    }
319
320
    /**
321
     * Set column orderSequence option value.
322
     *
323
     * @param mixed $value
324
     * @return $this
325
     * @see https://datatables.net/reference/option/columns.orderSequence
326
     */
327
    public function orderSequence($value)
328
    {
329
        $this->attributes['orderSequence'] = $value;
330
331
        return $this;
332
    }
333
334
    /**
335
     * Set column cellType option value.
336
     *
337
     * @param mixed $value
338
     * @return $this
339
     * @see https://datatables.net/reference/option/columns.cellType
340
     */
341
    public function cellType($value)
342
    {
343
        $this->attributes['cellType'] = $value;
344
345
        return $this;
346
    }
347
348
    /**
349
     * Set column type option value.
350
     *
351
     * @param mixed $value
352
     * @return $this
353
     * @see https://datatables.net/reference/option/columns.type
354
     */
355
    public function type($value)
356
    {
357
        $this->attributes['type'] = $value;
358
359
        return $this;
360
    }
361
362
    /**
363
     * Set column contentPadding option value.
364
     *
365
     * @param mixed $value
366
     * @return $this
367
     * @see https://datatables.net/reference/option/columns.contentPadding
368
     */
369
    public function contentPadding($value)
370
    {
371
        $this->attributes['contentPadding'] = $value;
372
373
        return $this;
374
    }
375
376
    /**
377
     * Set column createdCell option value.
378
     *
379
     * @param mixed $value
380
     * @return $this
381
     * @see https://datatables.net/reference/option/columns.createdCell
382
     */
383
    public function createdCell($value)
384
    {
385
        $this->attributes['createdCell'] = $value;
386
387
        return $this;
388
    }
389
390
    /**
391
     * Set column renderer.
392
     *
393
     * @param mixed $value
394
     * @return $this
395
     * @see https://datatables.net/reference/option/columns.render
396
     */
397
    public function render($value)
398
    {
399
        $this->attributes['render'] = $value;
400
401
        return $this;
402
    }
403
404
    /**
405
     * Set column footer.
406
     *
407
     * @param mixed $value
408
     * @return $this
409
     */
410
    public function footer($value)
411
    {
412
        $this->attributes['footer'] = $value;
413
414
        return $this;
415
    }
416
417
    /**
418
     * Parse render attribute.
419
     *
420
     * @param mixed $value
421
     * @return string|null
422
     */
423
    public function parseRender($value)
424
    {
425
        /** @var \Illuminate\Contracts\View\Factory $view */
426
        $view = app('view');
427
        $parameters = [];
428
429
        if (is_array($value)) {
430
            $parameters = array_except($value, 0);
0 ignored issues
show
Deprecated Code introduced by
The function array_except() has been deprecated with message: Arr::except() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
431
            $value = $value[0];
432
        }
433
434
        if (is_callable($value)) {
435
            return $value($parameters);
436
        } elseif ($this->isBuiltInRenderFunction($value)) {
437
            return $value;
438
        } elseif ($view->exists($value)) {
439
            return $view->make($value)->with($parameters)->render();
440
        }
441
442
        return $value ? $this->parseRenderAsString($value) : null;
443
    }
444
445
    /**
446
     * Check if given key & value is a valid datatables built-in renderer function.
447
     *
448
     * @param string $value
449
     * @return bool
450
     */
451
    private function isBuiltInRenderFunction($value)
452
    {
453
        if (empty($value)) {
454
            return false;
455
        }
456
457
        return Str::startsWith(trim($value), ['$.fn.dataTable.render']);
458
    }
459
460
    /**
461
     * Display render value as is.
462
     *
463
     * @param mixed $value
464
     * @return string
465
     */
466
    private function parseRenderAsString($value)
467
    {
468
        return "function(data,type,full,meta){return $value;}";
469
    }
470
471
    /**
472
     * @return array
473
     */
474
    public function toArray()
475
    {
476
        return array_except($this->attributes, ['printable', 'exportable', 'footer']);
0 ignored issues
show
Deprecated Code introduced by
The function array_except() has been deprecated with message: Arr::except() should be used directly instead. Will be removed in Laravel 5.9.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
477
    }
478
}
479