Complex classes like MenuItem 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 MenuItem, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class MenuItem |
||
12 | { |
||
13 | /** |
||
14 | * The properties array. |
||
15 | * |
||
16 | * @var array |
||
17 | */ |
||
18 | public $properties = []; |
||
19 | |||
20 | /** |
||
21 | * The childs collection. |
||
22 | * |
||
23 | * @var \Illuminate\Support\Collection |
||
24 | */ |
||
25 | protected $childs; |
||
26 | |||
27 | /** |
||
28 | * The hide callback. |
||
29 | * |
||
30 | * @var callable |
||
31 | */ |
||
32 | protected $hideWhen; |
||
33 | |||
34 | /** |
||
35 | * Constructor. |
||
36 | * |
||
37 | * @param array $properties |
||
38 | */ |
||
39 | public function __construct($properties = []) |
||
44 | |||
45 | /** |
||
46 | * Fill the properties. |
||
47 | * |
||
48 | * @param array $properties |
||
49 | * |
||
50 | * @return static |
||
51 | */ |
||
52 | public function fill($properties) |
||
58 | |||
59 | /** |
||
60 | * Add new child item. |
||
61 | * |
||
62 | * @param array $properties |
||
63 | * |
||
64 | * @return static |
||
65 | */ |
||
66 | protected function add(array $properties = []) |
||
73 | |||
74 | /** |
||
75 | * Create new menu with dropdown. |
||
76 | * |
||
77 | * @param callable $callback |
||
78 | * @param string $title |
||
79 | * @param int $order |
||
|
|||
80 | * @param string $icon |
||
81 | * @param array $attributes |
||
82 | * |
||
83 | * @return static |
||
84 | */ |
||
85 | public function dropdown(callable $callback, string $title, int $order = null, string $icon = null, array $attributes = []) |
||
91 | |||
92 | /** |
||
93 | * Register new menu item using registered route. |
||
94 | * |
||
95 | * @param string $route |
||
96 | * @param string $title |
||
97 | * @param int $order |
||
98 | * @param string $icon |
||
99 | * @param array $attributes |
||
100 | * |
||
101 | * @return static |
||
102 | */ |
||
103 | public function route(array $route, string $title, int $order = null, string $icon = null, array $attributes = []) |
||
107 | |||
108 | /** |
||
109 | * Register new menu item using url. |
||
110 | * |
||
111 | * @param string $url |
||
112 | * @param string $title |
||
113 | * @param int $order |
||
114 | * @param string $icon |
||
115 | * @param array $attributes |
||
116 | * |
||
117 | * @return static |
||
118 | */ |
||
119 | public function url(string $url, string $title, int $order = null, string $icon = null, array $attributes = []) |
||
123 | |||
124 | /** |
||
125 | * Add new header item. |
||
126 | * |
||
127 | * @param string $title |
||
128 | * @param int $order |
||
129 | * @param string $icon |
||
130 | * @param array $attributes |
||
131 | * |
||
132 | * @return static |
||
133 | */ |
||
134 | public function header(string $title, int $order = null, string $icon = null, array $attributes = []) |
||
140 | |||
141 | /** |
||
142 | * Add new divider item. |
||
143 | * |
||
144 | * @param int $order |
||
145 | * @param array $attributes |
||
146 | * |
||
147 | * @return static |
||
148 | */ |
||
149 | public function divider(int $order = null, array $attributes = []) |
||
153 | |||
154 | /** |
||
155 | * Get childs. |
||
156 | * |
||
157 | * @return \Illuminate\Support\Collection |
||
158 | */ |
||
159 | public function getChilds() |
||
163 | |||
164 | /** |
||
165 | * Get url. |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | public function getUrl() |
||
173 | |||
174 | /** |
||
175 | * Get HTML attribute data. |
||
176 | * |
||
177 | * @return mixed |
||
178 | */ |
||
179 | public function getAttributes() |
||
183 | |||
184 | /** |
||
185 | * Check if the current item is divider. |
||
186 | * |
||
187 | * @return bool |
||
188 | */ |
||
189 | public function isDivider(): bool |
||
193 | |||
194 | /** |
||
195 | * Check if the current item is header. |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | public function isHeader(): bool |
||
203 | |||
204 | /** |
||
205 | * Check is the current item has sub menu . |
||
206 | * |
||
207 | * @return bool |
||
208 | */ |
||
209 | public function hasChilds() |
||
213 | |||
214 | /** |
||
215 | * Check the active state for current menu. |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | public function hasActiveOnChild() |
||
227 | |||
228 | /** |
||
229 | * Check if the item has active state from childs. |
||
230 | * |
||
231 | * @return bool |
||
232 | */ |
||
233 | public function hasActiveStateFromChilds(): bool |
||
245 | |||
246 | /** |
||
247 | * Get inactive state. |
||
248 | * |
||
249 | * @return bool |
||
250 | */ |
||
251 | public function inactive(): bool |
||
263 | |||
264 | /** |
||
265 | * Get active state for current item. |
||
266 | * |
||
267 | * @return mixed |
||
268 | */ |
||
269 | public function isActive() |
||
289 | |||
290 | /** |
||
291 | * Get active status using route. |
||
292 | * |
||
293 | * @return bool |
||
294 | */ |
||
295 | protected function hasActiveStateFromRoute(): bool |
||
299 | |||
300 | /** |
||
301 | * Get active status using request url. |
||
302 | * |
||
303 | * @return bool |
||
304 | */ |
||
305 | protected function hasActiveStateFromUrl(): bool |
||
309 | |||
310 | /** |
||
311 | * Set hide callback for current menu item. |
||
312 | * |
||
313 | * @param callable $callback |
||
314 | * |
||
315 | * @return $this |
||
316 | */ |
||
317 | public function hideWhen(callable $callback) |
||
323 | |||
324 | /** |
||
325 | * Set authorization callback for current menu item. |
||
326 | * |
||
327 | * @param string $ability |
||
328 | * @param mixed $params |
||
329 | * |
||
330 | * @return $this |
||
331 | */ |
||
332 | public function can(string $ability, $params = null) |
||
340 | |||
341 | /** |
||
342 | * Set authentication callback for current menu item. |
||
343 | * |
||
344 | * @return $this |
||
345 | */ |
||
346 | public function user() |
||
354 | |||
355 | /** |
||
356 | * Set authentication callback for current menu item. |
||
357 | * |
||
358 | * @return $this |
||
359 | */ |
||
360 | public function guest() |
||
368 | |||
369 | /** |
||
370 | * Check if the menu item is hidden. |
||
371 | * |
||
372 | * @return bool |
||
373 | */ |
||
374 | public function hidden() |
||
378 | |||
379 | /** |
||
380 | * Get property. |
||
381 | * |
||
382 | * @param string $key |
||
383 | * |
||
384 | * @return mixed |
||
385 | */ |
||
386 | public function __get($key) |
||
390 | } |
||
391 |
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.
Most often this is a case of a parameter that can be null in addition to its declared types.