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 |
||
| 10 | class Menu implements Item, Countable |
||
| 11 | { |
||
| 12 | use HtmlAttributes, ParentAttributes; |
||
| 13 | |||
| 14 | /** @var array */ |
||
| 15 | protected $items = []; |
||
| 16 | |||
| 17 | /** @var string */ |
||
| 18 | protected $prepend = ''; |
||
| 19 | |||
| 20 | /** @var string */ |
||
| 21 | protected $append = ''; |
||
| 22 | |||
| 23 | /** @var array */ |
||
| 24 | protected $filters = []; |
||
| 25 | |||
| 26 | /** @var string */ |
||
| 27 | protected $activeClass = 'active'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @param \Spatie\Menu\Item[] ...$items |
||
| 31 | */ |
||
| 32 | protected function __construct(Item ...$items) |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Create a new menu, optionally prefilled with items. |
||
| 42 | * |
||
| 43 | * @param array $items |
||
| 44 | * |
||
| 45 | * @return static |
||
| 46 | */ |
||
| 47 | public static function new(array $items = []) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Add an item to the menu. This also applies all registered filters to the |
||
| 54 | * item. |
||
| 55 | * |
||
| 56 | * @param \Spatie\Menu\Item $item |
||
| 57 | * |
||
| 58 | * @return $this |
||
| 59 | */ |
||
| 60 | public function add(Item $item) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Add an item to the menu if a (non-strict) condition is met. |
||
| 73 | * |
||
| 74 | * @param bool $condition |
||
| 75 | * @param \Spatie\Menu\Item $item |
||
| 76 | * |
||
| 77 | * @return $this |
||
| 78 | */ |
||
| 79 | public function addIf($condition, Item $item) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Shortcut function to add a plain link to the menu. |
||
| 90 | * |
||
| 91 | * @param string $url |
||
| 92 | * @param string $text |
||
| 93 | * |
||
| 94 | * @return $this |
||
| 95 | */ |
||
| 96 | public function link(string $url, string $text) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Add a link to the menu if a (non-strict) condition is met. |
||
| 103 | * |
||
| 104 | * @param bool $condition |
||
| 105 | * @param string $url |
||
| 106 | * @param string $text |
||
| 107 | * |
||
| 108 | * @return $this |
||
| 109 | */ |
||
| 110 | public function linkIf($condition, string $url, string $text) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Shortcut function to add raw html to the menu. |
||
| 121 | * |
||
| 122 | * @param string $html |
||
| 123 | * |
||
| 124 | * @return $this |
||
| 125 | */ |
||
| 126 | public function html(string $html) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Add a chunk of html if a (non-strict) condition is met. |
||
| 133 | * |
||
| 134 | * @param bool $condition |
||
| 135 | * @param string $html |
||
| 136 | * |
||
| 137 | * @return $this |
||
| 138 | */ |
||
| 139 | public function htmlIf($condition, string $html) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Iterate over all the items and apply a callback. If you typehint the |
||
| 150 | * item parameter in the callable, it wil only be applied to items of that |
||
| 151 | * type. |
||
| 152 | * |
||
| 153 | * @param callable $callable |
||
| 154 | * |
||
| 155 | * @return $this |
||
| 156 | */ |
||
| 157 | public function each(callable $callable) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Register a filter to the menu. When an item is added, all filters will be |
||
| 174 | * applied to the item. If you typehint the item parameter in the callable, it |
||
| 175 | * will only be applied to items of that type. |
||
| 176 | * |
||
| 177 | * @param callable $callable |
||
| 178 | * |
||
| 179 | * @return $this |
||
| 180 | */ |
||
| 181 | public function registerFilter(callable $callable) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Apply a filter to an item. Returns the result of the filter. |
||
| 190 | * |
||
| 191 | * @param callable $filter |
||
| 192 | * @param \Spatie\Menu\Item $item |
||
| 193 | */ |
||
| 194 | protected function applyFilter(callable $filter, Item $item) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Apply a callable to all existing items, and register it as a filter so it |
||
| 207 | * will get applied to all new items too. If you typehint the item parameter |
||
| 208 | * in the callable, it wil only be applied to items of that type. |
||
| 209 | * |
||
| 210 | * @param callable $callable |
||
| 211 | * |
||
| 212 | * @return $this |
||
| 213 | */ |
||
| 214 | public function applyToAll(callable $callable) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Prefix all the links in the menu. |
||
| 224 | * |
||
| 225 | * @param string $prefix |
||
| 226 | * |
||
| 227 | * @return $this |
||
| 228 | */ |
||
| 229 | public function prefixLinks(string $prefix) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Prepend the menu with a string of html on render. |
||
| 238 | * |
||
| 239 | * @param string $prepend |
||
| 240 | * |
||
| 241 | * @return $this |
||
| 242 | */ |
||
| 243 | public function prepend(string $prepend) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Prepend the menu with a string of html on render if a certain condition is |
||
| 252 | * met. |
||
| 253 | * |
||
| 254 | * @param bool $condition |
||
| 255 | * @param string $prepend |
||
| 256 | * |
||
| 257 | * @return $this |
||
| 258 | */ |
||
| 259 | public function prependIf(bool $condition, string $prepend) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Append a string of html to the menu on render. |
||
| 270 | * |
||
| 271 | * @param string $append |
||
| 272 | * |
||
| 273 | * @return $this |
||
| 274 | */ |
||
| 275 | public function append(string $append) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Append the menu with a string of html on render if a certain condition is |
||
| 284 | * met. |
||
| 285 | * |
||
| 286 | * @param bool $condition |
||
| 287 | * @param string $append |
||
| 288 | * |
||
| 289 | * @return static |
||
| 290 | */ |
||
| 291 | public function appendIf(bool $condition, string $append) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Determine whether the menu is active. |
||
| 302 | * |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | public function isActive() : bool |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Set multiple items in the menu as active based on a callable that filters |
||
| 318 | * through items. If you typehint the item parameter in the callable, it will |
||
| 319 | * only be applied to items of that type. |
||
| 320 | * |
||
| 321 | * @param callable|string $urlOrCallable |
||
| 322 | * @param string $root |
||
| 323 | * |
||
| 324 | * @return $this |
||
| 325 | */ |
||
| 326 | public function setActive($urlOrCallable, string $root = '/') |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Set all relevant children active based on the current request's URL. |
||
| 341 | * |
||
| 342 | * /, /about, /contact => request to /about will set the about link active. |
||
| 343 | * |
||
| 344 | * /en, /en/about, /en/contact => request to /en won't set /en active if the |
||
| 345 | * request root is set to /en. |
||
| 346 | * |
||
| 347 | * @param string $url The current request url. |
||
| 348 | * @param string $root If the link's URL is an exact match with the request |
||
| 349 | * root, the link won't be set active. This behavior is |
||
| 350 | * to avoid having home links active on every request. |
||
| 351 | * |
||
| 352 | * @return $this |
||
| 353 | */ |
||
| 354 | public function setActiveFromUrl(string $url, string $root = '/') |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param callable $callable |
||
| 406 | * |
||
| 407 | * @return $this |
||
| 408 | */ |
||
| 409 | public function setActiveFromCallable(callable $callable) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Set the class name that will be used on active items for this menu. |
||
| 433 | * |
||
| 434 | * @param string $class |
||
| 435 | * |
||
| 436 | * @return $this |
||
| 437 | */ |
||
| 438 | public function setActiveClass(string $class) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Render the menu. |
||
| 447 | * |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | public function render() : string |
||
| 466 | |||
| 467 | /** |
||
| 468 | * The amount of items in the menu. |
||
| 469 | * |
||
| 470 | * @return int |
||
| 471 | */ |
||
| 472 | public function count() : int |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | public function __toString() : string |
||
| 484 | } |
||
| 485 |