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 HtmlAttributes, ParentAttributes; |
||
| 16 | |||
| 17 | /** @var array */ |
||
| 18 | protected $items = []; |
||
| 19 | |||
| 20 | /** @var string */ |
||
| 21 | protected $prepend = ''; |
||
| 22 | |||
| 23 | /** @var string */ |
||
| 24 | protected $append = ''; |
||
| 25 | |||
| 26 | /** @var array */ |
||
| 27 | protected $filters = []; |
||
| 28 | |||
| 29 | /** @var array */ |
||
| 30 | protected $wrap = []; |
||
| 31 | |||
| 32 | /** @var string */ |
||
| 33 | protected $activeClass = 'active'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param \Spatie\Menu\Item[] ...$items |
||
| 37 | */ |
||
| 38 | protected function __construct(Item ...$items) |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Create a new menu, optionally prefilled with items. |
||
| 48 | * |
||
| 49 | * @param array $items |
||
| 50 | * |
||
| 51 | * @return static |
||
| 52 | */ |
||
| 53 | public static function new(array $items = []) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Add an item to the menu. This also applies all registered filters to the |
||
| 60 | * item. |
||
| 61 | * |
||
| 62 | * @param \Spatie\Menu\Item $item |
||
| 63 | * |
||
| 64 | * @return $this |
||
| 65 | */ |
||
| 66 | public function add(Item $item) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Add an item to the menu if a (non-strict) condition is met. |
||
| 79 | * |
||
| 80 | * @param bool $condition |
||
| 81 | * @param \Spatie\Menu\Item $item |
||
| 82 | * |
||
| 83 | * @return $this |
||
| 84 | */ |
||
| 85 | public function addIf($condition, Item $item) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Shortcut function to add a plain link to the menu. |
||
| 96 | * |
||
| 97 | * @param string $url |
||
| 98 | * @param string $text |
||
| 99 | * |
||
| 100 | * @return $this |
||
| 101 | */ |
||
| 102 | public function link(string $url, string $text) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Add a link to the menu if a (non-strict) condition is met. |
||
| 109 | * |
||
| 110 | * @param bool $condition |
||
| 111 | * @param string $url |
||
| 112 | * @param string $text |
||
| 113 | * |
||
| 114 | * @return $this |
||
| 115 | */ |
||
| 116 | public function linkIf($condition, string $url, string $text) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Shortcut function to add raw html to the menu. |
||
| 127 | * |
||
| 128 | * @param string $html |
||
| 129 | * |
||
| 130 | * @return $this |
||
| 131 | */ |
||
| 132 | public function html(string $html) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Add a chunk of html if a (non-strict) condition is met. |
||
| 139 | * |
||
| 140 | * @param bool $condition |
||
| 141 | * @param string $html |
||
| 142 | * |
||
| 143 | * @return $this |
||
| 144 | */ |
||
| 145 | public function htmlIf($condition, string $html) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Add an empty item with parent attributes. |
||
| 156 | * |
||
| 157 | * @param array $parentAttributes |
||
| 158 | * |
||
| 159 | * @return $this |
||
| 160 | */ |
||
| 161 | public function void(array $parentAttributes = []) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Add an empty item with parent attributes if a (non-strict) condition is met. |
||
| 168 | * |
||
| 169 | * @param $condition |
||
| 170 | * @param array $parentAttributes |
||
| 171 | * |
||
| 172 | * @return $this |
||
| 173 | */ |
||
| 174 | public function voidIf($condition, array $parentAttributes = []) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
| 185 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
| 186 | * |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | public function submenu($header, $menu = null) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param bool $condition |
||
| 201 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
| 202 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
| 203 | * |
||
| 204 | * @return $this |
||
| 205 | */ |
||
| 206 | public function submenuIf($condition, $header, $menu = null) |
||
| 214 | |||
| 215 | protected function parseSubmenuArgs($args): array |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param \Spatie\Menu\Menu|callable $menu |
||
| 226 | * |
||
| 227 | * @return \Spatie\Menu\Menu |
||
| 228 | */ |
||
| 229 | protected function createSubmenuMenu($menu): Menu |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param \Spatie\Menu\Item|string $header |
||
| 242 | * |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | protected function createSubmenuHeader($header): string |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Iterate over all the items and apply a callback. If you typehint the |
||
| 256 | * item parameter in the callable, it wil only be applied to items of that |
||
| 257 | * type. |
||
| 258 | * |
||
| 259 | * @param callable $callable |
||
| 260 | * |
||
| 261 | * @return $this |
||
| 262 | */ |
||
| 263 | public function each(callable $callable) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Register a filter to the menu. When an item is added, all filters will be |
||
| 280 | * applied to the item. If you typehint the item parameter in the callable, it |
||
| 281 | * will only be applied to items of that type. |
||
| 282 | * |
||
| 283 | * @param callable $callable |
||
| 284 | * |
||
| 285 | * @return $this |
||
| 286 | */ |
||
| 287 | public function registerFilter(callable $callable) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Apply a filter to an item. Returns the result of the filter. |
||
| 296 | * |
||
| 297 | * @param callable $filter |
||
| 298 | * @param \Spatie\Menu\Item $item |
||
| 299 | */ |
||
| 300 | protected function applyFilter(callable $filter, Item $item) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Apply a callable to all existing items, and register it as a filter so it |
||
| 313 | * will get applied to all new items too. If you typehint the item parameter |
||
| 314 | * in the callable, it wil only be applied to items of that type. |
||
| 315 | * |
||
| 316 | * @param callable $callable |
||
| 317 | * |
||
| 318 | * @return $this |
||
| 319 | */ |
||
| 320 | public function applyToAll(callable $callable) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Prefix all the links in the menu. |
||
| 330 | * |
||
| 331 | * @param string $prefix |
||
| 332 | * |
||
| 333 | * @return $this |
||
| 334 | */ |
||
| 335 | public function prefixLinks(string $prefix) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Prepend the menu with a string of html on render. |
||
| 344 | * |
||
| 345 | * @param string $prepend |
||
| 346 | * |
||
| 347 | * @return $this |
||
| 348 | */ |
||
| 349 | public function prepend(string $prepend) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Prepend the menu with a string of html on render if a certain condition is |
||
| 358 | * met. |
||
| 359 | * |
||
| 360 | * @param bool $condition |
||
| 361 | * @param string $prepend |
||
| 362 | * |
||
| 363 | * @return $this |
||
| 364 | */ |
||
| 365 | public function prependIf(bool $condition, string $prepend) |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Append a string of html to the menu on render. |
||
| 376 | * |
||
| 377 | * @param string $append |
||
| 378 | * |
||
| 379 | * @return $this |
||
| 380 | */ |
||
| 381 | public function append(string $append) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Append the menu with a string of html on render if a certain condition is |
||
| 390 | * met. |
||
| 391 | * |
||
| 392 | * @param bool $condition |
||
| 393 | * @param string $append |
||
| 394 | * |
||
| 395 | * @return static |
||
| 396 | */ |
||
| 397 | public function appendIf(bool $condition, string $append) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Wrap the menu in an html element. |
||
| 408 | * |
||
| 409 | * @param string $element |
||
| 410 | * @param array $attributes |
||
| 411 | * |
||
| 412 | * @return $this |
||
| 413 | */ |
||
| 414 | public function wrap(string $element, $attributes = []) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Determine whether the menu is active. |
||
| 423 | * |
||
| 424 | * @return bool |
||
| 425 | */ |
||
| 426 | public function isActive(): bool |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Set multiple items in the menu as active based on a callable that filters |
||
| 439 | * through items. If you typehint the item parameter in the callable, it will |
||
| 440 | * only be applied to items of that type. |
||
| 441 | * |
||
| 442 | * @param callable|string $urlOrCallable |
||
| 443 | * @param string $root |
||
| 444 | * |
||
| 445 | * @return $this |
||
| 446 | */ |
||
| 447 | public function setActive($urlOrCallable, string $root = '/') |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Set all relevant children active based on the current request's URL. |
||
| 462 | * |
||
| 463 | * /, /about, /contact => request to /about will set the about link active. |
||
| 464 | * |
||
| 465 | * /en, /en/about, /en/contact => request to /en won't set /en active if the |
||
| 466 | * request root is set to /en. |
||
| 467 | * |
||
| 468 | * @param string $url The current request url. |
||
| 469 | * @param string $root If the link's URL is an exact match with the request |
||
| 470 | * root, the link won't be set active. This behavior is |
||
| 471 | * to avoid having home links active on every request. |
||
| 472 | * |
||
| 473 | * @return $this |
||
| 474 | */ |
||
| 475 | public function setActiveFromUrl(string $url, string $root = '/') |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @param callable $callable |
||
| 527 | * |
||
| 528 | * @return $this |
||
| 529 | */ |
||
| 530 | public function setActiveFromCallable(callable $callable) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Set the class name that will be used on active items for this menu. |
||
| 554 | * |
||
| 555 | * @param string $class |
||
| 556 | * |
||
| 557 | * @return $this |
||
| 558 | */ |
||
| 559 | public function setActiveClass(string $class) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Add a class to all items in the menu. |
||
| 568 | * |
||
| 569 | * @param string $class |
||
| 570 | * |
||
| 571 | * @return $this |
||
| 572 | */ |
||
| 573 | public function addItemClass(string $class) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Set an attribute on all items in the menu. |
||
| 584 | * |
||
| 585 | * @param string $attribute |
||
| 586 | * @param string $value |
||
| 587 | * |
||
| 588 | * @return $this |
||
| 589 | */ |
||
| 590 | public function setItemAttribute(string $attribute, string $value = '') |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Add a parent class to all items in the menu. |
||
| 601 | * |
||
| 602 | * @param string $class |
||
| 603 | * |
||
| 604 | * @return $this |
||
| 605 | */ |
||
| 606 | public function addItemParentClass(string $class) |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Add a parent attribute to all items in the menu. |
||
| 617 | * |
||
| 618 | * @param string $attribute |
||
| 619 | * @param string $value |
||
| 620 | * |
||
| 621 | * @return $this |
||
| 622 | */ |
||
| 623 | public function setItemParentAttribute(string $attribute, string $value = '') |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Create a empty blueprint of the menu (copies `filters` and `activeClass`). |
||
| 634 | * |
||
| 635 | * @return static |
||
| 636 | */ |
||
| 637 | public function blueprint() |
||
| 646 | |||
| 647 | /** |
||
| 648 | * Render the menu. |
||
| 649 | * |
||
| 650 | * @return string |
||
| 651 | */ |
||
| 652 | public function render(): string |
||
| 674 | |||
| 675 | /** |
||
| 676 | * The amount of items in the menu. |
||
| 677 | * |
||
| 678 | * @return int |
||
| 679 | */ |
||
| 680 | public function count(): int |
||
| 684 | |||
| 685 | /** |
||
| 686 | * @return string |
||
| 687 | */ |
||
| 688 | public function __toString(): string |
||
| 692 | } |
||
| 693 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.