Complex classes like MenuGenerator 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 MenuGenerator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class MenuGenerator implements Countable |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * The items collection. |
||
| 19 | * |
||
| 20 | * @var \Illuminate\Support\Collection |
||
| 21 | */ |
||
| 22 | protected $items; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The presenter class. |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $presenter = NavbarPresenter::class; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The URL prefix. |
||
| 33 | * |
||
| 34 | * @var string|null |
||
| 35 | */ |
||
| 36 | protected $urlPrefix; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The view name. |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $view; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The laravel view factory instance. |
||
| 47 | * |
||
| 48 | * @var \Illuminate\View\Factory |
||
| 49 | */ |
||
| 50 | protected $views; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Resolved item binding map. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $bindings = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Create a new MenuGenerator instance. |
||
| 61 | */ |
||
| 62 | public function __construct() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Find menu item by given key and value. |
||
| 69 | * |
||
| 70 | * @param string $key |
||
| 71 | * @param string $value |
||
| 72 | * @param callable $callback |
||
|
|
|||
| 73 | * |
||
| 74 | * @return \Rinvex\Menus\Models\MenuItem|null |
||
| 75 | */ |
||
| 76 | public function findBy(string $key, string $value, callable $callback = null): ?MenuItem |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Find menu item by given key and value. |
||
| 89 | * |
||
| 90 | * @param string $title |
||
| 91 | * @param int $order |
||
| 92 | * @param string $icon |
||
| 93 | * @param array $attributes |
||
| 94 | * @param callable $callback |
||
| 95 | * |
||
| 96 | * @return \Rinvex\Menus\Models\MenuItem|null |
||
| 97 | */ |
||
| 98 | public function findByTitleOrAdd(string $title, int $order = null, string $icon = null, array $attributes = [], callable $callback = null): ?MenuItem |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Set view factory instance. |
||
| 110 | * |
||
| 111 | * @param \Illuminate\View\Factory $views |
||
| 112 | * |
||
| 113 | * @return $this |
||
| 114 | */ |
||
| 115 | public function setViewFactory(ViewFactory $views) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Set view. |
||
| 124 | * |
||
| 125 | * @param string $view |
||
| 126 | * |
||
| 127 | * @return $this |
||
| 128 | */ |
||
| 129 | public function setView(string $view) |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Set Prefix URL. |
||
| 138 | * |
||
| 139 | * @param string $prefixUrl |
||
| 140 | * |
||
| 141 | * @return $this |
||
| 142 | */ |
||
| 143 | public function setUrlPrefix(string $urlPrefix) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Set new presenter class. |
||
| 152 | * |
||
| 153 | * @param string $presenter |
||
| 154 | * |
||
| 155 | * @return $this |
||
| 156 | */ |
||
| 157 | public function setPresenter(string $presenter) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Get presenter instance. |
||
| 166 | * |
||
| 167 | * @return \Rinvex\Menus\Contracts\PresenterContract |
||
| 168 | */ |
||
| 169 | public function getPresenter(): PresenterContract |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Determine if the given name in the presenter style. |
||
| 176 | * |
||
| 177 | * @param string $presenter |
||
| 178 | * |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | public function presenterExists(string $presenter): bool |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Set the resolved item bindings. |
||
| 188 | * |
||
| 189 | * @param array $bindings |
||
| 190 | * |
||
| 191 | * @return $this |
||
| 192 | */ |
||
| 193 | public function setBindings(array $bindings) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Resolves a key from the bindings array. |
||
| 202 | * |
||
| 203 | * @param string|array $key |
||
| 204 | * |
||
| 205 | * @return mixed |
||
| 206 | */ |
||
| 207 | public function resolve($key) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Resolves an array of menu items properties. |
||
| 231 | * |
||
| 232 | * @param \Illuminate\Support\Collection &$items |
||
| 233 | * |
||
| 234 | * @return void |
||
| 235 | */ |
||
| 236 | protected function resolveItems(Collection &$items): void |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Add new child menu. |
||
| 249 | * |
||
| 250 | * @param array $properties |
||
| 251 | * |
||
| 252 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 253 | */ |
||
| 254 | protected function add(array $properties = []): MenuItem |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Create new menu with dropdown. |
||
| 264 | * |
||
| 265 | * @param callable $callback |
||
| 266 | * @param string $title |
||
| 267 | * @param int $order |
||
| 268 | * @param string $icon |
||
| 269 | * @param array $attributes |
||
| 270 | * |
||
| 271 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 272 | */ |
||
| 273 | public function dropdown(callable $callback, string $title, int $order = null, string $icon = null, array $attributes = []): MenuItem |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Register new menu item using registered route. |
||
| 282 | * |
||
| 283 | * @param string $route |
||
| 284 | * @param string $title |
||
| 285 | * @param int $order |
||
| 286 | * @param string $icon |
||
| 287 | * @param array $attributes |
||
| 288 | * |
||
| 289 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 290 | */ |
||
| 291 | public function route(array $route, string $title, int $order = null, string $icon = null, array $attributes = []): MenuItem |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Register new menu item using url. |
||
| 298 | * |
||
| 299 | * @param string $url |
||
| 300 | * @param string $title |
||
| 301 | * @param int $order |
||
| 302 | * @param string $icon |
||
| 303 | * @param array $attributes |
||
| 304 | * |
||
| 305 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 306 | */ |
||
| 307 | public function url(string $url, string $title, int $order = null, string $icon = null, array $attributes = []): MenuItem |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Add new header item. |
||
| 316 | * |
||
| 317 | * @param string $title |
||
| 318 | * @param int $order |
||
| 319 | * @param string $icon |
||
| 320 | * @param array $attributes |
||
| 321 | * |
||
| 322 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 323 | */ |
||
| 324 | public function header(string $title, int $order = null, string $icon = null, array $attributes = []): MenuItem |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Add new divider item. |
||
| 333 | * |
||
| 334 | * @param int $order |
||
| 335 | * @param array $attributes |
||
| 336 | * |
||
| 337 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 338 | */ |
||
| 339 | public function divider(int $order = null, array $attributes = []): MenuItem |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Get items count. |
||
| 346 | * |
||
| 347 | * @return int |
||
| 348 | */ |
||
| 349 | public function count(): int |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Empty the current menu items. |
||
| 356 | * |
||
| 357 | * @return $this |
||
| 358 | */ |
||
| 359 | public function destroy() |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Get menu items and order it by 'order' key. |
||
| 368 | * |
||
| 369 | * @return \Illuminate\Support\Collection |
||
| 370 | */ |
||
| 371 | protected function getOrderedItems(): Collection |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Render the menu to HTML tag. |
||
| 378 | * |
||
| 379 | * @param string $presenter |
||
| 380 | * @param bool $specialSidebar |
||
| 381 | * |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | public function render(string $presenter = null, bool $specialSidebar = false): string |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Render menu via view presenter. |
||
| 399 | * |
||
| 400 | * @param string $view |
||
| 401 | * @param bool $specialSidebar |
||
| 402 | * |
||
| 403 | * @return \Illuminate\Contracts\View\View |
||
| 404 | */ |
||
| 405 | protected function renderView(string $view, bool $specialSidebar = false): ViewContract |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Render the menu. |
||
| 412 | * |
||
| 413 | * @param bool $specialSidebar |
||
| 414 | * |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | protected function renderMenu(bool $specialSidebar = false): string |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Format URL. |
||
| 445 | * |
||
| 446 | * @param string $url |
||
| 447 | * |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | protected function formatUrl(string $url): string |
||
| 456 | } |
||
| 457 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
arrayand suggests a stricter type likearray<String>.Most often this is a case of a parameter that can be null in addition to its declared types.