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 |
||
| 14 | class Menu implements Item, Countable, HasHtmlAttributes, HasParentAttributes |
||
| 15 | { |
||
| 16 | use HasHtmlAttributesTrait, HasParentAttributesTrait; |
||
| 17 | |||
| 18 | /** @var array */ |
||
| 19 | protected $items = []; |
||
| 20 | |||
| 21 | /** @var array */ |
||
| 22 | protected $filters = []; |
||
| 23 | |||
| 24 | /** @var string */ |
||
| 25 | protected $prepend, $append = ''; |
||
|
|
|||
| 26 | |||
| 27 | /** @var array */ |
||
| 28 | protected $wrap = []; |
||
| 29 | |||
| 30 | /** @var string */ |
||
| 31 | protected $activeClass = 'active'; |
||
| 32 | |||
| 33 | /** @var \Spatie\HtmlElement\Attributes */ |
||
| 34 | protected $htmlAttributes, $parentAttributes; |
||
| 35 | |||
| 36 | protected function __construct(Item ...$items) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Create a new menu, optionally prefilled with items. |
||
| 46 | * |
||
| 47 | * @param array $items |
||
| 48 | * |
||
| 49 | * @return static |
||
| 50 | */ |
||
| 51 | public static function new($items = []) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Build a new menu from an array. The callback receives a menu instance as |
||
| 58 | * the accumulator, the array item as the second parameter, and the item's |
||
| 59 | * key as the third. |
||
| 60 | * |
||
| 61 | * @param array|\Iterator $items |
||
| 62 | * @param callable $callback |
||
| 63 | * @param \Spatie\Menu\Menu|null $initial |
||
| 64 | * |
||
| 65 | * @return static |
||
| 66 | */ |
||
| 67 | public static function build($items, callable $callback, Menu $initial = null) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Fill a menu from an array. The callback receives a menu instance as |
||
| 74 | * the accumulator, the array item as the second parameter, and the item's |
||
| 75 | * key as the third. |
||
| 76 | * |
||
| 77 | * @param array|\Iterator $items |
||
| 78 | * @param callable $callback |
||
| 79 | * |
||
| 80 | * @return static |
||
| 81 | */ |
||
| 82 | public function fill($items, callable $callback) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Add an item to the menu. This also applies all registered filters to the |
||
| 95 | * item. |
||
| 96 | * |
||
| 97 | * @param \Spatie\Menu\Item $item |
||
| 98 | * |
||
| 99 | * @return $this |
||
| 100 | */ |
||
| 101 | public function add(Item $item) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Add an item to the menu if a (non-strict) condition is met. |
||
| 114 | * |
||
| 115 | * @param bool $condition |
||
| 116 | * @param \Spatie\Menu\Item $item |
||
| 117 | * |
||
| 118 | * @return $this |
||
| 119 | */ |
||
| 120 | public function addIf($condition, Item $item) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Shortcut function to add a plain link to the menu. |
||
| 131 | * |
||
| 132 | * @param string $url |
||
| 133 | * @param string $text |
||
| 134 | * |
||
| 135 | * @return $this |
||
| 136 | */ |
||
| 137 | public function link(string $url, string $text) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Add a link to the menu if a (non-strict) condition is met. |
||
| 144 | * |
||
| 145 | * @param bool $condition |
||
| 146 | * @param string $url |
||
| 147 | * @param string $text |
||
| 148 | * |
||
| 149 | * @return $this |
||
| 150 | */ |
||
| 151 | public function linkIf($condition, string $url, string $text) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Shortcut function to add raw html to the menu. |
||
| 162 | * |
||
| 163 | * @param string $html |
||
| 164 | * @param array $parentAttributes |
||
| 165 | * |
||
| 166 | * @return $this |
||
| 167 | */ |
||
| 168 | public function html(string $html, array $parentAttributes = []) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Add a chunk of html if a (non-strict) condition is met. |
||
| 175 | * |
||
| 176 | * @param bool $condition |
||
| 177 | * @param string $html |
||
| 178 | * @param array $parentAttributes |
||
| 179 | * |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | public function htmlIf($condition, string $html, array $parentAttributes = []) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param $conditional |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | protected function resolveCondition($conditional) |
||
| 199 | |||
| 200 | /** |
||
| 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 submenu($header, $menu = null) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @param bool $condition |
||
| 218 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
| 219 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
| 220 | * |
||
| 221 | * @return $this |
||
| 222 | */ |
||
| 223 | public function submenuIf($condition, $header, $menu = null) |
||
| 231 | |||
| 232 | protected function parseSubmenuArgs($args): array |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param \Spatie\Menu\Menu|callable $menu |
||
| 243 | * |
||
| 244 | * @return \Spatie\Menu\Menu |
||
| 245 | */ |
||
| 246 | protected function createSubmenuMenu($menu): Menu |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param \Spatie\Menu\Item|string $header |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | protected function createSubmenuHeader($header): string |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Iterate over all the items and apply a callback. If you typehint the |
||
| 273 | * item parameter in the callable, it wil only be applied to items of that |
||
| 274 | * type. |
||
| 275 | * |
||
| 276 | * @param callable $callable |
||
| 277 | * |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function each(callable $callable) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Register a filter to the menu. When an item is added, all filters will be |
||
| 297 | * applied to the item. If you typehint the item parameter in the callable, it |
||
| 298 | * will only be applied to items of that type. |
||
| 299 | * |
||
| 300 | * @param callable $callable |
||
| 301 | * |
||
| 302 | * @return $this |
||
| 303 | */ |
||
| 304 | public function registerFilter(callable $callable) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Apply a filter to an item. Returns the result of the filter. |
||
| 313 | * |
||
| 314 | * @param callable $filter |
||
| 315 | * @param \Spatie\Menu\Item $item |
||
| 316 | */ |
||
| 317 | protected function applyFilter(callable $filter, Item $item) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Apply a callable to all existing items, and register it as a filter so it |
||
| 330 | * will get applied to all new items too. If you typehint the item parameter |
||
| 331 | * in the callable, it wil only be applied to items of that type. |
||
| 332 | * |
||
| 333 | * @param callable $callable |
||
| 334 | * |
||
| 335 | * @return $this |
||
| 336 | */ |
||
| 337 | public function applyToAll(callable $callable) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Prepend the menu with a string of html on render. |
||
| 347 | * |
||
| 348 | * @param string $prepend |
||
| 349 | * |
||
| 350 | * @return $this |
||
| 351 | */ |
||
| 352 | public function prepend(string $prepend) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Prepend the menu with a string of html on render if a certain condition is |
||
| 361 | * met. |
||
| 362 | * |
||
| 363 | * @param bool $condition |
||
| 364 | * @param string $prepend |
||
| 365 | * |
||
| 366 | * @return $this |
||
| 367 | */ |
||
| 368 | public function prependIf($condition, string $prepend) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Append a string of html to the menu on render. |
||
| 379 | * |
||
| 380 | * @param string $append |
||
| 381 | * |
||
| 382 | * @return $this |
||
| 383 | */ |
||
| 384 | public function append(string $append) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Append the menu with a string of html on render if a certain condition is |
||
| 393 | * met. |
||
| 394 | * |
||
| 395 | * @param bool $condition |
||
| 396 | * @param string $append |
||
| 397 | * |
||
| 398 | * @return static |
||
| 399 | */ |
||
| 400 | public function appendIf($condition, string $append) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Wrap the menu in an html element. |
||
| 411 | * |
||
| 412 | * @param string $element |
||
| 413 | * @param array $attributes |
||
| 414 | * |
||
| 415 | * @return $this |
||
| 416 | */ |
||
| 417 | public function wrap(string $element, $attributes = []) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Determine whether the menu is active. |
||
| 426 | * |
||
| 427 | * @return bool |
||
| 428 | */ |
||
| 429 | public function isActive(): bool |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Set multiple items in the menu as active based on a callable that filters |
||
| 442 | * through items. If you typehint the item parameter in the callable, it will |
||
| 443 | * only be applied to items of that type. |
||
| 444 | * |
||
| 445 | * @param callable|string $urlOrCallable |
||
| 446 | * @param string $root |
||
| 447 | * |
||
| 448 | * @return $this |
||
| 449 | */ |
||
| 450 | public function setActive($urlOrCallable, string $root = '/') |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Set all relevant children active based on the current request's URL. |
||
| 465 | * |
||
| 466 | * /, /about, /contact => request to /about will set the about link active. |
||
| 467 | * |
||
| 468 | * /en, /en/about, /en/contact => request to /en won't set /en active if the |
||
| 469 | * request root is set to /en. |
||
| 470 | * |
||
| 471 | * @param string $url The current request url. |
||
| 472 | * @param string $root If the link's URL is an exact match with the request |
||
| 473 | * root, the link won't be set active. This behavior is |
||
| 474 | * to avoid having home links active on every request. |
||
| 475 | * |
||
| 476 | * @return $this |
||
| 477 | */ |
||
| 478 | public function setActiveFromUrl(string $url, string $root = '/') |
||
| 490 | |||
| 491 | /** |
||
| 492 | * @param callable $callable |
||
| 493 | * |
||
| 494 | * @return $this |
||
| 495 | */ |
||
| 496 | public function setActiveFromCallable(callable $callable) |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Set the class name that will be used on active items for this menu. |
||
| 519 | * |
||
| 520 | * @param string $class |
||
| 521 | * |
||
| 522 | * @return $this |
||
| 523 | */ |
||
| 524 | public function setActiveClass(string $class) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Add a class to all items in the menu. |
||
| 533 | * |
||
| 534 | * @param string $class |
||
| 535 | * |
||
| 536 | * @return $this |
||
| 537 | */ |
||
| 538 | public function addItemClass(string $class) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Set an attribute on all items in the menu. |
||
| 549 | * |
||
| 550 | * @param string $attribute |
||
| 551 | * @param string $value |
||
| 552 | * |
||
| 553 | * @return $this |
||
| 554 | */ |
||
| 555 | public function setItemAttribute(string $attribute, string $value = '') |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Add a parent class to all items in the menu. |
||
| 566 | * |
||
| 567 | * @param string $class |
||
| 568 | * |
||
| 569 | * @return $this |
||
| 570 | */ |
||
| 571 | public function addItemParentClass(string $class) |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Add a parent attribute to all items in the menu. |
||
| 582 | * |
||
| 583 | * @param string $attribute |
||
| 584 | * @param string $value |
||
| 585 | * |
||
| 586 | * @return $this |
||
| 587 | */ |
||
| 588 | public function setItemParentAttribute(string $attribute, string $value = '') |
||
| 596 | |||
| 597 | /** |
||
| 598 | * @param bool $condition |
||
| 599 | * @param callable $callable |
||
| 600 | * |
||
| 601 | * @return $this |
||
| 602 | */ |
||
| 603 | public function if(bool $condition, callable $callable) |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Create a empty blueprint of the menu (copies `filters` and `activeClass`). |
||
| 610 | * |
||
| 611 | * @return static |
||
| 612 | */ |
||
| 613 | public function blueprint() |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Render the menu. |
||
| 625 | * |
||
| 626 | * @return string |
||
| 627 | */ |
||
| 628 | public function render(): string |
||
| 650 | |||
| 651 | /** |
||
| 652 | * The amount of items in the menu. |
||
| 653 | * |
||
| 654 | * @return int |
||
| 655 | */ |
||
| 656 | public function count(): int |
||
| 660 | |||
| 661 | /** |
||
| 662 | * @return string |
||
| 663 | */ |
||
| 664 | public function __toString(): string |
||
| 668 | } |
||
| 669 |
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.