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') |
||
70 | |||
71 | /************************************************************ |
||
72 | * USERS |
||
73 | ************************************************************/ |
||
74 | |||
75 | /** |
||
76 | * @param array $criteria |
||
77 | * @return UserQuery |
||
78 | */ |
||
79 | public function userQuery($criteria = []): UserQuery |
||
98 | |||
99 | /** |
||
100 | * Get an array of users associated to an organization |
||
101 | * |
||
102 | * @param array $criteria |
||
103 | * @return UserQuery |
||
104 | */ |
||
105 | public function getUsers($criteria = []) |
||
120 | |||
121 | /** |
||
122 | * AssociateUserToOrganization users to an organization |
||
123 | * |
||
124 | * @param $users |
||
125 | * @return $this |
||
126 | */ |
||
127 | public function setUsers($users) |
||
144 | |||
145 | /** |
||
146 | * AssociateUserToOrganization an array of users to an organization |
||
147 | * |
||
148 | * @param $users |
||
149 | * @return $this |
||
150 | */ |
||
151 | public function addUsers(array $users) |
||
169 | |||
170 | protected function resolveUser($user) |
||
193 | |||
194 | /** |
||
195 | * AssociateUserToOrganization a user to an organization |
||
196 | * |
||
197 | * @param User $user |
||
198 | * @return $this |
||
199 | */ |
||
200 | public function addUser(User $user) |
||
218 | |||
219 | /** |
||
220 | * DissociateUserFromOrganization a user from an organization |
||
221 | * |
||
222 | * @param array $users |
||
223 | * @return $this |
||
224 | */ |
||
225 | public function removeUsers(array $users) |
||
243 | |||
244 | /** |
||
245 | * DissociateUserFromOrganization a user from an organization |
||
246 | * |
||
247 | * @param User $user |
||
248 | * @return $this |
||
249 | */ |
||
250 | public function removeUser(User $user) |
||
268 | |||
269 | /** |
||
270 | * Reset users |
||
271 | * |
||
272 | * @return $this |
||
273 | */ |
||
274 | public function resetUsers() |
||
279 | |||
280 | |||
281 | /******************************************* |
||
282 | * ASSOCIATE and/or DISASSOCIATE |
||
283 | *******************************************/ |
||
284 | |||
285 | /** |
||
286 | * @return bool |
||
287 | * @throws \Throwable |
||
288 | * @throws \yii\db\StaleObjectException |
||
289 | */ |
||
290 | public function saveUsers() |
||
338 | |||
339 | /** |
||
340 | * @param UserQuery $query |
||
341 | * @return bool |
||
342 | * @throws \Throwable |
||
343 | */ |
||
344 | public function associateUsers(UserQuery $query) |
||
378 | |||
379 | /** |
||
380 | * @param UserQuery $query |
||
381 | * @return bool |
||
382 | * @throws \Throwable |
||
383 | */ |
||
384 | public function dissociateUsers(UserQuery $query) |
||
417 | } |
||
418 |
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
Idable
provides a methodequalsId
that 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.