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

FieldConfig   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 477
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 3

Importance

Changes 11
Bugs 3 Features 2
Metric Value
wmc 46
c 11
b 3
f 2
lcom 5
cbo 3
dl 0
loc 477
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 by default we use the field name to sort, but in some cases may be different
255
     * name, for example, the relation column is 'profile.nickname' and inner join is 'profiles.nickname'.
256
     * @return $this|boolean
257
     */
258
    public function sortable($isSortable, $sortableColumn = null)
259
    {
260
        if (is_null($isSortable)) {
261
            return $this->is_sortable;
262
        }
263
264
        $this->sortable_column = $sortableColumn;
265
        $this->is_sortable = $isSortable;
266
267
        return $this;
268
    }
269
270
    /**
271
     * Returns current sorting order
272
     * or null if table rows are not sorted using this column.
273
     *
274
     * @return null|string null|Grid::SORT_ASC|Grid::SORT_DESC
275
     */
276
    public function getSorting()
277
    {
278
        return $this->sorting;
279
    }
280
281
    /**
282
     * Allows to specify sorting by this column for data rows.
283
     *
284
     * @param null|string $sortOrder null|Grid::SORT_ASC|Grid::SORT_DESC
285
     * @return $this
286
     */
287
    public function setSorting($sortOrder)
288
    {
289
        $this->sorting = $sortOrder;
290
291
        return $this;
292
    }
293
294
    /**
295
     * Allows to specify sorting by this column for data rows.
296
     *
297
     * @param null|string|boolean $sortOrder null|Grid::SORT_ASC|Grid::SORT_DESC|false
298
     * @return $this
299
     */
300
    public function sorting($sortOrder = false)
301
    {
302
        if ($sortOrder === false) {
303
            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 303 which is incompatible with the return type documented by Nayjest\Grids\FieldConfig::sorting of type Nayjest\Grids\FieldConfig|null.
Loading history...
304
        }
305
306
        $this->sorting = $sortOrder;
307
308
        return $this;
309
    }
310
311
    /**
312
     * Returns true if data rows are sorted ascending using this column.
313
     *
314
     * @return bool
315
     */
316
    public function isSortedAsc()
317
    {
318
        return $this->sorting === Grid::SORT_ASC;
319
    }
320
321
    /**
322
     * Returns true if data rows are sorted descending using this column.
323
     *
324
     * @return bool
325
     */
326
    public function isSortedDesc()
327
    {
328
        return $this->sorting === Grid::SORT_DESC;
329
    }
330
331
    /**
332
     * Allows to set callback function that will render
333
     * content of table cells for this column.
334
     *
335
     * @param callable $callback
336
     * @return $this
337
     */
338
    public function setCallback($callback)
339
    {
340
        $this->callback = $callback;
341
342
        return $this;
343
    }
344
345
    /**
346
     * Returns function that will render
347
     * content of table cells for this column if specified.
348
     *
349
     * @return callable|null
350
     */
351
    public function getCallback()
352
    {
353
        return $this->callback;
354
    }
355
356
    /**
357
     * Allows to set callback function that will render
358
     * content of table cells for this column.
359
     *
360
     * @param string|null $callback
361
     * @return $this|callable|null
362
     */
363
    public function callback($callback = null)
364
    {
365
        if (is_null($callback)) {
366
            return $this->callback;
367
        }
368
369
        $this->callback = $callback;
370
371
        return $this;
372
    }
373
374
    /**
375
     * Allows to specify filtering controls for column.
376
     *
377
     * @param Collection|FilterConfig[] $filters
378
     * @return $this
379
     */
380
    public function setFilters($filters)
381
    {
382
        $this->filters = Collection::make($filters);
383
        foreach ($this->filters as $filterConfig) {
384
            $filterConfig->attach($this);
385
        }
386
387
        return $this;
388
    }
389
390
    /**
391
     * Allows to specify filtering controls for column.
392
     *
393
     * @param Collection|FilterConfig[]|null $filters
394
     * @return $this|Collection|FilterConfig[]
395
     */
396
    public function filters($filters = null)
397
    {
398
        if (is_null($filters)) {
399
            return $this->filters;
400
        }
401
402
        $this->filters = Collection::make($filters);
403
        foreach ($this->filters as $filterConfig) {
404
            $filterConfig->attach($this);
405
        }
406
407
        return $this;
408
    }
409
410
    /**
411
     * Allows to add filtering control to column.
412
     *
413
     * @param FilterConfig $filter
414
     * @return $this
415
     */
416
    public function addFilter(FilterConfig $filter)
417
    {
418
        $this->getFilters()->push($filter);
419
        $filter->attach($this);
420
421
        return $this;
422
    }
423
424
    /**
425
     * Allows to add filtering control to column.
426
     *
427
     * @param FilterConfig $filter
428
     * @return $this
429
     */
430
    public function filter(FilterConfig $filter)
431
    {
432
        $this->addFilter($filter);
433
434
        return $this;
435
    }
436
437
    /**
438
     * Creates instance of filtering control configuration
439
     * and binds it to the column.
440
     *
441
     * @param string $class
442
     * @return FilterConfig
443
     */
444
    public function makeFilter($class = '\Nayjest\Grids\FilterConfig')
445
    {
446
        $filter = new $class;
447
        $this->addFilter($filter);
448
449
        return $filter;
450
    }
451
452
    /**
453
     * Returns true if any filtering controls specified for the column.
454
     *
455
     * @return bool
456
     */
457
    public function hasFilters()
458
    {
459
        return !$this->getFilters()->isEmpty();
460
    }
461
462
    /**
463
     * Returns list of filtering controls specified for the column.
464
     *
465
     * @return Collection|FilterConfig[]
466
     */
467
    public function getFilters()
468
    {
469
        if (null === $this->filters) {
470
            $this->filters = new Collection();
471
        }
472
473
        return $this->filters;
474
    }
475
476
    /**
477
     * @todo move to Field instance
478
     * @param DataRowInterface $row
479
     * @return mixed
480
     */
481
    public function getValue(DataRowInterface $row)
482
    {
483
        if ($function = $this->getCallback()) {
484
            return call_user_func($function, $row->getCellValue($this), $row);
485
        } else {
486
            return $row->getCellValue($this);
487
        }
488
    }
489
}
490