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 |
||
| 15 | class Menu implements Item, Countable, HasHtmlAttributes, HasParentAttributes |
||
| 16 | { |
||
| 17 | use HasHtmlAttributesTrait, HasParentAttributesTrait, ConditionsTrait, HasAttributesTrait; |
||
| 18 | |||
| 19 | /** @var array */ |
||
| 20 | protected $items = []; |
||
| 21 | |||
| 22 | /** @var array */ |
||
| 23 | protected $filters = []; |
||
| 24 | |||
| 25 | /** @var string */ |
||
| 26 | protected $prepend, $append = ''; |
||
|
|
|||
| 27 | |||
| 28 | /** @var array */ |
||
| 29 | protected $wrap = []; |
||
| 30 | |||
| 31 | /** @var string */ |
||
| 32 | protected $activeClass = 'active'; |
||
| 33 | |||
| 34 | /** @var \Spatie\Menu\Html\Attributes */ |
||
| 35 | protected $htmlAttributes, $parentAttributes; |
||
| 36 | |||
| 37 | protected function __construct(Item ...$items) |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Create a new menu, optionally prefilled with items. |
||
| 47 | * |
||
| 48 | * @param array $items |
||
| 49 | * |
||
| 50 | * @return static |
||
| 51 | */ |
||
| 52 | public static function new($items = []) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Build a new menu from an array. The callback receives a menu instance as |
||
| 59 | * the accumulator, the array item as the second parameter, and the item's |
||
| 60 | * key as the third. |
||
| 61 | * |
||
| 62 | * @param array|\Iterator $items |
||
| 63 | * @param callable $callback |
||
| 64 | * @param \Spatie\Menu\Menu|null $initial |
||
| 65 | * |
||
| 66 | * @return static |
||
| 67 | */ |
||
| 68 | public static function build($items, callable $callback, Menu $initial = null) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Fill a menu from an array. The callback receives a menu instance as |
||
| 75 | * the accumulator, the array item as the second parameter, and the item's |
||
| 76 | * key as the third. |
||
| 77 | * |
||
| 78 | * @param array|\Iterator $items |
||
| 79 | * @param callable $callback |
||
| 80 | * |
||
| 81 | * @return static |
||
| 82 | */ |
||
| 83 | public function fill($items, callable $callback) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Add an item to the menu. This also applies all registered filters to the |
||
| 96 | * item. |
||
| 97 | * |
||
| 98 | * @param \Spatie\Menu\Item $item |
||
| 99 | * |
||
| 100 | * @return $this |
||
| 101 | */ |
||
| 102 | public function add(Item $item) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Add an item to the menu if a (non-strict) condition is met. |
||
| 115 | * |
||
| 116 | * @param bool $condition |
||
| 117 | * @param \Spatie\Menu\Item $item |
||
| 118 | * |
||
| 119 | * @return $this |
||
| 120 | */ |
||
| 121 | public function addIf($condition, Item $item) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Shortcut function to add a plain link to the menu. |
||
| 132 | * |
||
| 133 | * @param string $url |
||
| 134 | * @param string $text |
||
| 135 | * |
||
| 136 | * @return $this |
||
| 137 | */ |
||
| 138 | public function link(string $url, string $text) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Add a link to the menu if a (non-strict) condition is met. |
||
| 145 | * |
||
| 146 | * @param bool $condition |
||
| 147 | * @param string $url |
||
| 148 | * @param string $text |
||
| 149 | * |
||
| 150 | * @return $this |
||
| 151 | */ |
||
| 152 | public function linkIf($condition, string $url, string $text) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Shortcut function to add raw html to the menu. |
||
| 163 | * |
||
| 164 | * @param string $html |
||
| 165 | * @param array $parentAttributes |
||
| 166 | * |
||
| 167 | * @return $this |
||
| 168 | */ |
||
| 169 | public function html(string $html, array $parentAttributes = []) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Add a chunk of html if a (non-strict) condition is met. |
||
| 176 | * |
||
| 177 | * @param bool $condition |
||
| 178 | * @param string $html |
||
| 179 | * @param array $parentAttributes |
||
| 180 | * |
||
| 181 | * @return $this |
||
| 182 | */ |
||
| 183 | public function htmlIf($condition, string $html, array $parentAttributes = []) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
| 194 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
| 195 | * |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | public function submenu($header, $menu = null) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param bool $condition |
||
| 210 | * @param callable|\Spatie\Menu\Menu|\Spatie\Menu\Item $header |
||
| 211 | * @param callable|\Spatie\Menu\Menu|null $menu |
||
| 212 | * |
||
| 213 | * @return $this |
||
| 214 | */ |
||
| 215 | public function submenuIf($condition, $header, $menu = null) |
||
| 223 | |||
| 224 | protected function parseSubmenuArgs($args): array |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param \Spatie\Menu\Menu|callable $menu |
||
| 235 | * |
||
| 236 | * @return \Spatie\Menu\Menu |
||
| 237 | */ |
||
| 238 | protected function createSubmenuMenu($menu): Menu |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param \Spatie\Menu\Item|string $header |
||
| 251 | * |
||
| 252 | * @return string |
||
| 253 | */ |
||
| 254 | protected function createSubmenuHeader($header): string |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Iterate over all the items and apply a callback. If you typehint the |
||
| 265 | * item parameter in the callable, it wil only be applied to items of that |
||
| 266 | * type. |
||
| 267 | * |
||
| 268 | * @param callable $callable |
||
| 269 | * |
||
| 270 | * @return $this |
||
| 271 | */ |
||
| 272 | public function each(callable $callable) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Register a filter to the menu. When an item is added, all filters will be |
||
| 289 | * applied to the item. If you typehint the item parameter in the callable, it |
||
| 290 | * will only be applied to items of that type. |
||
| 291 | * |
||
| 292 | * @param callable $callable |
||
| 293 | * |
||
| 294 | * @return $this |
||
| 295 | */ |
||
| 296 | public function registerFilter(callable $callable) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Apply a filter to an item. Returns the result of the filter. |
||
| 305 | * |
||
| 306 | * @param callable $filter |
||
| 307 | * @param \Spatie\Menu\Item $item |
||
| 308 | */ |
||
| 309 | protected function applyFilter(callable $filter, Item $item) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Apply a callable to all existing items, and register it as a filter so it |
||
| 322 | * will get applied to all new items too. If you typehint the item parameter |
||
| 323 | * in the callable, it wil only be applied to items of that type. |
||
| 324 | * |
||
| 325 | * @param callable $callable |
||
| 326 | * |
||
| 327 | * @return $this |
||
| 328 | */ |
||
| 329 | public function applyToAll(callable $callable) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Wrap the menu in an html element. |
||
| 339 | * |
||
| 340 | * @param string $element |
||
| 341 | * @param array $attributes |
||
| 342 | * |
||
| 343 | * @return $this |
||
| 344 | */ |
||
| 345 | public function wrap(string $element, $attributes = []) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Determine whether the menu is active. |
||
| 354 | * |
||
| 355 | * @return bool |
||
| 356 | */ |
||
| 357 | public function isActive(): bool |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Set multiple items in the menu as active based on a callable that filters |
||
| 370 | * through items. If you typehint the item parameter in the callable, it will |
||
| 371 | * only be applied to items of that type. |
||
| 372 | * |
||
| 373 | * @param callable|string $urlOrCallable |
||
| 374 | * @param string $root |
||
| 375 | * |
||
| 376 | * @return $this |
||
| 377 | */ |
||
| 378 | public function setActive($urlOrCallable, string $root = '/') |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Set all relevant children active based on the current request's URL. |
||
| 393 | * |
||
| 394 | * /, /about, /contact => request to /about will set the about link active. |
||
| 395 | * |
||
| 396 | * /en, /en/about, /en/contact => request to /en won't set /en active if the |
||
| 397 | * request root is set to /en. |
||
| 398 | * |
||
| 399 | * @param string $url The current request url. |
||
| 400 | * @param string $root If the link's URL is an exact match with the request |
||
| 401 | * root, the link won't be set active. This behavior is |
||
| 402 | * to avoid having home links active on every request. |
||
| 403 | * |
||
| 404 | * @return $this |
||
| 405 | */ |
||
| 406 | public function setActiveFromUrl(string $url, string $root = '/') |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param callable $callable |
||
| 421 | * |
||
| 422 | * @return $this |
||
| 423 | */ |
||
| 424 | public function setActiveFromCallable(callable $callable) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Set the class name that will be used on active items for this menu. |
||
| 447 | * |
||
| 448 | * @param string $class |
||
| 449 | * |
||
| 450 | * @return $this |
||
| 451 | */ |
||
| 452 | public function setActiveClass(string $class) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Add a class to all items in the menu. |
||
| 461 | * |
||
| 462 | * @param string $class |
||
| 463 | * |
||
| 464 | * @return $this |
||
| 465 | */ |
||
| 466 | public function addItemClass(string $class) |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Set an attribute on all items in the menu. |
||
| 477 | * |
||
| 478 | * @param string $attribute |
||
| 479 | * @param string $value |
||
| 480 | * |
||
| 481 | * @return $this |
||
| 482 | */ |
||
| 483 | public function setItemAttribute(string $attribute, string $value = '') |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Add a parent class to all items in the menu. |
||
| 494 | * |
||
| 495 | * @param string $class |
||
| 496 | * |
||
| 497 | * @return $this |
||
| 498 | */ |
||
| 499 | public function addItemParentClass(string $class) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Add a parent attribute to all items in the menu. |
||
| 510 | * |
||
| 511 | * @param string $attribute |
||
| 512 | * @param string $value |
||
| 513 | * |
||
| 514 | * @return $this |
||
| 515 | */ |
||
| 516 | public function setItemParentAttribute(string $attribute, string $value = '') |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @param bool $condition |
||
| 527 | * @param callable $callable |
||
| 528 | * |
||
| 529 | * @return $this |
||
| 530 | */ |
||
| 531 | public function if(bool $condition, callable $callable) |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Create a empty blueprint of the menu (copies `filters` and `activeClass`). |
||
| 538 | * |
||
| 539 | * @return static |
||
| 540 | */ |
||
| 541 | public function blueprint() |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Render the menu. |
||
| 553 | * |
||
| 554 | * @return string |
||
| 555 | */ |
||
| 556 | public function render(): string |
||
| 570 | |||
| 571 | protected function renderItem(Item $item): string |
||
| 585 | |||
| 586 | /** |
||
| 587 | * The amount of items in the menu. |
||
| 588 | * |
||
| 589 | * @return int |
||
| 590 | */ |
||
| 591 | public function count(): int |
||
| 595 | |||
| 596 | /** |
||
| 597 | * @return string |
||
| 598 | */ |
||
| 599 | public function __toString(): string |
||
| 603 | } |
||
| 604 |
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.