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 |
||
| 14 | class MenuGenerator implements Countable |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * The items collection. |
||
| 18 | * |
||
| 19 | * @var \Illuminate\Support\Collection |
||
| 20 | */ |
||
| 21 | protected $items; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The presenter class. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $presenter = NavbarPresenter::class; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The URL prefix. |
||
| 32 | * |
||
| 33 | * @var string|null |
||
| 34 | */ |
||
| 35 | protected $urlPrefix; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The view name. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $view; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The laravel view factory instance. |
||
| 46 | * |
||
| 47 | * @var \Illuminate\View\Factory |
||
| 48 | */ |
||
| 49 | protected $views; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Resolved item binding map. |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $bindings = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Create a new MenuGenerator instance. |
||
| 60 | */ |
||
| 61 | public function __construct() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Find menu item by given key and value. |
||
| 68 | * |
||
| 69 | * @param string $key |
||
| 70 | * @param string $value |
||
| 71 | * @param callable $callback |
||
|
|
|||
| 72 | * |
||
| 73 | * @return \Rinvex\Menus\Models\MenuItem|null |
||
| 74 | */ |
||
| 75 | public function findBy(string $key, string $value, callable $callback = null): ?MenuItem |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Find menu item by given key and value. |
||
| 88 | * |
||
| 89 | * @param string $title |
||
| 90 | * @param int $order |
||
| 91 | * @param string $icon |
||
| 92 | * @param string $type |
||
| 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, string $type = 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 |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Register new menu item using registered route. |
||
| 284 | * |
||
| 285 | * @param string $route |
||
| 286 | * @param string $title |
||
| 287 | * @param int $order |
||
| 288 | * @param string $icon |
||
| 289 | * @param array $attributes |
||
| 290 | * |
||
| 291 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 292 | */ |
||
| 293 | public function route(array $route, string $title, int $order = null, string $icon = null, array $attributes = []): MenuItem |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Register new menu item using url. |
||
| 302 | * |
||
| 303 | * @param string $url |
||
| 304 | * @param string $title |
||
| 305 | * @param int $order |
||
| 306 | * @param string $icon |
||
| 307 | * @param array $attributes |
||
| 308 | * |
||
| 309 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 310 | */ |
||
| 311 | public function url(string $url, string $title, int $order = null, string $icon = null, array $attributes = []): MenuItem |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Add new header item. |
||
| 321 | * |
||
| 322 | * @param string $title |
||
| 323 | * @param int $order |
||
| 324 | * @param string $icon |
||
| 325 | * @param array $attributes |
||
| 326 | * |
||
| 327 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 328 | */ |
||
| 329 | public function header(string $title, int $order = null, string $icon = null, array $attributes = []): MenuItem |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Add new divider item. |
||
| 338 | * |
||
| 339 | * @param int $order |
||
| 340 | * @param array $attributes |
||
| 341 | * |
||
| 342 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 343 | */ |
||
| 344 | public function divider(int $order = null, array $attributes = []): MenuItem |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Get items count. |
||
| 353 | * |
||
| 354 | * @return int |
||
| 355 | */ |
||
| 356 | public function count(): int |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Empty the current menu items. |
||
| 363 | * |
||
| 364 | * @return $this |
||
| 365 | */ |
||
| 366 | public function destroy() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Get menu items and order it by 'order' key. |
||
| 375 | * |
||
| 376 | * @return \Illuminate\Support\Collection |
||
| 377 | */ |
||
| 378 | protected function getOrderedItems(): Collection |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Render the menu to HTML tag. |
||
| 391 | * |
||
| 392 | * @param string $presenter |
||
| 393 | * @param bool $specialSidebar |
||
| 394 | * |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | public function render(string $presenter = null, bool $specialSidebar = false): string |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Render menu via view presenter. |
||
| 412 | * |
||
| 413 | * @param string $view |
||
| 414 | * @param bool $specialSidebar |
||
| 415 | * |
||
| 416 | * @return \Illuminate\Contracts\View\View |
||
| 417 | */ |
||
| 418 | protected function renderView(string $view, bool $specialSidebar = false): ViewContract |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Render the menu. |
||
| 425 | * |
||
| 426 | * @param bool $specialSidebar |
||
| 427 | * |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | protected function renderMenu(bool $specialSidebar = false): string |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Format URL. |
||
| 458 | * |
||
| 459 | * @param string $url |
||
| 460 | * |
||
| 461 | * @return string |
||
| 462 | */ |
||
| 463 | protected function formatUrl(string $url): string |
||
| 469 | } |
||
| 470 |
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.