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 array $attributes |
||
93 | * @param callable $callback |
||
94 | * |
||
95 | * @return \Rinvex\Menus\Models\MenuItem|null |
||
96 | */ |
||
97 | public function findByTitleOrAdd(string $title, int $order = null, string $icon = null, array $attributes = [], callable $callback = null): ?MenuItem |
||
106 | |||
107 | /** |
||
108 | * Set view factory instance. |
||
109 | * |
||
110 | * @param \Illuminate\View\Factory $views |
||
111 | * |
||
112 | * @return $this |
||
113 | */ |
||
114 | public function setViewFactory(ViewFactory $views) |
||
120 | |||
121 | /** |
||
122 | * Set view. |
||
123 | * |
||
124 | * @param string $view |
||
125 | * |
||
126 | * @return $this |
||
127 | */ |
||
128 | public function setView(string $view) |
||
134 | |||
135 | /** |
||
136 | * Set Prefix URL. |
||
137 | * |
||
138 | * @param string $prefixUrl |
||
139 | * |
||
140 | * @return $this |
||
141 | */ |
||
142 | public function setUrlPrefix(string $urlPrefix) |
||
148 | |||
149 | /** |
||
150 | * Set new presenter class. |
||
151 | * |
||
152 | * @param string $presenter |
||
153 | * |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function setPresenter(string $presenter) |
||
162 | |||
163 | /** |
||
164 | * Get presenter instance. |
||
165 | * |
||
166 | * @return \Rinvex\Menus\Contracts\PresenterContract |
||
167 | */ |
||
168 | public function getPresenter(): PresenterContract |
||
172 | |||
173 | /** |
||
174 | * Determine if the given name in the presenter style. |
||
175 | * |
||
176 | * @param string $presenter |
||
177 | * |
||
178 | * @return bool |
||
179 | */ |
||
180 | public function presenterExists(string $presenter): bool |
||
184 | |||
185 | /** |
||
186 | * Set the resolved item bindings. |
||
187 | * |
||
188 | * @param array $bindings |
||
189 | * |
||
190 | * @return $this |
||
191 | */ |
||
192 | public function setBindings(array $bindings) |
||
198 | |||
199 | /** |
||
200 | * Resolves a key from the bindings array. |
||
201 | * |
||
202 | * @param string|array $key |
||
203 | * |
||
204 | * @return mixed |
||
205 | */ |
||
206 | public function resolve($key) |
||
227 | |||
228 | /** |
||
229 | * Resolves an array of menu items properties. |
||
230 | * |
||
231 | * @param \Illuminate\Support\Collection &$items |
||
232 | * |
||
233 | * @return void |
||
234 | */ |
||
235 | protected function resolveItems(Collection &$items): void |
||
245 | |||
246 | /** |
||
247 | * Add new child menu. |
||
248 | * |
||
249 | * @param array $properties |
||
250 | * |
||
251 | * @return \Rinvex\Menus\Models\MenuItem |
||
252 | */ |
||
253 | protected function add(array $properties = []): MenuItem |
||
260 | |||
261 | /** |
||
262 | * Create new menu with dropdown. |
||
263 | * |
||
264 | * @param callable $callback |
||
265 | * @param string $title |
||
266 | * @param int $order |
||
267 | * @param string $icon |
||
268 | * @param array $attributes |
||
269 | * |
||
270 | * @return \Rinvex\Menus\Models\MenuItem |
||
271 | */ |
||
272 | public function dropdown(callable $callback, string $title, int $order = null, string $icon = null, array $attributes = []): MenuItem |
||
278 | |||
279 | /** |
||
280 | * Register new menu item using registered route. |
||
281 | * |
||
282 | * @param string $route |
||
283 | * @param string $title |
||
284 | * @param int $order |
||
285 | * @param string $icon |
||
286 | * @param array $attributes |
||
287 | * |
||
288 | * @return \Rinvex\Menus\Models\MenuItem |
||
289 | */ |
||
290 | public function route(array $route, string $title, int $order = null, string $icon = null, array $attributes = []): MenuItem |
||
294 | |||
295 | /** |
||
296 | * Register new menu item using url. |
||
297 | * |
||
298 | * @param string $url |
||
299 | * @param string $title |
||
300 | * @param int $order |
||
301 | * @param string $icon |
||
302 | * @param array $attributes |
||
303 | * |
||
304 | * @return \Rinvex\Menus\Models\MenuItem |
||
305 | */ |
||
306 | public function url(string $url, string $title, int $order = null, string $icon = null, array $attributes = []): MenuItem |
||
312 | |||
313 | /** |
||
314 | * Add new header item. |
||
315 | * |
||
316 | * @param string $title |
||
317 | * @param int $order |
||
318 | * @param string $icon |
||
319 | * @param array $attributes |
||
320 | * |
||
321 | * @return \Rinvex\Menus\Models\MenuItem |
||
322 | */ |
||
323 | public function header(string $title, int $order = null, string $icon = null, array $attributes = []): MenuItem |
||
329 | |||
330 | /** |
||
331 | * Add new divider item. |
||
332 | * |
||
333 | * @param int $order |
||
334 | * @param array $attributes |
||
335 | * |
||
336 | * @return \Rinvex\Menus\Models\MenuItem |
||
337 | */ |
||
338 | public function divider(int $order = null, array $attributes = []): MenuItem |
||
342 | |||
343 | /** |
||
344 | * Get items count. |
||
345 | * |
||
346 | * @return int |
||
347 | */ |
||
348 | public function count(): int |
||
352 | |||
353 | /** |
||
354 | * Empty the current menu items. |
||
355 | * |
||
356 | * @return $this |
||
357 | */ |
||
358 | public function destroy() |
||
364 | |||
365 | /** |
||
366 | * Get menu items and order it by 'order' key. |
||
367 | * |
||
368 | * @return \Illuminate\Support\Collection |
||
369 | */ |
||
370 | protected function getOrderedItems(): Collection |
||
374 | |||
375 | /** |
||
376 | * Render the menu to HTML tag. |
||
377 | * |
||
378 | * @param string $presenter |
||
379 | * @param bool $specialSidebar |
||
380 | * |
||
381 | * @return string |
||
382 | */ |
||
383 | public function render(string $presenter = null, bool $specialSidebar = false): string |
||
395 | |||
396 | /** |
||
397 | * Render menu via view presenter. |
||
398 | * |
||
399 | * @param string $view |
||
400 | * @param bool $specialSidebar |
||
401 | * |
||
402 | * @return \Illuminate\Contracts\View\View |
||
403 | */ |
||
404 | protected function renderView(string $view, bool $specialSidebar = false): ViewContract |
||
408 | |||
409 | /** |
||
410 | * Render the menu. |
||
411 | * |
||
412 | * @param bool $specialSidebar |
||
413 | * |
||
414 | * @return string |
||
415 | */ |
||
416 | protected function renderMenu(bool $specialSidebar = false): string |
||
441 | |||
442 | /** |
||
443 | * Format URL. |
||
444 | * |
||
445 | * @param string $url |
||
446 | * |
||
447 | * @return string |
||
448 | */ |
||
449 | protected function formatUrl(string $url): string |
||
455 | } |
||
456 |
This check looks for
@param
annotations 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
array
and 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.