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; |
||
| 15 | class Item implements Arrayable, Jsonable, JsonSerializable |
||
| 16 | { |
||
| 17 | /* ----------------------------------------------------------------- |
||
| 18 | | Properties |
||
| 19 | | ----------------------------------------------------------------- |
||
| 20 | */ |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The item name. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $name; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The item title. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $title; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The item url. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $url; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The item icon. |
||
| 45 | * |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | protected $icon; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The item active state. |
||
| 52 | * |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | protected $active = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The item roles. |
||
| 59 | * |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $roles = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The item permissions. |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $permissions = []; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The item children (sub-items). |
||
| 73 | * |
||
| 74 | * @var \Arcanesoft\Sidebar\Entities\ItemCollection |
||
| 75 | */ |
||
| 76 | protected $children; |
||
| 77 | |||
| 78 | /* ----------------------------------------------------------------- |
||
| 79 | | Constructor |
||
| 80 | | ----------------------------------------------------------------- |
||
| 81 | */ |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Item constructor. |
||
| 85 | * |
||
| 86 | * @param string $name |
||
| 87 | * @param string $title |
||
| 88 | * @param string $url |
||
| 89 | * @param string|null $icon |
||
| 90 | */ |
||
| 91 | 42 | public function __construct($name, $title, $url, $icon = null) |
|
| 100 | |||
| 101 | /* ----------------------------------------------------------------- |
||
| 102 | | Getters & Setters |
||
| 103 | | ----------------------------------------------------------------- |
||
| 104 | */ |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Get the item name. |
||
| 108 | * |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | 18 | public function name() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Get the item title. |
||
| 118 | * |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | 27 | public function title() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Get the item url. |
||
| 131 | * |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | 21 | public function url() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Get the item icon. |
||
| 141 | * |
||
| 142 | * @return string|null |
||
| 143 | */ |
||
| 144 | 24 | public function icon() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Set the current name. |
||
| 151 | * |
||
| 152 | * @param string $name |
||
| 153 | * |
||
| 154 | * @return self |
||
| 155 | */ |
||
| 156 | 9 | public function setCurrent($name) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Get the roles. |
||
| 166 | * |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | 12 | public function getRoles() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Set the roles. |
||
| 176 | * |
||
| 177 | * @param array $roles |
||
| 178 | * |
||
| 179 | * @return self |
||
| 180 | */ |
||
| 181 | 33 | public function setRoles(array $roles) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Get the permissions. |
||
| 190 | * |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | 12 | public function getPermissions() |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Set the permissions. |
||
| 200 | * |
||
| 201 | * @param array $permissions |
||
| 202 | * |
||
| 203 | * @return self |
||
| 204 | */ |
||
| 205 | 33 | public function setPermissions(array $permissions) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Get the sub-items. |
||
| 214 | * |
||
| 215 | * @return \Arcanesoft\Sidebar\Entities\ItemCollection |
||
| 216 | */ |
||
| 217 | 15 | public function children() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Get the active class. |
||
| 224 | * |
||
| 225 | * @param string $class |
||
| 226 | * |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | 6 | public function activeClass($class = 'active') |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Get the sub-items class. |
||
| 236 | * |
||
| 237 | * @param string $class |
||
| 238 | * |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | 6 | public function childrenClass($class = 'treeview') |
|
| 245 | |||
| 246 | /* ----------------------------------------------------------------- |
||
| 247 | | Main Methods |
||
| 248 | | ----------------------------------------------------------------- |
||
| 249 | */ |
||
| 250 | /** |
||
| 251 | * Make the item. |
||
| 252 | * |
||
| 253 | * @param string $name |
||
| 254 | * @param string $title |
||
| 255 | * @param string $url |
||
| 256 | * @param string|null $icon |
||
| 257 | * |
||
| 258 | * @return self |
||
| 259 | */ |
||
| 260 | 27 | public static function make($name, $title, $url, $icon = null) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Make a Sidebar item from array. |
||
| 267 | * |
||
| 268 | * @param array $array |
||
| 269 | * |
||
| 270 | * @return self |
||
| 271 | */ |
||
| 272 | 18 | public static function makeFromArray(array $array) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Get url from array. |
||
| 286 | * |
||
| 287 | * @param array $array |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | 18 | private static function getUrlFromArray(array $array) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Add children to the parent. |
||
| 300 | * |
||
| 301 | * @param array $children |
||
| 302 | * |
||
| 303 | * @return self |
||
| 304 | */ |
||
| 305 | 18 | public function addChildren(array $children) |
|
| 313 | |||
| 314 | /** |
||
| 315 | * Add a sub-item to the parent. |
||
| 316 | * |
||
| 317 | * @param array $child |
||
| 318 | * |
||
| 319 | * @return self |
||
| 320 | */ |
||
| 321 | 12 | public function addChild(array $child) |
|
| 330 | |||
| 331 | /* ----------------------------------------------------------------- |
||
| 332 | | Check Methods |
||
| 333 | | ----------------------------------------------------------------- |
||
| 334 | */ |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Check if the item is active one. |
||
| 338 | * |
||
| 339 | * @return bool |
||
| 340 | */ |
||
| 341 | 27 | public function isActive() |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Check if the item has children. |
||
| 348 | * |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | 12 | public function hasChildren() |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Check the user is allowed to see this item. |
||
| 358 | * |
||
| 359 | * @return bool |
||
| 360 | */ |
||
| 361 | 18 | public function allowed() |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Check if the item has roles. |
||
| 377 | * |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | 9 | public function hasRoles() |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Check if the item has permissions. |
||
| 387 | * |
||
| 388 | * @return bool |
||
| 389 | */ |
||
| 390 | 6 | public function hasPermissions() |
|
| 394 | |||
| 395 | /* ----------------------------------------------------------------- |
||
| 396 | | Other Methods |
||
| 397 | | ----------------------------------------------------------------- |
||
| 398 | */ |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Get the instance as an array. |
||
| 402 | * |
||
| 403 | * @return array |
||
| 404 | */ |
||
| 405 | 12 | public function toArray() |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Convert the object to its JSON representation. |
||
| 421 | * |
||
| 422 | * @param int $options |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | 3 | public function toJson($options = 0) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Convert the object into something JSON serializable. |
||
| 433 | * |
||
| 434 | * @return array |
||
| 435 | */ |
||
| 436 | 6 | public function jsonSerialize() |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Check if the user has role. |
||
| 443 | * |
||
| 444 | * @param \Arcanesoft\Contracts\Auth\Models\User $user |
||
| 445 | * |
||
| 446 | * @return bool |
||
| 447 | */ |
||
| 448 | 3 | private function checkRoles(User $user) |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Check if the user has permission. |
||
| 459 | * |
||
| 460 | * @param \Arcanesoft\Contracts\Auth\Models\User $user |
||
| 461 | * |
||
| 462 | * @return bool |
||
| 463 | */ |
||
| 464 | 3 | private function checkPermission(User $user) |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Check if has an allowed child. |
||
| 475 | * |
||
| 476 | * @return bool |
||
| 477 | */ |
||
| 478 | private function hasAllowedChild() |
||
| 484 | } |
||
| 485 |