Complex classes like UserRelationship 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 UserRelationship, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class UserRelationship implements RelationshipInterface |
||
31 | { |
||
32 | use RelationshipTrait; |
||
33 | |||
34 | /** |
||
35 | * @var Organization |
||
36 | */ |
||
37 | private $organization; |
||
38 | |||
39 | /** |
||
40 | * @param Organization $organization |
||
41 | */ |
||
42 | public function __construct(Organization $organization) |
||
46 | |||
47 | |||
48 | /************************************************************ |
||
49 | * COLLECTION |
||
50 | ************************************************************/ |
||
51 | |||
52 | /** |
||
53 | * Get a collection of associated users |
||
54 | * |
||
55 | * @return User[]|Collection |
||
56 | */ |
||
57 | public function getCollection(): Collection |
||
68 | |||
69 | /** |
||
70 | * @return Collection |
||
71 | */ |
||
72 | protected function createCollectionFromRelations() |
||
97 | |||
98 | /** |
||
99 | * @return Collection |
||
100 | */ |
||
101 | protected function existingRelationships(): Collection |
||
109 | |||
110 | /************************************************************ |
||
111 | * QUERY |
||
112 | ************************************************************/ |
||
113 | |||
114 | /** |
||
115 | * @return UserQuery |
||
116 | */ |
||
117 | private function elementQuery(): UserQuery |
||
124 | |||
125 | /** |
||
126 | * @return UserAssociationQuery |
||
127 | */ |
||
128 | private function associationQuery(): UserAssociationQuery |
||
138 | |||
139 | /************************************************************ |
||
140 | * CREATE |
||
141 | ************************************************************/ |
||
142 | |||
143 | /** |
||
144 | * @param $object |
||
145 | * @return UserAssociation |
||
146 | */ |
||
147 | protected function create($object): UserAssociation |
||
157 | |||
158 | |||
159 | /******************************************* |
||
160 | * DELTA |
||
161 | *******************************************/ |
||
162 | |||
163 | /** |
||
164 | * @inheritDoc |
||
165 | */ |
||
166 | protected function delta(): array |
||
197 | |||
198 | /** |
||
199 | * @param UserAssociation $new |
||
200 | * @param UserAssociation $existing |
||
201 | * @return bool |
||
202 | */ |
||
203 | private function hasChanged(UserAssociation $new, UserAssociation $existing): bool |
||
211 | |||
212 | /** |
||
213 | * @param UserAssociation $from |
||
214 | * @param UserAssociation $to |
||
215 | * |
||
216 | * @return UserAssociation |
||
217 | */ |
||
218 | private function sync(UserAssociation $to, UserAssociation $from): UserAssociation |
||
233 | |||
234 | |||
235 | /******************************************* |
||
236 | * COLLECTION UTILS |
||
237 | *******************************************/ |
||
238 | |||
239 | /** |
||
240 | * @inheritDoc |
||
241 | */ |
||
242 | protected function insertCollection(Collection $collection, UserAssociation $association) |
||
251 | |||
252 | /** |
||
253 | * @inheritDoc |
||
254 | */ |
||
255 | protected function updateCollection(Collection $collection, UserAssociation $association) |
||
267 | |||
268 | |||
269 | /******************************************* |
||
270 | * UTILS |
||
271 | *******************************************/ |
||
272 | |||
273 | /** |
||
274 | * @param UserAssociation|User|int|array|null $object |
||
275 | * @return int|null |
||
276 | */ |
||
277 | protected function findKey($object = null) |
||
294 | |||
295 | /** |
||
296 | * @param $identifier |
||
297 | * @return int|string|null |
||
298 | */ |
||
299 | private function findRelationshipKey($identifier) |
||
310 | |||
311 | /** |
||
312 | * @param UserAssociation|User|int|array|null $user |
||
313 | * @return User|null |
||
314 | */ |
||
315 | protected function resolveObjectInternal($user) |
||
335 | } |
||
336 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: