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 \Spatie\Menu\Html\Attributes */ |
||
34 | protected $htmlAttributes, $parentAttributes; |
||
35 | |||
36 | protected function __construct(Item ...$items) |
||
43 | |||
44 | /** |
||
45 | * Create a new menu, optionally prefilled with items. |
||
46 | * |
||
47 | * @param array $items |
||
48 | * |
||
49 | * @return static |
||
50 | */ |
||
51 | public static function new($items = []) |
||
55 | |||
56 | /** |
||
57 | * Build a new menu from an array. The callback receives a menu instance as |
||
58 | * the accumulator, the array item as the second parameter, and the item's |
||
59 | * key as the third. |
||
60 | * |
||
61 | * @param array|\Iterator $items |
||
62 | * @param callable $callback |
||
63 | * @param \Spatie\Menu\Menu|null $initial |
||
64 | * |
||
65 | * @return static |
||
66 | */ |
||
67 | public static function build($items, callable $callback, Menu $initial = null) |
||
71 | |||
72 | /** |
||
73 | * Fill a menu from an array. The callback receives a menu instance as |
||
74 | * the accumulator, the array item as the second parameter, and the item's |
||
75 | * key as the third. |
||
76 | * |
||
77 | * @param array|\Iterator $items |
||
78 | * @param callable $callback |
||
79 | * |
||
80 | * @return static |
||
81 | */ |
||
82 | public function fill($items, callable $callback) |
||
83 | { |
||
84 | $menu = $this; |
||
85 | |||
86 | foreach ($items as $key => $item) { |
||
87 | $menu = $callback($menu, $item, $key) ?: $menu; |
||
88 | } |
||
89 | |||
90 | return $menu; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Add an item to the menu. This also applies all registered filters to the |
||
95 | * item. |
||
96 | * |
||
97 | * @param \Spatie\Menu\Item $item |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | public function add(Item $item) |
||
111 | |||
112 | /** |
||
113 | * Add an item to the menu if a (non-strict) condition is met. |
||
114 | * |
||
115 | * @param bool $condition |
||
116 | * @param \Spatie\Menu\Item $item |
||
117 | * |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function addIf($condition, Item $item) |
||
128 | |||
129 | /** |
||
130 | * Shortcut function to add a plain link to the menu. |
||
131 | * |
||
132 | * @param string $url |
||
133 | * @param string $text |
||
134 | * |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function link(string $url, string $text) |
||
141 | |||
142 | /** |
||
143 | * Shortcut function to add an empty item to the menu. |
||
144 | * |
||
145 | * @return $this |
||
146 | */ |
||
147 | public function empty() |
||
151 | |||
152 | /** |
||
153 | * Add a link to the menu if a (non-strict) condition is met. |
||
154 | * |
||
155 | * @param bool $condition |
||
156 | * @param string $url |
||
157 | * @param string $text |
||
158 | * |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function linkIf($condition, string $url, string $text) |
||
169 | |||
170 | /** |
||
171 | * Shortcut function to add raw html to the menu. |
||
172 | * |
||
173 | * @param string $html |
||
174 | * @param array $parentAttributes |
||
175 | * |
||
176 | * @return $this |
||
177 | */ |
||
178 | public function html(string $html, array $parentAttributes = []) |
||
182 | |||
183 | /** |
||
184 | * Add a chunk of html if a (non-strict) condition is met. |
||
185 | * |
||
186 | * @param bool $condition |
||
187 | * @param string $html |
||
188 | * @param array $parentAttributes |
||
189 | * |
||
190 | * @return $this |
||
191 | */ |
||
192 | public function htmlIf($condition, string $html, array $parentAttributes = []) |
||
200 | |||
201 | /** |
||
202 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
203 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
204 | * |
||
205 | * @return $this |
||
206 | */ |
||
207 | public function submenu($header, $menu = null) |
||
216 | |||
217 | /** |
||
218 | * @param bool $condition |
||
219 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
220 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
221 | * |
||
222 | * @return $this |
||
223 | */ |
||
224 | public function submenuIf($condition, $header, $menu = null) |
||
232 | |||
233 | protected function parseSubmenuArgs($args): array |
||
241 | |||
242 | /** |
||
243 | * @param \Spatie\Menu\Menu|callable $menu |
||
244 | * |
||
245 | * @return \Spatie\Menu\Menu |
||
246 | */ |
||
247 | protected function createSubmenuMenu($menu): Menu |
||
257 | |||
258 | /** |
||
259 | * @param \Spatie\Menu\Item|string $header |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | protected function createSubmenuHeader($header): string |
||
271 | |||
272 | /** |
||
273 | * Iterate over all the items and apply a callback. If you typehint the |
||
274 | * item parameter in the callable, it wil only be applied to items of that |
||
275 | * type. |
||
276 | * |
||
277 | * @param callable $callable |
||
278 | * |
||
279 | * @return $this |
||
280 | */ |
||
281 | public function each(callable $callable) |
||
295 | |||
296 | /** |
||
297 | * Register a filter to the menu. When an item is added, all filters will be |
||
298 | * applied to the item. If you typehint the item parameter in the callable, it |
||
299 | * will only be applied to items of that type. |
||
300 | * |
||
301 | * @param callable $callable |
||
302 | * |
||
303 | * @return $this |
||
304 | */ |
||
305 | public function registerFilter(callable $callable) |
||
311 | |||
312 | /** |
||
313 | * Apply a filter to an item. Returns the result of the filter. |
||
314 | * |
||
315 | * @param callable $filter |
||
316 | * @param \Spatie\Menu\Item $item |
||
317 | */ |
||
318 | protected function applyFilter(callable $filter, Item $item) |
||
328 | |||
329 | /** |
||
330 | * Apply a callable to all existing items, and register it as a filter so it |
||
331 | * will get applied to all new items too. If you typehint the item parameter |
||
332 | * in the callable, it wil only be applied to items of that type. |
||
333 | * |
||
334 | * @param callable $callable |
||
335 | * |
||
336 | * @return $this |
||
337 | */ |
||
338 | public function applyToAll(callable $callable) |
||
345 | |||
346 | /** |
||
347 | * Wrap the menu in an html element. |
||
348 | * |
||
349 | * @param string $element |
||
350 | * @param array $attributes |
||
351 | * |
||
352 | * @return $this |
||
353 | */ |
||
354 | public function wrap(string $element, $attributes = []) |
||
360 | |||
361 | /** |
||
362 | * Determine whether the menu is active. |
||
363 | * |
||
364 | * @return bool |
||
365 | */ |
||
366 | public function isActive(): bool |
||
376 | |||
377 | /** |
||
378 | * Set multiple items in the menu as active based on a callable that filters |
||
379 | * through items. If you typehint the item parameter in the callable, it will |
||
380 | * only be applied to items of that type. |
||
381 | * |
||
382 | * @param callable|string $urlOrCallable |
||
383 | * @param string $root |
||
384 | * |
||
385 | * @return $this |
||
386 | */ |
||
387 | public function setActive($urlOrCallable, string $root = '/') |
||
399 | |||
400 | /** |
||
401 | * Set all relevant children active based on the current request's URL. |
||
402 | * |
||
403 | * /, /about, /contact => request to /about will set the about link active. |
||
404 | * |
||
405 | * /en, /en/about, /en/contact => request to /en won't set /en active if the |
||
406 | * request root is set to /en. |
||
407 | * |
||
408 | * @param string $url The current request url. |
||
409 | * @param string $root If the link's URL is an exact match with the request |
||
410 | * root, the link won't be set active. This behavior is |
||
411 | * to avoid having home links active on every request. |
||
412 | * |
||
413 | * @return $this |
||
414 | */ |
||
415 | public function setActiveFromUrl(string $url, string $root = '/') |
||
427 | |||
428 | /** |
||
429 | * @param callable $callable |
||
430 | * |
||
431 | * @return $this |
||
432 | */ |
||
433 | public function setActiveFromCallable(callable $callable) |
||
453 | |||
454 | /** |
||
455 | * Set the class name that will be used on active items for this menu. |
||
456 | * |
||
457 | * @param string $class |
||
458 | * |
||
459 | * @return $this |
||
460 | */ |
||
461 | public function setActiveClass(string $class) |
||
467 | |||
468 | /** |
||
469 | * Add a class to all items in the menu. |
||
470 | * |
||
471 | * @param string $class |
||
472 | * |
||
473 | * @return $this |
||
474 | */ |
||
475 | public function addItemClass(string $class) |
||
483 | |||
484 | /** |
||
485 | * Set an attribute on all items in the menu. |
||
486 | * |
||
487 | * @param string $attribute |
||
488 | * @param string $value |
||
489 | * |
||
490 | * @return $this |
||
491 | */ |
||
492 | public function setItemAttribute(string $attribute, string $value = '') |
||
500 | |||
501 | /** |
||
502 | * Add a parent class to all items in the menu. |
||
503 | * |
||
504 | * @param string $class |
||
505 | * |
||
506 | * @return $this |
||
507 | */ |
||
508 | public function addItemParentClass(string $class) |
||
516 | |||
517 | /** |
||
518 | * Add a parent attribute to all items in the menu. |
||
519 | * |
||
520 | * @param string $attribute |
||
521 | * @param string $value |
||
522 | * |
||
523 | * @return $this |
||
524 | */ |
||
525 | public function setItemParentAttribute(string $attribute, string $value = '') |
||
533 | |||
534 | /** |
||
535 | * @param bool $condition |
||
536 | * @param callable $callable |
||
537 | * |
||
538 | * @return $this |
||
539 | */ |
||
540 | public function if(bool $condition, callable $callable) |
||
544 | |||
545 | /** |
||
546 | * Create a empty blueprint of the menu (copies `filters` and `activeClass`). |
||
547 | * |
||
548 | * @return static |
||
549 | */ |
||
550 | public function blueprint() |
||
559 | |||
560 | /** |
||
561 | * Render the menu. |
||
562 | * |
||
563 | * @return string |
||
564 | */ |
||
565 | public function render(): string |
||
579 | |||
580 | protected function renderItem(Item $item): string |
||
594 | |||
595 | /** |
||
596 | * The amount of items in the menu. |
||
597 | * |
||
598 | * @return int |
||
599 | */ |
||
600 | public function count(): int |
||
604 | |||
605 | /** |
||
606 | * @return string |
||
607 | */ |
||
608 | public function __toString(): string |
||
612 | } |
||
613 |
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.