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 HasHtmlAttributesTrait, HasParentAttributesTrait; |
||
| 16 | |||
| 17 | /** @var array */ |
||
| 18 | protected $items = []; |
||
| 19 | |||
| 20 | /** @var array */ |
||
| 21 | protected $filters = []; |
||
| 22 | |||
| 23 | /** @var string */ |
||
| 24 | protected $prepend, $append = ''; |
||
|
|
|||
| 25 | |||
| 26 | /** @var array */ |
||
| 27 | protected $wrap = []; |
||
| 28 | |||
| 29 | /** @var string */ |
||
| 30 | protected $activeClass = 'active'; |
||
| 31 | |||
| 32 | /** @var \Spatie\HtmlElement\Attributes */ |
||
| 33 | protected $htmlAttributes, $parentAttributes; |
||
| 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($items = []) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Build a new menu from an array. The callback receives a menu instance as |
||
| 57 | * the accumulator, the array item as the second parameter, and the item's |
||
| 58 | * key as the third. |
||
| 59 | * |
||
| 60 | * @param array|\Iterator $items |
||
| 61 | * @param callable $callback |
||
| 62 | * @param \Spatie\Menu\Menu|null $initial |
||
| 63 | * |
||
| 64 | * @return static |
||
| 65 | */ |
||
| 66 | public static function build($items, callable $callback, Menu $initial = null) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Fill a 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 | * |
||
| 79 | * @return static |
||
| 80 | */ |
||
| 81 | public function fill($items, callable $callback) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Add an item to the menu. This also applies all registered filters to the |
||
| 94 | * item. |
||
| 95 | * |
||
| 96 | * @param \Spatie\Menu\Item $item |
||
| 97 | * |
||
| 98 | * @return $this |
||
| 99 | */ |
||
| 100 | public function add(Item $item) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Add an item to the menu if a (non-strict) condition is met. |
||
| 113 | * |
||
| 114 | * @param bool $condition |
||
| 115 | * @param \Spatie\Menu\Item $item |
||
| 116 | * |
||
| 117 | * @return $this |
||
| 118 | */ |
||
| 119 | public function addIf($condition, Item $item) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Shortcut function to add a plain link to the menu. |
||
| 130 | * |
||
| 131 | * @param string $url |
||
| 132 | * @param string $text |
||
| 133 | * |
||
| 134 | * @return $this |
||
| 135 | */ |
||
| 136 | public function link(string $url, string $text) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Add a link to the menu if a (non-strict) condition is met. |
||
| 143 | * |
||
| 144 | * @param bool $condition |
||
| 145 | * @param string $url |
||
| 146 | * @param string $text |
||
| 147 | * |
||
| 148 | * @return $this |
||
| 149 | */ |
||
| 150 | public function linkIf($condition, string $url, string $text) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Shortcut function to add raw html to the menu. |
||
| 161 | * |
||
| 162 | * @param string $html |
||
| 163 | * @param array $parentAttributes |
||
| 164 | * |
||
| 165 | * @return $this |
||
| 166 | */ |
||
| 167 | public function html(string $html, array $parentAttributes = []) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Add a chunk of html if a (non-strict) condition is met. |
||
| 174 | * |
||
| 175 | * @param bool $condition |
||
| 176 | * @param string $html |
||
| 177 | * @param array $parentAttributes |
||
| 178 | * |
||
| 179 | * @return $this |
||
| 180 | */ |
||
| 181 | public function htmlIf($condition, string $html, array $parentAttributes = []) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
| 192 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
| 193 | * |
||
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | public function submenu($header, $menu = null) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param bool $condition |
||
| 208 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
| 209 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
| 210 | * |
||
| 211 | * @return $this |
||
| 212 | */ |
||
| 213 | public function submenuIf($condition, $header, $menu = null) |
||
| 221 | |||
| 222 | protected function parseSubmenuArgs($args): array |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @param \Spatie\Menu\Menu|callable $menu |
||
| 233 | * |
||
| 234 | * @return \Spatie\Menu\Menu |
||
| 235 | */ |
||
| 236 | protected function createSubmenuMenu($menu): Menu |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @param \Spatie\Menu\Item|string $header |
||
| 249 | * |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | protected function createSubmenuHeader($header): string |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Iterate over all the items and apply a callback. If you typehint the |
||
| 263 | * item parameter in the callable, it wil only be applied to items of that |
||
| 264 | * type. |
||
| 265 | * |
||
| 266 | * @param callable $callable |
||
| 267 | * |
||
| 268 | * @return $this |
||
| 269 | */ |
||
| 270 | public function each(callable $callable) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Register a filter to the menu. When an item is added, all filters will be |
||
| 287 | * applied to the item. If you typehint the item parameter in the callable, it |
||
| 288 | * will only be applied to items of that type. |
||
| 289 | * |
||
| 290 | * @param callable $callable |
||
| 291 | * |
||
| 292 | * @return $this |
||
| 293 | */ |
||
| 294 | public function registerFilter(callable $callable) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Apply a filter to an item. Returns the result of the filter. |
||
| 303 | * |
||
| 304 | * @param callable $filter |
||
| 305 | * @param \Spatie\Menu\Item $item |
||
| 306 | */ |
||
| 307 | protected function applyFilter(callable $filter, Item $item) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Apply a callable to all existing items, and register it as a filter so it |
||
| 320 | * will get applied to all new items too. If you typehint the item parameter |
||
| 321 | * in the callable, it wil only be applied to items of that type. |
||
| 322 | * |
||
| 323 | * @param callable $callable |
||
| 324 | * |
||
| 325 | * @return $this |
||
| 326 | */ |
||
| 327 | public function applyToAll(callable $callable) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Prepend the menu with a string of html on render. |
||
| 337 | * |
||
| 338 | * @param string $prepend |
||
| 339 | * |
||
| 340 | * @return $this |
||
| 341 | */ |
||
| 342 | public function prepend(string $prepend) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Prepend the menu with a string of html on render if a certain condition is |
||
| 351 | * met. |
||
| 352 | * |
||
| 353 | * @param bool $condition |
||
| 354 | * @param string $prepend |
||
| 355 | * |
||
| 356 | * @return $this |
||
| 357 | */ |
||
| 358 | public function prependIf(bool $condition, string $prepend) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Append a string of html to the menu on render. |
||
| 369 | * |
||
| 370 | * @param string $append |
||
| 371 | * |
||
| 372 | * @return $this |
||
| 373 | */ |
||
| 374 | public function append(string $append) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Append the menu with a string of html on render if a certain condition is |
||
| 383 | * met. |
||
| 384 | * |
||
| 385 | * @param bool $condition |
||
| 386 | * @param string $append |
||
| 387 | * |
||
| 388 | * @return static |
||
| 389 | */ |
||
| 390 | public function appendIf(bool $condition, string $append) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Wrap the menu in an html element. |
||
| 401 | * |
||
| 402 | * @param string $element |
||
| 403 | * @param array $attributes |
||
| 404 | * |
||
| 405 | * @return $this |
||
| 406 | */ |
||
| 407 | public function wrap(string $element, $attributes = []) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Determine whether the menu is active. |
||
| 416 | * |
||
| 417 | * @return bool |
||
| 418 | */ |
||
| 419 | public function isActive(): bool |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Set multiple items in the menu as active based on a callable that filters |
||
| 432 | * through items. If you typehint the item parameter in the callable, it will |
||
| 433 | * only be applied to items of that type. |
||
| 434 | * |
||
| 435 | * @param callable|string $urlOrCallable |
||
| 436 | * @param string $root |
||
| 437 | * |
||
| 438 | * @return $this |
||
| 439 | */ |
||
| 440 | public function setActive($urlOrCallable, string $root = '/') |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Set all relevant children active based on the current request's URL. |
||
| 455 | * |
||
| 456 | * /, /about, /contact => request to /about will set the about link active. |
||
| 457 | * |
||
| 458 | * /en, /en/about, /en/contact => request to /en won't set /en active if the |
||
| 459 | * request root is set to /en. |
||
| 460 | * |
||
| 461 | * @param string $url The current request url. |
||
| 462 | * @param string $root If the link's URL is an exact match with the request |
||
| 463 | * root, the link won't be set active. This behavior is |
||
| 464 | * to avoid having home links active on every request. |
||
| 465 | * |
||
| 466 | * @return $this |
||
| 467 | */ |
||
| 468 | public function setActiveFromUrl(string $url, string $root = '/') |
||
| 480 | |||
| 481 | /** |
||
| 482 | * @param callable $callable |
||
| 483 | * |
||
| 484 | * @return $this |
||
| 485 | */ |
||
| 486 | public function setActiveFromCallable(callable $callable) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Set the class name that will be used on active items for this menu. |
||
| 509 | * |
||
| 510 | * @param string $class |
||
| 511 | * |
||
| 512 | * @return $this |
||
| 513 | */ |
||
| 514 | public function setActiveClass(string $class) |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Add a class to all items in the menu. |
||
| 523 | * |
||
| 524 | * @param string $class |
||
| 525 | * |
||
| 526 | * @return $this |
||
| 527 | */ |
||
| 528 | public function addItemClass(string $class) |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Set an attribute on all items in the menu. |
||
| 539 | * |
||
| 540 | * @param string $attribute |
||
| 541 | * @param string $value |
||
| 542 | * |
||
| 543 | * @return $this |
||
| 544 | */ |
||
| 545 | public function setItemAttribute(string $attribute, string $value = '') |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Add a parent class to all items in the menu. |
||
| 556 | * |
||
| 557 | * @param string $class |
||
| 558 | * |
||
| 559 | * @return $this |
||
| 560 | */ |
||
| 561 | public function addItemParentClass(string $class) |
||
| 569 | |||
| 570 | /** |
||
| 571 | * Add a parent attribute to all items in the menu. |
||
| 572 | * |
||
| 573 | * @param string $attribute |
||
| 574 | * @param string $value |
||
| 575 | * |
||
| 576 | * @return $this |
||
| 577 | */ |
||
| 578 | public function setItemParentAttribute(string $attribute, string $value = '') |
||
| 586 | |||
| 587 | /** |
||
| 588 | * @param bool $condition |
||
| 589 | * @param callable $callable |
||
| 590 | * |
||
| 591 | * @return $this |
||
| 592 | */ |
||
| 593 | public function if(bool $condition, callable $callable) |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Create a empty blueprint of the menu (copies `filters` and `activeClass`). |
||
| 600 | * |
||
| 601 | * @return static |
||
| 602 | */ |
||
| 603 | public function blueprint() |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Render the menu. |
||
| 615 | * |
||
| 616 | * @return string |
||
| 617 | */ |
||
| 618 | public function render(): string |
||
| 640 | |||
| 641 | /** |
||
| 642 | * The amount of items in the menu. |
||
| 643 | * |
||
| 644 | * @return int |
||
| 645 | */ |
||
| 646 | public function count(): int |
||
| 650 | |||
| 651 | /** |
||
| 652 | * @return string |
||
| 653 | */ |
||
| 654 | public function __toString(): string |
||
| 658 | } |
||
| 659 |
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.