Complex classes like MenuFactory 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 MenuFactory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class MenuFactory implements Countable |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The items collection. |
||
| 17 | * |
||
| 18 | * @var \Illuminate\Support\Collection |
||
| 19 | */ |
||
| 20 | protected $items; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The presenter class. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $presenter = NavbarPresenter::class; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The URL prefix. |
||
| 31 | * |
||
| 32 | * @var string|null |
||
| 33 | */ |
||
| 34 | protected $urlPrefix; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The view name. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $view; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The laravel view factory instance. |
||
| 45 | * |
||
| 46 | * @var \Illuminate\View\Factory |
||
| 47 | */ |
||
| 48 | protected $views; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Resolved item binding map. |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $bindings = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Create a new MenuFactory instance. |
||
| 59 | */ |
||
| 60 | public function __construct() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Find menu item by given key and value. |
||
| 67 | * |
||
| 68 | * @param string $key |
||
| 69 | * @param string $value |
||
| 70 | * @param callable $callback |
||
|
|
|||
| 71 | * |
||
| 72 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 73 | */ |
||
| 74 | public function findBy(string $key, string $value, callable $callback = null) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Find menu item by given key and value. |
||
| 87 | * |
||
| 88 | * @param string $title |
||
| 89 | * @param int $order |
||
| 90 | * @param string $icon |
||
| 91 | * @param array $attributes |
||
| 92 | * @param callable $callback |
||
| 93 | * |
||
| 94 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 95 | */ |
||
| 96 | public function findByTitleOrAdd(string $title, int $order = null, string $icon = null, array $attributes = [], callable $callback = null) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Set view factory instance. |
||
| 108 | * |
||
| 109 | * @param \Illuminate\View\Factory $views |
||
| 110 | * |
||
| 111 | * @return $this |
||
| 112 | */ |
||
| 113 | public function setViewFactory(ViewFactory $views) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Set view. |
||
| 122 | * |
||
| 123 | * @param string $view |
||
| 124 | * |
||
| 125 | * @return $this |
||
| 126 | */ |
||
| 127 | public function setView(string $view) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Set Prefix URL. |
||
| 136 | * |
||
| 137 | * @param string $prefixUrl |
||
| 138 | * |
||
| 139 | * @return $this |
||
| 140 | */ |
||
| 141 | public function setUrlPrefix(string $urlPrefix) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Set new presenter class. |
||
| 150 | * |
||
| 151 | * @param string $presenter |
||
| 152 | * |
||
| 153 | * @return $this |
||
| 154 | */ |
||
| 155 | public function setPresenter(string $presenter) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Get presenter instance. |
||
| 164 | * |
||
| 165 | * @return \Rinvex\Menus\Contracts\PresenterContract |
||
| 166 | */ |
||
| 167 | public function getPresenter() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Determine if the given name in the presenter style. |
||
| 174 | * |
||
| 175 | * @param string $presenter |
||
| 176 | * |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | public function presenterExists(string $presenter) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Set the resolved item bindings. |
||
| 186 | * |
||
| 187 | * @param array $bindings |
||
| 188 | * |
||
| 189 | * @return $this |
||
| 190 | */ |
||
| 191 | public function setBindings(array $bindings) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Resolves a key from the bindings array. |
||
| 200 | * |
||
| 201 | * @param string|array $key |
||
| 202 | * |
||
| 203 | * @return mixed |
||
| 204 | */ |
||
| 205 | public function resolve($key) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Resolves an array of menu items properties. |
||
| 229 | * |
||
| 230 | * @param \Illuminate\Support\Collection &$items |
||
| 231 | * |
||
| 232 | * @return void |
||
| 233 | */ |
||
| 234 | protected function resolveItems(Collection &$items) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Add new child menu. |
||
| 247 | * |
||
| 248 | * @param array $properties |
||
| 249 | * |
||
| 250 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 251 | */ |
||
| 252 | protected function add(array $properties = []) |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Create new menu with dropdown. |
||
| 262 | * |
||
| 263 | * @param callable $callback |
||
| 264 | * @param string $title |
||
| 265 | * @param int $order |
||
| 266 | * @param string $icon |
||
| 267 | * @param array $attributes |
||
| 268 | * |
||
| 269 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 270 | */ |
||
| 271 | public function dropdown(callable $callback, string $title, int $order = null, string $icon = null, array $attributes = []) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Register new menu item using registered route. |
||
| 280 | * |
||
| 281 | * @param string $route |
||
| 282 | * @param string $title |
||
| 283 | * @param int $order |
||
| 284 | * @param string $icon |
||
| 285 | * @param array $attributes |
||
| 286 | * |
||
| 287 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 288 | */ |
||
| 289 | public function route(array $route, string $title, int $order = null, string $icon = null, array $attributes = []) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Register new menu item using url. |
||
| 296 | * |
||
| 297 | * @param string $url |
||
| 298 | * @param string $title |
||
| 299 | * @param int $order |
||
| 300 | * @param string $icon |
||
| 301 | * @param array $attributes |
||
| 302 | * |
||
| 303 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 304 | */ |
||
| 305 | public function url(string $url, string $title, int $order = null, string $icon = null, array $attributes = []) |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Add new header item. |
||
| 314 | * |
||
| 315 | * @param string $title |
||
| 316 | * @param int $order |
||
| 317 | * @param string $icon |
||
| 318 | * @param array $attributes |
||
| 319 | * |
||
| 320 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 321 | */ |
||
| 322 | public function header(string $title, int $order = null, string $icon = null, array $attributes = []) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Add new divider item. |
||
| 331 | * |
||
| 332 | * @param int $order |
||
| 333 | * @param array $attributes |
||
| 334 | * |
||
| 335 | * @return \Rinvex\Menus\Models\MenuItem |
||
| 336 | */ |
||
| 337 | public function divider(int $order = null, array $attributes = []) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Get items count. |
||
| 344 | * |
||
| 345 | * @return int |
||
| 346 | */ |
||
| 347 | public function count() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Empty the current menu items. |
||
| 354 | * |
||
| 355 | * @return $this |
||
| 356 | */ |
||
| 357 | public function destroy() |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Get menu items and order it by 'order' key. |
||
| 366 | * |
||
| 367 | * @return \Illuminate\Support\Collection |
||
| 368 | */ |
||
| 369 | protected function getOrderedItems() |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Render the menu to HTML tag. |
||
| 376 | * |
||
| 377 | * @param string $presenter |
||
| 378 | * @param bool $specialSidebar |
||
| 379 | * |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | public function render(string $presenter = null, bool $specialSidebar = false) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Render menu via view presenter. |
||
| 397 | * |
||
| 398 | * @param string $view |
||
| 399 | * @param bool $specialSidebar |
||
| 400 | * |
||
| 401 | * @return \Illuminate\Contracts\View\View |
||
| 402 | */ |
||
| 403 | protected function renderView(string $view, bool $specialSidebar = false) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Render the menu. |
||
| 410 | * |
||
| 411 | * @param bool $specialSidebar |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | protected function renderMenu(bool $specialSidebar = false) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Format URL. |
||
| 443 | * |
||
| 444 | * @param string $url |
||
| 445 | * |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | protected function formatUrl(string $url) |
||
| 454 | } |
||
| 455 |
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.