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 MenuBuilder 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 MenuBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class MenuBuilder implements Countable |
||
10 | { |
||
11 | /** |
||
12 | * Menu name. |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $menu; |
||
17 | |||
18 | /** |
||
19 | * Array menu items. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $items = []; |
||
24 | |||
25 | /** |
||
26 | * Default presenter class. |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $presenter = Presenters\Bootstrap\NavbarPresenter::class; |
||
31 | |||
32 | /** |
||
33 | * Style name for each presenter. |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $styles = []; |
||
38 | |||
39 | /** |
||
40 | * Prefix URL. |
||
41 | * |
||
42 | * @var string|null |
||
43 | */ |
||
44 | protected $prefixUrl; |
||
45 | |||
46 | /** |
||
47 | * The name of view presenter. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $view; |
||
52 | |||
53 | /** |
||
54 | * The laravel view factory instance. |
||
55 | * |
||
56 | * @var \Illuminate\View\Factory |
||
57 | */ |
||
58 | protected $views; |
||
59 | |||
60 | /** |
||
61 | * Determine whether the ordering feature is enabled or not. |
||
62 | * |
||
63 | * @var boolean |
||
64 | */ |
||
65 | protected $ordering = false; |
||
66 | |||
67 | /** |
||
68 | * Resolved item binding map. |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | protected $bindings = []; |
||
73 | /** |
||
74 | * @var Repository |
||
75 | */ |
||
76 | private $config; |
||
77 | |||
78 | /** |
||
79 | * Constructor. |
||
80 | * |
||
81 | * @param string $menu |
||
82 | * @param Repository $config |
||
83 | */ |
||
84 | 10 | public function __construct($menu, Repository $config) |
|
89 | |||
90 | /** |
||
91 | * Get menu name. |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | 1 | public function getName() |
|
96 | { |
||
97 | 1 | return $this->menu; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Find menu item by given its title. |
||
102 | * |
||
103 | * @param string $title |
||
104 | * @param callable|null $callback |
||
105 | * @return mixed |
||
106 | */ |
||
107 | public function whereTitle($title, callable $callback = null) |
||
117 | |||
118 | /** |
||
119 | * Find menu item by given key and value. |
||
120 | * |
||
121 | * @param string $key |
||
122 | * @param string $value |
||
123 | * @return \Nwidart\Menus\MenuItem |
||
124 | */ |
||
125 | public function findBy($key, $value) |
||
131 | |||
132 | /** |
||
133 | * Set view factory instance. |
||
134 | * |
||
135 | * @param ViewFactory $views |
||
136 | * |
||
137 | * @return $this |
||
138 | */ |
||
139 | 9 | public function setViewFactory(ViewFactory $views) |
|
145 | |||
146 | /** |
||
147 | * Set view. |
||
148 | * |
||
149 | * @param string $view |
||
150 | * |
||
151 | * @return $this |
||
152 | */ |
||
153 | public function setView($view) |
||
159 | |||
160 | /** |
||
161 | * Set Prefix URL. |
||
162 | * |
||
163 | * @param string $prefixUrl |
||
164 | * |
||
165 | * @return $this |
||
166 | */ |
||
167 | public function setPrefixUrl($prefixUrl) |
||
173 | |||
174 | /** |
||
175 | * Set styles. |
||
176 | * |
||
177 | * @param array $styles |
||
178 | */ |
||
179 | public function setStyles(array $styles) |
||
183 | |||
184 | /** |
||
185 | * Set new presenter class. |
||
186 | * |
||
187 | * @param string $presenter |
||
188 | */ |
||
189 | public function setPresenter($presenter) |
||
193 | |||
194 | /** |
||
195 | * Get presenter instance. |
||
196 | * |
||
197 | * @return \Nwidart\Menus\Presenters\PresenterInterface |
||
198 | */ |
||
199 | 3 | public function getPresenter() |
|
203 | |||
204 | /** |
||
205 | * Set new presenter class by given style name. |
||
206 | * |
||
207 | * @param string $name |
||
208 | * |
||
209 | * @return self |
||
210 | */ |
||
211 | public function style($name) |
||
219 | |||
220 | /** |
||
221 | * Determine if the given name in the presenter style. |
||
222 | * |
||
223 | * @param $name |
||
224 | * |
||
225 | * @return bool |
||
226 | */ |
||
227 | 3 | public function hasStyle($name) |
|
231 | |||
232 | /** |
||
233 | * Get style aliases. |
||
234 | * |
||
235 | * @return mixed |
||
236 | */ |
||
237 | 3 | public function getStyles() |
|
241 | |||
242 | /** |
||
243 | * Get the presenter class name by given alias name. |
||
244 | * |
||
245 | * @param $name |
||
246 | * |
||
247 | * @return mixed |
||
248 | */ |
||
249 | public function getStyle($name) |
||
255 | |||
256 | /** |
||
257 | * Set new presenter class from given alias name. |
||
258 | * |
||
259 | * @param $name |
||
260 | */ |
||
261 | public function setPresenterFromStyle($name) |
||
265 | |||
266 | /** |
||
267 | * Set the resolved item bindings |
||
268 | * |
||
269 | * @param array $bindings |
||
270 | * @return $this |
||
271 | */ |
||
272 | 3 | public function setBindings(array $bindings) |
|
278 | |||
279 | /** |
||
280 | * Resolves a key from the bindings array. |
||
281 | * |
||
282 | * @param string|array $key |
||
283 | * @return mixed |
||
284 | */ |
||
285 | public function resolve($key) |
||
303 | |||
304 | /** |
||
305 | * Resolves an array of menu items properties. |
||
306 | * |
||
307 | * @param array &$items |
||
308 | * @return void |
||
309 | */ |
||
310 | 3 | protected function resolveItems(array &$items) |
|
321 | |||
322 | /** |
||
323 | * Add new child menu. |
||
324 | * |
||
325 | * @param array $attributes |
||
326 | * |
||
327 | * @return \Nwidart\Menus\MenuItem |
||
328 | */ |
||
329 | public function add(array $attributes = array()) |
||
337 | |||
338 | /** |
||
339 | * Create new menu with dropdown. |
||
340 | * |
||
341 | * @param $title |
||
342 | * @param callable $callback |
||
343 | * @param array $attributes |
||
344 | * |
||
345 | * @return $this |
||
346 | */ |
||
347 | View Code Duplication | public function dropdown($title, \Closure $callback, $order = null, array $attributes = array()) |
|
368 | |||
369 | /** |
||
370 | * Register new menu item using registered route. |
||
371 | * |
||
372 | * @param $route |
||
373 | * @param $title |
||
374 | * @param array $parameters |
||
375 | * @param array $attributes |
||
376 | * |
||
377 | * @return static |
||
378 | */ |
||
379 | public function route($route, $title, $parameters = array(), $order = null, $attributes = array()) |
||
401 | |||
402 | /** |
||
403 | * Format URL. |
||
404 | * |
||
405 | * @param string $url |
||
406 | * |
||
407 | * @return string |
||
408 | */ |
||
409 | 2 | protected function formatUrl($url) |
|
415 | |||
416 | /** |
||
417 | * Register new menu item using url. |
||
418 | * |
||
419 | * @param $url |
||
420 | * @param $title |
||
421 | * @param array $attributes |
||
422 | * |
||
423 | * @return static |
||
424 | */ |
||
425 | 2 | public function url($url, $title, $order = 0, $attributes = array()) |
|
445 | |||
446 | /** |
||
447 | * Add new divider item. |
||
448 | * |
||
449 | * @param int $order |
||
450 | * @return \Nwidart\Menus\MenuItem |
||
451 | */ |
||
452 | public function addDivider($order = null) |
||
458 | |||
459 | /** |
||
460 | * Add new header item. |
||
461 | * |
||
462 | * @return \Nwidart\Menus\MenuItem |
||
463 | */ |
||
464 | public function addHeader($title, $order = null) |
||
474 | |||
475 | /** |
||
476 | * Alias for "addHeader" method. |
||
477 | * |
||
478 | * @param string $title |
||
479 | * |
||
480 | * @return $this |
||
481 | */ |
||
482 | public function header($title) |
||
486 | |||
487 | /** |
||
488 | * Alias for "addDivider" method. |
||
489 | * |
||
490 | * @return $this |
||
491 | */ |
||
492 | public function divider() |
||
496 | |||
497 | /** |
||
498 | * Get items count. |
||
499 | * |
||
500 | * @return int |
||
501 | */ |
||
502 | 1 | public function count() |
|
503 | { |
||
504 | 1 | return count($this->items); |
|
505 | } |
||
506 | |||
507 | /** |
||
508 | * Empty the current menu items. |
||
509 | */ |
||
510 | public function destroy() |
||
516 | |||
517 | /** |
||
518 | * Render the menu to HTML tag. |
||
519 | * |
||
520 | * @param string $presenter |
||
521 | * |
||
522 | * @return string |
||
523 | */ |
||
524 | 3 | public function render($presenter = null) |
|
542 | |||
543 | /** |
||
544 | * Render menu via view presenter. |
||
545 | * |
||
546 | * @return \Illuminate\View\View |
||
547 | */ |
||
548 | public function renderView($presenter = null) |
||
554 | |||
555 | /** |
||
556 | * Get original items. |
||
557 | * |
||
558 | * @return array |
||
559 | */ |
||
560 | public function getItems() |
||
564 | |||
565 | /** |
||
566 | * Get menu items as laravel collection instance. |
||
567 | * |
||
568 | * @return \Illuminate\Support\Collection |
||
569 | */ |
||
570 | public function toCollection() |
||
574 | |||
575 | /** |
||
576 | * Get menu items as array. |
||
577 | * |
||
578 | * @return array |
||
579 | */ |
||
580 | public function toArray() |
||
584 | |||
585 | /** |
||
586 | * Enable menu ordering. |
||
587 | * |
||
588 | * @return self |
||
589 | */ |
||
590 | public function enableOrdering() |
||
596 | |||
597 | /** |
||
598 | * Disable menu ordering. |
||
599 | * |
||
600 | * @return self |
||
601 | */ |
||
602 | public function disableOrdering() |
||
608 | |||
609 | /** |
||
610 | * Get menu items and order it by 'order' key. |
||
611 | * |
||
612 | * @return array |
||
613 | */ |
||
614 | 3 | public function getOrderedItems() |
|
624 | |||
625 | /** |
||
626 | * Render the menu. |
||
627 | * |
||
628 | * @return string |
||
629 | */ |
||
630 | 3 | protected function renderMenu() |
|
655 | } |
||
656 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.