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 |
||
| 17 | class Menu implements Item, Countable, HasHtmlAttributes, HasParentAttributes, IteratorAggregate |
||
| 18 | { |
||
| 19 | use HasHtmlAttributesTrait, HasParentAttributesTrait, ConditionsTrait, HasAttributesTrait; |
||
| 20 | |||
| 21 | /** @var array */ |
||
| 22 | protected $items = []; |
||
| 23 | |||
| 24 | /** @var array */ |
||
| 25 | protected $filters = []; |
||
| 26 | |||
| 27 | /** @var string */ |
||
| 28 | protected $prepend, $append = ''; |
||
| 29 | |||
| 30 | /** @var array */ |
||
| 31 | protected $wrap = []; |
||
| 32 | |||
| 33 | /** @var string */ |
||
| 34 | protected $activeClass = 'active'; |
||
| 35 | |||
| 36 | /** @var string */ |
||
| 37 | protected $exactActiveClass = 'exact-active'; |
||
| 38 | |||
| 39 | /** @var string */ |
||
| 40 | protected $wrapperTagName = 'ul'; |
||
| 41 | |||
| 42 | /** @var bool */ |
||
| 43 | protected $parentTagName = 'li'; |
||
| 44 | |||
| 45 | /** @var bool */ |
||
| 46 | protected $activeClassOnParent = true; |
||
| 47 | |||
| 48 | /** @var bool */ |
||
| 49 | protected $activeClassOnLink = false; |
||
| 50 | |||
| 51 | /** @var \Spatie\Menu\Html\Attributes */ |
||
| 52 | protected $htmlAttributes, $parentAttributes; |
||
| 53 | |||
| 54 | protected function __construct(Item ...$items) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Create a new menu, optionally prefilled with items. |
||
| 64 | * |
||
| 65 | * @param array $items |
||
| 66 | * |
||
| 67 | * @return static |
||
| 68 | */ |
||
| 69 | public static function new($items = []) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Build a new menu from an array. The callback receives a menu instance as |
||
| 76 | * the accumulator, the array item as the second parameter, and the item's |
||
| 77 | * key as the third. |
||
| 78 | * |
||
| 79 | * @param array|\Iterator $items |
||
| 80 | * @param callable $callback |
||
| 81 | * @param \Spatie\Menu\Menu|null $initial |
||
| 82 | * |
||
| 83 | * @return static |
||
| 84 | */ |
||
| 85 | public static function build($items, callable $callback, self $initial = null) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Fill a menu from an array. The callback receives a menu instance as |
||
| 92 | * the accumulator, the array item as the second parameter, and the item's |
||
| 93 | * key as the third. |
||
| 94 | * |
||
| 95 | * @param array|\Iterator $items |
||
| 96 | * @param callable $callback |
||
| 97 | * |
||
| 98 | * @return static |
||
| 99 | */ |
||
| 100 | public function fill($items, callable $callback) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Add an item to the menu. This also applies all registered filters to the |
||
| 113 | * item. |
||
| 114 | * |
||
| 115 | * @param \Spatie\Menu\Item $item |
||
| 116 | * |
||
| 117 | * @return $this |
||
| 118 | */ |
||
| 119 | public function add(Item $item) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Add an item to the menu if a (non-strict) condition is met. |
||
| 132 | * |
||
| 133 | * @param bool $condition |
||
| 134 | * @param \Spatie\Menu\Item $item |
||
| 135 | * |
||
| 136 | * @return $this |
||
| 137 | */ |
||
| 138 | public function addIf($condition, Item $item) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Shortcut function to add a plain link to the menu. |
||
| 149 | * |
||
| 150 | * @param string $url |
||
| 151 | * @param string $text |
||
| 152 | * |
||
| 153 | * @return $this |
||
| 154 | */ |
||
| 155 | public function link(string $url, string $text) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Shortcut function to add an empty item to the menu. |
||
| 162 | * |
||
| 163 | * @return $this |
||
| 164 | */ |
||
| 165 | public function empty() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Add a link to the menu if a (non-strict) condition is met. |
||
| 172 | * |
||
| 173 | * @param bool $condition |
||
| 174 | * @param string $url |
||
| 175 | * @param string $text |
||
| 176 | * |
||
| 177 | * @return $this |
||
| 178 | */ |
||
| 179 | public function linkIf($condition, string $url, string $text) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Shortcut function to add raw html to the menu. |
||
| 190 | * |
||
| 191 | * @param string $html |
||
| 192 | * @param array $parentAttributes |
||
| 193 | * |
||
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | public function html(string $html, array $parentAttributes = []) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Add a chunk of html if a (non-strict) condition is met. |
||
| 203 | * |
||
| 204 | * @param bool $condition |
||
| 205 | * @param string $html |
||
| 206 | * @param array $parentAttributes |
||
| 207 | * |
||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | public function htmlIf($condition, string $html, array $parentAttributes = []) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
| 221 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
| 222 | * |
||
| 223 | * @return $this |
||
| 224 | */ |
||
| 225 | public function submenu($header, $menu = null) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param bool $condition |
||
| 237 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
| 238 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
| 239 | * |
||
| 240 | * @return $this |
||
| 241 | */ |
||
| 242 | public function submenuIf($condition, $header, $menu = null) |
||
| 250 | |||
| 251 | protected function parseSubmenuArgs($args): array |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param \Spatie\Menu\Menu|callable $menu |
||
| 262 | * |
||
| 263 | * @return \Spatie\Menu\Menu |
||
| 264 | */ |
||
| 265 | protected function createSubmenuMenu($menu): self |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param \Spatie\Menu\Item|string $header |
||
| 278 | * |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | protected function createSubmenuHeader($header): string |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Iterate over all the items and apply a callback. If you typehint the |
||
| 292 | * item parameter in the callable, it wil only be applied to items of that |
||
| 293 | * type. |
||
| 294 | * |
||
| 295 | * @param callable $callable |
||
| 296 | * |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | public function each(callable $callable) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Register a filter to the menu. When an item is added, all filters will be |
||
| 316 | * applied to the item. If you typehint the item parameter in the callable, it |
||
| 317 | * will only be applied to items of that type. |
||
| 318 | * |
||
| 319 | * @param callable $callable |
||
| 320 | * |
||
| 321 | * @return $this |
||
| 322 | */ |
||
| 323 | public function registerFilter(callable $callable) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Apply a filter to an item. Returns the result of the filter. |
||
| 332 | * |
||
| 333 | * @param callable $filter |
||
| 334 | * @param \Spatie\Menu\Item $item |
||
| 335 | */ |
||
| 336 | protected function applyFilter(callable $filter, Item $item) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Apply a callable to all existing items, and register it as a filter so it |
||
| 349 | * will get applied to all new items too. If you typehint the item parameter |
||
| 350 | * in the callable, it wil only be applied to items of that type. |
||
| 351 | * |
||
| 352 | * @param callable $callable |
||
| 353 | * |
||
| 354 | * @return $this |
||
| 355 | */ |
||
| 356 | public function applyToAll(callable $callable) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Wrap the entire menu in an html element. This is another level of |
||
| 366 | * wrapping above the `wrapperTag`. |
||
| 367 | * |
||
| 368 | * @param string $element |
||
| 369 | * @param array $attributes |
||
| 370 | * |
||
| 371 | * @return $this |
||
| 372 | */ |
||
| 373 | public function wrap(string $element, $attributes = []) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Determine whether the menu is active. |
||
| 382 | * |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | public function isActive(): bool |
||
| 395 | |||
| 396 | /** |
||
| 397 | * A menu can be active but not exact-active. |
||
| 398 | * |
||
| 399 | * @return bool |
||
| 400 | */ |
||
| 401 | public function isExactActive(): bool |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Set multiple items in the menu as active based on a callable that filters |
||
| 408 | * through items. If you typehint the item parameter in the callable, it will |
||
| 409 | * only be applied to items of that type. |
||
| 410 | * |
||
| 411 | * @param callable|string $urlOrCallable |
||
| 412 | * @param string $root |
||
| 413 | * |
||
| 414 | * @return $this |
||
| 415 | */ |
||
| 416 | public function setActive($urlOrCallable, string $root = '/') |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Set the class name that will be used on exact-active items for this menu. |
||
| 431 | * |
||
| 432 | * @param string $class |
||
| 433 | * |
||
| 434 | * @return $this |
||
| 435 | */ |
||
| 436 | public function setExactActiveClass(string $class) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Set all relevant children active based on the current request's URL. |
||
| 445 | * |
||
| 446 | * /, /about, /contact => request to /about will set the about link active. |
||
| 447 | * |
||
| 448 | * /en, /en/about, /en/contact => request to /en won't set /en active if the |
||
| 449 | * request root is set to /en. |
||
| 450 | * |
||
| 451 | * @param string $url The current request url. |
||
| 452 | * @param string $root If the link's URL is an exact match with the request |
||
| 453 | * root, the link won't be set active. This behavior is |
||
| 454 | * to avoid having home links active on every request. |
||
| 455 | * |
||
| 456 | * @return $this |
||
| 457 | */ |
||
| 458 | public function setActiveFromUrl(string $url, string $root = '/') |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @param callable $callable |
||
| 473 | * |
||
| 474 | * @return $this |
||
| 475 | */ |
||
| 476 | public function setActiveFromCallable(callable $callable) |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Set the class name that will be used on active items for this menu. |
||
| 500 | * |
||
| 501 | * @param string $class |
||
| 502 | * |
||
| 503 | * @return $this |
||
| 504 | */ |
||
| 505 | public function setActiveClass(string $class) |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Add a class to all items in the menu. |
||
| 514 | * |
||
| 515 | * @param string $class |
||
| 516 | * |
||
| 517 | * @return $this |
||
| 518 | */ |
||
| 519 | public function addItemClass(string $class) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Set an attribute on all items in the menu. |
||
| 530 | * |
||
| 531 | * @param string $attribute |
||
| 532 | * @param string $value |
||
| 533 | * |
||
| 534 | * @return $this |
||
| 535 | */ |
||
| 536 | public function setItemAttribute(string $attribute, string $value = '') |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Add a parent class to all items in the menu. |
||
| 547 | * |
||
| 548 | * @param string $class |
||
| 549 | * |
||
| 550 | * @return $this |
||
| 551 | */ |
||
| 552 | public function addItemParentClass(string $class) |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Add a parent attribute to all items in the menu. |
||
| 563 | * |
||
| 564 | * @param string $attribute |
||
| 565 | * @param string $value |
||
| 566 | * |
||
| 567 | * @return $this |
||
| 568 | */ |
||
| 569 | public function setItemParentAttribute(string $attribute, string $value = '') |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Set tag for items wrapper. |
||
| 580 | * |
||
| 581 | * @param string|null $wrapperTagName |
||
| 582 | * @return $this |
||
| 583 | */ |
||
| 584 | public function setWrapperTag($wrapperTagName = null) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Set tag for items wrapper. |
||
| 593 | * |
||
| 594 | * @param string|null $wrapperTagName |
||
| 595 | * @return $this |
||
| 596 | */ |
||
| 597 | public function withoutWrapperTag() |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Set the parent tag name. |
||
| 606 | * |
||
| 607 | * @param string|null $parentTagName |
||
| 608 | * @return $this |
||
| 609 | */ |
||
| 610 | public function setParentTag($parentTagName = null) |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Render items without a parent tag. |
||
| 619 | * |
||
| 620 | * @return $this |
||
| 621 | */ |
||
| 622 | public function withoutParentTag() |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Set whether active class should (also) be on link. |
||
| 631 | * |
||
| 632 | * @param $activeClassOnLink |
||
| 633 | * @return $this |
||
| 634 | */ |
||
| 635 | public function setActiveClassOnLink(bool $activeClassOnLink = true) |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Set whether active class should (also) be on parent. |
||
| 644 | * |
||
| 645 | * @param $activeClassOnParent |
||
| 646 | * @return $this |
||
| 647 | */ |
||
| 648 | public function setActiveClassOnParent(bool $activeClassOnParent = true) |
||
| 654 | |||
| 655 | /** |
||
| 656 | * @param bool $condition |
||
| 657 | * @param callable $callable |
||
| 658 | * |
||
| 659 | * @return $this |
||
| 660 | */ |
||
| 661 | public function if(bool $condition, callable $callable) |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Create a empty blueprint of the menu (copies `filters` and `activeClass`). |
||
| 668 | * |
||
| 669 | * @return static |
||
| 670 | */ |
||
| 671 | public function blueprint() |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Render the menu. |
||
| 683 | * |
||
| 684 | * @return string |
||
| 685 | */ |
||
| 686 | public function render(): string |
||
| 704 | |||
| 705 | protected function renderItem(Item $item): string |
||
| 745 | |||
| 746 | /** |
||
| 747 | * The amount of items in the menu. |
||
| 748 | * |
||
| 749 | * @return int |
||
| 750 | */ |
||
| 751 | public function count(): int |
||
| 755 | |||
| 756 | /** |
||
| 757 | * @return string |
||
| 758 | */ |
||
| 759 | public function __toString(): string |
||
| 763 | |||
| 764 | public function getIterator(): Traversable |
||
| 768 | } |
||
| 769 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.