Complex classes like Item 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 Item, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Arcanesoft\Sidebar\Entities; |
||
14 | class Item implements Arrayable, Jsonable, JsonSerializable |
||
15 | { |
||
16 | /* ----------------------------------------------------------------- |
||
17 | | Properties |
||
18 | | ----------------------------------------------------------------- |
||
19 | */ |
||
20 | |||
21 | /** |
||
22 | * The item name. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $name; |
||
27 | |||
28 | /** |
||
29 | * The item title. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $title; |
||
34 | |||
35 | /** |
||
36 | * The item url. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $url; |
||
41 | |||
42 | /** |
||
43 | * The item icon. |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $icon; |
||
48 | |||
49 | /** |
||
50 | * The item active state. |
||
51 | * |
||
52 | * @var bool |
||
53 | */ |
||
54 | protected $active = false; |
||
55 | |||
56 | /** |
||
57 | * The item roles. |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $roles = []; |
||
62 | |||
63 | /** |
||
64 | * The item permissions. |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $permissions = []; |
||
69 | |||
70 | /** |
||
71 | * The item children (sub-items). |
||
72 | * |
||
73 | * @var \Arcanesoft\Sidebar\Entities\ItemCollection |
||
74 | */ |
||
75 | protected $children; |
||
76 | |||
77 | /* ----------------------------------------------------------------- |
||
78 | | Constructor |
||
79 | | ----------------------------------------------------------------- |
||
80 | */ |
||
81 | |||
82 | /** |
||
83 | * Item constructor. |
||
84 | * |
||
85 | * @param string $name |
||
86 | * @param string $title |
||
87 | * @param string $url |
||
88 | * @param string|null $icon |
||
89 | */ |
||
90 | 24 | public function __construct($name, $title, $url, $icon = null) |
|
99 | |||
100 | /* ----------------------------------------------------------------- |
||
101 | | Getters & Setters |
||
102 | | ----------------------------------------------------------------- |
||
103 | */ |
||
104 | |||
105 | /** |
||
106 | * Get the item name. |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | 12 | public function name() |
|
114 | |||
115 | /** |
||
116 | * Get the item title. |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | 18 | public function title() |
|
127 | |||
128 | /** |
||
129 | * Set the title. |
||
130 | * |
||
131 | * @param string $title |
||
132 | * |
||
133 | * @return self |
||
134 | */ |
||
135 | public function setTitle($title) |
||
141 | |||
142 | /** |
||
143 | * Get the item url. |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | 12 | public function url() |
|
151 | |||
152 | /** |
||
153 | * Get the item icon. |
||
154 | * |
||
155 | * @return string|null |
||
156 | */ |
||
157 | 15 | public function icon() |
|
161 | |||
162 | /** |
||
163 | * Set the item icon. |
||
164 | * |
||
165 | * @param string $icon |
||
166 | * |
||
167 | * @return self |
||
168 | */ |
||
169 | public function setIcon($icon) |
||
175 | |||
176 | /** |
||
177 | * Set the current name. |
||
178 | * |
||
179 | * @param string $name |
||
180 | * |
||
181 | * @return self |
||
182 | */ |
||
183 | 3 | public function setCurrent($name) |
|
190 | |||
191 | /** |
||
192 | * Get the roles. |
||
193 | * |
||
194 | * @return array |
||
195 | */ |
||
196 | 9 | public function getRoles() |
|
200 | |||
201 | /** |
||
202 | * Set the roles. |
||
203 | * |
||
204 | * @param array $roles |
||
205 | * |
||
206 | * @return self |
||
207 | */ |
||
208 | 21 | public function setRoles(array $roles) |
|
214 | |||
215 | /** |
||
216 | * Get the permissions. |
||
217 | * |
||
218 | * @return array |
||
219 | */ |
||
220 | 9 | public function getPermissions() |
|
224 | |||
225 | /** |
||
226 | * Set the permissions. |
||
227 | * |
||
228 | * @param array $permissions |
||
229 | * |
||
230 | * @return self |
||
231 | */ |
||
232 | 21 | public function setPermissions(array $permissions) |
|
238 | |||
239 | /** |
||
240 | * Get the sub-items. |
||
241 | * |
||
242 | * @return \Arcanesoft\Sidebar\Entities\ItemCollection |
||
243 | */ |
||
244 | 9 | public function children() |
|
248 | |||
249 | /** |
||
250 | * Get the active class. |
||
251 | * |
||
252 | * @param string $class |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | 3 | public function activeClass($class = 'active') |
|
260 | |||
261 | /** |
||
262 | * Get the sub-items class. |
||
263 | * |
||
264 | * @param string $class |
||
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | 3 | public function childrenClass($class = 'treeview') |
|
272 | |||
273 | /* ----------------------------------------------------------------- |
||
274 | | Main Methods |
||
275 | | ----------------------------------------------------------------- |
||
276 | */ |
||
277 | /** |
||
278 | * Make the item. |
||
279 | * |
||
280 | * @param string $name |
||
281 | * @param string $title |
||
282 | * @param string $url |
||
283 | * @param string|null $icon |
||
284 | * |
||
285 | * @return self |
||
286 | */ |
||
287 | 9 | public static function make($name, $title, $url, $icon = null) |
|
291 | |||
292 | /** |
||
293 | * Make a Sidebar item from array. |
||
294 | * |
||
295 | * @param array $array |
||
296 | * |
||
297 | * @return self |
||
298 | */ |
||
299 | 6 | public static function makeFromArray(array $array) |
|
314 | |||
315 | /** |
||
316 | * Get url from array. |
||
317 | * |
||
318 | * @param array $array |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | 6 | private static function getUrlFromArray(array $array) |
|
329 | |||
330 | /** |
||
331 | * Add children to the parent. |
||
332 | * |
||
333 | * @param array $children |
||
334 | * |
||
335 | * @return self |
||
336 | */ |
||
337 | 6 | public function addChildren(array $children) |
|
345 | |||
346 | /** |
||
347 | * Add a sub-item to the parent. |
||
348 | * |
||
349 | * @param array $child |
||
350 | * |
||
351 | * @return self |
||
352 | */ |
||
353 | 3 | public function addChild(array $child) |
|
362 | |||
363 | /* ----------------------------------------------------------------- |
||
364 | | Check Methods |
||
365 | | ----------------------------------------------------------------- |
||
366 | */ |
||
367 | |||
368 | /** |
||
369 | * Check if the item is active one. |
||
370 | * |
||
371 | * @return bool |
||
372 | */ |
||
373 | 15 | public function isActive() |
|
377 | |||
378 | /** |
||
379 | * Check if the item has children. |
||
380 | * |
||
381 | * @return bool |
||
382 | */ |
||
383 | 9 | public function hasChildren() |
|
387 | |||
388 | /** |
||
389 | * Check the user is allowed to see this item. |
||
390 | * |
||
391 | * @return bool |
||
392 | */ |
||
393 | 6 | public function allowed() |
|
418 | |||
419 | /** |
||
420 | * Check if the item has roles. |
||
421 | * |
||
422 | * @return bool |
||
423 | */ |
||
424 | 6 | public function hasRoles() |
|
428 | |||
429 | /** |
||
430 | * Check if the item has permissions. |
||
431 | * |
||
432 | * @return bool |
||
433 | */ |
||
434 | 6 | public function hasPermissions() |
|
438 | |||
439 | /* ----------------------------------------------------------------- |
||
440 | | Other Methods |
||
441 | | ----------------------------------------------------------------- |
||
442 | */ |
||
443 | |||
444 | /** |
||
445 | * Get the instance as an array. |
||
446 | * |
||
447 | * @return array |
||
448 | */ |
||
449 | 6 | public function toArray() |
|
462 | |||
463 | /** |
||
464 | * Convert the object to its JSON representation. |
||
465 | * |
||
466 | * @param int $options |
||
467 | * |
||
468 | * @return string |
||
469 | */ |
||
470 | 3 | public function toJson($options = 0) |
|
474 | |||
475 | /** |
||
476 | * Convert the object into something JSON serializable. |
||
477 | * |
||
478 | * @return array |
||
479 | */ |
||
480 | 3 | public function jsonSerialize() |
|
484 | } |
||
485 |