Complex classes like NodeTrait 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 NodeTrait, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Arcanedev\LaravelNestedSet; |
||
46 | trait NodeTrait |
||
47 | { |
||
48 | /* ------------------------------------------------------------------------------------------------ |
||
49 | | Traits |
||
50 | | ------------------------------------------------------------------------------------------------ |
||
51 | */ |
||
52 | use EloquentTrait, SoftDeleteTrait; |
||
53 | |||
54 | /* ------------------------------------------------------------------------------------------------ |
||
55 | | Properties |
||
56 | | ------------------------------------------------------------------------------------------------ |
||
57 | */ |
||
58 | /** |
||
59 | * Pending operation. |
||
60 | * |
||
61 | * @var array|null |
||
62 | */ |
||
63 | protected $pending; |
||
64 | |||
65 | /** |
||
66 | * Whether the node has moved since last save. |
||
67 | * |
||
68 | * @var bool |
||
69 | */ |
||
70 | protected $moved = false; |
||
71 | |||
72 | /** |
||
73 | * Keep track of the number of performed operations. |
||
74 | * |
||
75 | * @var int |
||
76 | */ |
||
77 | public static $actionsPerformed = 0; |
||
78 | |||
79 | /* ------------------------------------------------------------------------------------------------ |
||
80 | | Boot Function |
||
81 | | ------------------------------------------------------------------------------------------------ |
||
82 | */ |
||
83 | /** |
||
84 | * Sign on model events. |
||
85 | */ |
||
86 | 284 | public static function bootNodeTrait() |
|
131 | |||
132 | /* ------------------------------------------------------------------------------------------------ |
||
133 | | Relationships |
||
134 | | ------------------------------------------------------------------------------------------------ |
||
135 | */ |
||
136 | /** |
||
137 | * Relation to the parent. |
||
138 | * |
||
139 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
140 | */ |
||
141 | 16 | public function parent() |
|
146 | |||
147 | /** |
||
148 | * Relation to children. |
||
149 | * |
||
150 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
||
151 | */ |
||
152 | 16 | public function children() |
|
157 | |||
158 | /** |
||
159 | * Get query for descendants of the node. |
||
160 | * |
||
161 | * @return \Arcanedev\LaravelNestedSet\Eloquent\DescendantsRelation |
||
162 | */ |
||
163 | 44 | public function descendants() |
|
167 | |||
168 | /** |
||
169 | * Get query for siblings of the node. |
||
170 | * |
||
171 | * @return \Arcanedev\LaravelNestedSet\Eloquent\QueryBuilder |
||
172 | */ |
||
173 | 8 | public function siblings() |
|
179 | |||
180 | /* ------------------------------------------------------------------------------------------------ |
||
181 | | Getters & Setters |
||
182 | | ------------------------------------------------------------------------------------------------ |
||
183 | */ |
||
184 | /** |
||
185 | * Get the lft key name. |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | 260 | public function getLftName() |
|
193 | |||
194 | /** |
||
195 | * Get the rgt key name. |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | 224 | public function getRgtName() |
|
203 | |||
204 | /** |
||
205 | * Get the parent id key name. |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | 168 | public function getParentIdName() |
|
213 | |||
214 | /** |
||
215 | * Get the value of the model's lft key. |
||
216 | * |
||
217 | * @return int |
||
218 | */ |
||
219 | 188 | public function getLft() |
|
223 | |||
224 | /** |
||
225 | * Set the value of the model's lft key. |
||
226 | * |
||
227 | * @param int $value |
||
228 | * |
||
229 | * @return self |
||
230 | */ |
||
231 | 96 | public function setLft($value) |
|
237 | |||
238 | /** |
||
239 | * Get the value of the model's rgt key. |
||
240 | * |
||
241 | * @return int |
||
242 | */ |
||
243 | 124 | public function getRgt() |
|
247 | |||
248 | /** |
||
249 | * Set the value of the model's rgt key. |
||
250 | * |
||
251 | * @param int $value |
||
252 | * |
||
253 | * @return self |
||
254 | */ |
||
255 | 96 | public function setRgt($value) |
|
261 | |||
262 | /** |
||
263 | * Get the value of the model's parent id key. |
||
264 | * |
||
265 | * @return int |
||
266 | */ |
||
267 | 100 | public function getParentId() |
|
271 | |||
272 | /** |
||
273 | * Set the value of the model's parent id key. |
||
274 | * |
||
275 | * @param int $value |
||
276 | * |
||
277 | * @return self |
||
278 | */ |
||
279 | 68 | public function setParentId($value) |
|
285 | |||
286 | /** |
||
287 | * Apply parent model. |
||
288 | * |
||
289 | * @param \Illuminate\Database\Eloquent\Model|null $value |
||
290 | * |
||
291 | * @return self |
||
292 | */ |
||
293 | 56 | protected function setParent($value) |
|
300 | |||
301 | /** |
||
302 | * Set the value of model's parent id key. |
||
303 | * |
||
304 | * Behind the scenes node is appended to found parent node. |
||
305 | * |
||
306 | * @param int $value |
||
307 | * |
||
308 | * @throws \Exception If parent node doesn't exists |
||
309 | */ |
||
310 | 12 | public function setParentIdAttribute($value) |
|
323 | |||
324 | /** |
||
325 | * Get the boundaries. |
||
326 | * |
||
327 | * @return array |
||
328 | */ |
||
329 | 44 | public function getBounds() |
|
333 | |||
334 | /** |
||
335 | * Get the scope attributes. |
||
336 | * |
||
337 | * @return array |
||
338 | */ |
||
339 | 160 | protected function getScopeAttributes() |
|
343 | |||
344 | /** |
||
345 | * Set the lft and rgt boundaries to null. |
||
346 | * |
||
347 | * @return self |
||
348 | */ |
||
349 | 68 | protected function dirtyBounds() |
|
353 | |||
354 | /** |
||
355 | * Returns node that is next to current node without constraining to siblings. |
||
356 | * This can be either a next sibling or a next sibling of the parent node. |
||
357 | * |
||
358 | * @param array $columns |
||
359 | * |
||
360 | * @return self |
||
361 | */ |
||
362 | public function getNextNode(array $columns = ['*']) |
||
366 | |||
367 | /** |
||
368 | * Returns node that is before current node without constraining to siblings. |
||
369 | * This can be either a prev sibling or parent node. |
||
370 | * |
||
371 | * @param array $columns |
||
372 | * |
||
373 | * @return self |
||
374 | */ |
||
375 | 4 | public function getPrevNode(array $columns = ['*']) |
|
379 | |||
380 | /** |
||
381 | * Get the ancestors nodes. |
||
382 | * |
||
383 | * @param array $columns |
||
384 | * |
||
385 | * @return \Arcanedev\LaravelNestedSet\Eloquent\Collection |
||
386 | */ |
||
387 | 8 | public function getAncestors(array $columns = ['*']) |
|
393 | |||
394 | /** |
||
395 | * Get the descendants nodes. |
||
396 | * |
||
397 | * @param array $columns |
||
398 | * |
||
399 | * @return \Arcanedev\LaravelNestedSet\Eloquent\Collection |
||
400 | */ |
||
401 | 12 | public function getDescendants(array $columns = ['*']) |
|
405 | |||
406 | /** |
||
407 | * Get the siblings nodes. |
||
408 | * |
||
409 | * @param array $columns |
||
410 | * |
||
411 | * @return \Arcanedev\LaravelNestedSet\Eloquent\Collection |
||
412 | */ |
||
413 | 8 | public function getSiblings(array $columns = ['*']) |
|
417 | |||
418 | /** |
||
419 | * Get the next siblings nodes. |
||
420 | * |
||
421 | * @param array $columns |
||
422 | * |
||
423 | * @return \Arcanedev\LaravelNestedSet\Eloquent\Collection |
||
424 | */ |
||
425 | 8 | public function getNextSiblings(array $columns = ['*']) |
|
429 | |||
430 | /** |
||
431 | * Get the previous siblings nodes. |
||
432 | * |
||
433 | * @param array $columns |
||
434 | * |
||
435 | * @return \Arcanedev\LaravelNestedSet\Eloquent\Collection |
||
436 | */ |
||
437 | 8 | public function getPrevSiblings(array $columns = ['*']) |
|
441 | |||
442 | /** |
||
443 | * Get the next sibling node. |
||
444 | * |
||
445 | * @param array $columns |
||
446 | * |
||
447 | * @return self |
||
448 | */ |
||
449 | 4 | public function getNextSibling(array $columns = ['*']) |
|
453 | |||
454 | /** |
||
455 | * Get the previous sibling node. |
||
456 | * |
||
457 | * @param array $columns |
||
458 | * |
||
459 | * @return self |
||
460 | */ |
||
461 | 4 | public function getPrevSibling(array $columns = ['*']) |
|
465 | |||
466 | /** |
||
467 | * Get node height (rgt - lft + 1). |
||
468 | * |
||
469 | * @return int |
||
470 | */ |
||
471 | 36 | public function getNodeHeight() |
|
477 | |||
478 | /** |
||
479 | * Get number of descendant nodes. |
||
480 | * |
||
481 | * @return int |
||
482 | */ |
||
483 | 4 | public function getDescendantCount() |
|
487 | |||
488 | /** |
||
489 | * Set raw node. |
||
490 | * |
||
491 | * @param int $lft |
||
492 | * @param int $rgt |
||
493 | * @param int $parentId |
||
494 | * |
||
495 | * @return self |
||
496 | */ |
||
497 | 12 | public function rawNode($lft, $rgt, $parentId) |
|
503 | |||
504 | /** |
||
505 | * Set an action. |
||
506 | * |
||
507 | * @param string $action |
||
508 | * |
||
509 | * @return self |
||
510 | */ |
||
511 | 120 | protected function setNodeAction($action) |
|
518 | |||
519 | /* ------------------------------------------------------------------------------------------------ |
||
520 | | Other Functions |
||
521 | | ------------------------------------------------------------------------------------------------ |
||
522 | */ |
||
523 | /** |
||
524 | * Get the lower bound. |
||
525 | * |
||
526 | * @return int |
||
527 | */ |
||
528 | 32 | protected function getLowerBound() |
|
532 | |||
533 | /** |
||
534 | * Call pending action. |
||
535 | * |
||
536 | * @return null|false |
||
537 | */ |
||
538 | 100 | protected function callPendingAction() |
|
554 | |||
555 | /** |
||
556 | * @return bool |
||
557 | */ |
||
558 | 12 | protected function actionRaw() |
|
562 | |||
563 | /** |
||
564 | * Make a root node. |
||
565 | */ |
||
566 | 32 | protected function actionRoot() |
|
586 | |||
587 | /** |
||
588 | * Append or prepend a node to the parent. |
||
589 | * |
||
590 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $parent |
||
591 | * @param bool $prepend |
||
592 | * |
||
593 | * @return bool |
||
594 | */ |
||
595 | 32 | protected function actionAppendOrPrepend(Nodeable $parent, $prepend = false) |
|
609 | |||
610 | /** |
||
611 | * Insert node before or after another node. |
||
612 | * |
||
613 | * @param self $node |
||
614 | * @param bool $after |
||
615 | * |
||
616 | * @return bool |
||
617 | */ |
||
618 | 28 | protected function actionBeforeOrAfter(self $node, $after = false) |
|
624 | |||
625 | /** |
||
626 | * Refresh node's crucial attributes. |
||
627 | */ |
||
628 | 88 | public function refreshNode() |
|
637 | |||
638 | /** |
||
639 | * Get query for siblings after the node. |
||
640 | * |
||
641 | * @return \Arcanedev\LaravelNestedSet\Eloquent\QueryBuilder |
||
642 | */ |
||
643 | 24 | public function nextSiblings() |
|
648 | |||
649 | /** |
||
650 | * Get query for siblings before the node. |
||
651 | * |
||
652 | * @return \Arcanedev\LaravelNestedSet\Eloquent\QueryBuilder |
||
653 | */ |
||
654 | 16 | public function prevSiblings() |
|
659 | |||
660 | /** |
||
661 | * Get query for nodes after current node. |
||
662 | * |
||
663 | * @return \Arcanedev\LaravelNestedSet\Eloquent\QueryBuilder |
||
664 | */ |
||
665 | 28 | public function nextNodes() |
|
670 | |||
671 | /** |
||
672 | * Get query for nodes before current node in reversed order. |
||
673 | * |
||
674 | * @return \Arcanedev\LaravelNestedSet\Eloquent\QueryBuilder |
||
675 | */ |
||
676 | 20 | public function prevNodes() |
|
681 | |||
682 | /** |
||
683 | * Get query for ancestors to the node not including the node itself. |
||
684 | * |
||
685 | * @return \Arcanedev\LaravelNestedSet\Eloquent\QueryBuilder |
||
686 | */ |
||
687 | 4 | public function ancestors() |
|
692 | |||
693 | /** |
||
694 | * Make this node a root node. |
||
695 | * |
||
696 | * @return self |
||
697 | */ |
||
698 | 48 | public function makeRoot() |
|
702 | |||
703 | /** |
||
704 | * Save node as root. |
||
705 | * |
||
706 | * @return bool |
||
707 | */ |
||
708 | 8 | public function saveAsRoot() |
|
716 | |||
717 | /** |
||
718 | * Append and save a node. |
||
719 | * |
||
720 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $node |
||
721 | * |
||
722 | * @return bool |
||
723 | */ |
||
724 | 16 | public function appendNode(Nodeable $node) |
|
728 | |||
729 | /** |
||
730 | * Prepend and save a node. |
||
731 | * |
||
732 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $node |
||
733 | * |
||
734 | * @return bool |
||
735 | */ |
||
736 | 4 | public function prependNode(Nodeable $node) |
|
740 | |||
741 | /** |
||
742 | * Append a node to the new parent. |
||
743 | * |
||
744 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $parent |
||
745 | * |
||
746 | * @return self |
||
747 | */ |
||
748 | 44 | public function appendToNode(Nodeable $parent) |
|
752 | |||
753 | /** |
||
754 | * Prepend a node to the new parent. |
||
755 | * |
||
756 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $parent |
||
757 | * |
||
758 | * @return \Arcanedev\LaravelNestedSet\Contracts\Nodeable |
||
759 | */ |
||
760 | 8 | public function prependToNode(Nodeable $parent) |
|
764 | |||
765 | /** |
||
766 | * Append or prepend a node to parent. |
||
767 | * |
||
768 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $parent |
||
769 | * @param bool $prepend |
||
770 | * |
||
771 | * @return \Arcanedev\LaravelNestedSet\Contracts\Nodeable |
||
772 | */ |
||
773 | 52 | public function appendOrPrependTo(Nodeable $parent, $prepend = false) |
|
782 | |||
783 | /** |
||
784 | * Insert self after a node. |
||
785 | * |
||
786 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $node |
||
787 | * |
||
788 | * @return \Arcanedev\LaravelNestedSet\Contracts\Nodeable |
||
789 | */ |
||
790 | 28 | public function afterNode(Nodeable $node) |
|
794 | |||
795 | /** |
||
796 | * Insert self before node. |
||
797 | * |
||
798 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $node |
||
799 | * |
||
800 | * @return \Arcanedev\LaravelNestedSet\Contracts\Nodeable |
||
801 | */ |
||
802 | 8 | public function beforeNode(Nodeable $node) |
|
806 | |||
807 | /** |
||
808 | * Set before or after a node. |
||
809 | * |
||
810 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $node |
||
811 | * @param bool $after |
||
812 | * |
||
813 | * @return \Arcanedev\LaravelNestedSet\Contracts\Nodeable |
||
814 | */ |
||
815 | 36 | public function beforeOrAfterNode(Nodeable $node, $after = false) |
|
827 | |||
828 | /** |
||
829 | * Insert after a node and save. |
||
830 | * |
||
831 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $node |
||
832 | * |
||
833 | * @return bool |
||
834 | */ |
||
835 | 16 | public function insertAfterNode(Nodeable $node) |
|
839 | |||
840 | /** |
||
841 | * Insert self before a node and save. |
||
842 | * |
||
843 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $node |
||
844 | * |
||
845 | * @return bool |
||
846 | */ |
||
847 | 4 | public function insertBeforeNode(Nodeable $node) |
|
856 | |||
857 | /** |
||
858 | * Move node up given amount of positions. |
||
859 | * |
||
860 | * @param int $amount |
||
861 | * |
||
862 | * @return bool |
||
863 | */ |
||
864 | 7 | public function up($amount = 1) |
|
875 | |||
876 | /** |
||
877 | * Move node down given amount of positions. |
||
878 | * |
||
879 | * @param int $amount |
||
880 | * |
||
881 | * @return bool |
||
882 | */ |
||
883 | 16 | public function down($amount = 1) |
|
894 | |||
895 | /** |
||
896 | * Insert node at specific position. |
||
897 | * |
||
898 | * @param int $position |
||
899 | * |
||
900 | * @return bool |
||
901 | */ |
||
902 | 68 | protected function insertAt($position) |
|
912 | |||
913 | /** |
||
914 | * Move a node to the new position. |
||
915 | * |
||
916 | * @param int $position |
||
917 | * |
||
918 | * @return int |
||
919 | */ |
||
920 | 40 | protected function moveNode($position) |
|
929 | |||
930 | /** |
||
931 | * Insert new node at specified position. |
||
932 | * |
||
933 | * @param int $position |
||
934 | * |
||
935 | * @return bool |
||
936 | */ |
||
937 | 32 | protected function insertNode($position) |
|
948 | |||
949 | /** |
||
950 | * Update the tree when the node is removed physically. |
||
951 | */ |
||
952 | 20 | protected function deleteDescendants() |
|
974 | |||
975 | /** |
||
976 | * Restore the descendants. |
||
977 | * |
||
978 | * @param \Carbon\Carbon $deletedAt |
||
979 | */ |
||
980 | 4 | protected function restoreDescendants($deletedAt) |
|
987 | |||
988 | /** |
||
989 | * Get a new base query that includes deleted nodes. |
||
990 | * |
||
991 | * @param string|null $table |
||
992 | * |
||
993 | * @return \Arcanedev\LaravelNestedSet\Eloquent\QueryBuilder |
||
994 | */ |
||
995 | 116 | public function newNestedSetQuery($table = null) |
|
1003 | |||
1004 | /** |
||
1005 | * @param string|null $table |
||
1006 | * |
||
1007 | * @return \Arcanedev\LaravelNestedSet\Eloquent\QueryBuilder |
||
1008 | */ |
||
1009 | 136 | public function newScopedQuery($table = null) |
|
1013 | |||
1014 | /** |
||
1015 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
1016 | * @param string $table |
||
1017 | * |
||
1018 | * @return \Arcanedev\LaravelNestedSet\Eloquent\QueryBuilder|\Illuminate\Database\Query\Builder |
||
1019 | */ |
||
1020 | 204 | public function applyNestedSetScope($query, $table = null) |
|
1036 | |||
1037 | /** |
||
1038 | * @param array $attributes |
||
1039 | * |
||
1040 | * @return self |
||
1041 | */ |
||
1042 | 12 | public static function scoped(array $attributes) |
|
1050 | |||
1051 | /** |
||
1052 | * Save a new model and return the instance. |
||
1053 | * |
||
1054 | * Use `children` key on `$attributes` to create child nodes. |
||
1055 | * |
||
1056 | * @param array $attributes |
||
1057 | * @param self $parent |
||
1058 | * |
||
1059 | * @return self |
||
1060 | */ |
||
1061 | 12 | public static function create(array $attributes = [], self $parent = null) |
|
1083 | |||
1084 | /* ------------------------------------------------------------------------------------------------ |
||
1085 | | Check Functions |
||
1086 | | ------------------------------------------------------------------------------------------------ |
||
1087 | */ |
||
1088 | /** |
||
1089 | * Get whether node is root. |
||
1090 | * |
||
1091 | * @return bool |
||
1092 | */ |
||
1093 | 16 | public function isRoot() |
|
1097 | |||
1098 | /** |
||
1099 | * Get whether a node is a descendant of other node. |
||
1100 | * |
||
1101 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $node |
||
1102 | * |
||
1103 | * @return bool |
||
1104 | */ |
||
1105 | 76 | public function isDescendantOf(Nodeable $node) |
|
1112 | |||
1113 | /** |
||
1114 | * Get whether the node is immediate children of other node. |
||
1115 | * |
||
1116 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $node |
||
1117 | * |
||
1118 | * @return bool |
||
1119 | */ |
||
1120 | 4 | public function isChildOf(Nodeable $node) |
|
1124 | |||
1125 | /** |
||
1126 | * Get whether the node is a sibling of another node. |
||
1127 | * |
||
1128 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $node |
||
1129 | * |
||
1130 | * @return bool |
||
1131 | */ |
||
1132 | 32 | public function isSiblingOf(Nodeable $node) |
|
1136 | |||
1137 | /** |
||
1138 | * Get whether the node is an ancestor of other node, including immediate parent. |
||
1139 | * |
||
1140 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $node |
||
1141 | * |
||
1142 | * @return bool |
||
1143 | */ |
||
1144 | 4 | public function isAncestorOf(Nodeable $node) |
|
1149 | |||
1150 | /** |
||
1151 | * Get whether the node has moved since last save. |
||
1152 | * |
||
1153 | * @return bool |
||
1154 | */ |
||
1155 | 24 | public function hasMoved() |
|
1159 | |||
1160 | /* ------------------------------------------------------------------------------------------------ |
||
1161 | | Assertion Functions |
||
1162 | | ------------------------------------------------------------------------------------------------ |
||
1163 | */ |
||
1164 | /** |
||
1165 | * Assert that the node is not a descendant. |
||
1166 | * |
||
1167 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $node |
||
1168 | * |
||
1169 | * @return self |
||
1170 | * |
||
1171 | * @throws \LogicException |
||
1172 | */ |
||
1173 | 80 | protected function assertNotDescendant(Nodeable $node) |
|
1182 | |||
1183 | /** |
||
1184 | * Assert node exists. |
||
1185 | * |
||
1186 | * @param \Arcanedev\LaravelNestedSet\Contracts\Nodeable $node |
||
1187 | * |
||
1188 | * @return self |
||
1189 | * |
||
1190 | * @throws \LogicException |
||
1191 | */ |
||
1192 | 84 | protected function assertNodeExists(Nodeable $node) |
|
1200 | } |
||
1201 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.