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 | 36 | 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 | 18 | public function name() |
|
114 | |||
115 | /** |
||
116 | * Get the item title. |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | 24 | public function title() |
|
127 | |||
128 | /** |
||
129 | * Get the item url. |
||
130 | * |
||
131 | * @return string |
||
132 | */ |
||
133 | 18 | public function url() |
|
137 | |||
138 | /** |
||
139 | * Get the item icon. |
||
140 | * |
||
141 | * @return string|null |
||
142 | */ |
||
143 | 21 | public function icon() |
|
147 | |||
148 | /** |
||
149 | * Set the current name. |
||
150 | * |
||
151 | * @param string $name |
||
152 | * |
||
153 | * @return self |
||
154 | */ |
||
155 | 6 | public function setCurrent($name) |
|
162 | |||
163 | /** |
||
164 | * Get the roles. |
||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | 12 | public function getRoles() |
|
172 | |||
173 | /** |
||
174 | * Set the roles. |
||
175 | * |
||
176 | * @param array $roles |
||
177 | * |
||
178 | * @return self |
||
179 | */ |
||
180 | 27 | public function setRoles(array $roles) |
|
186 | |||
187 | /** |
||
188 | * Get the permissions. |
||
189 | * |
||
190 | * @return array |
||
191 | */ |
||
192 | 12 | public function getPermissions() |
|
196 | |||
197 | /** |
||
198 | * Set the permissions. |
||
199 | * |
||
200 | * @param array $permissions |
||
201 | * |
||
202 | * @return self |
||
203 | */ |
||
204 | 27 | public function setPermissions(array $permissions) |
|
210 | |||
211 | /** |
||
212 | * Get the sub-items. |
||
213 | * |
||
214 | * @return \Arcanesoft\Sidebar\Entities\ItemCollection |
||
215 | */ |
||
216 | 12 | public function children() |
|
220 | |||
221 | /** |
||
222 | * Get the active class. |
||
223 | * |
||
224 | * @param string $class |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | 3 | public function activeClass($class = 'active') |
|
232 | |||
233 | /** |
||
234 | * Get the sub-items class. |
||
235 | * |
||
236 | * @param string $class |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | 3 | public function childrenClass($class = 'treeview') |
|
244 | |||
245 | /* ----------------------------------------------------------------- |
||
246 | | Main Methods |
||
247 | | ----------------------------------------------------------------- |
||
248 | */ |
||
249 | /** |
||
250 | * Make the item. |
||
251 | * |
||
252 | * @param string $name |
||
253 | * @param string $title |
||
254 | * @param string $url |
||
255 | * @param string|null $icon |
||
256 | * |
||
257 | * @return self |
||
258 | */ |
||
259 | 21 | public static function make($name, $title, $url, $icon = null) |
|
263 | |||
264 | /** |
||
265 | * Make a Sidebar item from array. |
||
266 | * |
||
267 | * @param array $array |
||
268 | * |
||
269 | * @return self |
||
270 | */ |
||
271 | 12 | public static function makeFromArray(array $array) |
|
282 | |||
283 | /** |
||
284 | * Get url from array. |
||
285 | * |
||
286 | * @param array $array |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | 12 | private static function getUrlFromArray(array $array) |
|
296 | |||
297 | /** |
||
298 | * Add children to the parent. |
||
299 | * |
||
300 | * @param array $children |
||
301 | * |
||
302 | * @return self |
||
303 | */ |
||
304 | 12 | public function addChildren(array $children) |
|
312 | |||
313 | /** |
||
314 | * Add a sub-item to the parent. |
||
315 | * |
||
316 | * @param array $child |
||
317 | * |
||
318 | * @return self |
||
319 | */ |
||
320 | 6 | public function addChild(array $child) |
|
329 | |||
330 | /* ----------------------------------------------------------------- |
||
331 | | Check Methods |
||
332 | | ----------------------------------------------------------------- |
||
333 | */ |
||
334 | |||
335 | /** |
||
336 | * Check if the item is active one. |
||
337 | * |
||
338 | * @return bool |
||
339 | */ |
||
340 | 24 | public function isActive() |
|
344 | |||
345 | /** |
||
346 | * Check if the item has children. |
||
347 | * |
||
348 | * @return bool |
||
349 | */ |
||
350 | 9 | public function hasChildren() |
|
354 | |||
355 | /** |
||
356 | * Check the user is allowed to see this item. |
||
357 | * |
||
358 | * @return bool |
||
359 | */ |
||
360 | 12 | public function allowed() |
|
385 | |||
386 | /** |
||
387 | * Check if the item has roles. |
||
388 | * |
||
389 | * @return bool |
||
390 | */ |
||
391 | 9 | public function hasRoles() |
|
395 | |||
396 | /** |
||
397 | * Check if the item has permissions. |
||
398 | * |
||
399 | * @return bool |
||
400 | */ |
||
401 | 6 | public function hasPermissions() |
|
405 | |||
406 | /* ----------------------------------------------------------------- |
||
407 | | Other Methods |
||
408 | | ----------------------------------------------------------------- |
||
409 | */ |
||
410 | |||
411 | /** |
||
412 | * Get the instance as an array. |
||
413 | * |
||
414 | * @return array |
||
415 | */ |
||
416 | 12 | public function toArray() |
|
429 | |||
430 | /** |
||
431 | * Convert the object to its JSON representation. |
||
432 | * |
||
433 | * @param int $options |
||
434 | * |
||
435 | * @return string |
||
436 | */ |
||
437 | 3 | public function toJson($options = 0) |
|
441 | |||
442 | /** |
||
443 | * Convert the object into something JSON serializable. |
||
444 | * |
||
445 | * @return array |
||
446 | */ |
||
447 | 6 | public function jsonSerialize() |
|
451 | } |
||
452 |