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 | 30 | public function rules() |
|
137 | |||
138 | /** |
||
139 | * Validation rules associated with user relation. |
||
140 | * @return array rules. |
||
141 | */ |
||
142 | 30 | public function getUserRelationRules() |
|
143 | { |
||
144 | 30 | $rules = []; |
|
145 | 30 | if ($this->relationType == static::$relationMutual) { |
|
146 | $rules = [ |
||
147 | 12 | [[$this->mutualTypeAttribute], 'in', 'range' => array_keys(static::$mutualTypes)], |
|
148 | 12 | [[$this->mutualTypeAttribute], 'default', 'value' => static::$mutualTypeNormal], |
|
149 | ]; |
||
150 | } |
||
151 | 30 | 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 | 30 | public function getRemarkRules() |
|
186 | |||
187 | /** |
||
188 | * Validation rules associated with favorites attribute. |
||
189 | * @return array rules. |
||
190 | */ |
||
191 | 30 | public function getFavoriteRules() |
|
198 | |||
199 | /** |
||
200 | * Validation rules associated with other guid attribute. |
||
201 | * @return array rules. |
||
202 | */ |
||
203 | 30 | public function getOtherGuidRules() |
|
210 | |||
211 | /** |
||
212 | * Attach events associated with user relation. |
||
213 | */ |
||
214 | 33 | 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 | 6 | 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 | 6 | 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 | 15 | 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 | 33 | 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 | 2 | public static function transformSuspendToNormal($relation) |
|
341 | { |
||
342 | 2 | if (!$relation || !($relation instanceof static) || $relation->getIsNewRecord() || $relation->relationType != static::$relationMutual) { |
|
343 | 1 | return false; |
|
344 | } |
||
345 | 1 | $new = static::buildNormalRelation($relation->initiator, $relation->recipient); |
|
346 | 1 | return $new->save() && $relation->refresh(); |
|
347 | } |
||
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 | 2 | public static function revertNormalToSuspend($relation) |
|
356 | { |
||
357 | 2 | if (!$relation || !($relation instanceof static) || $relation->getIsNewRecord() || $relation->relationType != static::$relationMutual) { |
|
358 | 1 | return false; |
|
359 | } |
||
360 | 1 | $new = static::buildSuspendRelation($relation->initiator, $relation->recipient); |
|
361 | 1 | return $new->save() && $relation->refresh(); |
|
362 | } |
||
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 | 33 | 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 | 12 | protected static function buildOppositeRelation($relation) |
|
422 | |||
423 | /** |
||
424 | * Get mutual type. |
||
425 | * @return integer |
||
426 | */ |
||
427 | 12 | public function getMutualType() |
|
435 | |||
436 | /** |
||
437 | * Set mutual type. |
||
438 | * @param integer $type |
||
439 | * @return integer |
||
440 | */ |
||
441 | 14 | 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 | 1 | public static function insertRelation($relation, Connection $db = null) |
|
492 | |||
493 | /** |
||
494 | * Remove relation, the process is placed in transaction. |
||
495 | * Note: This feature only support relational databases and skip all errors. |
||
496 | * If you don't want to use transaction or database doesn't support it, |
||
497 | * please use `remove()` directly. |
||
498 | * @param static $relation |
||
499 | * @param Connection $db |
||
500 | * @return boolean|integer |
||
501 | * @throws InvalidConfigException |
||
502 | */ |
||
503 | 1 | public static function removeRelation($relation, Connection $db = null) |
|
526 | |||
527 | /** |
||
528 | * Remove myself. |
||
529 | * @return integer|false The number of relations removed, or false if the remove |
||
530 | * is unsuccessful for some reason. Note that it is possible the number of relations |
||
531 | * removed is 0, even though the remove execution is successful. |
||
532 | */ |
||
533 | 33 | public function remove() |
|
537 | |||
538 | /** |
||
539 | * Remove first relation between initiator(s) and recipient(s). |
||
540 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
541 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
542 | * @return integer|false The number of relations removed. |
||
543 | */ |
||
544 | 1 | public static function removeOneRelation($user, $other) |
|
552 | |||
553 | /** |
||
554 | * Remove all relations between initiator(s) and recipient(s). |
||
555 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
556 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
557 | * @return integer The number of relations removed. |
||
558 | */ |
||
559 | 15 | public static function removeAllRelations($user, $other) |
|
566 | |||
567 | /** |
||
568 | * Get first relation between initiator(s) and recipient(s). |
||
569 | * @param BaseUserModel|string|array $user Initiator or its guid, or array of them. |
||
570 | * @param BaseUserModel|string|array $other Recipient or its guid, or array of them. |
||
571 | * @return static |
||
572 | */ |
||
573 | 4 | public static function findOneRelation($user, $other) |
|
577 | |||
578 | /** |
||
579 | * Get first opposite relation between initiator(s) and recipient(s). |
||
580 | * @param BaseUserModel|string $user Initiator or its guid, or array of them. |
||
581 | * @param BaseUserModel|string $other Recipient or its guid, or array of them. |
||
582 | * @return static |
||
583 | */ |
||
584 | 1 | public static function findOneOppositeRelation($user, $other) |
|
588 | |||
589 | /** |
||
590 | * Get user's or users' all relations, or by specified groups. |
||
591 | * @param BaseUserModel|string|array $user Initiator or its GUID, or Initiators or their GUIDs. |
||
592 | * @param BaseUserRelationGroupModel|string|array|null $groups UserRelationGroup |
||
593 | * or its guid, or array of them. If you do not want to delimit the groups, please assign null. |
||
594 | * @return array all eligible relations |
||
595 | */ |
||
596 | 1 | public static function findOnesAllRelations($user, $groups = null) |
|
600 | |||
601 | /** |
||
602 | * Initialize groups attribute. |
||
603 | * @param ModelEvent $event |
||
604 | */ |
||
605 | 33 | public function onInitGroups($event) |
|
610 | |||
611 | /** |
||
612 | * Initialize remark attribute. |
||
613 | * @param ModelEvent $event |
||
614 | */ |
||
615 | 33 | public function onInitRemark($event) |
|
621 | |||
622 | /** |
||
623 | * The event triggered after insert new relation. |
||
624 | * The opposite relation should be inserted without triggering events |
||
625 | * simultaneously after new relation inserted, |
||
626 | * @param ModelEvent $event |
||
627 | * @throws IntegrityException throw if insert failed. |
||
628 | */ |
||
629 | 30 | public function onInsertRelation($event) |
|
642 | |||
643 | /** |
||
644 | * The event triggered after update relation. |
||
645 | * The opposite relation should be updated without triggering events |
||
646 | * simultaneously after existed relation removed. |
||
647 | * @param ModelEvent $event |
||
648 | * @throw IntegrityException throw if update failed. |
||
649 | */ |
||
650 | 4 | public function onUpdateRelation($event) |
|
663 | |||
664 | /** |
||
665 | * The event triggered after delete relation. |
||
666 | * The opposite relation should be deleted without triggering events |
||
667 | * simultaneously after existed relation removed. |
||
668 | * @param ModelEvent $event |
||
669 | */ |
||
670 | 33 | public function onDeleteRelation($event) |
|
681 | } |
||
682 |
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.