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 static $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 | 24 | public function rules() |
|
137 | |||
138 | /** |
||
139 | * Validation rules associated with user relation. |
||
140 | * @return array rules. |
||
141 | */ |
||
142 | 24 | public function getUserRelationRules() |
|
143 | { |
||
144 | 24 | $rules = []; |
|
145 | 24 | if ($this->relationType == static::$relationMutual) { |
|
146 | $rules = [ |
||
147 | 6 | [[$this->mutualTypeAttribute], 'in', 'range' => array_keys(static::$mutualTypes)], |
|
148 | 6 | [[$this->mutualTypeAttribute], 'default', 'value' => static::$mutualTypeNormal], |
|
149 | ]; |
||
150 | } |
||
151 | 24 | return array_merge($rules, $this->getRemarkRules(), $this->getFavoriteRules(), $this->getGroupsRules(), $this->getOtherGuidRules()); |
|
|
|||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Get remark. |
||
156 | * @return string remark. |
||
157 | */ |
||
158 | 1 | public function getRemark() |
|
159 | { |
||
160 | 1 | $remarkAttribute = $this->remarkAttribute; |
|
161 | 1 | return (is_string($remarkAttribute) && !empty($remarkAttribute)) ? $this->$remarkAttribute : null; |
|
162 | } |
||
163 | |||
164 | /** |
||
165 | * Set remark. |
||
166 | * @param string $remark |
||
167 | * @return string remark. |
||
168 | */ |
||
169 | 1 | public function setRemark($remark) |
|
170 | { |
||
171 | 1 | $remarkAttribute = $this->remarkAttribute; |
|
172 | 1 | return (is_string($remarkAttribute) && !empty($remarkAttribute)) ? $this->$remarkAttribute = $remark : null; |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * Validation rules associated with remark attribute. |
||
177 | * @return array rules. |
||
178 | */ |
||
179 | 24 | public function getRemarkRules() |
|
186 | |||
187 | /** |
||
188 | * Validation rules associated with favorites attribute. |
||
189 | * @return array rules. |
||
190 | */ |
||
191 | 24 | public function getFavoriteRules() |
|
198 | |||
199 | /** |
||
200 | * Validation rules associated with other guid attribute. |
||
201 | * @return array rules. |
||
202 | */ |
||
203 | 24 | public function getOtherGuidRules() |
|
210 | |||
211 | /** |
||
212 | * Attach events associated with user relation. |
||
213 | */ |
||
214 | 27 | 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 | 6 | 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 | 9 | public static function buildSuspendRelation($user, $other) |
|
312 | |||
313 | /** |
||
314 | * Build new or return existed normal relation. |
||
315 | * The status of mutual relation will be changed to normal if it is not. |
||
316 | * @see buildRelation() |
||
317 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
318 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
319 | * @return static The relation will be |
||
320 | * given if exists, or return a new relation. |
||
321 | */ |
||
322 | 27 | public static function buildNormalRelation($user, $other) |
|
333 | |||
334 | /** |
||
335 | * Transform relation from suspend to normal. |
||
336 | * Note: You should ensure the relation model is not new one. |
||
337 | * @param static $relation |
||
338 | * @return boolean |
||
339 | */ |
||
340 | 1 | public static function transformSuspendToNormal($relation) |
|
348 | |||
349 | /** |
||
350 | * Revert relation from normal to suspend. |
||
351 | * Note: You should ensure the relation model is not new one. |
||
352 | * @param static $relation |
||
353 | * @return boolean |
||
354 | */ |
||
355 | 1 | public static function revertNormalToSuspend($relation) |
|
363 | |||
364 | /** |
||
365 | * Build new or return existed relation between initiator and recipient. |
||
366 | * If relation between initiator and recipient is not found, new relation will |
||
367 | * be built. If initiator and recipient are the same one and it is not allowed |
||
368 | * to build self relation, null will be given. |
||
369 | * If you want to know whether the relation exists, you can check the return |
||
370 | * value of `getIsNewRecord()` method. |
||
371 | * @param BaseUserModel|string $user Initiator or its GUID. |
||
372 | * @param BaseUserModel|string $other Recipient or its GUID. |
||
373 | * @return static The relation will be |
||
374 | * given if exists, or return a new relation. Or return null if not allowed |
||
375 | * to build self relation, |
||
376 | */ |
||
377 | 27 | protected static function buildRelation($user, $other) |
|
400 | |||
401 | /** |
||
402 | * Build opposite relation throughout the current relation. The opposite |
||
403 | * relation will be given if existed. |
||
404 | * @param static $relation |
||
405 | * @return static |
||
406 | */ |
||
407 | 6 | protected static function buildOppositeRelation($relation) |
|
422 | |||
423 | /** |
||
424 | * Get mutual type. |
||
425 | * @return integer |
||
426 | */ |
||
427 | 6 | public function getMutualType() |
|
435 | |||
436 | /** |
||
437 | * Set mutual type. |
||
438 | * @param integer $type |
||
439 | * @return integer |
||
440 | */ |
||
441 | 8 | protected function setMutualType($type) |
|
452 | |||
453 | /** |
||
454 | * Insert relation, the process is placed in a transaction. |
||
455 | * Note: This feature only support relational databases and skip all errors. |
||
456 | * If you don't want to use transaction or database doesn't support it, |
||
457 | * please use `save()` directly. |
||
458 | * @param static $relation |
||
459 | * @param Connection $db |
||
460 | * @return boolean |
||
461 | * @throws InvalidValueException |
||
462 | * @throws InvalidConfigException |
||
463 | * @throws IntegrityException |
||
464 | */ |
||
465 | public static function insertRelation($relation, Connection $db = null) |
||
492 | |||
493 | /** |
||
494 | * Remove myself. |
||
495 | * @return integer|false The number of relations removed, or false if the remove |
||
496 | * is unsuccessful for some reason. Note that it is possible the number of relations |
||
497 | * removed is 0, even though the remove execution is successful. |
||
498 | */ |
||
499 | 27 | public function remove() |
|
503 | |||
504 | /** |
||
505 | * Remove first relation between initiator(s) and recipient(s). |
||
506 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
507 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
508 | * @return integer|false The number of relations removed. |
||
509 | */ |
||
510 | 1 | public static function removeOneRelation($user, $other) |
|
514 | |||
515 | /** |
||
516 | * Remove all relations between initiator(s) and recipient(s). |
||
517 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
518 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
519 | * @return integer The number of relations removed. |
||
520 | */ |
||
521 | 9 | public static function removeAllRelations($user, $other) |
|
528 | |||
529 | /** |
||
530 | * Get first relation between initiator(s) and recipient(s). |
||
531 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
532 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
533 | * @return static |
||
534 | */ |
||
535 | 4 | public static function findOneRelation($user, $other) |
|
539 | |||
540 | /** |
||
541 | * Get first opposite relation between initiator(s) and recipient(s). |
||
542 | * @param BaseUserModel|string $user Initiator or its guid, or array of them. |
||
543 | * @param BaseUserModel|string $other Recipient or its guid, or array of them. |
||
544 | * @return static |
||
545 | */ |
||
546 | 1 | public static function findOneOppositeRelation($user, $other) |
|
550 | |||
551 | /** |
||
552 | * Get user's or users' all relations, or by specified groups. |
||
553 | * @param BaseUserModel|string|array $user Initiator or its GUID, or Initiators or their GUIDs. |
||
554 | * @param BaseUserRelationGroupModel|string|array|null $groups UserRelationGroup |
||
555 | * or its guid, or array of them. If you do not want to delimit the groups, please assign null. |
||
556 | * @return array all eligible relations |
||
557 | */ |
||
558 | 1 | public static function findOnesAllRelations($user, $groups = null) |
|
562 | |||
563 | /** |
||
564 | * Initialize groups attribute. |
||
565 | * @param ModelEvent $event |
||
566 | */ |
||
567 | 27 | public function onInitGroups($event) |
|
572 | |||
573 | /** |
||
574 | * Initialize remark attribute. |
||
575 | * @param ModelEvent $event |
||
576 | */ |
||
577 | 27 | public function onInitRemark($event) |
|
583 | |||
584 | /** |
||
585 | * The event triggered after insert new relation. |
||
586 | * The opposite relation should be inserted without triggering events |
||
587 | * simultaneously after new relation inserted, |
||
588 | * @param ModelEvent $event |
||
589 | * @throws IntegrityException throw if insert failed. |
||
590 | */ |
||
591 | 24 | public function onInsertRelation($event) |
|
604 | |||
605 | /** |
||
606 | * The event triggered after update relation. |
||
607 | * The opposite relation should be updated without triggering events |
||
608 | * simultaneously after existed relation removed. |
||
609 | * @param ModelEvent $event |
||
610 | * @throw IntegrityException throw if update failed. |
||
611 | */ |
||
612 | 4 | public function onUpdateRelation($event) |
|
625 | |||
626 | /** |
||
627 | * The event triggered after delete relation. |
||
628 | * The opposite relation should be deleted without triggering events |
||
629 | * simultaneously after existed relation removed. |
||
630 | * @param ModelEvent $event |
||
631 | */ |
||
632 | 27 | public function onDeleteRelation($event) |
|
643 | } |
||
644 |
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.