Complex classes like Menu 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 Menu, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Menu implements Item, Countable, HasHtmlAttributes, HasParentAttributes |
||
15 | { |
||
16 | use HasHtmlAttributesTrait, HasParentAttributesTrait, ConditionsTrait, HasAttributesTrait; |
||
17 | |||
18 | /** @var array */ |
||
19 | protected $items = []; |
||
20 | |||
21 | /** @var array */ |
||
22 | protected $filters = []; |
||
23 | |||
24 | /** @var string */ |
||
25 | protected $prepend, $append = ''; |
||
|
|||
26 | |||
27 | /** @var array */ |
||
28 | protected $wrap = []; |
||
29 | |||
30 | /** @var string */ |
||
31 | protected $activeClass = 'active'; |
||
32 | |||
33 | /** @var string */ |
||
34 | protected $wrapperTagName = 'ul'; |
||
35 | |||
36 | /** @var bool */ |
||
37 | protected $parentTagName = 'li'; |
||
38 | |||
39 | /** @var bool */ |
||
40 | protected $activeClassOnParent = true; |
||
41 | |||
42 | /** @var bool */ |
||
43 | protected $activeClassOnLink = false; |
||
44 | |||
45 | /** @var \Spatie\Menu\Html\Attributes */ |
||
46 | protected $htmlAttributes, $parentAttributes; |
||
47 | |||
48 | protected function __construct(Item ...$items) |
||
55 | |||
56 | /** |
||
57 | * Create a new menu, optionally prefilled with items. |
||
58 | * |
||
59 | * @param array $items |
||
60 | * |
||
61 | * @return static |
||
62 | */ |
||
63 | public static function new($items = []) |
||
67 | |||
68 | /** |
||
69 | * Build a new menu from an array. The callback receives a menu instance as |
||
70 | * the accumulator, the array item as the second parameter, and the item's |
||
71 | * key as the third. |
||
72 | * |
||
73 | * @param array|\Iterator $items |
||
74 | * @param callable $callback |
||
75 | * @param \Spatie\Menu\Menu|null $initial |
||
76 | * |
||
77 | * @return static |
||
78 | */ |
||
79 | public static function build($items, callable $callback, self $initial = null) |
||
83 | |||
84 | /** |
||
85 | * Fill a menu from an array. The callback receives a menu instance as |
||
86 | * the accumulator, the array item as the second parameter, and the item's |
||
87 | * key as the third. |
||
88 | * |
||
89 | * @param array|\Iterator $items |
||
90 | * @param callable $callback |
||
91 | * |
||
92 | * @return static |
||
93 | */ |
||
94 | public function fill($items, callable $callback) |
||
104 | |||
105 | /** |
||
106 | * Add an item to the menu. This also applies all registered filters to the |
||
107 | * item. |
||
108 | * |
||
109 | * @param \Spatie\Menu\Item $item |
||
110 | * |
||
111 | * @return $this |
||
112 | */ |
||
113 | public function add(Item $item) |
||
123 | |||
124 | /** |
||
125 | * Add an item to the menu if a (non-strict) condition is met. |
||
126 | * |
||
127 | * @param bool $condition |
||
128 | * @param \Spatie\Menu\Item $item |
||
129 | * |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function addIf($condition, Item $item) |
||
140 | |||
141 | /** |
||
142 | * Shortcut function to add a plain link to the menu. |
||
143 | * |
||
144 | * @param string $url |
||
145 | * @param string $text |
||
146 | * |
||
147 | * @return $this |
||
148 | */ |
||
149 | public function link(string $url, string $text) |
||
153 | |||
154 | /** |
||
155 | * Shortcut function to add an empty item to the menu. |
||
156 | * |
||
157 | * @return $this |
||
158 | */ |
||
159 | public function empty() |
||
163 | |||
164 | /** |
||
165 | * Add a link to the menu if a (non-strict) condition is met. |
||
166 | * |
||
167 | * @param bool $condition |
||
168 | * @param string $url |
||
169 | * @param string $text |
||
170 | * |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function linkIf($condition, string $url, string $text) |
||
181 | |||
182 | /** |
||
183 | * Shortcut function to add raw html to the menu. |
||
184 | * |
||
185 | * @param string $html |
||
186 | * @param array $parentAttributes |
||
187 | * |
||
188 | * @return $this |
||
189 | */ |
||
190 | public function html(string $html, array $parentAttributes = []) |
||
194 | |||
195 | /** |
||
196 | * Add a chunk of html if a (non-strict) condition is met. |
||
197 | * |
||
198 | * @param bool $condition |
||
199 | * @param string $html |
||
200 | * @param array $parentAttributes |
||
201 | * |
||
202 | * @return $this |
||
203 | */ |
||
204 | public function htmlIf($condition, string $html, array $parentAttributes = []) |
||
212 | |||
213 | /** |
||
214 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
215 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
216 | * |
||
217 | * @return $this |
||
218 | */ |
||
219 | public function submenu($header, $menu = null) |
||
228 | |||
229 | /** |
||
230 | * @param bool $condition |
||
231 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
232 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
233 | * |
||
234 | * @return $this |
||
235 | */ |
||
236 | public function submenuIf($condition, $header, $menu = null) |
||
244 | |||
245 | protected function parseSubmenuArgs($args): array |
||
253 | |||
254 | /** |
||
255 | * @param \Spatie\Menu\Menu|callable $menu |
||
256 | * |
||
257 | * @return \Spatie\Menu\Menu |
||
258 | */ |
||
259 | protected function createSubmenuMenu($menu): self |
||
269 | |||
270 | /** |
||
271 | * @param \Spatie\Menu\Item|string $header |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | protected function createSubmenuHeader($header): string |
||
283 | |||
284 | /** |
||
285 | * Iterate over all the items and apply a callback. If you typehint the |
||
286 | * item parameter in the callable, it wil only be applied to items of that |
||
287 | * type. |
||
288 | * |
||
289 | * @param callable $callable |
||
290 | * |
||
291 | * @return $this |
||
292 | */ |
||
293 | public function each(callable $callable) |
||
307 | |||
308 | /** |
||
309 | * Register a filter to the menu. When an item is added, all filters will be |
||
310 | * applied to the item. If you typehint the item parameter in the callable, it |
||
311 | * will only be applied to items of that type. |
||
312 | * |
||
313 | * @param callable $callable |
||
314 | * |
||
315 | * @return $this |
||
316 | */ |
||
317 | public function registerFilter(callable $callable) |
||
323 | |||
324 | /** |
||
325 | * Apply a filter to an item. Returns the result of the filter. |
||
326 | * |
||
327 | * @param callable $filter |
||
328 | * @param \Spatie\Menu\Item $item |
||
329 | */ |
||
330 | protected function applyFilter(callable $filter, Item $item) |
||
340 | |||
341 | /** |
||
342 | * Apply a callable to all existing items, and register it as a filter so it |
||
343 | * will get applied to all new items too. If you typehint the item parameter |
||
344 | * in the callable, it wil only be applied to items of that type. |
||
345 | * |
||
346 | * @param callable $callable |
||
347 | * |
||
348 | * @return $this |
||
349 | */ |
||
350 | public function applyToAll(callable $callable) |
||
357 | |||
358 | /** |
||
359 | * Wrap the entire menu in an html element. This is another level of |
||
360 | * wrapping above the `wrapperTag`. |
||
361 | * |
||
362 | * @param string $element |
||
363 | * @param array $attributes |
||
364 | * |
||
365 | * @return $this |
||
366 | */ |
||
367 | public function wrap(string $element, $attributes = []) |
||
373 | |||
374 | /** |
||
375 | * Determine whether the menu is active. |
||
376 | * |
||
377 | * @return bool |
||
378 | */ |
||
379 | public function isActive(): bool |
||
389 | |||
390 | /** |
||
391 | * Set multiple items in the menu as active based on a callable that filters |
||
392 | * through items. If you typehint the item parameter in the callable, it will |
||
393 | * only be applied to items of that type. |
||
394 | * |
||
395 | * @param callable|string $urlOrCallable |
||
396 | * @param string $root |
||
397 | * |
||
398 | * @return $this |
||
399 | */ |
||
400 | public function setActive($urlOrCallable, string $root = '/') |
||
412 | |||
413 | /** |
||
414 | * Set all relevant children active based on the current request's URL. |
||
415 | * |
||
416 | * /, /about, /contact => request to /about will set the about link active. |
||
417 | * |
||
418 | * /en, /en/about, /en/contact => request to /en won't set /en active if the |
||
419 | * request root is set to /en. |
||
420 | * |
||
421 | * @param string $url The current request url. |
||
422 | * @param string $root If the link's URL is an exact match with the request |
||
423 | * root, the link won't be set active. This behavior is |
||
424 | * to avoid having home links active on every request. |
||
425 | * |
||
426 | * @return $this |
||
427 | */ |
||
428 | public function setActiveFromUrl(string $url, string $root = '/') |
||
440 | |||
441 | /** |
||
442 | * @param callable $callable |
||
443 | * |
||
444 | * @return $this |
||
445 | */ |
||
446 | public function setActiveFromCallable(callable $callable) |
||
466 | |||
467 | /** |
||
468 | * Set the class name that will be used on active items for this menu. |
||
469 | * |
||
470 | * @param string $class |
||
471 | * |
||
472 | * @return $this |
||
473 | */ |
||
474 | public function setActiveClass(string $class) |
||
480 | |||
481 | /** |
||
482 | * Add a class to all items in the menu. |
||
483 | * |
||
484 | * @param string $class |
||
485 | * |
||
486 | * @return $this |
||
487 | */ |
||
488 | public function addItemClass(string $class) |
||
496 | |||
497 | /** |
||
498 | * Set an attribute on all items in the menu. |
||
499 | * |
||
500 | * @param string $attribute |
||
501 | * @param string $value |
||
502 | * |
||
503 | * @return $this |
||
504 | */ |
||
505 | public function setItemAttribute(string $attribute, string $value = '') |
||
513 | |||
514 | /** |
||
515 | * Add a parent class to all items in the menu. |
||
516 | * |
||
517 | * @param string $class |
||
518 | * |
||
519 | * @return $this |
||
520 | */ |
||
521 | public function addItemParentClass(string $class) |
||
529 | |||
530 | /** |
||
531 | * Add a parent attribute to all items in the menu. |
||
532 | * |
||
533 | * @param string $attribute |
||
534 | * @param string $value |
||
535 | * |
||
536 | * @return $this |
||
537 | */ |
||
538 | public function setItemParentAttribute(string $attribute, string $value = '') |
||
546 | |||
547 | /** |
||
548 | * Set tag for items wrapper. |
||
549 | * |
||
550 | * @param string|null $wrapperTagName |
||
551 | * @return $this |
||
552 | */ |
||
553 | public function setWrapperTag($wrapperTagName = null) |
||
559 | |||
560 | /** |
||
561 | * Set tag for items wrapper. |
||
562 | * |
||
563 | * @param string|null $wrapperTagName |
||
564 | * @return $this |
||
565 | */ |
||
566 | public function withoutWrapperTag() |
||
572 | |||
573 | /** |
||
574 | * Set the parent tag name. |
||
575 | * |
||
576 | * @param string|null $parentTagName |
||
577 | * @return $this |
||
578 | */ |
||
579 | public function setParentTag($parentTagName = null) |
||
585 | |||
586 | /** |
||
587 | * Render items without a parent tag. |
||
588 | * |
||
589 | * @return $this |
||
590 | */ |
||
591 | public function withoutParentTag() |
||
597 | |||
598 | /** |
||
599 | * Set whether active class should (also) be on link. |
||
600 | * |
||
601 | * @param $activeClassOnLink |
||
602 | * @return $this |
||
603 | */ |
||
604 | public function setActiveClassOnLink(bool $activeClassOnLink = true) |
||
610 | |||
611 | /** |
||
612 | * Set whether active class should (also) be on parent. |
||
613 | * |
||
614 | * @param $activeClassOnParent |
||
615 | * @return $this |
||
616 | */ |
||
617 | public function setActiveClassOnParent(bool $activeClassOnParent = true) |
||
623 | |||
624 | /** |
||
625 | * @param bool $condition |
||
626 | * @param callable $callable |
||
627 | * |
||
628 | * @return $this |
||
629 | */ |
||
630 | public function if(bool $condition, callable $callable) |
||
634 | |||
635 | /** |
||
636 | * Create a empty blueprint of the menu (copies `filters` and `activeClass`). |
||
637 | * |
||
638 | * @return static |
||
639 | */ |
||
640 | public function blueprint() |
||
649 | |||
650 | /** |
||
651 | * Render the menu. |
||
652 | * |
||
653 | * @return string |
||
654 | */ |
||
655 | public function render(): string |
||
673 | |||
674 | protected function renderItem(Item $item): string |
||
698 | |||
699 | /** |
||
700 | * The amount of items in the menu. |
||
701 | * |
||
702 | * @return int |
||
703 | */ |
||
704 | public function count(): int |
||
708 | |||
709 | /** |
||
710 | * @return string |
||
711 | */ |
||
712 | public function __toString(): string |
||
716 | } |
||
717 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.