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 string */ |
||
| 30 | protected $activeClass = 'active'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param \Spatie\Menu\Item[] ...$items |
||
| 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(array $items = []) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Add an item to the menu. This also applies all registered filters to the |
||
| 57 | * item. |
||
| 58 | * |
||
| 59 | * @param \Spatie\Menu\Item $item |
||
| 60 | * |
||
| 61 | * @return $this |
||
| 62 | */ |
||
| 63 | public function add(Item $item) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Add an item to the menu if a (non-strict) condition is met. |
||
| 76 | * |
||
| 77 | * @param bool $condition |
||
| 78 | * @param \Spatie\Menu\Item $item |
||
| 79 | * |
||
| 80 | * @return $this |
||
| 81 | */ |
||
| 82 | public function addIf($condition, Item $item) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Shortcut function to add a plain link to the menu. |
||
| 93 | * |
||
| 94 | * @param string $url |
||
| 95 | * @param string $text |
||
| 96 | * |
||
| 97 | * @return $this |
||
| 98 | */ |
||
| 99 | public function link(string $url, string $text) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Add a link to the menu if a (non-strict) condition is met. |
||
| 106 | * |
||
| 107 | * @param bool $condition |
||
| 108 | * @param string $url |
||
| 109 | * @param string $text |
||
| 110 | * |
||
| 111 | * @return $this |
||
| 112 | */ |
||
| 113 | public function linkIf($condition, string $url, string $text) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Shortcut function to add raw html to the menu. |
||
| 124 | * |
||
| 125 | * @param string $html |
||
| 126 | * |
||
| 127 | * @return $this |
||
| 128 | */ |
||
| 129 | public function html(string $html) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Add a chunk of html if a (non-strict) condition is met. |
||
| 136 | * |
||
| 137 | * @param bool $condition |
||
| 138 | * @param string $html |
||
| 139 | * |
||
| 140 | * @return $this |
||
| 141 | */ |
||
| 142 | public function htmlIf($condition, string $html) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
| 153 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
| 154 | * |
||
| 155 | * @return $this |
||
| 156 | */ |
||
| 157 | public function submenu($header, $menu = null) |
||
| 173 | |||
| 174 | protected function parseSubmenuArgs($args): array |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Iterate over all the items and apply a callback. If you typehint the |
||
| 185 | * item parameter in the callable, it wil only be applied to items of that |
||
| 186 | * type. |
||
| 187 | * |
||
| 188 | * @param callable $callable |
||
| 189 | * |
||
| 190 | * @return $this |
||
| 191 | */ |
||
| 192 | public function each(callable $callable) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Register a filter to the menu. When an item is added, all filters will be |
||
| 209 | * applied to the item. If you typehint the item parameter in the callable, it |
||
| 210 | * will only be applied to items of that type. |
||
| 211 | * |
||
| 212 | * @param callable $callable |
||
| 213 | * |
||
| 214 | * @return $this |
||
| 215 | */ |
||
| 216 | public function registerFilter(callable $callable) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Apply a filter to an item. Returns the result of the filter. |
||
| 225 | * |
||
| 226 | * @param callable $filter |
||
| 227 | * @param \Spatie\Menu\Item $item |
||
| 228 | */ |
||
| 229 | protected function applyFilter(callable $filter, Item $item) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Apply a callable to all existing items, and register it as a filter so it |
||
| 242 | * will get applied to all new items too. If you typehint the item parameter |
||
| 243 | * in the callable, it wil only be applied to items of that type. |
||
| 244 | * |
||
| 245 | * @param callable $callable |
||
| 246 | * |
||
| 247 | * @return $this |
||
| 248 | */ |
||
| 249 | public function applyToAll(callable $callable) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Prefix all the links in the menu. |
||
| 259 | * |
||
| 260 | * @param string $prefix |
||
| 261 | * |
||
| 262 | * @return $this |
||
| 263 | */ |
||
| 264 | public function prefixLinks(string $prefix) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Prepend the menu with a string of html on render. |
||
| 273 | * |
||
| 274 | * @param string $prepend |
||
| 275 | * |
||
| 276 | * @return $this |
||
| 277 | */ |
||
| 278 | public function prepend(string $prepend) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Prepend the menu with a string of html on render if a certain condition is |
||
| 287 | * met. |
||
| 288 | * |
||
| 289 | * @param bool $condition |
||
| 290 | * @param string $prepend |
||
| 291 | * |
||
| 292 | * @return $this |
||
| 293 | */ |
||
| 294 | public function prependIf(bool $condition, string $prepend) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Append a string of html to the menu on render. |
||
| 305 | * |
||
| 306 | * @param string $append |
||
| 307 | * |
||
| 308 | * @return $this |
||
| 309 | */ |
||
| 310 | public function append(string $append) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Append the menu with a string of html on render if a certain condition is |
||
| 319 | * met. |
||
| 320 | * |
||
| 321 | * @param bool $condition |
||
| 322 | * @param string $append |
||
| 323 | * |
||
| 324 | * @return static |
||
| 325 | */ |
||
| 326 | public function appendIf(bool $condition, string $append) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Determine whether the menu is active. |
||
| 337 | * |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | public function isActive(): bool |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Set multiple items in the menu as active based on a callable that filters |
||
| 353 | * through items. If you typehint the item parameter in the callable, it will |
||
| 354 | * only be applied to items of that type. |
||
| 355 | * |
||
| 356 | * @param callable|string $urlOrCallable |
||
| 357 | * @param string $root |
||
| 358 | * |
||
| 359 | * @return $this |
||
| 360 | */ |
||
| 361 | public function setActive($urlOrCallable, string $root = '/') |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Set all relevant children active based on the current request's URL. |
||
| 376 | * |
||
| 377 | * /, /about, /contact => request to /about will set the about link active. |
||
| 378 | * |
||
| 379 | * /en, /en/about, /en/contact => request to /en won't set /en active if the |
||
| 380 | * request root is set to /en. |
||
| 381 | * |
||
| 382 | * @param string $url The current request url. |
||
| 383 | * @param string $root If the link's URL is an exact match with the request |
||
| 384 | * root, the link won't be set active. This behavior is |
||
| 385 | * to avoid having home links active on every request. |
||
| 386 | * |
||
| 387 | * @return $this |
||
| 388 | */ |
||
| 389 | public function setActiveFromUrl(string $url, string $root = '/') |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @param callable $callable |
||
| 441 | * |
||
| 442 | * @return $this |
||
| 443 | */ |
||
| 444 | public function setActiveFromCallable(callable $callable) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Set the class name that will be used on active items for this menu. |
||
| 468 | * |
||
| 469 | * @param string $class |
||
| 470 | * |
||
| 471 | * @return $this |
||
| 472 | */ |
||
| 473 | public function setActiveClass(string $class) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Add a class to all items in the menu. |
||
| 482 | * |
||
| 483 | * @param string $class |
||
| 484 | * |
||
| 485 | * @return $this |
||
| 486 | */ |
||
| 487 | public function addItemClass(string $class) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Set an attribute on all items in the menu. |
||
| 498 | * |
||
| 499 | * @param string $attribute |
||
| 500 | * @param string $value |
||
| 501 | * |
||
| 502 | * @return $this |
||
| 503 | */ |
||
| 504 | public function setItemAttribute(string $attribute, string $value = '') |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Add a parent class to all items in the menu. |
||
| 515 | * |
||
| 516 | * @param string $class |
||
| 517 | * |
||
| 518 | * @return $this |
||
| 519 | */ |
||
| 520 | public function addItemParentClass(string $class) |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Add a parent attribute to all items in the menu. |
||
| 531 | * |
||
| 532 | * @param string $attribute |
||
| 533 | * @param string $value |
||
| 534 | * |
||
| 535 | * @return $this |
||
| 536 | */ |
||
| 537 | public function setItemParentAttribute(string $attribute, string $value = '') |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Create a empty blueprint of the menu (copies `filters` and `activeClass`). |
||
| 548 | * |
||
| 549 | * @return static |
||
| 550 | */ |
||
| 551 | public function blueprint() |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Render the menu. |
||
| 563 | * |
||
| 564 | * @return string |
||
| 565 | */ |
||
| 566 | public function render(): string |
||
| 582 | |||
| 583 | /** |
||
| 584 | * The amount of items in the menu. |
||
| 585 | * |
||
| 586 | * @return int |
||
| 587 | */ |
||
| 588 | public function count(): int |
||
| 592 | |||
| 593 | /** |
||
| 594 | * @return string |
||
| 595 | */ |
||
| 596 | public function __toString(): string |
||
| 600 | } |
||
| 601 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.