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