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 $this |
||
| 78 | */ |
||
| 79 | public function link(string $url, string $text) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Shortcut function to add raw html to the menu. |
||
| 86 | * |
||
| 87 | * @param string $html |
||
| 88 | * |
||
| 89 | * @return $this |
||
| 90 | */ |
||
| 91 | public function html(string $html) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Apply a filter to an item. Returns the result of the filter. |
||
| 98 | * |
||
| 99 | * @param callable $filter |
||
| 100 | * @param \Spatie\Menu\Item $item |
||
| 101 | */ |
||
| 102 | protected function applyFilter(callable $filter, Item $item) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Iterate over all the items and apply a callback. If you typehint the |
||
| 115 | * item parameter in the callable, it wil only be applied to items of that type. |
||
| 116 | * |
||
| 117 | * @param callable $callable |
||
| 118 | * |
||
| 119 | * @return $this |
||
| 120 | */ |
||
| 121 | public function each(callable $callable) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Register a filter to the menu. When an item is added, all filters will be applied to the |
||
| 138 | * item. If you typehint the item |
||
| 139 | * parameter in the callable, it wil only be applied to items of that type. |
||
| 140 | * |
||
| 141 | * @param callable $callable |
||
| 142 | * |
||
| 143 | * @return $this |
||
| 144 | */ |
||
| 145 | public function registerFilter(callable $callable) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Apply a callable to all existing items, and register it as a filter so it will get applied |
||
| 154 | * to all new items too. If you typehint the item parameter in the callable, it wil only be |
||
| 155 | * applied to items of that type. |
||
| 156 | * |
||
| 157 | * @param callable $callable |
||
| 158 | * |
||
| 159 | * @return $this |
||
| 160 | */ |
||
| 161 | public function applyToAll(callable $callable) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Prepend a string of html to the menu on render. |
||
| 171 | * |
||
| 172 | * @param string $prefix |
||
| 173 | * |
||
| 174 | * @return $this |
||
| 175 | */ |
||
| 176 | public function prefixLinks(string $prefix) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Prepend the menu with a string of html on render. |
||
| 185 | * |
||
| 186 | * @param string $prepend |
||
| 187 | * |
||
| 188 | * @return $this |
||
| 189 | */ |
||
| 190 | public function prepend(string $prepend) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Prepend the menu with a string of html on render if a certain condition is met. |
||
| 199 | * |
||
| 200 | * @param bool $condition |
||
| 201 | * @param string $prepend |
||
| 202 | * |
||
| 203 | * @return $this |
||
| 204 | */ |
||
| 205 | public function prependIf(bool $condition, string $prepend) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Append a string of html to the menu on render. |
||
| 216 | * |
||
| 217 | * @param string $append |
||
| 218 | * |
||
| 219 | * @return $this |
||
| 220 | */ |
||
| 221 | public function append(string $append) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Append the menu with a string of html on render if a certain condition is met. |
||
| 230 | * |
||
| 231 | * @param bool $condition |
||
| 232 | * @param string $append |
||
| 233 | * |
||
| 234 | * @return static |
||
| 235 | */ |
||
| 236 | public function appendIf(bool $condition, string $append) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Determine whether the menu is active. |
||
| 247 | * |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | public function isActive() : bool |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Set multiple items in the menu as active based on a callable that filters through items. |
||
| 263 | * If you typehint the item parameter in the callable, it wil only be applied to items of |
||
| 264 | * that type. |
||
| 265 | * |
||
| 266 | * @param callable|string $patternOrCallable |
||
| 267 | * @param string $root |
||
| 268 | * |
||
| 269 | * @return $this |
||
| 270 | */ |
||
| 271 | public function setActive($patternOrCallable, string $root = '/') |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Set all relevant children active based on the current request's URL. |
||
| 286 | * |
||
| 287 | * /, /about, /contact => request to /about will set the about link active. |
||
| 288 | * |
||
| 289 | * /en, /en/about, /en/contact => request to /en won't set /en active if the request root |
||
| 290 | * is set to /en. |
||
| 291 | * |
||
| 292 | * @param string $url The current request url. |
||
| 293 | * @param string $root If the link's URL is an exact match with the request root, the |
||
| 294 | * link won't be set active. This behavior is to avoid having home |
||
| 295 | * links active on every request. |
||
| 296 | * |
||
| 297 | * @return $this |
||
| 298 | */ |
||
| 299 | public function setActiveFromUrl(string $url, string $root = '/') |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param callable $callable |
||
| 342 | * |
||
| 343 | * @return $this |
||
| 344 | */ |
||
| 345 | public function setActiveFromCallable(callable $callable) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @param string $class |
||
| 362 | * |
||
| 363 | * @return $this |
||
| 364 | */ |
||
| 365 | public function setActiveClass(string $class) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | public function render() : string |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @return int |
||
| 394 | */ |
||
| 395 | public function count() : int |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public function __toString() : string |
||
| 407 | } |
||
| 408 |