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 |
||
52 | trait UserRelationTrait |
||
53 | { |
||
54 | use mb, |
||
55 | MutualTrait { |
||
56 | mb::addBlame as addGroup; |
||
57 | mb::createBlame as createGroup; |
||
58 | mb::addOrCreateBlame as addOrCreateGroup; |
||
59 | mb::removeBlame as removeGroup; |
||
60 | mb::removeAllBlames as removeAllGroups; |
||
61 | mb::getBlame as getGroup; |
||
62 | mb::getOrCreateBlame as getOrCreateGroup; |
||
63 | mb::getBlameds as getGroupMembers; |
||
64 | mb::getBlameGuids as getGroupGuids; |
||
65 | mb::setBlameGuids as setGroupGuids; |
||
66 | mb::getAllBlames as getAllGroups; |
||
67 | mb::getNonBlameds as getNonGroupMembers; |
||
68 | mb::getBlamesCount as getGroupsCount; |
||
69 | mb::getMultipleBlameableAttributeRules as getGroupsRules; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | public $remarkAttribute = 'remark'; |
||
76 | public static $relationSingle = 0; |
||
77 | public static $relationMutual = 1; |
||
78 | public $relationType = 1; |
||
79 | public $relationTypes = [ |
||
80 | 0 => 'Single', |
||
81 | 1 => 'Mutual', |
||
82 | ]; |
||
83 | |||
84 | /** |
||
85 | * @var string the attribute name of which determines the relation type. |
||
86 | */ |
||
87 | public $mutualTypeAttribute = 'type'; |
||
88 | public static $mutualTypeNormal = 0x00; |
||
89 | public static $mutualTypeSuspend = 0x01; |
||
90 | |||
91 | /** |
||
92 | * @var array Mutual types. |
||
93 | */ |
||
94 | public static $mutualTypes = [ |
||
95 | 0x00 => 'Normal', |
||
96 | 0x01 => 'Suspend', |
||
97 | ]; |
||
98 | |||
99 | /** |
||
100 | * @var string the attribute name of which determines the `favorite` field. |
||
101 | */ |
||
102 | public $favoriteAttribute = 'favorite'; |
||
103 | |||
104 | /** |
||
105 | * Permit to build self relation. |
||
106 | * @var boolean |
||
107 | */ |
||
108 | public $relationSelf = false; |
||
109 | |||
110 | /** |
||
111 | * Get whether this relation is favorite or not. |
||
112 | * @return boolean |
||
113 | */ |
||
114 | 1 | public function getIsFavorite() |
|
119 | |||
120 | /** |
||
121 | * Set favorite. |
||
122 | * @param boolean $fav |
||
123 | */ |
||
124 | 1 | public function setIsFavorite($fav) |
|
129 | |||
130 | /** |
||
131 | * @inheritdoc |
||
132 | */ |
||
133 | 18 | public function rules() |
|
137 | |||
138 | /** |
||
139 | * Validation rules associated with user relation. |
||
140 | * @return array rules. |
||
141 | */ |
||
142 | 18 | public function getUserRelationRules() |
|
153 | |||
154 | /** |
||
155 | * Get remark. |
||
156 | * @return string remark. |
||
157 | */ |
||
158 | 1 | public function getRemark() |
|
163 | |||
164 | /** |
||
165 | * Set remark. |
||
166 | * @param string $remark |
||
167 | * @return string remark. |
||
168 | */ |
||
169 | 1 | public function setRemark($remark) |
|
174 | |||
175 | /** |
||
176 | * Validation rules associated with remark attribute. |
||
177 | * @return array rules. |
||
178 | */ |
||
179 | 18 | public function getRemarkRules() |
|
186 | |||
187 | /** |
||
188 | * Validation rules associated with favorites attribute. |
||
189 | * @return array rules. |
||
190 | */ |
||
191 | 18 | public function getFavoriteRules() |
|
198 | |||
199 | /** |
||
200 | * Validation rules associated with other guid attribute. |
||
201 | * @return array rules. |
||
202 | */ |
||
203 | 18 | public function getOtherGuidRules() |
|
210 | |||
211 | /** |
||
212 | * Attach events associated with user relation. |
||
213 | */ |
||
214 | 19 | public function initUserRelationEvents() |
|
224 | |||
225 | /** |
||
226 | * Get opposite relation against self. |
||
227 | * @return static |
||
228 | */ |
||
229 | 1 | public function getOpposite() |
|
236 | |||
237 | /** |
||
238 | * Check whether the initiator is followed by recipient. |
||
239 | * @param BaseUserModel $initiator |
||
240 | * @param BaseUserModel $recipient |
||
241 | * @return boolean |
||
242 | */ |
||
243 | 4 | public static function isFollowed($initiator, $recipient) |
|
247 | |||
248 | /** |
||
249 | * Check whether the initiator is following recipient. |
||
250 | * @param BaseUserModel $initiator |
||
251 | * @param BaseUserModel $recipient |
||
252 | * @return boolean |
||
253 | */ |
||
254 | 4 | public static function isFollowing($initiator, $recipient) |
|
258 | |||
259 | /** |
||
260 | * Check whether the initiator is following and followed by recipient mutually (Single Relation). |
||
261 | * Or check whether the initiator and recipient are friend whatever the mutual type is normal or suspend. |
||
262 | * @param BaseUserModel $initiator |
||
263 | * @param BaseUserModel $recipient |
||
264 | * @return boolean |
||
265 | */ |
||
266 | 3 | public static function isMutual($initiator, $recipient) |
|
270 | |||
271 | /** |
||
272 | * Check whether the initiator is following and followed by recipient mutually (Single Relation). |
||
273 | * Or check whether the initiator and recipient are friend if the mutual type is normal. |
||
274 | * @param BaseUserModel $initiator |
||
275 | * @param BaseUserModel $recipient |
||
276 | * @return boolean |
||
277 | */ |
||
278 | 2 | public static function isFriend($initiator, $recipient) |
|
293 | |||
294 | /** |
||
295 | * Build new or return existed suspend mutual relation, or return null if |
||
296 | * current type is not mutual. |
||
297 | * @see buildRelation() |
||
298 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
299 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
300 | * @return static The relation will be |
||
301 | * given if exists, or return a new relation. |
||
302 | */ |
||
303 | 1 | public static function buildSuspendRelation($user, $other) |
|
313 | |||
314 | /** |
||
315 | * Build new or return existed normal relation. |
||
316 | * The status of mutual relation will be changed to normal if it is not. |
||
317 | * @see buildRelation() |
||
318 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
319 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
320 | * @return static The relation will be |
||
321 | * given if exists, or return a new relation. |
||
322 | */ |
||
323 | 19 | public static function buildNormalRelation($user, $other) |
|
335 | |||
336 | /** |
||
337 | * Build new or return existed relation between initiator and recipient. |
||
338 | * If relation between initiator and recipient is not found, new relation will |
||
339 | * be built. If initiator and recipient are the same one and it is not allowed |
||
340 | * to build self relation, null will be given. |
||
341 | * If you want to know whether the relation exists, you can check the return |
||
342 | * value of `getIsNewRecord()` method. |
||
343 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
344 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
345 | * @return static The relation will be |
||
346 | * given if exists, or return a new relation. Or return null if not allowed |
||
347 | * to build self relation, |
||
348 | */ |
||
349 | 19 | protected static function buildRelation($user, $other) |
|
372 | |||
373 | /** |
||
374 | * Build opposite relation throughout the current relation. The opposite |
||
375 | * relation will be given if existed. |
||
376 | * @param static $relation |
||
377 | * @return static |
||
378 | */ |
||
379 | protected static function buildOppositeRelation($relation) |
||
395 | |||
396 | /** |
||
397 | * Insert relation, the process is placed in a transaction. |
||
398 | * Note: This feature only support relational databases and skip all errors. |
||
399 | * If you don't want to use transaction or database doesn't support it, |
||
400 | * please use `save()` directly. |
||
401 | * @param static $relation |
||
402 | * @param Connection $db |
||
403 | * @return boolean |
||
404 | * @throws InvalidValueException |
||
405 | * @throws InvalidConfigException |
||
406 | * @throws IntegrityException |
||
407 | */ |
||
408 | public static function insertRelation($relation, Connection $db = null) |
||
435 | |||
436 | /** |
||
437 | * Remove myself. |
||
438 | * @return integer|false The number of relations removed, or false if the remove |
||
439 | * is unsuccessful for some reason. Note that it is possible the number of relations |
||
440 | * removed is 0, even though the remove execution is successful. |
||
441 | */ |
||
442 | 19 | public function remove() |
|
446 | |||
447 | /** |
||
448 | * Remove first relation between initiator(s) and recipient(s). |
||
449 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
450 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
451 | * @return integer|false The number of relations removed. |
||
452 | */ |
||
453 | 1 | public static function removeOneRelation($user, $other) |
|
457 | |||
458 | /** |
||
459 | * Remove all relations between initiator(s) and recipient(s). |
||
460 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
461 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
462 | * @return integer The number of relations removed. |
||
463 | */ |
||
464 | 1 | public static function removeAllRelations($user, $other) |
|
471 | |||
472 | /** |
||
473 | * Get first relation between initiator(s) and recipient(s). |
||
474 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
475 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
476 | * @return static |
||
477 | */ |
||
478 | 4 | public static function findOneRelation($user, $other) |
|
482 | |||
483 | /** |
||
484 | * Get first opposite relation between initiator(s) and recipient(s). |
||
485 | * @param BaseUserModel|string $user Initiator or its guid, or array of them. |
||
486 | * @param BaseUserModel|string $other Recipient or its guid, or array of them. |
||
487 | * @return static |
||
488 | */ |
||
489 | 1 | public static function findOneOppositeRelation($user, $other) |
|
493 | |||
494 | /** |
||
495 | * Get user's or users' all relations, or by specified groups. |
||
496 | * @param BaseUserModel|string|array $user Initiator or its GUID, or Initiators or their GUIDs. |
||
497 | * @param BaseUserRelationGroupModel|string|array|null $groups UserRelationGroup |
||
498 | * or its guid, or array of them. If you do not want to delimit the groups, please assign null. |
||
499 | * @return array all eligible relations |
||
500 | */ |
||
501 | 1 | public static function findOnesAllRelations($user, $groups = null) |
|
505 | |||
506 | /** |
||
507 | * Initialize groups attribute. |
||
508 | * @param ModelEvent $event |
||
509 | */ |
||
510 | 19 | public function onInitGroups($event) |
|
515 | |||
516 | /** |
||
517 | * Initialize remark attribute. |
||
518 | * @param ModelEvent $event |
||
519 | */ |
||
520 | 19 | public function onInitRemark($event) |
|
526 | |||
527 | /** |
||
528 | * The event triggered after insert new relation. |
||
529 | * The opposite relation should be inserted without triggering events |
||
530 | * simultaneously after new relation inserted, |
||
531 | * @param ModelEvent $event |
||
532 | * @throws IntegrityException throw if insert failed. |
||
533 | */ |
||
534 | 18 | public function onInsertRelation($event) |
|
547 | |||
548 | /** |
||
549 | * The event triggered after update relation. |
||
550 | * The opposite relation should be updated without triggering events |
||
551 | * simultaneously after existed relation removed. |
||
552 | * @param ModelEvent $event |
||
553 | * @throw IntegrityException throw if update failed. |
||
554 | */ |
||
555 | 2 | public function onUpdateRelation($event) |
|
568 | |||
569 | /** |
||
570 | * The event triggered after delete relation. |
||
571 | * The opposite relation should be deleted without triggering events |
||
572 | * simultaneously after existed relation removed. |
||
573 | * @param ModelEvent $event |
||
574 | */ |
||
575 | 19 | public function onDeleteRelation($event) |
|
586 | } |
||
587 |
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.