Completed
Pull Request — master (#155)
by
unknown
02:36
created

FieldConfig   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 476
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 3

Importance

Changes 10
Bugs 3 Features 2
Metric Value
wmc 46
c 10
b 3
f 2
lcom 5
cbo 3
dl 0
loc 476
rs 8.3999

32 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 3
A getOrder() 0 4 1
A setOrder() 0 6 1
A getName() 0 4 1
A setName() 0 6 1
A getSortableName() 0 8 2
A name() 0 10 2
A isHidden() 0 4 1
A hide() 0 6 1
A show() 0 6 1
A getLabel() 0 4 2
A setLabel() 0 6 1
A label() 0 10 2
A isSortable() 0 4 1
A setSortable() 0 7 1
A sortable() 0 11 2
A getSorting() 0 4 1
A setSorting() 0 6 1
A sorting() 0 10 2
A isSortedAsc() 0 4 1
A isSortedDesc() 0 4 1
A setCallback() 0 6 1
A getCallback() 0 4 1
A callback() 0 10 2
A setFilters() 0 9 2
A filters() 0 13 3
A addFilter() 0 7 1
A filter() 0 6 1
A makeFilter() 0 7 1
A hasFilters() 0 4 1
A getFilters() 0 8 2
A getValue() 0 8 2

How to fix   Complexity   

Complex Class

Complex classes like FieldConfig 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 FieldConfig, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace Nayjest\Grids;
3
4
use Illuminate\Support\Collection;
5
6
/**
7
 * Class FieldConfig
8
 *
9
 * This class describes grid column.
10
 *
11
 * @package Nayjest\Grids
12
 */
13
class FieldConfig
14
{
15
    /**
16
     * Field name.
17
     *
18
     * @var string
19
     */
20
    protected $name;
21
22
    /**
23
     * Text label that will be rendered in table header.
24
     *
25
     * @var string
26
     */
27
    protected $label;
28
29
    /**
30
     * @var int
31
     */
32
    protected $order = 0;
33
34
    /**
35
     * @var bool
36
     */
37
    protected $is_sortable = false;
38
39
    /**
40
     * @var string|null
41
     */
42
    protected $sortable_column;
43
44
    protected $sorting;
45
46
    /** @var  Collection|FilterConfig[] */
47
    protected $filters;
48
49
    /** @var  callable */
50
    protected $callback;
51
52
    protected $is_hidden = false;
53
54
    /**
55
     * Constructor.
56
     *
57
     * @param string|null $name column unique name for internal usage
58
     * @param string|null $label column label
59
     */
60
    public function __construct($name = null, $label = null)
61
    {
62
        if ($name !== null) {
63
            $this->setName($name);
64
        }
65
        if ($label !== null) {
66
            $this->setLabel($label);
67
        }
68
    }
69
70
    /**
71
     * Returns column order.
72
     *
73
     * This property used to to identify column position in grid.
74
     *
75
     * @return int
76
     */
77
    public function getOrder()
78
    {
79
        return $this->order;
80
    }
81
82
    /**
83
     * Sets column order.
84
     *
85
     * This property used to to identify column position in grid.
86
     *
87
     * @param $order
88
     * @return $this
89
     */
90
    public function setOrder($order)
91
    {
92
        $this->order = $order;
93
94
        return $this;
95
    }
96
97
    /**
98
     * Returns field name.
99
     *
100
     * @return string
101
     */
102
    public function getName()
103
    {
104
        return $this->name;
105
    }
106
107
    /**
108
     * Sets field name.
109
     *
110
     * @param string $name
111
     * @return $this
112
     */
113
    public function setName($name)
114
    {
115
        $this->name = $name;
116
117
        return $this;
118
    }
119
120
    /**
121
     * Returns the sortable name.
122
     *
123
     * @returns string
124
     */
125
    public function getSortableName()
126
    {
127
        if ($this->sortable_column) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->sortable_column of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
128
            return $this->sortable_column;
129
        }
130
131
        return $this->getName();
132
    }
133
134
    /**
135
     * Sets field name.
136
     *
137
     * @param string|null $name
138
     * @return $this|string
139
     */
140
    public function name($name = null)
141
    {
142
        if (is_null($name)) {
143
            return $this->name;
144
        }
145
146
        $this->name = $name;
147
148
        return $this;
149
    }
150
151
    /**
152
     * Returns true if column is hidden.
153
     *
154
     * @return bool
155
     */
156
    public function isHidden()
157
    {
158
        return $this->is_hidden;
159
    }
160
161
    /**
162
     * Makes column hidden.
163
     *
164
     * @return $this
165
     */
166
    public function hide()
167
    {
168
        $this->is_hidden = true;
169
170
        return $this;
171
    }
172
173
    /**
174
     * Makes column visible.
175
     *
176
     * @return $this
177
     */
178
    public function show()
179
    {
180
        $this->is_hidden = false;
181
182
        return $this;
183
    }
184
185
    /**
186
     * Returns text label that will be rendered in table header.
187
     *
188
     * @return string
189
     */
190
    public function getLabel()
191
    {
192
        return $this->label ?: ucwords(str_replace(['-', '_', '.'], ' ', $this->name));
193
    }
194
195
    /**
196
     * Sets text label that will be rendered in table header.
197
     *
198
     * @param string $label
199
     * @return $this
200
     */
201
    public function setLabel($label)
202
    {
203
        $this->label = $label;
204
205
        return $this;
206
    }
207
208
    /**
209
     * Sets text label that will be rendered in table header.
210
     *
211
     * @param string|null $label
212
     * @return $this|string
213
     */
214
    public function label($label = null)
215
    {
216
        if (is_null($label)) {
217
            return $this->label;
218
        }
219
220
        $this->label = $label;
221
222
        return $this;
223
    }
224
225
    /**
226
     * Returns true if column is sortable (sorting controls must be rendered).
227
     *
228
     * @return bool
229
     */
230
    public function isSortable()
231
    {
232
        return $this->is_sortable;
233
    }
234
235
    /**
236
     * Allows to enable or disable sorting controls for column.
237
     *
238
     * @param boolean $isSortable
239
     * @param string|null $sortableColumn
240
     * @return $this
241
     */
242
    public function setSortable($isSortable, $sortableColumn = null)
243
    {
244
        $this->sortable_column = $sortableColumn;
245
        $this->is_sortable = $isSortable;
246
247
        return $this;
248
    }
249
250
    /**
251
     * Allows to enable or disable sorting controls for column.
252
     *
253
     * @param boolean|null $isSortable
254
     * @param string|null $sortableColumn
255
     * @return $this|boolean
256
     */
257
    public function sortable($isSortable, $sortableColumn = null)
258
    {
259
        if (is_null($isSortable)) {
260
            return $this->is_sortable;
261
        }
262
263
        $this->sortable_column = $sortableColumn;
264
        $this->is_sortable = $isSortable;
265
266
        return $this;
267
    }
268
269
    /**
270
     * Returns current sorting order
271
     * or null if table rows are not sorted using this column.
272
     *
273
     * @return null|string null|Grid::SORT_ASC|Grid::SORT_DESC
274
     */
275
    public function getSorting()
276
    {
277
        return $this->sorting;
278
    }
279
280
    /**
281
     * Allows to specify sorting by this column for data rows.
282
     *
283
     * @param null|string $sortOrder null|Grid::SORT_ASC|Grid::SORT_DESC
284
     * @return $this
285
     */
286
    public function setSorting($sortOrder)
287
    {
288
        $this->sorting = $sortOrder;
289
290
        return $this;
291
    }
292
293
    /**
294
     * Allows to specify sorting by this column for data rows.
295
     *
296
     * @param null|string|boolean $sortOrder null|Grid::SORT_ASC|Grid::SORT_DESC|false
297
     * @return $this
298
     */
299
    public function sorting($sortOrder = false)
300
    {
301
        if ($sortOrder === false) {
302
            return $this->sorting;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->sorting; of type null|string adds the type string to the return on line 302 which is incompatible with the return type documented by Nayjest\Grids\FieldConfig::sorting of type Nayjest\Grids\FieldConfig|null.
Loading history...
303
        }
304
305
        $this->sorting = $sortOrder;
306
307
        return $this;
308
    }
309
310
    /**
311
     * Returns true if data rows are sorted ascending using this column.
312
     *
313
     * @return bool
314
     */
315
    public function isSortedAsc()
316
    {
317
        return $this->sorting === Grid::SORT_ASC;
318
    }
319
320
    /**
321
     * Returns true if data rows are sorted descending using this column.
322
     *
323
     * @return bool
324
     */
325
    public function isSortedDesc()
326
    {
327
        return $this->sorting === Grid::SORT_DESC;
328
    }
329
330
    /**
331
     * Allows to set callback function that will render
332
     * content of table cells for this column.
333
     *
334
     * @param callable $callback
335
     * @return $this
336
     */
337
    public function setCallback($callback)
338
    {
339
        $this->callback = $callback;
340
341
        return $this;
342
    }
343
344
    /**
345
     * Returns function that will render
346
     * content of table cells for this column if specified.
347
     *
348
     * @return callable|null
349
     */
350
    public function getCallback()
351
    {
352
        return $this->callback;
353
    }
354
355
    /**
356
     * Allows to set callback function that will render
357
     * content of table cells for this column.
358
     *
359
     * @param string|null $callback
360
     * @return $this|callable|null
361
     */
362
    public function callback($callback = null)
363
    {
364
        if (is_null($callback)) {
365
            return $this->callback;
366
        }
367
368
        $this->callback = $callback;
369
370
        return $this;
371
    }
372
373
    /**
374
     * Allows to specify filtering controls for column.
375
     *
376
     * @param Collection|FilterConfig[] $filters
377
     * @return $this
378
     */
379
    public function setFilters($filters)
380
    {
381
        $this->filters = Collection::make($filters);
382
        foreach ($this->filters as $filterConfig) {
383
            $filterConfig->attach($this);
384
        }
385
386
        return $this;
387
    }
388
389
    /**
390
     * Allows to specify filtering controls for column.
391
     *
392
     * @param Collection|FilterConfig[]|null $filters
393
     * @return $this|Collection|FilterConfig[]
394
     */
395
    public function filters($filters = null)
396
    {
397
        if (is_null($filters)) {
398
            return $this->filters;
399
        }
400
401
        $this->filters = Collection::make($filters);
402
        foreach ($this->filters as $filterConfig) {
403
            $filterConfig->attach($this);
404
        }
405
406
        return $this;
407
    }
408
409
    /**
410
     * Allows to add filtering control to column.
411
     *
412
     * @param FilterConfig $filter
413
     * @return $this
414
     */
415
    public function addFilter(FilterConfig $filter)
416
    {
417
        $this->getFilters()->push($filter);
418
        $filter->attach($this);
419
420
        return $this;
421
    }
422
423
    /**
424
     * Allows to add filtering control to column.
425
     *
426
     * @param FilterConfig $filter
427
     * @return $this
428
     */
429
    public function filter(FilterConfig $filter)
430
    {
431
        $this->addFilter($filter);
432
433
        return $this;
434
    }
435
436
    /**
437
     * Creates instance of filtering control configuration
438
     * and binds it to the column.
439
     *
440
     * @param string $class
441
     * @return FilterConfig
442
     */
443
    public function makeFilter($class = '\Nayjest\Grids\FilterConfig')
444
    {
445
        $filter = new $class;
446
        $this->addFilter($filter);
447
448
        return $filter;
449
    }
450
451
    /**
452
     * Returns true if any filtering controls specified for the column.
453
     *
454
     * @return bool
455
     */
456
    public function hasFilters()
457
    {
458
        return !$this->getFilters()->isEmpty();
459
    }
460
461
    /**
462
     * Returns list of filtering controls specified for the column.
463
     *
464
     * @return Collection|FilterConfig[]
465
     */
466
    public function getFilters()
467
    {
468
        if (null === $this->filters) {
469
            $this->filters = new Collection();
470
        }
471
472
        return $this->filters;
473
    }
474
475
    /**
476
     * @todo move to Field instance
477
     * @param DataRowInterface $row
478
     * @return mixed
479
     */
480
    public function getValue(DataRowInterface $row)
481
    {
482
        if ($function = $this->getCallback()) {
483
            return call_user_func($function, $row->getCellValue($this), $row);
484
        } else {
485
            return $row->getCellValue($this);
486
        }
487
    }
488
}
489