Complex classes like UserRelationTrait 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 UserRelationTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | trait UserRelationTrait |
||
43 | { |
||
44 | use mb, |
||
45 | MutualTrait { |
||
46 | mb::addBlame as addGroup; |
||
47 | mb::createBlame as createGroup; |
||
48 | mb::addOrCreateBlame as addOrCreateGroup; |
||
49 | mb::removeBlame as removeGroup; |
||
50 | mb::removeAllBlames as removeAllGroups; |
||
51 | mb::getBlame as getGroup; |
||
52 | mb::getOrCreateBlame as getOrCreateGroup; |
||
53 | mb::getBlameds as getGroupMembers; |
||
54 | mb::getBlameGuids as getGroupGuids; |
||
55 | mb::setBlameGuids as setGroupGuids; |
||
56 | mb::getAllBlames as getAllGroups; |
||
57 | mb::getNonBlameds as getNonGroupMembers; |
||
58 | mb::getBlamesCount as getGroupsCount; |
||
59 | mb::getMultipleBlameableAttributeRules as getGroupsRules; |
||
60 | mb::getEmptyBlamesJson as getEmptyGroupJson; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | public $remarkAttribute = 'remark'; |
||
67 | public static $relationSingle = 0; |
||
68 | public static $relationMutual = 1; |
||
69 | public $relationType = 1; |
||
70 | public $relationTypes = [ |
||
71 | 0 => 'Single', |
||
72 | 1 => 'Mutual', |
||
73 | ]; |
||
74 | |||
75 | /** |
||
76 | * @var string the attribute name of which determines the relation type. |
||
77 | */ |
||
78 | public $mutualTypeAttribute = 'type'; |
||
79 | public static $mutualTypeNormal = 0x00; |
||
80 | public static $mutualTypeSuspend = 0x01; |
||
81 | |||
82 | /** |
||
83 | * @var array Mutual types. |
||
84 | */ |
||
85 | public static $mutualTypes = [ |
||
86 | 0x00 => 'Normal', |
||
87 | 0x01 => 'Suspend', |
||
88 | ]; |
||
89 | |||
90 | /** |
||
91 | * @var string the attribute name of which determines the `favorite` field. |
||
92 | */ |
||
93 | public $favoriteAttribute = 'favorite'; |
||
94 | |||
95 | /** |
||
96 | * Permit to build self relation. |
||
97 | * @var boolean |
||
98 | */ |
||
99 | public $relationSelf = false; |
||
100 | |||
101 | /** |
||
102 | * Get whether this relation is favorite or not. |
||
103 | * @return boolean |
||
104 | */ |
||
105 | 1 | public function getIsFavorite() |
|
110 | |||
111 | /** |
||
112 | * Set favorite. |
||
113 | * @param boolean $fav |
||
114 | */ |
||
115 | 1 | public function setIsFavorite($fav) |
|
120 | |||
121 | /** |
||
122 | * @inheritdoc |
||
123 | */ |
||
124 | 10 | public function rules() |
|
128 | |||
129 | /** |
||
130 | * Validation rules associated with user relation. |
||
131 | * @return array rules. |
||
132 | */ |
||
133 | 10 | public function getUserRelationRules() |
|
144 | |||
145 | /** |
||
146 | * Get remark. |
||
147 | * @return string remark. |
||
148 | */ |
||
149 | public function getRemark() |
||
154 | |||
155 | /** |
||
156 | * Set remark. |
||
157 | * @param string $remark |
||
158 | * @return string remark. |
||
159 | */ |
||
160 | public function setRemark($remark) |
||
165 | |||
166 | /** |
||
167 | * Validation rules associated with remark attribute. |
||
168 | * @return array rules. |
||
169 | */ |
||
170 | 10 | public function getRemarkRules() |
|
177 | |||
178 | /** |
||
179 | * Validation rules associated with favorites attribute. |
||
180 | * @return array rules. |
||
181 | */ |
||
182 | 10 | public function getFavoriteRules() |
|
189 | |||
190 | /** |
||
191 | * Validation rules associated with other guid attribute. |
||
192 | * @return array rules. |
||
193 | */ |
||
194 | 10 | public function getOtherGuidRules() |
|
201 | |||
202 | /** |
||
203 | * Attach events associated with user relation. |
||
204 | */ |
||
205 | 10 | public function initUserRelationEvents() |
|
215 | |||
216 | /** |
||
217 | * Get opposite relation against self. |
||
218 | * @return static |
||
219 | */ |
||
220 | 8 | public function getOpposite() |
|
229 | |||
230 | /** |
||
231 | * Check whether the initiator is followed by recipient. |
||
232 | * @param BaseUserModel $initiator |
||
233 | * @param BaseUserModel $recipient |
||
234 | * @return boolean |
||
235 | */ |
||
236 | 2 | public static function isFollowed($initiator, $recipient) |
|
240 | |||
241 | /** |
||
242 | * Check whether the initiator is following recipient. |
||
243 | * @param BaseUserModel $initiator |
||
244 | * @param BaseUserModel $recipient |
||
245 | * @return boolean |
||
246 | */ |
||
247 | 2 | public static function isFollowing($initiator, $recipient) |
|
251 | |||
252 | /** |
||
253 | * Check whether the initiator is following and followed by recipient mutually (Single Relation). |
||
254 | * Or check whether the initiator and recipient are friend whatever the mutual type is normal or suspend. |
||
255 | * @param BaseUserModel $initiator |
||
256 | * @param BaseUserModel $recipient |
||
257 | * @return boolean |
||
258 | */ |
||
259 | 2 | public static function isMutual($initiator, $recipient) |
|
263 | |||
264 | /** |
||
265 | * Check whether the initiator is following and followed by recipient mutually (Single Relation). |
||
266 | * Or check whether the initiator and recipient are friend if the mutual type is normal. |
||
267 | * @param BaseUserModel $initiator |
||
268 | * @param BaseUserModel $recipient |
||
269 | * @return boolean |
||
270 | */ |
||
271 | 2 | public static function isFriend($initiator, $recipient) |
|
286 | |||
287 | /** |
||
288 | * Build new or return existed suspend mutual relation, of return null if |
||
289 | * current type is not mutual. |
||
290 | * @see buildRelation() |
||
291 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
292 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
293 | * @return static The relation will be |
||
294 | * given if exists, or return a new relation. |
||
295 | */ |
||
296 | 3 | public static function buildSuspendRelation($user, $other) |
|
306 | |||
307 | /** |
||
308 | * Build new or return existed normal relation. |
||
309 | * The status of mutual relation will be changed to normal if it is not. |
||
310 | * @see buildRelation() |
||
311 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
312 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
313 | * @return static The relation will be |
||
314 | * given if exists, or return a new relation. |
||
315 | */ |
||
316 | 10 | public static function buildNormalRelation($user, $other) |
|
325 | |||
326 | /** |
||
327 | * Build new or return existed relation between initiator and recipient. |
||
328 | * If relation between initiator and recipient is not found, new relation will |
||
329 | * be built. If initiator and recipient are the same one and it is not allowed |
||
330 | * to build self relation, null will be given. |
||
331 | * If you want to know whether the relation exists, you can check the return |
||
332 | * value of `getIsNewRecord()` method. |
||
333 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
334 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
335 | * @return static The relation will be |
||
336 | * given if exists, or return a new relation. Or return null if not allowed |
||
337 | * to build self relation, |
||
338 | */ |
||
339 | 10 | protected static function buildRelation($user, $other) |
|
362 | |||
363 | /** |
||
364 | * Build opposite relation throughout the current relation. The opposite |
||
365 | * relation will be given if existed. |
||
366 | * @param static $relation |
||
367 | * @return static |
||
368 | */ |
||
369 | 8 | protected static function buildOppositeRelation($relation) |
|
385 | |||
386 | /** |
||
387 | * Remove myself. |
||
388 | * @return integer|false The number of relations removed, or false if the remove |
||
389 | * is unsuccessful for some reason. Note that it is possible the number of relations |
||
390 | * removed is 0, even though the remove execution is successful. |
||
391 | */ |
||
392 | 2 | public function remove() |
|
396 | |||
397 | /** |
||
398 | * Remove first relation between initiator(s) and recipient(s). |
||
399 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
400 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
401 | * @return integer|false The number of relations removed. |
||
402 | */ |
||
403 | 1 | public static function removeOneRelation($user, $other) |
|
407 | |||
408 | /** |
||
409 | * Remove all relations between initiator(s) and recipient(s). |
||
410 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
411 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
412 | * @return integer The number of relations removed. |
||
413 | */ |
||
414 | 2 | public static function removeAllRelations($user, $other) |
|
421 | |||
422 | /** |
||
423 | * Get first relation between initiator(s) and recipient(s). |
||
424 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
425 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
426 | * @return static |
||
427 | */ |
||
428 | 1 | public static function findOneRelation($user, $other) |
|
432 | |||
433 | /** |
||
434 | * Get first opposite relation between initiator(s) and recipient(s). |
||
435 | * @param BaseUserModel|string $user Initiator or its guid, or array of them. |
||
436 | * @param BaseUserModel|string $other Recipient or its guid, or array of them. |
||
437 | * @return static |
||
438 | */ |
||
439 | 8 | public static function findOneOppositeRelation($user, $other) |
|
443 | |||
444 | /** |
||
445 | * Get user's or users' all relations, or by specified groups. |
||
446 | * @param BaseUserModel|string|array $user Initiator or its GUID, or Initiators or their GUIDs. |
||
447 | * @param BaseUserRelationGroupModel|string|array|null $groups UserRelationGroup |
||
448 | * or its guid, or array of them. If you do not want to delimit the groups, please assign null. |
||
449 | * @return array all eligible relations |
||
450 | */ |
||
451 | public static function findOnesAllRelations($user, $groups = null) |
||
455 | |||
456 | /** |
||
457 | * Initialize groups attribute. |
||
458 | * @param ModelEvent $event |
||
459 | */ |
||
460 | 10 | public function onInitGroups($event) |
|
465 | |||
466 | /** |
||
467 | * Initialize remark attribute. |
||
468 | * @param ModelEvent $event |
||
469 | */ |
||
470 | 10 | public function onInitRemark($event) |
|
476 | |||
477 | /** |
||
478 | * The event triggered after insert new relation. |
||
479 | * The opposite relation should be inserted without triggering events |
||
480 | * simultaneously after new relation inserted, |
||
481 | * @param ModelEvent $event |
||
482 | */ |
||
483 | 10 | public function onInsertRelation($event) |
|
495 | |||
496 | /** |
||
497 | * The event triggered after update relation. |
||
498 | * The opposite relation should be updated without triggering events |
||
499 | * simultaneously after existed relation removed. |
||
500 | * @param ModelEvent $event |
||
501 | */ |
||
502 | 4 | public function onUpdateRelation($event) |
|
514 | |||
515 | /** |
||
516 | * The event triggered after delete relation. |
||
517 | * The opposite relation should be deleted without triggering events |
||
518 | * simultaneously after existed relation removed. |
||
519 | * @param ModelEvent $event |
||
520 | */ |
||
521 | 3 | public function onDeleteRelation($event) |
|
532 | } |
||
533 |
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.