Complex classes like UsersAttributeTrait 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 UsersAttributeTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | trait UsersAttributeTrait |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var UserQuery |
||
| 28 | */ |
||
| 29 | private $users; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param array $sourceElements |
||
| 33 | * @return array |
||
| 34 | */ |
||
| 35 | private static function eagerLoadingUsersMap(array $sourceElements) |
||
| 51 | |||
| 52 | /************************************************************ |
||
| 53 | * REQUEST |
||
| 54 | ************************************************************/ |
||
| 55 | |||
| 56 | /** |
||
| 57 | * AssociateUserToOrganization an array of users from request input |
||
| 58 | * |
||
| 59 | * @param string $identifier |
||
| 60 | * @return $this |
||
| 61 | */ |
||
| 62 | public function setUsersFromRequest(string $identifier = 'users') |
||
| 71 | |||
| 72 | /************************************************************ |
||
| 73 | * USERS |
||
| 74 | ************************************************************/ |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param array $criteria |
||
| 78 | * @return UserQuery |
||
| 79 | */ |
||
| 80 | public function userQuery($criteria = []): UserQuery |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get an array of users associated to an organization |
||
| 102 | * |
||
| 103 | * @param array $criteria |
||
| 104 | * @return UserQuery |
||
| 105 | */ |
||
| 106 | public function getUsers($criteria = []) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * AssociateUserToOrganization users to an organization |
||
| 124 | * |
||
| 125 | * @param $users |
||
| 126 | * @return $this |
||
| 127 | */ |
||
| 128 | public function setUsers($users) |
||
| 145 | |||
| 146 | /** |
||
| 147 | * AssociateUserToOrganization an array of users to an organization |
||
| 148 | * |
||
| 149 | * @param $users |
||
| 150 | * @return $this |
||
| 151 | */ |
||
| 152 | public function addUsers(array $users) |
||
| 170 | |||
| 171 | protected function resolveUser($user) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * AssociateUserToOrganization a user to an organization |
||
| 197 | * |
||
| 198 | * @param User $user |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | public function addUser(User $user) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * DissociateUserFromOrganization a user from an organization |
||
| 222 | * |
||
| 223 | * @param array $users |
||
| 224 | * @return $this |
||
| 225 | */ |
||
| 226 | public function removeUsers(array $users) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * DissociateUserFromOrganization a user from an organization |
||
| 247 | * |
||
| 248 | * @param User $user |
||
| 249 | * @return $this |
||
| 250 | */ |
||
| 251 | public function removeUser(User $user) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Reset users |
||
| 272 | * |
||
| 273 | * @return $this |
||
| 274 | */ |
||
| 275 | public function resetUsers() |
||
| 280 | |||
| 281 | |||
| 282 | /******************************************* |
||
| 283 | * ASSOCIATE and/or DISASSOCIATE |
||
| 284 | *******************************************/ |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return bool |
||
| 288 | * @throws \Throwable |
||
| 289 | * @throws \yii\db\StaleObjectException |
||
| 290 | */ |
||
| 291 | public function saveUsers() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @param UserQuery $query |
||
| 351 | * @return bool |
||
| 352 | * @throws \Throwable |
||
| 353 | */ |
||
| 354 | public function associateUsers(UserQuery $query) |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @param UserQuery $query |
||
| 391 | * @return bool |
||
| 392 | * @throws \Throwable |
||
| 393 | */ |
||
| 394 | public function dissociateUsers(UserQuery $query) |
||
| 427 | } |
||
| 428 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.