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, ConditionsTrait, HasAttributesTrait; |
||
| 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\Menu\Html\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) |
||
| 83 | { |
||
| 84 | $menu = $this; |
||
| 85 | |||
| 86 | foreach ($items as $key => $item) { |
||
| 87 | $menu = $callback($menu, $item, $key) ?: $menu; |
||
| 88 | } |
||
| 89 | |||
| 90 | return $menu; |
||
| 91 | } |
||
| 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 callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
| 193 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
| 194 | * |
||
| 195 | * @return $this |
||
| 196 | */ |
||
| 197 | public function submenu($header, $menu = null) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param bool $condition |
||
| 209 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
| 210 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
| 211 | * |
||
| 212 | * @return $this |
||
| 213 | */ |
||
| 214 | public function submenuIf($condition, $header, $menu = null) |
||
| 222 | |||
| 223 | protected function parseSubmenuArgs($args): array |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param \Spatie\Menu\Menu|callable $menu |
||
| 234 | * |
||
| 235 | * @return \Spatie\Menu\Menu |
||
| 236 | */ |
||
| 237 | protected function createSubmenuMenu($menu): Menu |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param \Spatie\Menu\Item|string $header |
||
| 250 | * |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | protected function createSubmenuHeader($header): string |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Iterate over all the items and apply a callback. If you typehint the |
||
| 264 | * item parameter in the callable, it wil only be applied to items of that |
||
| 265 | * type. |
||
| 266 | * |
||
| 267 | * @param callable $callable |
||
| 268 | * |
||
| 269 | * @return $this |
||
| 270 | */ |
||
| 271 | public function each(callable $callable) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Register a filter to the menu. When an item is added, all filters will be |
||
| 288 | * applied to the item. If you typehint the item parameter in the callable, it |
||
| 289 | * will only be applied to items of that type. |
||
| 290 | * |
||
| 291 | * @param callable $callable |
||
| 292 | * |
||
| 293 | * @return $this |
||
| 294 | */ |
||
| 295 | public function registerFilter(callable $callable) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Apply a filter to an item. Returns the result of the filter. |
||
| 304 | * |
||
| 305 | * @param callable $filter |
||
| 306 | * @param \Spatie\Menu\Item $item |
||
| 307 | */ |
||
| 308 | protected function applyFilter(callable $filter, Item $item) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Apply a callable to all existing items, and register it as a filter so it |
||
| 321 | * will get applied to all new items too. If you typehint the item parameter |
||
| 322 | * in the callable, it wil only be applied to items of that type. |
||
| 323 | * |
||
| 324 | * @param callable $callable |
||
| 325 | * |
||
| 326 | * @return $this |
||
| 327 | */ |
||
| 328 | public function applyToAll(callable $callable) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Wrap the menu in an html element. |
||
| 338 | * |
||
| 339 | * @param string $element |
||
| 340 | * @param array $attributes |
||
| 341 | * |
||
| 342 | * @return $this |
||
| 343 | */ |
||
| 344 | public function wrap(string $element, $attributes = []) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Determine whether the menu is active. |
||
| 353 | * |
||
| 354 | * @return bool |
||
| 355 | */ |
||
| 356 | public function isActive(): bool |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Set multiple items in the menu as active based on a callable that filters |
||
| 369 | * through items. If you typehint the item parameter in the callable, it will |
||
| 370 | * only be applied to items of that type. |
||
| 371 | * |
||
| 372 | * @param callable|string $urlOrCallable |
||
| 373 | * @param string $root |
||
| 374 | * |
||
| 375 | * @return $this |
||
| 376 | */ |
||
| 377 | public function setActive($urlOrCallable, string $root = '/') |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Set all relevant children active based on the current request's URL. |
||
| 392 | * |
||
| 393 | * /, /about, /contact => request to /about will set the about link active. |
||
| 394 | * |
||
| 395 | * /en, /en/about, /en/contact => request to /en won't set /en active if the |
||
| 396 | * request root is set to /en. |
||
| 397 | * |
||
| 398 | * @param string $url The current request url. |
||
| 399 | * @param string $root If the link's URL is an exact match with the request |
||
| 400 | * root, the link won't be set active. This behavior is |
||
| 401 | * to avoid having home links active on every request. |
||
| 402 | * |
||
| 403 | * @return $this |
||
| 404 | */ |
||
| 405 | public function setActiveFromUrl(string $url, string $root = '/') |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @param callable $callable |
||
| 420 | * |
||
| 421 | * @return $this |
||
| 422 | */ |
||
| 423 | public function setActiveFromCallable(callable $callable) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Set the class name that will be used on active items for this menu. |
||
| 446 | * |
||
| 447 | * @param string $class |
||
| 448 | * |
||
| 449 | * @return $this |
||
| 450 | */ |
||
| 451 | public function setActiveClass(string $class) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Add a class to all items in the menu. |
||
| 460 | * |
||
| 461 | * @param string $class |
||
| 462 | * |
||
| 463 | * @return $this |
||
| 464 | */ |
||
| 465 | public function addItemClass(string $class) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Set an attribute on all items in the menu. |
||
| 476 | * |
||
| 477 | * @param string $attribute |
||
| 478 | * @param string $value |
||
| 479 | * |
||
| 480 | * @return $this |
||
| 481 | */ |
||
| 482 | public function setItemAttribute(string $attribute, string $value = '') |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Add a parent class to all items in the menu. |
||
| 493 | * |
||
| 494 | * @param string $class |
||
| 495 | * |
||
| 496 | * @return $this |
||
| 497 | */ |
||
| 498 | public function addItemParentClass(string $class) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Add a parent attribute to all items in the menu. |
||
| 509 | * |
||
| 510 | * @param string $attribute |
||
| 511 | * @param string $value |
||
| 512 | * |
||
| 513 | * @return $this |
||
| 514 | */ |
||
| 515 | public function setItemParentAttribute(string $attribute, string $value = '') |
||
| 523 | |||
| 524 | /** |
||
| 525 | * @param bool $condition |
||
| 526 | * @param callable $callable |
||
| 527 | * |
||
| 528 | * @return $this |
||
| 529 | */ |
||
| 530 | public function if(bool $condition, callable $callable) |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Create a empty blueprint of the menu (copies `filters` and `activeClass`). |
||
| 537 | * |
||
| 538 | * @return static |
||
| 539 | */ |
||
| 540 | public function blueprint() |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Render the menu. |
||
| 552 | * |
||
| 553 | * @return string |
||
| 554 | */ |
||
| 555 | public function render(): string |
||
| 569 | |||
| 570 | protected function renderItem(Item $item): string |
||
| 584 | |||
| 585 | /** |
||
| 586 | * The amount of items in the menu. |
||
| 587 | * |
||
| 588 | * @return int |
||
| 589 | */ |
||
| 590 | public function count(): int |
||
| 594 | |||
| 595 | /** |
||
| 596 | * @return string |
||
| 597 | */ |
||
| 598 | public function __toString(): string |
||
| 602 | } |
||
| 603 |
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.