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 Countable, Item |
||
| 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 on the item. If a filter |
||
| 54 | * returns false, the item won't be added. |
||
| 55 | * |
||
| 56 | * @param \Spatie\Menu\Item $item |
||
| 57 | * |
||
| 58 | * @return $this |
||
| 59 | */ |
||
| 60 | public function add(Item $item) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Shortcut function to add a plain link to the menu. |
||
| 73 | * |
||
| 74 | * @param string $url |
||
| 75 | * @param string $text |
||
| 76 | * |
||
| 77 | * @return \Spatie\Menu\Menu |
||
| 78 | */ |
||
| 79 | public function link(string $url, string $text) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Apply a filter to an item. Returns the result of the filter. |
||
| 86 | * |
||
| 87 | * @param callable $filter |
||
| 88 | * @param \Spatie\Menu\Item $item |
||
| 89 | */ |
||
| 90 | protected function applyFilter(callable $filter, Item $item) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Iterate over all the items and apply a callback. If you typehint the |
||
| 103 | * item parameter in the callable, it wil only be applied to items of that type. |
||
| 104 | * |
||
| 105 | * @param callable $callable |
||
| 106 | * |
||
| 107 | * @return $this |
||
| 108 | */ |
||
| 109 | public function each(callable $callable) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Register a filter to the menu. When an item is added, all filters will be applied to the |
||
| 126 | * item. If you typehint the item |
||
| 127 | * parameter in the callable, it wil only be applied to items of that type. |
||
| 128 | * |
||
| 129 | * @param callable $callable |
||
| 130 | * |
||
| 131 | * @return $this |
||
| 132 | */ |
||
| 133 | public function registerFilter(callable $callable) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Apply a callable to all existing items, and register it as a filter so it will get applied |
||
| 142 | * to all new items too. If you typehint the item parameter in the callable, it wil only be |
||
| 143 | * applied to items of that type. |
||
| 144 | * |
||
| 145 | * @param callable $callable |
||
| 146 | * |
||
| 147 | * @return $this |
||
| 148 | */ |
||
| 149 | public function applyToAll(callable $callable) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Prepend a string of html to the menu on render. |
||
| 159 | * |
||
| 160 | * @param string $prefix |
||
| 161 | * |
||
| 162 | * @return $this |
||
| 163 | */ |
||
| 164 | public function prefixLinks(string $prefix) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Prepend the menu with a string of html on render. |
||
| 173 | * |
||
| 174 | * @param string $prepend |
||
| 175 | * |
||
| 176 | * @return $this |
||
| 177 | */ |
||
| 178 | public function prepend(string $prepend) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Prepend the menu with a string of html on render if a certain condition is met. |
||
| 187 | * |
||
| 188 | * @param bool $condition |
||
| 189 | * @param string $prepend |
||
| 190 | * |
||
| 191 | * @return $this |
||
| 192 | */ |
||
| 193 | public function prependIf(bool $condition, string $prepend) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Append a string of html to the menu on render. |
||
| 204 | * |
||
| 205 | * @param string $append |
||
| 206 | * |
||
| 207 | * @return $this |
||
| 208 | */ |
||
| 209 | public function append(string $append) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Append the menu with a string of html on render if a certain condition is met. |
||
| 218 | * |
||
| 219 | * @param bool $condition |
||
| 220 | * @param string $append |
||
| 221 | * |
||
| 222 | * @return static |
||
| 223 | */ |
||
| 224 | public function appendIf(bool $condition, string $append) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Determine whether the menu is active. |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public function isActive() : bool |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Set multiple items in the menu as active based on a callable that filters through items. |
||
| 251 | * If you typehint the item parameter in the callable, it wil only be applied to items of |
||
| 252 | * that type. |
||
| 253 | * |
||
| 254 | * @param callable|string $patternOrCallable |
||
| 255 | * @param string $root |
||
| 256 | * |
||
| 257 | * @return $this |
||
| 258 | */ |
||
| 259 | public function setActive($patternOrCallable, string $root = '/') |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Set all relevant children active based on the current request's URL. |
||
| 274 | * |
||
| 275 | * /, /about, /contact => request to /about will set the about link active. |
||
| 276 | * |
||
| 277 | * /en, /en/about, /en/contact => request to /en won't set /en active if the request root |
||
| 278 | * is set to /en. |
||
| 279 | * |
||
| 280 | * @param string $url The current request url. |
||
| 281 | * @param string $root If the link's URL is an exact match with the request root, the |
||
| 282 | * link won't be set active. This behavior is to avoid having home |
||
| 283 | * links active on every request. |
||
| 284 | * |
||
| 285 | * @return $this |
||
| 286 | */ |
||
| 287 | public function setActiveFromUrl(string $url, string $root = '/') |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param callable $callable |
||
| 324 | * |
||
| 325 | * @return $this |
||
| 326 | */ |
||
| 327 | public function setActiveFromCallable(callable $callable) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @param string $class |
||
| 344 | */ |
||
| 345 | public function setActiveClass(string $class) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public function render() : string |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @return int |
||
| 372 | */ |
||
| 373 | public function count() : int |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | public function __toString() : string |
||
| 385 | } |
||
| 386 |