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 |
||
13 | class Menu implements Item, Countable, HasHtmlAttributes, HasParentAttributes |
||
14 | { |
||
15 | use HasHtmlAttributesTrait, HasParentAttributesTrait; |
||
16 | |||
17 | /** @var array */ |
||
18 | protected $items = []; |
||
19 | |||
20 | /** @var array */ |
||
21 | protected $filters = []; |
||
22 | |||
23 | /** @var string */ |
||
24 | protected $prepend, $append = ''; |
||
|
|||
25 | |||
26 | /** @var array */ |
||
27 | protected $wrap = []; |
||
28 | |||
29 | /** @var string */ |
||
30 | protected $activeClass = 'active'; |
||
31 | |||
32 | /** @var \Spatie\HtmlElement\Attributes */ |
||
33 | protected $htmlAttributes, $parentAttributes; |
||
34 | |||
35 | protected function __construct(Item ...$items) |
||
42 | |||
43 | /** |
||
44 | * Create a new menu, optionally prefilled with items. |
||
45 | * |
||
46 | * @param array $items |
||
47 | * |
||
48 | * @return static |
||
49 | */ |
||
50 | public static function new($items = []) |
||
54 | |||
55 | /** |
||
56 | * Build a new menu from an array. The callback receives a menu instance as |
||
57 | * the accumulator, the array item as the second parameter, and the item's |
||
58 | * key as the third. |
||
59 | * |
||
60 | * @param array|\Iterator $items |
||
61 | * @param callable $callback |
||
62 | * @param \Spatie\Menu\Menu|null $initial |
||
63 | * |
||
64 | * @return static |
||
65 | */ |
||
66 | public static function build($items, callable $callback, Menu $initial = null) |
||
70 | |||
71 | /** |
||
72 | * Fill a menu from an array. The callback receives a menu instance as |
||
73 | * the accumulator, the array item as the second parameter, and the item's |
||
74 | * key as the third. |
||
75 | * |
||
76 | * @param array|\Iterator $items |
||
77 | * @param callable $callback |
||
78 | * |
||
79 | * @return static |
||
80 | */ |
||
81 | public function fill($items, callable $callback) |
||
91 | |||
92 | /** |
||
93 | * Add an item to the menu. This also applies all registered filters to the |
||
94 | * item. |
||
95 | * |
||
96 | * @param \Spatie\Menu\Item $item |
||
97 | * |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function add(Item $item) |
||
110 | |||
111 | /** |
||
112 | * Add an item to the menu if a (non-strict) condition is met. |
||
113 | * |
||
114 | * @param bool $condition |
||
115 | * @param \Spatie\Menu\Item $item |
||
116 | * |
||
117 | * @return $this |
||
118 | */ |
||
119 | public function addIf($condition, Item $item) |
||
127 | |||
128 | /** |
||
129 | * Shortcut function to add a plain link to the menu. |
||
130 | * |
||
131 | * @param string $url |
||
132 | * @param string $text |
||
133 | * |
||
134 | * @return $this |
||
135 | */ |
||
136 | public function link(string $url, string $text) |
||
140 | |||
141 | /** |
||
142 | * Add a link to the menu if a (non-strict) condition is met. |
||
143 | * |
||
144 | * @param bool $condition |
||
145 | * @param string $url |
||
146 | * @param string $text |
||
147 | * |
||
148 | * @return $this |
||
149 | */ |
||
150 | public function linkIf($condition, string $url, string $text) |
||
158 | |||
159 | /** |
||
160 | * Shortcut function to add raw html to the menu. |
||
161 | * |
||
162 | * @param string $html |
||
163 | * @param array $parentAttributes |
||
164 | * |
||
165 | * @return $this |
||
166 | */ |
||
167 | public function html(string $html, array $parentAttributes = []) |
||
171 | |||
172 | /** |
||
173 | * Add a chunk of html if a (non-strict) condition is met. |
||
174 | * |
||
175 | * @param bool $condition |
||
176 | * @param string $html |
||
177 | * @param array $parentAttributes |
||
178 | * |
||
179 | * @return $this |
||
180 | */ |
||
181 | public function htmlIf($condition, string $html, array $parentAttributes = []) |
||
189 | |||
190 | /** |
||
191 | * @param $conditional |
||
192 | * @return bool |
||
193 | */ |
||
194 | protected function resolveCondition($conditional) |
||
198 | |||
199 | /** |
||
200 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
201 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
202 | * |
||
203 | * @return $this |
||
204 | */ |
||
205 | public function submenu($header, $menu = null) |
||
214 | |||
215 | /** |
||
216 | * @param bool $condition |
||
217 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
218 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
219 | * |
||
220 | * @return $this |
||
221 | */ |
||
222 | public function submenuIf($condition, $header, $menu = null) |
||
230 | |||
231 | protected function parseSubmenuArgs($args): array |
||
239 | |||
240 | /** |
||
241 | * @param \Spatie\Menu\Menu|callable $menu |
||
242 | * |
||
243 | * @return \Spatie\Menu\Menu |
||
244 | */ |
||
245 | protected function createSubmenuMenu($menu): Menu |
||
255 | |||
256 | /** |
||
257 | * @param \Spatie\Menu\Item|string $header |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | protected function createSubmenuHeader($header): string |
||
269 | |||
270 | /** |
||
271 | * Iterate over all the items and apply a callback. If you typehint the |
||
272 | * item parameter in the callable, it wil only be applied to items of that |
||
273 | * type. |
||
274 | * |
||
275 | * @param callable $callable |
||
276 | * |
||
277 | * @return $this |
||
278 | */ |
||
279 | public function each(callable $callable) |
||
293 | |||
294 | /** |
||
295 | * Register a filter to the menu. When an item is added, all filters will be |
||
296 | * applied to the item. If you typehint the item parameter in the callable, it |
||
297 | * will only be applied to items of that type. |
||
298 | * |
||
299 | * @param callable $callable |
||
300 | * |
||
301 | * @return $this |
||
302 | */ |
||
303 | public function registerFilter(callable $callable) |
||
309 | |||
310 | /** |
||
311 | * Apply a filter to an item. Returns the result of the filter. |
||
312 | * |
||
313 | * @param callable $filter |
||
314 | * @param \Spatie\Menu\Item $item |
||
315 | */ |
||
316 | protected function applyFilter(callable $filter, Item $item) |
||
326 | |||
327 | /** |
||
328 | * Apply a callable to all existing items, and register it as a filter so it |
||
329 | * will get applied to all new items too. If you typehint the item parameter |
||
330 | * in the callable, it wil only be applied to items of that type. |
||
331 | * |
||
332 | * @param callable $callable |
||
333 | * |
||
334 | * @return $this |
||
335 | */ |
||
336 | public function applyToAll(callable $callable) |
||
343 | |||
344 | /** |
||
345 | * Prepend the menu with a string of html on render. |
||
346 | * |
||
347 | * @param string $prepend |
||
348 | * |
||
349 | * @return $this |
||
350 | */ |
||
351 | public function prepend(string $prepend) |
||
357 | |||
358 | /** |
||
359 | * Prepend the menu with a string of html on render if a certain condition is |
||
360 | * met. |
||
361 | * |
||
362 | * @param bool $condition |
||
363 | * @param string $prepend |
||
364 | * |
||
365 | * @return $this |
||
366 | */ |
||
367 | public function prependIf($condition, string $prepend) |
||
375 | |||
376 | /** |
||
377 | * Append a string of html to the menu on render. |
||
378 | * |
||
379 | * @param string $append |
||
380 | * |
||
381 | * @return $this |
||
382 | */ |
||
383 | public function append(string $append) |
||
389 | |||
390 | /** |
||
391 | * Append the menu with a string of html on render if a certain condition is |
||
392 | * met. |
||
393 | * |
||
394 | * @param bool $condition |
||
395 | * @param string $append |
||
396 | * |
||
397 | * @return static |
||
398 | */ |
||
399 | public function appendIf($condition, string $append) |
||
407 | |||
408 | /** |
||
409 | * Wrap the menu in an html element. |
||
410 | * |
||
411 | * @param string $element |
||
412 | * @param array $attributes |
||
413 | * |
||
414 | * @return $this |
||
415 | */ |
||
416 | public function wrap(string $element, $attributes = []) |
||
422 | |||
423 | /** |
||
424 | * Determine whether the menu is active. |
||
425 | * |
||
426 | * @return bool |
||
427 | */ |
||
428 | public function isActive(): bool |
||
438 | |||
439 | /** |
||
440 | * Set multiple items in the menu as active based on a callable that filters |
||
441 | * through items. If you typehint the item parameter in the callable, it will |
||
442 | * only be applied to items of that type. |
||
443 | * |
||
444 | * @param callable|string $urlOrCallable |
||
445 | * @param string $root |
||
446 | * |
||
447 | * @return $this |
||
448 | */ |
||
449 | public function setActive($urlOrCallable, string $root = '/') |
||
461 | |||
462 | /** |
||
463 | * Set all relevant children active based on the current request's URL. |
||
464 | * |
||
465 | * /, /about, /contact => request to /about will set the about link active. |
||
466 | * |
||
467 | * /en, /en/about, /en/contact => request to /en won't set /en active if the |
||
468 | * request root is set to /en. |
||
469 | * |
||
470 | * @param string $url The current request url. |
||
471 | * @param string $root If the link's URL is an exact match with the request |
||
472 | * root, the link won't be set active. This behavior is |
||
473 | * to avoid having home links active on every request. |
||
474 | * |
||
475 | * @return $this |
||
476 | */ |
||
477 | public function setActiveFromUrl(string $url, string $root = '/') |
||
489 | |||
490 | /** |
||
491 | * @param callable $callable |
||
492 | * |
||
493 | * @return $this |
||
494 | */ |
||
495 | public function setActiveFromCallable(callable $callable) |
||
515 | |||
516 | /** |
||
517 | * Set the class name that will be used on active items for this menu. |
||
518 | * |
||
519 | * @param string $class |
||
520 | * |
||
521 | * @return $this |
||
522 | */ |
||
523 | public function setActiveClass(string $class) |
||
529 | |||
530 | /** |
||
531 | * Add a class to all items in the menu. |
||
532 | * |
||
533 | * @param string $class |
||
534 | * |
||
535 | * @return $this |
||
536 | */ |
||
537 | public function addItemClass(string $class) |
||
545 | |||
546 | /** |
||
547 | * Set an attribute on all items in the menu. |
||
548 | * |
||
549 | * @param string $attribute |
||
550 | * @param string $value |
||
551 | * |
||
552 | * @return $this |
||
553 | */ |
||
554 | public function setItemAttribute(string $attribute, string $value = '') |
||
562 | |||
563 | /** |
||
564 | * Add a parent class to all items in the menu. |
||
565 | * |
||
566 | * @param string $class |
||
567 | * |
||
568 | * @return $this |
||
569 | */ |
||
570 | public function addItemParentClass(string $class) |
||
578 | |||
579 | /** |
||
580 | * Add a parent attribute to all items in the menu. |
||
581 | * |
||
582 | * @param string $attribute |
||
583 | * @param string $value |
||
584 | * |
||
585 | * @return $this |
||
586 | */ |
||
587 | public function setItemParentAttribute(string $attribute, string $value = '') |
||
595 | |||
596 | /** |
||
597 | * @param bool $condition |
||
598 | * @param callable $callable |
||
599 | * |
||
600 | * @return $this |
||
601 | */ |
||
602 | public function if(bool $condition, callable $callable) |
||
606 | |||
607 | /** |
||
608 | * Create a empty blueprint of the menu (copies `filters` and `activeClass`). |
||
609 | * |
||
610 | * @return static |
||
611 | */ |
||
612 | public function blueprint() |
||
621 | |||
622 | /** |
||
623 | * Render the menu. |
||
624 | * |
||
625 | * @return string |
||
626 | */ |
||
627 | public function render(): string |
||
649 | |||
650 | /** |
||
651 | * The amount of items in the menu. |
||
652 | * |
||
653 | * @return int |
||
654 | */ |
||
655 | public function count(): int |
||
659 | |||
660 | /** |
||
661 | * @return string |
||
662 | */ |
||
663 | public function __toString(): string |
||
667 | } |
||
668 |
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.