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 $wrapperTagName = 'ul'; |
||
| 38 | |||
| 39 | /** @var bool */ |
||
| 40 | protected $parentTagName = 'li'; |
||
| 41 | |||
| 42 | /** @var bool */ |
||
| 43 | protected $activeClassOnParent = true; |
||
| 44 | |||
| 45 | /** @var bool */ |
||
| 46 | protected $activeClassOnLink = false; |
||
| 47 | |||
| 48 | /** @var \Spatie\Menu\Html\Attributes */ |
||
| 49 | protected $htmlAttributes, $parentAttributes; |
||
| 50 | |||
| 51 | protected function __construct(Item ...$items) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Create a new menu, optionally prefilled with items. |
||
| 61 | * |
||
| 62 | * @param array $items |
||
| 63 | * |
||
| 64 | * @return static |
||
| 65 | */ |
||
| 66 | public static function new($items = []) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Build a new 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 | * @param \Spatie\Menu\Menu|null $initial |
||
| 79 | * |
||
| 80 | * @return static |
||
| 81 | */ |
||
| 82 | public static function build($items, callable $callback, self $initial = null) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Fill a menu from an array. The callback receives a menu instance as |
||
| 89 | * the accumulator, the array item as the second parameter, and the item's |
||
| 90 | * key as the third. |
||
| 91 | * |
||
| 92 | * @param array|\Iterator $items |
||
| 93 | * @param callable $callback |
||
| 94 | * |
||
| 95 | * @return static |
||
| 96 | */ |
||
| 97 | public function fill($items, callable $callback) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Add an item to the menu. This also applies all registered filters to the |
||
| 110 | * item. |
||
| 111 | * |
||
| 112 | * @param \Spatie\Menu\Item $item |
||
| 113 | * |
||
| 114 | * @return $this |
||
| 115 | */ |
||
| 116 | public function add(Item $item) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Add an item to the menu if a (non-strict) condition is met. |
||
| 129 | * |
||
| 130 | * @param bool $condition |
||
| 131 | * @param \Spatie\Menu\Item $item |
||
| 132 | * |
||
| 133 | * @return $this |
||
| 134 | */ |
||
| 135 | public function addIf($condition, Item $item) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Shortcut function to add a plain link to the menu. |
||
| 146 | * |
||
| 147 | * @param string $url |
||
| 148 | * @param string $text |
||
| 149 | * |
||
| 150 | * @return $this |
||
| 151 | */ |
||
| 152 | public function link(string $url, string $text) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Shortcut function to add an empty item to the menu. |
||
| 159 | * |
||
| 160 | * @return $this |
||
| 161 | */ |
||
| 162 | public function empty() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Add a link to the menu if a (non-strict) condition is met. |
||
| 169 | * |
||
| 170 | * @param bool $condition |
||
| 171 | * @param string $url |
||
| 172 | * @param string $text |
||
| 173 | * |
||
| 174 | * @return $this |
||
| 175 | */ |
||
| 176 | public function linkIf($condition, string $url, string $text) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Shortcut function to add raw html to the menu. |
||
| 187 | * |
||
| 188 | * @param string $html |
||
| 189 | * @param array $parentAttributes |
||
| 190 | * |
||
| 191 | * @return $this |
||
| 192 | */ |
||
| 193 | public function html(string $html, array $parentAttributes = []) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Add a chunk of html if a (non-strict) condition is met. |
||
| 200 | * |
||
| 201 | * @param bool $condition |
||
| 202 | * @param string $html |
||
| 203 | * @param array $parentAttributes |
||
| 204 | * |
||
| 205 | * @return $this |
||
| 206 | */ |
||
| 207 | public function htmlIf($condition, string $html, array $parentAttributes = []) |
||
| 215 | |||
| 216 | /** |
||
| 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 submenu($header, $menu = null) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param bool $condition |
||
| 234 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
| 235 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
| 236 | * |
||
| 237 | * @return $this |
||
| 238 | */ |
||
| 239 | public function submenuIf($condition, $header, $menu = null) |
||
| 247 | |||
| 248 | protected function parseSubmenuArgs($args): array |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param \Spatie\Menu\Menu|callable $menu |
||
| 259 | * |
||
| 260 | * @return \Spatie\Menu\Menu |
||
| 261 | */ |
||
| 262 | protected function createSubmenuMenu($menu): self |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param \Spatie\Menu\Item|string $header |
||
| 275 | * |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | protected function createSubmenuHeader($header): string |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Iterate over all the items and apply a callback. If you typehint the |
||
| 289 | * item parameter in the callable, it wil only be applied to items of that |
||
| 290 | * type. |
||
| 291 | * |
||
| 292 | * @param callable $callable |
||
| 293 | * |
||
| 294 | * @return $this |
||
| 295 | */ |
||
| 296 | public function each(callable $callable) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Register a filter to the menu. When an item is added, all filters will be |
||
| 313 | * applied to the item. If you typehint the item parameter in the callable, it |
||
| 314 | * will only be applied to items of that type. |
||
| 315 | * |
||
| 316 | * @param callable $callable |
||
| 317 | * |
||
| 318 | * @return $this |
||
| 319 | */ |
||
| 320 | public function registerFilter(callable $callable) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Apply a filter to an item. Returns the result of the filter. |
||
| 329 | * |
||
| 330 | * @param callable $filter |
||
| 331 | * @param \Spatie\Menu\Item $item |
||
| 332 | */ |
||
| 333 | protected function applyFilter(callable $filter, Item $item) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Apply a callable to all existing items, and register it as a filter so it |
||
| 346 | * will get applied to all new items too. If you typehint the item parameter |
||
| 347 | * in the callable, it wil only be applied to items of that type. |
||
| 348 | * |
||
| 349 | * @param callable $callable |
||
| 350 | * |
||
| 351 | * @return $this |
||
| 352 | */ |
||
| 353 | public function applyToAll(callable $callable) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Wrap the entire menu in an html element. This is another level of |
||
| 363 | * wrapping above the `wrapperTag`. |
||
| 364 | * |
||
| 365 | * @param string $element |
||
| 366 | * @param array $attributes |
||
| 367 | * |
||
| 368 | * @return $this |
||
| 369 | */ |
||
| 370 | public function wrap(string $element, $attributes = []) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Determine whether the menu is active. |
||
| 379 | * |
||
| 380 | * @return bool |
||
| 381 | */ |
||
| 382 | public function isActive(): bool |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Set multiple items in the menu as active based on a callable that filters |
||
| 395 | * through items. If you typehint the item parameter in the callable, it will |
||
| 396 | * only be applied to items of that type. |
||
| 397 | * |
||
| 398 | * @param callable|string $urlOrCallable |
||
| 399 | * @param string $root |
||
| 400 | * |
||
| 401 | * @return $this |
||
| 402 | */ |
||
| 403 | public function setActive($urlOrCallable, string $root = '/') |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Set all relevant children active based on the current request's URL. |
||
| 418 | * |
||
| 419 | * /, /about, /contact => request to /about will set the about link active. |
||
| 420 | * |
||
| 421 | * /en, /en/about, /en/contact => request to /en won't set /en active if the |
||
| 422 | * request root is set to /en. |
||
| 423 | * |
||
| 424 | * @param string $url The current request url. |
||
| 425 | * @param string $root If the link's URL is an exact match with the request |
||
| 426 | * root, the link won't be set active. This behavior is |
||
| 427 | * to avoid having home links active on every request. |
||
| 428 | * |
||
| 429 | * @return $this |
||
| 430 | */ |
||
| 431 | public function setActiveFromUrl(string $url, string $root = '/') |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param callable $callable |
||
| 446 | * |
||
| 447 | * @return $this |
||
| 448 | */ |
||
| 449 | public function setActiveFromCallable(callable $callable) |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Set the class name that will be used on active items for this menu. |
||
| 472 | * |
||
| 473 | * @param string $class |
||
| 474 | * |
||
| 475 | * @return $this |
||
| 476 | */ |
||
| 477 | public function setActiveClass(string $class) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Add a class to all items in the menu. |
||
| 486 | * |
||
| 487 | * @param string $class |
||
| 488 | * |
||
| 489 | * @return $this |
||
| 490 | */ |
||
| 491 | public function addItemClass(string $class) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Set an attribute on all items in the menu. |
||
| 502 | * |
||
| 503 | * @param string $attribute |
||
| 504 | * @param string $value |
||
| 505 | * |
||
| 506 | * @return $this |
||
| 507 | */ |
||
| 508 | public function setItemAttribute(string $attribute, string $value = '') |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Add a parent class to all items in the menu. |
||
| 519 | * |
||
| 520 | * @param string $class |
||
| 521 | * |
||
| 522 | * @return $this |
||
| 523 | */ |
||
| 524 | public function addItemParentClass(string $class) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Add a parent attribute to all items in the menu. |
||
| 535 | * |
||
| 536 | * @param string $attribute |
||
| 537 | * @param string $value |
||
| 538 | * |
||
| 539 | * @return $this |
||
| 540 | */ |
||
| 541 | public function setItemParentAttribute(string $attribute, string $value = '') |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Set tag for items wrapper. |
||
| 552 | * |
||
| 553 | * @param string|null $wrapperTagName |
||
| 554 | * @return $this |
||
| 555 | */ |
||
| 556 | public function setWrapperTag($wrapperTagName = null) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Set tag for items wrapper. |
||
| 565 | * |
||
| 566 | * @param string|null $wrapperTagName |
||
| 567 | * @return $this |
||
| 568 | */ |
||
| 569 | public function withoutWrapperTag() |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Set the parent tag name. |
||
| 578 | * |
||
| 579 | * @param string|null $parentTagName |
||
| 580 | * @return $this |
||
| 581 | */ |
||
| 582 | public function setParentTag($parentTagName = null) |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Render items without a parent tag. |
||
| 591 | * |
||
| 592 | * @return $this |
||
| 593 | */ |
||
| 594 | public function withoutParentTag() |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Set whether active class should (also) be on link. |
||
| 603 | * |
||
| 604 | * @param $activeClassOnLink |
||
| 605 | * @return $this |
||
| 606 | */ |
||
| 607 | public function setActiveClassOnLink(bool $activeClassOnLink = true) |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Set whether active class should (also) be on parent. |
||
| 616 | * |
||
| 617 | * @param $activeClassOnParent |
||
| 618 | * @return $this |
||
| 619 | */ |
||
| 620 | public function setActiveClassOnParent(bool $activeClassOnParent = true) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @param bool $condition |
||
| 629 | * @param callable $callable |
||
| 630 | * |
||
| 631 | * @return $this |
||
| 632 | */ |
||
| 633 | public function if(bool $condition, callable $callable) |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Create a empty blueprint of the menu (copies `filters` and `activeClass`). |
||
| 640 | * |
||
| 641 | * @return static |
||
| 642 | */ |
||
| 643 | public function blueprint() |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Render the menu. |
||
| 655 | * |
||
| 656 | * @return string |
||
| 657 | */ |
||
| 658 | public function render(): string |
||
| 676 | |||
| 677 | protected function renderItem(Item $item): string |
||
| 709 | |||
| 710 | /** |
||
| 711 | * The amount of items in the menu. |
||
| 712 | * |
||
| 713 | * @return int |
||
| 714 | */ |
||
| 715 | public function count(): int |
||
| 719 | |||
| 720 | /** |
||
| 721 | * @return string |
||
| 722 | */ |
||
| 723 | public function __toString(): string |
||
| 727 | |||
| 728 | public function getIterator(): Traversable |
||
| 732 | } |
||
| 733 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.