Complex classes like SelfBlameableTrait 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 SelfBlameableTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | trait SelfBlameableTrait |
||
30 | { |
||
31 | |||
32 | /** |
||
33 | * @var false|string attribute name of which store the parent's guid. |
||
34 | */ |
||
35 | public $parentAttribute = false; |
||
36 | |||
37 | /** |
||
38 | * @var string|array rule name and parameters of parent attribute, as well |
||
39 | * as self referenced ID attribute. |
||
40 | */ |
||
41 | public $parentAttributeRule = ['string', 'max' => 36]; |
||
42 | |||
43 | /** |
||
44 | * @var string self referenced ID attribute. |
||
45 | */ |
||
46 | public $refIdAttribute = 'guid'; |
||
47 | public static $parentNone = 0; |
||
48 | public static $parentParent = 1; |
||
49 | public static $parentTypes = [ |
||
50 | 0 => 'none', |
||
51 | 1 => 'parent', |
||
52 | ]; |
||
53 | public static $onNoAction = 0; |
||
54 | public static $onRestrict = 1; |
||
55 | public static $onCascade = 2; |
||
56 | public static $onSetNull = 3; |
||
57 | public static $onUpdateTypes = [ |
||
58 | 0 => 'on action', |
||
59 | 1 => 'restrict', |
||
60 | 2 => 'cascade', |
||
61 | 3 => 'set null', |
||
62 | ]; |
||
63 | |||
64 | /** |
||
65 | * @var integer indicates the on delete type. default to cascade. |
||
66 | */ |
||
67 | public $onDeleteType = 2; |
||
68 | |||
69 | /** |
||
70 | * @var integer indicates the on update type. default to cascade. |
||
71 | */ |
||
72 | public $onUpdateType = 2; |
||
73 | |||
74 | /** |
||
75 | * @var boolean indicates whether throw exception or not when restriction occured on updating or deleting operation. |
||
76 | */ |
||
77 | public $throwRestrictException = false; |
||
78 | private $localSelfBlameableRules = []; |
||
79 | public static $eventParentChanged = 'parentChanged'; |
||
80 | |||
81 | /** |
||
82 | * Get rules associated with self blameable attribute. |
||
83 | * @return array rules. |
||
84 | */ |
||
85 | 13 | public function getSelfBlameableRules() |
|
101 | |||
102 | /** |
||
103 | * Set rules associated with self blameable attribute. |
||
104 | * @param array $rules rules. |
||
105 | */ |
||
106 | 1 | public function setSelfBlameableRules($rules = []) |
|
110 | |||
111 | /** |
||
112 | * Bear a child. |
||
113 | * @param array $config |
||
114 | * @return static |
||
115 | */ |
||
116 | 10 | public function bear($config = []) |
|
125 | |||
126 | /** |
||
127 | * Event triggered before deleting itself. |
||
128 | * @param ModelEvent $event |
||
129 | * @return boolean true if parentAttribute not specified. |
||
130 | * @throws IntegrityException throw if $throwRestrictException is true when $onDeleteType is on restrict. |
||
131 | */ |
||
132 | 19 | public function onDeleteChildren($event) |
|
157 | |||
158 | /** |
||
159 | * Event triggered before updating itself. |
||
160 | * @param ModelEvent $event |
||
161 | * @return boolean true if parentAttribute not specified. |
||
162 | * @throws IntegrityException throw if $throwRestrictException is true when $onUpdateType is on restrict. |
||
163 | */ |
||
164 | 4 | public function onUpdateChildren($event) |
|
189 | |||
190 | /** |
||
191 | * Get parent query. |
||
192 | * Or get parent instance if access by magic property. |
||
193 | * @return ActiveQuery |
||
194 | */ |
||
195 | 3 | public function getParent() |
|
199 | |||
200 | /** |
||
201 | * Set parent. |
||
202 | * Don't forget save model after setting it. |
||
203 | * @param static $parent |
||
204 | * @return false|string |
||
205 | */ |
||
206 | 1 | public function setParent($parent) |
|
215 | |||
216 | /** |
||
217 | * Check whether this model has parent. |
||
218 | * @return boolean |
||
219 | */ |
||
220 | 1 | public function hasParent() |
|
224 | |||
225 | 1 | public function hasAncestor($ancestor) |
|
235 | |||
236 | /** |
||
237 | * Get ancestor chain. (Ancestor GUID Only!) |
||
238 | * If this model has ancestor, the return array consists all the ancestor in order. |
||
239 | * The first element is parent, and the last element is root, otherwise return empty array. |
||
240 | * If you want to get ancestor model, you can simplify instance a query and specify the |
||
241 | * condition with the return value. But it will not return models under the order of ancestor chain. |
||
242 | * @param string[] $ancestor |
||
243 | * @return string[] |
||
244 | */ |
||
245 | 1 | public function getAncestorChain($ancestor = []) |
|
256 | |||
257 | /** |
||
258 | * Get ancestors with specified ancestor chain. |
||
259 | * @param string[] $ancestor Ancestor chain. |
||
260 | * @return static[]|null |
||
261 | */ |
||
262 | 1 | public static function getAncestorModels($ancestor) |
|
273 | |||
274 | /** |
||
275 | * Get ancestors. |
||
276 | * @return static[]|null |
||
277 | */ |
||
278 | 1 | public function getAncestors() |
|
282 | |||
283 | /** |
||
284 | * Get children query. |
||
285 | * Or get children instances if access magic property. |
||
286 | * @return ActiveQuery |
||
287 | */ |
||
288 | 3 | public function getChildren() |
|
292 | |||
293 | /** |
||
294 | * Get children which parent attribute point to the my old guid. |
||
295 | * @return static[] |
||
296 | */ |
||
297 | 4 | public function getOldChildren() |
|
301 | |||
302 | /** |
||
303 | * Update all children, not grandchildren. |
||
304 | * If onUpdateType is on cascade, the children will be updated automatically. |
||
305 | * @param mixed $value set guid if false, set empty string if empty() return |
||
306 | * true, otherwise set it to $parentAttribute. |
||
307 | * @return IntegrityException|boolean true if all update operations |
||
308 | * succeeded to execute, or false if anyone of them failed. If not production |
||
309 | * environment or enable debug mode, it will return exception. |
||
310 | * @throws IntegrityException throw if anyone update failed. |
||
311 | */ |
||
312 | 3 | public function updateChildren($value = false) |
|
346 | |||
347 | /** |
||
348 | * Delete all children, not grandchildren. |
||
349 | * If onDeleteType is on cascade, the children will be deleted automatically. |
||
350 | * If onDeleteType is on restrict and contains children, the deletion will |
||
351 | * be restricted. |
||
352 | * @return IntegrityException|boolean true if all delete operations |
||
353 | * succeeded to execute, or false if anyone of them failed. If not production |
||
354 | * environment or enable debug mode, it will return exception. |
||
355 | * @throws IntegrityException throw if anyone delete failed. |
||
356 | */ |
||
357 | 1 | public function deleteChildren() |
|
382 | |||
383 | /** |
||
384 | * Update children's parent attribute. |
||
385 | * Event triggered before updating. |
||
386 | * @param ModelEvent $event |
||
387 | * @return boolean |
||
388 | */ |
||
389 | 16 | public function onParentRefIdChanged($event) |
|
396 | |||
397 | 38 | protected function initSelfBlameableEvents() |
|
402 | |||
403 | /** |
||
404 | * Check whether if this model has common ancestor with $model. |
||
405 | * @param static $model |
||
406 | * @return boolean |
||
407 | */ |
||
408 | 1 | public function hasCommonAncestor($model) |
|
412 | |||
413 | /** |
||
414 | * Get common ancestor. If there isn't common ancestor, null will be given. |
||
415 | * @param static $model |
||
416 | * @return static |
||
417 | */ |
||
418 | 1 | public function getCommonAncestor($model) |
|
429 | } |
||
430 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.