Passed
Push — master ( ba5de7...5655bd )
by
unknown
04:42
created

UiItemTrait   B

Complexity

Total Complexity 51

Size/Duplication

Total Lines 588
Duplicated Lines 2.04 %

Coupling/Cohesion

Components 3
Dependencies 2

Importance

Changes 0
Metric Value
wmc 51
lcom 3
cbo 2
dl 12
loc 588
rs 7.92
c 0
b 0
f 0

36 Methods

Rating   Name   Duplication   Size   Complexity  
A setActive() 0 6 1
A active() 0 4 1
A setType() 12 12 3
A type() 0 7 2
A setTemplate() 0 12 2
A template() 0 8 2
A setTitle() 0 5 1
A title() 0 4 1
A setTabTitle() 0 5 1
A tabTitle() 0 4 2
A setSubtitle() 0 5 1
A subtitle() 0 4 1
A setDescription() 0 5 1
A description() 0 4 1
A setNotes() 0 5 1
A notes() 0 4 1
A icon() 0 4 1
A setIcon() 0 6 1
A setShowTitle() 0 6 1
A showTitle() 0 8 2
A setShowSubtitle() 0 6 1
A showSubtitle() 0 8 2
A setShowDescription() 0 6 1
A showDescription() 0 8 2
A setShowNotes() 0 6 1
A showNotes() 0 8 2
A setShowIcon() 0 6 1
A showIcon() 0 8 2
A setShowHeader() 0 6 1
A showHeader() 0 8 2
A setShowFooter() 0 6 1
A showFooter() 0 8 2
A setShowTabTitle() 0 6 1
A showTabTitle() 0 8 2
A sortItemsByPriority() 0 12 3
translator() 0 1 ?

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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

1
<?php
2
3
namespace Charcoal\Ui;
4
5
use InvalidArgumentException;
6
7
// From 'charcoal-ui'
8
use Charcoal\Ui\PrioritizableInterface;
9
10
/**
11
 * Provides an implementation of {@see \Charcoal\Ui\UiItemInterface}.
12
 */
13
trait UiItemTrait
14
{
15
    /**
16
     * @var boolean
17
     */
18
    private $active = true;
19
20
    /**
21
     * The UI item type.
22
     *
23
     * @var string|null
24
     */
25
    private $type;
26
27
    /**
28
     * The UI item's template.
29
     *
30
     * @var string|null
31
     */
32
    private $template;
33
34
    /**
35
     * The UI item's icon.
36
     *
37
     * Note: Only icons from the {@link http://fontawesome.io/ Font Awesome}
38
     * library are supported.
39
     *
40
     * @var string|null
41
     */
42
    private $icon;
43
44
    /**
45
     * The UI item's title.
46
     *
47
     * @var \Charcoal\Translator\Translation
48
     */
49
    private $title = '';
50
51
    /**
52
     * The UI item's tab title.
53
     *
54
     * @var \Charcoal\Translator\Translation
55
     */
56
    private $tabTitle = '';
57
58
    /**
59
     * The UI item's sub-title.
60
     *
61
     * @var \Charcoal\Translator\Translation
62
     */
63
    private $subtitle = '';
64
65
    /**
66
     * The UI item's description.
67
     *
68
     * @var \Charcoal\Translator\Translation
69
     */
70
    private $description = '';
71
72
    /**
73
     * The UI item's notes.
74
     *
75
     * @var \Charcoal\Translator\Translation
76
     */
77
    private $notes = '';
78
79
    /**
80
     * The title is displayed by default.
81
     *
82
     * @var boolean
83
     */
84
    private $showTitle = true;
85
86
    /**
87
     * The sub-title is displayed by default.
88
     *
89
     * @var boolean
90
     */
91
    private $showSubtitle = true;
92
93
    /**
94
     * The description is displayed by default.
95
     *
96
     * @var boolean
97
     */
98
    private $showDescription = true;
99
100
    /**
101
     * The notes are displayed by default.
102
     *
103
     * @var boolean
104
     */
105
    private $showNotes = true;
106
107
    /**
108
     * The icon is displayed by default.
109
     *
110
     * @var boolean
111
     */
112
    private $showIcon = true;
113
114
    /**
115
     * The header is displayed by default.
116
     *
117
     * @var boolean
118
     */
119
    private $showHeader = true;
120
121
    /**
122
     * The footer is displayed by default.
123
     *
124
     * @var boolean
125
     */
126
    private $showFooter = true;
127
128
    /**
129
     * The tab title is dislpayed by default.
130
     *
131
     * @var boolean
132
     */
133
    private $showTabTitle = true;
134
135
    /**
136
     * Activates/deactivates the UI item.
137
     *
138
     * @param  boolean $active Activate (TRUE) or deactivate (FALSE) the UI item.
139
     * @return self
140
     */
141
    public function setActive($active)
142
    {
143
        $this->active = !!$active;
144
145
        return $this;
146
    }
147
148
    /**
149
     * Determine if the UI item is active.
150
     *
151
     * @return boolean
152
     */
153
    public function active()
154
    {
155
        return $this->active;
156
    }
157
158
    /**
159
     * Set the UI item type.
160
     *
161
     * @param  string|null $type The UI item type.
162
     * @throws InvalidArgumentException If the type is not a string (or null).
163
     * @return self
164
     */
165 View Code Duplication
    public function setType($type)
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...
166
    {
167
        if (is_string($type) || $type === null) {
168
            $this->type = $type;
169
        } else {
170
            throw new InvalidArgumentException(
171
                'Can not set UI item config type: Type must be a string or NULL'
172
            );
173
        }
174
175
        return $this;
176
    }
177
178
    /**
179
     * Retrieve the UI item type.
180
     *
181
     * If it is not explicitely set (or null), then return the object's FQN.
182
     *
183
     * @return string
184
     */
185
    public function type()
186
    {
187
        if ($this->type === null) {
188
            return static::class;
189
        }
190
        return $this->type;
191
    }
192
193
    /**
194
     * Set the UI item's template.
195
     *
196
     * Usually, a path to a file containing the template to be rendered.
197
     *
198
     * @param  string $template A template (identifier).
199
     * @throws InvalidArgumentException If the template is not a string.
200
     * @return self
201
     */
202
    public function setTemplate($template)
203
    {
204
        if (!is_string($template)) {
205
            throw new InvalidArgumentException(
206
                'The UI Item can not set the template, must be a string'
207
            );
208
        }
209
210
        $this->template = $template;
211
212
        return $this;
213
    }
214
215
    /**
216
     * Retrieve the UI item's template.
217
     *
218
     * @return string If unset, returns the UI item type.
219
     */
220
    public function template()
221
    {
222
        if ($this->template === null) {
223
            return $this->type();
224
        }
225
226
        return $this->template;
227
    }
228
229
    /**
230
     * Set the UI item's title.
231
     *
232
     * @param  mixed $title A title.
233
     * @return self
234
     */
235
    public function setTitle($title)
236
    {
237
        $this->title = $this->translator()->translation($title);
238
        return $this;
239
    }
240
241
    /**
242
     * Retrieve the title.
243
     *
244
     * @return \Charcoal\Translator\Translation|null
245
     */
246
    public function title()
247
    {
248
        return $this->title;
249
    }
250
251
    /**
252
     * Set the UI item's tab title.
253
     *
254
     * @param  mixed $title A title.
255
     * @return self
256
     */
257
    public function setTabTitle($title)
258
    {
259
        $this->tabTitle = $this->translator()->translation($title);
260
        return $this;
261
    }
262
263
    /**
264
     * Retrieve the tab title.
265
     *
266
     * @return \Charcoal\Translator\Translation|null
267
     */
268
    public function tabTitle()
269
    {
270
        return $this->tabTitle ?: $this->title();
271
    }
272
273
    /**
274
     * Set the UI item's sub-title.
275
     *
276
     * @param  mixed $subtitle A sub-title.
277
     * @return self
278
     */
279
    public function setSubtitle($subtitle)
280
    {
281
        $this->subtitle = $this->translator()->translation($subtitle);
282
        return $this;
283
    }
284
285
    /**
286
     * Retrieve the sub-title.
287
     *
288
     * @return \Charcoal\Translator\Translation|null
289
     */
290
    public function subtitle()
291
    {
292
        return $this->subtitle;
293
    }
294
295
    /**
296
     * Set the UI item's description.
297
     *
298
     * @param  mixed $description A description.
299
     * @return self
300
     */
301
    public function setDescription($description)
302
    {
303
        $this->description = $this->translator()->translation($description);
304
        return $this;
305
    }
306
307
    /**
308
     * Retrieve the description.
309
     *
310
     * @return \Charcoal\Translator\Translation|null
311
     */
312
    public function description()
313
    {
314
        return $this->description;
315
    }
316
317
    /**
318
     * Set notes about the UI item.
319
     *
320
     * @param  mixed $notes Notes.
321
     * @return self
322
     */
323
    public function setNotes($notes)
324
    {
325
        $this->notes = $this->translator()->translation($notes);
326
        return $this;
327
    }
328
329
    /**
330
     * Retrieve the notes.
331
     *
332
     * @return \Charcoal\Translator\Translation|null
333
     */
334
    public function notes()
335
    {
336
        return $this->notes;
337
    }
338
339
    /**
340
     * Retrieve the path to the item's icon.
341
     *
342
     * @return string
343
     */
344
    public function icon()
345
    {
346
        return $this->icon;
347
    }
348
349
    /**
350
     * Set the path to the item's icon associated with the object.
351
     *
352
     * @param  string $icon A path to an image.
353
     * @return self
354
     */
355
    public function setIcon($icon)
356
    {
357
        $this->icon = $icon;
358
359
        return $this;
360
    }
361
362
    /**
363
     * Show/hide the UI item's title.
364
     *
365
     * @param  boolean $show Show (TRUE) or hide (FALSE) the title.
366
     * @return self
367
     */
368
    public function setShowTitle($show)
369
    {
370
        $this->showTitle = !!$show;
371
372
        return $this;
373
    }
374
375
    /**
376
     * Determine if the title is to be displayed.
377
     *
378
     * @return boolean If TRUE or unset, check if there is a title.
379
     */
380
    public function showTitle()
381
    {
382
        if ($this->showTitle === false) {
383
            return false;
384
        } else {
385
            return !!$this->title();
386
        }
387
    }
388
389
    /**
390
     * Show/hide the UI item's sub-title.
391
     *
392
     * @param  boolean $show Show (TRUE) or hide (FALSE) the sub-title.
393
     * @return self
394
     */
395
    public function setShowSubtitle($show)
396
    {
397
        $this->showSubtitle = !!$show;
398
399
        return $this;
400
    }
401
402
    /**
403
     * Determine if the sub-title is to be displayed.
404
     *
405
     * @return boolean If TRUE or unset, check if there is a sub-title.
406
     */
407
    public function showSubtitle()
408
    {
409
        if ($this->showSubtitle === false) {
410
            return false;
411
        } else {
412
            return !!$this->subtitle();
413
        }
414
    }
415
416
    /**
417
     * Show/hide the UI item's description.
418
     *
419
     * @param  boolean $show Show (TRUE) or hide (FALSE) the description.
420
     * @return self
421
     */
422
    public function setShowDescription($show)
423
    {
424
        $this->showDescription = !!$show;
425
426
        return $this;
427
    }
428
429
    /**
430
     * Determine if the description is to be displayed.
431
     *
432
     * @return boolean If TRUE or unset, check if there is a description.
433
     */
434
    public function showDescription()
435
    {
436
        if ($this->showDescription === false) {
437
            return false;
438
        } else {
439
            return !!$this->description();
440
        }
441
    }
442
443
    /**
444
     * Show/hide the UI item's notes.
445
     *
446
     * @param  boolean $show Show (TRUE) or hide (FALSE) the notes.
447
     * @return self
448
     */
449
    public function setShowNotes($show)
450
    {
451
        $this->showNotes = !!$show;
452
453
        return $this;
454
    }
455
456
    /**
457
     * Determine if the notes is to be displayed.
458
     *
459
     * @return boolean If TRUE or unset, check if there are notes.
460
     */
461
    public function showNotes()
462
    {
463
        if ($this->showNotes === false) {
464
            return false;
465
        } else {
466
            return !!$this->notes();
467
        }
468
    }
469
470
    /**
471
     * Show/hide the UI item's icon.
472
     *
473
     * @param  boolean $show Show (TRUE) or hide (FALSE) the icon.
474
     * @return self
475
     */
476
    public function setShowIcon($show)
477
    {
478
        $this->showIcon = !!$show;
479
480
        return $this;
481
    }
482
483
    /**
484
     * Determine if the icon is to be displayed.
485
     *
486
     * @return boolean If TRUE or unset, check if there is an icon.
487
     */
488
    public function showIcon()
489
    {
490
        if ($this->showIcon === false) {
491
            return false;
492
        } else {
493
            return !!$this->icon();
494
        }
495
    }
496
497
    /**
498
     * Show/hide the UI item's header.
499
     *
500
     * @param  boolean $show Show (TRUE) or hide (FALSE) the header.
501
     * @return self
502
     */
503
    public function setShowHeader($show)
504
    {
505
        $this->showHeader = !!$show;
506
507
        return $this;
508
    }
509
510
    /**
511
     * Determine if the header is to be displayed.
512
     *
513
     * @return boolean If TRUE or unset, check if there is a title.
514
     */
515
    public function showHeader()
516
    {
517
        if ($this->showHeader === false) {
518
            return false;
519
        } else {
520
            return $this->showTitle();
521
        }
522
    }
523
524
    /**
525
     * Show/hide the UI item's footer.
526
     *
527
     * @param  boolean $show Show (TRUE) or hide (FALSE) the footer.
528
     * @return self
529
     */
530
    public function setShowFooter($show)
531
    {
532
        $this->showFooter = !!$show;
533
534
        return $this;
535
    }
536
537
    /**
538
     * Determine if the footer is to be displayed.
539
     *
540
     * @return boolean If TRUE or unset, check if there are notes.
541
     */
542
    public function showFooter()
543
    {
544
        if ($this->showFooter === false) {
545
            return false;
546
        } else {
547
            return $this->showNotes();
548
        }
549
    }
550
551
    /**
552
     * @param  boolean $showTabTitle Show (TRUE) or hide (FALSE) the tab title.
553
     * @return self
554
     */
555
    public function setShowTabTitle($showTabTitle)
556
    {
557
        $this->showTabTitle = !!$showTabTitle;
558
559
        return $this;
560
    }
561
562
    /**
563
     * @return boolean If TRUE or unset, check if there is a title.
564
     */
565
    public function showTabTitle()
566
    {
567
        if ($this->showTabTitle === false) {
568
            return false;
569
        } else {
570
            return $this->showTitle();
571
        }
572
    }
573
574
    /**
575
     * Comparison function used by {@see uasort()}.
576
     *
577
     * @param  PrioritizableInterface $a Sortable entity A.
578
     * @param  PrioritizableInterface $b Sortable entity B.
579
     * @return integer Sorting value: -1 or 1.
580
     */
581
    protected function sortItemsByPriority(
582
        PrioritizableInterface $a,
583
        PrioritizableInterface $b
584
    ) {
585
        $priorityA = $a->priority();
586
        $priorityB = $b->priority();
587
588
        if ($priorityA === $priorityB) {
589
            return 0;
590
        }
591
        return ($priorityA < $priorityB) ? (-1) : 1;
592
    }
593
594
    /**
595
     * All UI objects are translatable, therefore are translator-aware.
596
     *
597
     * @return \Charcoal\Translator\Translator
598
     */
599
    abstract protected function translator();
600
}
601