Complex classes like User 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 User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class User implements UserInterface |
||
| 19 | { |
||
| 20 | use FromArray; |
||
| 21 | |||
| 22 | private $id; |
||
| 23 | private $name; |
||
| 24 | private $language; |
||
| 25 | private $email; |
||
| 26 | private $emailVerifiedAt; |
||
| 27 | private $password; |
||
| 28 | private $rememberToken; |
||
| 29 | private $createdAt; |
||
| 30 | private $updatedAt; |
||
| 31 | private $permissions = []; |
||
| 32 | private $roles = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @return int|null |
||
| 36 | */ |
||
| 37 | 16 | public function getId(): ?int |
|
| 41 | |||
| 42 | /** |
||
| 43 | * @param int $id |
||
| 44 | * @return User |
||
| 45 | */ |
||
| 46 | 16 | public function setId(int $id): self |
|
| 52 | |||
| 53 | /** |
||
| 54 | * @return string |
||
| 55 | */ |
||
| 56 | 16 | public function getName(): string |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $name |
||
| 63 | * @return User |
||
| 64 | */ |
||
| 65 | 18 | public function setName(string $name): self |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @return string |
||
| 74 | */ |
||
| 75 | 16 | public function getLanguage(): string |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @param string $language |
||
| 82 | * @return User |
||
| 83 | */ |
||
| 84 | 18 | public function setLanguage(string $language): self |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | 16 | public function getEmail(): string |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @param string $email |
||
| 101 | * @return User |
||
| 102 | */ |
||
| 103 | 18 | public function setEmail(string $email): self |
|
| 109 | |||
| 110 | /** |
||
| 111 | * @return string|null |
||
| 112 | */ |
||
| 113 | 16 | public function getEmailVerifiedAt(): ?string |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @param string|null $emailVerifiedAt |
||
| 120 | * @return User |
||
| 121 | */ |
||
| 122 | 2 | public function setEmailVerifiedAt(?string $emailVerifiedAt): self |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | 16 | public function getPassword(): string |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $password |
||
| 139 | * @return User |
||
| 140 | */ |
||
| 141 | 18 | public function setPassword(string $password): self |
|
| 147 | |||
| 148 | |||
| 149 | /** |
||
| 150 | * @return string|null |
||
| 151 | */ |
||
| 152 | 16 | public function getRememberToken(): ?string |
|
| 156 | |||
| 157 | /** |
||
| 158 | * @param string|null $rememberToken |
||
| 159 | * @return User |
||
| 160 | */ |
||
| 161 | 2 | public function setRememberToken(?string $rememberToken): self |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @return string |
||
| 170 | */ |
||
| 171 | 6 | public function getCreatedAt(): string |
|
| 175 | |||
| 176 | /** |
||
| 177 | * @param string $createdAt |
||
| 178 | * @return User |
||
| 179 | */ |
||
| 180 | 16 | public function setCreatedAt(string $createdAt): self |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | 6 | public function getUpdatedAt(): string |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @param string $updatedAt |
||
| 197 | * @return User |
||
| 198 | */ |
||
| 199 | 16 | public function setUpdatedAt(string $updatedAt): self |
|
| 205 | |||
| 206 | /** |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | 17 | public function getPermissions(): array |
|
| 213 | |||
| 214 | /** |
||
| 215 | * @return array |
||
| 216 | */ |
||
| 217 | 16 | public function getRoles(): array |
|
| 221 | |||
| 222 | /** |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | 16 | public function getPermissionsIds(): array |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | 16 | public function getRolesIds(): array |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @param GetAllInterface $all |
||
| 244 | * @return mixed |
||
| 245 | * @throws \Illuminate\Contracts\Container\BindingResolutionException |
||
| 246 | */ |
||
| 247 | 1 | public static function all(GetAllInterface $all) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @param int $id |
||
| 254 | * @return User |
||
| 255 | * @throws \Illuminate\Contracts\Container\BindingResolutionException |
||
| 256 | */ |
||
| 257 | 3 | public static function find(int $id): self |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @return bool |
||
| 264 | * @throws \Illuminate\Contracts\Container\BindingResolutionException |
||
| 265 | */ |
||
| 266 | 16 | public function save(): bool |
|
| 270 | |||
| 271 | /** |
||
| 272 | * @return bool |
||
| 273 | * @throws \Illuminate\Contracts\Container\BindingResolutionException |
||
| 274 | */ |
||
| 275 | 2 | public function delete(): bool |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @param PermissionInterface $permission |
||
| 282 | * @return User |
||
| 283 | * @throws PermissionIsNotSavedException |
||
| 284 | */ |
||
| 285 | public function assignPermission(PermissionInterface $permission): self |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param array $permissions |
||
| 295 | * @return User |
||
| 296 | * @throws PermissionIsNotSavedException |
||
| 297 | */ |
||
| 298 | 7 | public function assignPermissions(array $permissions): self |
|
| 305 | |||
| 306 | /** |
||
| 307 | * @param PermissionInterface $permission |
||
| 308 | * @return User |
||
| 309 | * @throws PermissionIsNotSavedException |
||
| 310 | */ |
||
| 311 | public function removePermission(PermissionInterface $permission): self |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param array $permissions |
||
| 321 | * @return User |
||
| 322 | * @throws PermissionIsNotSavedException |
||
| 323 | */ |
||
| 324 | 2 | public function removePermissions(array $permissions): self |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @param RoleInterface $role |
||
| 334 | * @return User |
||
| 335 | * @throws RoleIsNotSavedException |
||
| 336 | */ |
||
| 337 | 6 | public function assignRole(RoleInterface $role): self |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @param array $roles |
||
| 352 | * @return User |
||
| 353 | * @throws RoleIsNotSavedException |
||
| 354 | */ |
||
| 355 | 7 | public function assignRoles(array $roles): self |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @param RoleInterface $role |
||
| 368 | * @return User |
||
| 369 | * @throws RoleIsNotSavedException |
||
| 370 | */ |
||
| 371 | 2 | public function removeRole(RoleInterface $role): self |
|
| 386 | |||
| 387 | /** |
||
| 388 | * @param array $roles |
||
| 389 | * @return User |
||
| 390 | * @throws RoleIsNotSavedException |
||
| 391 | */ |
||
| 392 | 2 | public function removeRoles(array $roles): self |
|
| 402 | } |
||
| 403 |