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 classes like MenuItem 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 MenuItem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class MenuItem implements MenuItemInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var mixed |
||
27 | */ |
||
28 | protected $id; |
||
29 | |||
30 | /** |
||
31 | * @var MenuItemInterface |
||
32 | */ |
||
33 | protected $root; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | protected $lft; |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | protected $rgt; |
||
44 | |||
45 | /** |
||
46 | * @var int |
||
47 | */ |
||
48 | protected $level; |
||
49 | |||
50 | /** |
||
51 | * Name of this menu item (used for id by parent menu). |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $name = null; |
||
56 | |||
57 | /** |
||
58 | * Label to output, name is used by default. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $label = null; |
||
63 | |||
64 | /** |
||
65 | * Attributes for the item link. |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $linkAttributes = []; |
||
70 | |||
71 | /** |
||
72 | * Attributes for the children list. |
||
73 | * |
||
74 | * @var array |
||
75 | */ |
||
76 | protected $childrenAttributes = []; |
||
77 | |||
78 | /** |
||
79 | * Attributes for the item text. |
||
80 | * |
||
81 | * @var array |
||
82 | */ |
||
83 | protected $labelAttributes = []; |
||
84 | |||
85 | /** |
||
86 | * Uri to use in the anchor tag. |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $uri = null; |
||
91 | |||
92 | /** |
||
93 | * Attributes for the item. |
||
94 | * |
||
95 | * @var array |
||
96 | */ |
||
97 | protected $attributes = []; |
||
98 | |||
99 | /** |
||
100 | * Extra stuff associated to the item. |
||
101 | * |
||
102 | * @var array |
||
103 | */ |
||
104 | protected $extras = []; |
||
105 | |||
106 | /** |
||
107 | * Whether the item is displayed. |
||
108 | * |
||
109 | * @var bool |
||
110 | */ |
||
111 | protected $display = true; |
||
112 | |||
113 | /** |
||
114 | * Whether the children of the item are displayed. |
||
115 | * |
||
116 | * @var bool |
||
117 | */ |
||
118 | protected $displayChildren = true; |
||
119 | |||
120 | /** |
||
121 | * Child items. |
||
122 | * |
||
123 | * @var ItemInterface[] |
||
124 | */ |
||
125 | protected $children = []; |
||
126 | |||
127 | /** |
||
128 | * Parent item. |
||
129 | * |
||
130 | * @var ItemInterface|null |
||
131 | */ |
||
132 | protected $parent = null; |
||
133 | |||
134 | /** |
||
135 | * whether the item is current. null means unknown. |
||
136 | * |
||
137 | * @var bool|null |
||
138 | */ |
||
139 | protected $isCurrent = null; |
||
140 | |||
141 | /** |
||
142 | * MenuItem constructor. |
||
143 | */ |
||
144 | 12 | public function __construct() |
|
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | 7 | public function getId() |
|
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | */ |
||
160 | public function getRoot() |
||
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | */ |
||
168 | public function getLeft(): int |
||
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | public function setLeft(int $left) |
||
180 | |||
181 | /** |
||
182 | * {@inheritdoc} |
||
183 | */ |
||
184 | public function getRight(): int |
||
188 | |||
189 | /** |
||
190 | * @param int $right |
||
191 | */ |
||
192 | public function setRight(int $right) |
||
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | 3 | public function getLevel(): int |
|
204 | |||
205 | /** |
||
206 | * {@inheritdoc} |
||
207 | */ |
||
208 | public function setLevel(int $level) |
||
212 | |||
213 | public function setFactory(FactoryInterface $factory) |
||
216 | |||
217 | 8 | public function getName() |
|
221 | |||
222 | 12 | public function setName($name) |
|
228 | |||
229 | 8 | public function getUri() |
|
233 | |||
234 | 12 | public function setUri($uri) |
|
240 | |||
241 | 8 | public function getLabel() |
|
245 | |||
246 | 12 | public function setLabel($label) |
|
252 | |||
253 | 3 | public function getAttributes() |
|
257 | |||
258 | 12 | public function setAttributes(array $attributes) |
|
264 | |||
265 | 3 | public function getAttribute($name, $default = null) |
|
273 | |||
274 | public function setAttribute($name, $value) |
||
280 | |||
281 | 3 | public function getLinkAttributes() |
|
285 | |||
286 | 12 | public function setLinkAttributes(array $linkAttributes) |
|
292 | |||
293 | public function getLinkAttribute($name, $default = null) |
||
301 | |||
302 | public function setLinkAttribute($name, $value) |
||
308 | |||
309 | 3 | public function getChildrenAttributes() |
|
313 | |||
314 | 12 | public function setChildrenAttributes(array $childrenAttributes) |
|
320 | |||
321 | 3 | public function getChildrenAttribute($name, $default = null) |
|
329 | |||
330 | public function setChildrenAttribute($name, $value) |
||
336 | |||
337 | public function getLabelAttributes() |
||
341 | |||
342 | 12 | public function setLabelAttributes(array $labelAttributes) |
|
348 | |||
349 | public function getLabelAttribute($name, $default = null) |
||
357 | |||
358 | public function setLabelAttribute($name, $value) |
||
364 | |||
365 | public function getExtras() |
||
369 | |||
370 | public function setExtras(array $extras) |
||
376 | |||
377 | 3 | public function getExtra($name, $default = null) |
|
385 | |||
386 | public function setExtra($name, $value) |
||
392 | |||
393 | 3 | public function getDisplayChildren() |
|
397 | |||
398 | 12 | public function setDisplayChildren($bool) |
|
404 | |||
405 | 3 | public function isDisplayed() |
|
409 | |||
410 | 12 | public function setDisplay($bool) |
|
416 | |||
417 | /** |
||
418 | * @param ItemInterface $menuItem |
||
419 | * |
||
420 | * @return bool |
||
421 | */ |
||
422 | public function hasChild(ItemInterface $menuItem): bool |
||
426 | |||
427 | /** |
||
428 | * {@inheritdoc} |
||
429 | */ |
||
430 | public function addChild($child, array $options = array()) |
||
439 | |||
440 | /** |
||
441 | * {@inheritdoc} |
||
442 | */ |
||
443 | public function getChild($name) |
||
447 | |||
448 | public function reorderChildren($order) |
||
468 | |||
469 | public function copy() |
||
480 | |||
481 | 3 | public function isRoot() |
|
485 | |||
486 | 8 | public function getParent() |
|
490 | |||
491 | 6 | public function setParent(ItemInterface $parent = null) |
|
501 | |||
502 | 3 | public function getChildren() |
|
510 | |||
511 | 12 | public function setChildren(array $children) |
|
517 | |||
518 | public function removeChild($name) |
||
530 | |||
531 | /** |
||
532 | * {@inheritdoc} |
||
533 | */ |
||
534 | 3 | public function getFirstChild() |
|
538 | |||
539 | /** |
||
540 | * {@inheritdoc} |
||
541 | */ |
||
542 | 3 | public function getLastChild() |
|
546 | |||
547 | 3 | public function hasChildren() |
|
557 | |||
558 | 12 | public function setCurrent($bool) |
|
564 | |||
565 | 3 | public function isCurrent() |
|
569 | |||
570 | 3 | public function isLast() |
|
579 | |||
580 | 3 | public function isFirst() |
|
589 | |||
590 | 3 | View Code Duplication | public function actsLikeFirst() |
617 | |||
618 | 3 | View Code Duplication | public function actsLikeLast() |
645 | |||
646 | /** |
||
647 | * Implements Countable. |
||
648 | */ |
||
649 | public function count() |
||
653 | |||
654 | /** |
||
655 | * Implements IteratorAggregate. |
||
656 | */ |
||
657 | 5 | public function getIterator() |
|
661 | |||
662 | /** |
||
663 | * Implements ArrayAccess. |
||
664 | */ |
||
665 | 3 | public function offsetExists($name) |
|
669 | |||
670 | /** |
||
671 | * Implements ArrayAccess. |
||
672 | */ |
||
673 | public function offsetGet($name) |
||
677 | |||
678 | /** |
||
679 | * Implements ArrayAccess. |
||
680 | */ |
||
681 | public function offsetSet($name, $value) |
||
685 | |||
686 | /** |
||
687 | * Implements ArrayAccess. |
||
688 | */ |
||
689 | public function offsetUnset($name) |
||
693 | } |
||
694 |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.