Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AdjacencyListBehavior 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 AdjacencyListBehavior, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class AdjacencyListBehavior extends Behavior |
||
26 | { |
||
27 | const OPERATION_MAKE_ROOT = 1; |
||
28 | const OPERATION_PREPEND_TO = 2; |
||
29 | const OPERATION_APPEND_TO = 3; |
||
30 | const OPERATION_INSERT_BEFORE = 4; |
||
31 | const OPERATION_INSERT_AFTER = 5; |
||
32 | const OPERATION_DELETE_ALL = 6; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | public $parentAttribute = 'parent_id'; |
||
38 | |||
39 | /** |
||
40 | * @var array|false SortableBehavior config |
||
41 | */ |
||
42 | public $sortable = []; |
||
43 | |||
44 | /** |
||
45 | * @var bool |
||
46 | */ |
||
47 | public $checkLoop = false; |
||
48 | |||
49 | /** |
||
50 | * @var int |
||
51 | */ |
||
52 | public $parentsJoinLevels = 3; |
||
53 | |||
54 | /** |
||
55 | * @var int |
||
56 | */ |
||
57 | public $childrenJoinLevels = 3; |
||
58 | |||
59 | /** |
||
60 | * @var bool |
||
61 | */ |
||
62 | protected $operation; |
||
63 | |||
64 | /** |
||
65 | * @var ActiveRecord|self|null |
||
66 | */ |
||
67 | protected $node; |
||
68 | |||
69 | /** |
||
70 | * @var SortableBehavior |
||
71 | */ |
||
72 | protected $behavior; |
||
73 | |||
74 | /** |
||
75 | * @var ActiveRecord[] |
||
76 | */ |
||
77 | private $_parentsOrdered; |
||
78 | |||
79 | /** |
||
80 | * @var array |
||
81 | */ |
||
82 | private $_parentsIds; |
||
83 | |||
84 | /** |
||
85 | * @var array |
||
86 | */ |
||
87 | private $_childrenIds; |
||
88 | |||
89 | |||
90 | /** |
||
91 | * @inheritdoc |
||
92 | */ |
||
93 | 225 | public function events() |
|
94 | { |
||
95 | return [ |
||
96 | 225 | ActiveRecord::EVENT_BEFORE_INSERT => 'beforeSave', |
|
97 | 225 | ActiveRecord::EVENT_AFTER_INSERT => 'afterSave', |
|
98 | 225 | ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeSave', |
|
99 | 225 | ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave', |
|
100 | 225 | ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete', |
|
101 | 225 | ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete', |
|
102 | 225 | ]; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param ActiveRecord $owner |
||
107 | */ |
||
108 | 225 | public function attach($owner) |
|
122 | |||
123 | /** |
||
124 | * @param int|null $depth |
||
125 | * @return \yii\db\ActiveQuery |
||
126 | * @throws Exception |
||
127 | */ |
||
128 | 6 | public function getParents($depth = null) |
|
129 | { |
||
130 | 6 | $tableName = $this->owner->tableName(); |
|
131 | 6 | $ids = $this->getParentsIds($depth); |
|
132 | 6 | $query = $this->owner->find() |
|
133 | 6 | ->andWhere(["{$tableName}.[[" . $this->getPrimaryKey() . "]]" => $ids]); |
|
134 | 6 | $query->multiple = true; |
|
135 | 6 | return $query; |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * @return ActiveRecord[] |
||
140 | * @throws Exception |
||
141 | */ |
||
142 | 3 | public function getParentsOrdered() |
|
143 | { |
||
144 | 3 | if ($this->_parentsOrdered !== null) { |
|
145 | return $this->_parentsOrdered; |
||
146 | } |
||
147 | 3 | $parents = $this->getParents()->all(); |
|
148 | 3 | $ids = array_flip($this->getParentsIds()); |
|
149 | 3 | $primaryKey = $this->getPrimaryKey(); |
|
150 | View Code Duplication | usort($parents, function($a, $b) use ($ids, $primaryKey) { |
|
|
|||
151 | 3 | $aIdx = $ids[$a->$primaryKey]; |
|
152 | 3 | $bIdx = $ids[$b->$primaryKey]; |
|
153 | 3 | if ($aIdx == $bIdx) { |
|
154 | return 0; |
||
155 | } else { |
||
156 | 3 | return $aIdx > $bIdx ? -1 : 1; |
|
157 | } |
||
158 | 3 | }); |
|
159 | 3 | return $this->_parentsOrdered = $parents; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * @return \yii\db\ActiveQuery |
||
164 | * @throws Exception |
||
165 | */ |
||
166 | 3 | public function getParent() |
|
167 | { |
||
168 | 3 | return $this->owner->hasOne($this->owner->className(), [$this->getPrimaryKey() => $this->parentAttribute]); |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * @return \yii\db\ActiveQuery |
||
173 | */ |
||
174 | 3 | public function getRoot() |
|
175 | { |
||
176 | 3 | $tableName = $this->owner->tableName(); |
|
177 | 3 | $id = $this->getParentsIds(); |
|
178 | 3 | $id = $id ? $id[count($id) - 1] : $this->owner->primaryKey; |
|
179 | 3 | $query = $this->owner->find() |
|
180 | 3 | ->andWhere(["{$tableName}.[[" . $this->getPrimaryKey() . "]]" => $id]); |
|
181 | 3 | $query->multiple = false; |
|
182 | 3 | return $query; |
|
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param int|null $depth |
||
187 | * @param bool $andSelf |
||
188 | * @return \yii\db\ActiveQuery |
||
189 | */ |
||
190 | 12 | public function getDescendants($depth = null, $andSelf = false) |
|
191 | { |
||
192 | 12 | $tableName = $this->owner->tableName(); |
|
193 | 12 | $ids = $this->getDescendantsIds($depth, true); |
|
194 | 12 | if ($andSelf) { |
|
195 | 3 | $ids[] = $this->owner->getPrimaryKey(); |
|
196 | 3 | } |
|
197 | 12 | $query = $this->owner->find() |
|
198 | 12 | ->andWhere(["{$tableName}.[[" . $this->getPrimaryKey() . "]]" => $ids]); |
|
199 | 12 | $query->multiple = true; |
|
200 | 12 | return $query; |
|
201 | } |
||
202 | |||
203 | /** |
||
204 | * @return ActiveRecord[] |
||
205 | * @throws Exception |
||
206 | */ |
||
207 | 6 | public function getDescendantsOrdered() |
|
223 | |||
224 | /** |
||
225 | * @return \yii\db\ActiveQuery |
||
226 | */ |
||
227 | 12 | public function getChildren() |
|
235 | |||
236 | /** |
||
237 | * @param int|null $depth |
||
238 | * @return \yii\db\ActiveQuery |
||
239 | */ |
||
240 | 3 | public function getLeaves($depth = null) |
|
254 | |||
255 | /** |
||
256 | * @return \yii\db\ActiveQuery |
||
257 | * @throws NotSupportedException |
||
258 | */ |
||
259 | 3 | View Code Duplication | public function getPrev() |
276 | |||
277 | /** |
||
278 | * @return \yii\db\ActiveQuery |
||
279 | * @throws NotSupportedException |
||
280 | */ |
||
281 | 3 | View Code Duplication | public function getNext() |
298 | |||
299 | /** |
||
300 | * @param int|null $depth |
||
301 | * @param bool $cache |
||
302 | * @return array |
||
303 | */ |
||
304 | 27 | public function getParentsIds($depth = null, $cache = true) |
|
305 | { |
||
306 | 27 | if ($cache && $this->_parentsIds !== null) { |
|
307 | 3 | return $depth === null ? $this->_parentsIds : array_slice($this->_parentsIds, 0, $depth); |
|
308 | } |
||
309 | |||
310 | 27 | $parentId = $this->owner->getAttribute($this->parentAttribute); |
|
311 | 27 | if ($parentId === null) { |
|
312 | 9 | if ($cache) { |
|
313 | 9 | $this->_parentsIds = []; |
|
314 | 9 | } |
|
315 | 9 | return []; |
|
316 | } |
||
317 | 27 | $result = [(string)$parentId]; |
|
318 | 27 | $tableName = $this->owner->tableName(); |
|
319 | 27 | $primaryKey = $this->getPrimaryKey(); |
|
320 | 27 | $depthCur = 1; |
|
321 | 27 | while ($parentId !== null && ($depth === null || $depthCur < $depth)) { |
|
322 | 27 | $query = (new Query()) |
|
323 | 27 | ->select(["lvl0.[[{$this->parentAttribute}]] AS lvl0"]) |
|
324 | 27 | ->from("{$tableName} lvl0") |
|
325 | 27 | ->where(["lvl0.[[{$primaryKey}]]" => $parentId]); |
|
326 | 27 | for ($i = 0; $i < $this->parentsJoinLevels && ($depth === null || $i + $depthCur + 1 < $depth); $i++) { |
|
327 | 15 | $j = $i + 1; |
|
328 | $query |
||
329 | 15 | ->addSelect(["lvl{$j}.[[{$this->parentAttribute}]] as lvl{$j}"]) |
|
330 | 15 | ->leftJoin("{$tableName} lvl{$j}", "lvl{$j}.[[{$primaryKey}]] = lvl{$i}.[[{$this->parentAttribute}]]"); |
|
331 | 15 | } |
|
332 | 27 | if ($parentIds = $query->one($this->owner->getDb())) { |
|
333 | 27 | foreach ($parentIds as $parentId) { |
|
334 | 27 | $depthCur++; |
|
335 | 27 | if ($parentId === null) { |
|
336 | 27 | break; |
|
337 | } |
||
338 | 24 | $result[] = $parentId; |
|
339 | 27 | } |
|
340 | 27 | } else { |
|
341 | $parentId = null; |
||
342 | } |
||
343 | 27 | } |
|
344 | 27 | if ($cache && $depth === null) { |
|
345 | 24 | $this->_parentsIds = $result; |
|
346 | 24 | } |
|
347 | 27 | return $result; |
|
348 | } |
||
349 | |||
350 | /** |
||
351 | * @param int|null $depth |
||
352 | * @param bool $flat |
||
353 | * @param bool $cache |
||
354 | * @return array |
||
355 | */ |
||
356 | 24 | public function getDescendantsIds($depth = null, $flat = false, $cache = true) |
|
425 | |||
426 | /** |
||
427 | * Populate children relations for self and all descendants |
||
428 | * |
||
429 | * @param int $depth = null |
||
430 | * @return static |
||
431 | */ |
||
432 | 3 | public function populateTree($depth = null) |
|
464 | |||
465 | /** |
||
466 | * @return bool |
||
467 | */ |
||
468 | 81 | public function isRoot() |
|
469 | { |
||
470 | 81 | return $this->owner->getAttribute($this->parentAttribute) === null; |
|
471 | } |
||
472 | |||
473 | /** |
||
474 | * @param ActiveRecord $node |
||
475 | * @return bool |
||
476 | */ |
||
477 | 15 | public function isChildOf($node) |
|
478 | { |
||
479 | 15 | $ids = $this->getParentsIds(); |
|
480 | 15 | return in_array($node->getPrimaryKey(), $ids); |
|
481 | } |
||
482 | |||
483 | /** |
||
484 | * @return bool |
||
485 | */ |
||
486 | 3 | public function isLeaf() |
|
487 | { |
||
488 | 3 | return count($this->owner->children) === 0; |
|
489 | } |
||
490 | |||
491 | /** |
||
492 | * @return ActiveRecord |
||
493 | */ |
||
494 | 6 | public function makeRoot() |
|
495 | { |
||
496 | 6 | $this->operation = self::OPERATION_MAKE_ROOT; |
|
497 | 6 | return $this->owner; |
|
498 | } |
||
499 | |||
500 | /** |
||
501 | * @param ActiveRecord $node |
||
502 | * @return ActiveRecord |
||
503 | */ |
||
504 | 36 | public function prependTo($node) |
|
505 | { |
||
506 | 36 | $this->operation = self::OPERATION_PREPEND_TO; |
|
507 | 36 | $this->node = $node; |
|
508 | 36 | return $this->owner; |
|
509 | } |
||
510 | |||
511 | /** |
||
512 | * @param ActiveRecord $node |
||
513 | * @return ActiveRecord |
||
514 | */ |
||
515 | 36 | public function appendTo($node) |
|
516 | { |
||
517 | 36 | $this->operation = self::OPERATION_APPEND_TO; |
|
518 | 36 | $this->node = $node; |
|
519 | 36 | return $this->owner; |
|
520 | } |
||
521 | |||
522 | /** |
||
523 | * @param ActiveRecord $node |
||
524 | * @return ActiveRecord |
||
525 | */ |
||
526 | 30 | public function insertBefore($node) |
|
532 | |||
533 | /** |
||
534 | * @param ActiveRecord $node |
||
535 | * @return ActiveRecord |
||
536 | */ |
||
537 | 33 | public function insertAfter($node) |
|
538 | { |
||
539 | 33 | $this->operation = self::OPERATION_INSERT_AFTER; |
|
540 | 33 | $this->node = $node; |
|
541 | 33 | return $this->owner; |
|
542 | } |
||
543 | |||
544 | /** |
||
545 | * Need for paulzi/auto-tree |
||
546 | */ |
||
547 | public function preDeleteWithChildren() |
||
551 | |||
552 | /** |
||
553 | * @return bool|int |
||
554 | * @throws \Exception |
||
555 | * @throws \yii\db\Exception |
||
556 | */ |
||
557 | 12 | public function deleteWithChildren() |
|
579 | |||
580 | /** |
||
581 | * @param bool $middle |
||
582 | * @return int |
||
583 | */ |
||
584 | 3 | public function reorderChildren($middle = true) |
|
594 | |||
595 | /** |
||
596 | * @throws Exception |
||
597 | * @throws NotSupportedException |
||
598 | */ |
||
599 | 147 | public function beforeSave() |
|
634 | |||
635 | /** |
||
636 | * |
||
637 | */ |
||
638 | 99 | public function afterSave() |
|
643 | |||
644 | /** |
||
645 | * @param \yii\base\ModelEvent $event |
||
646 | * @throws Exception |
||
647 | */ |
||
648 | 21 | public function beforeDelete($event) |
|
658 | |||
659 | /** |
||
660 | * |
||
661 | */ |
||
662 | 12 | public function afterDelete() |
|
672 | |||
673 | /** |
||
674 | * @return mixed |
||
675 | * @throws Exception |
||
676 | */ |
||
677 | 63 | protected function getPrimaryKey() |
|
685 | |||
686 | /** |
||
687 | * @param bool $forInsertNear |
||
688 | * @throws Exception |
||
689 | */ |
||
690 | 135 | protected function checkNode($forInsertNear = false) |
|
707 | |||
708 | /** |
||
709 | * Append to operation internal handler |
||
710 | * @param bool $append |
||
711 | * @throws Exception |
||
712 | */ |
||
713 | 72 | protected function insertIntoInternal($append) |
|
725 | |||
726 | /** |
||
727 | * Insert operation internal handler |
||
728 | * @param bool $forward |
||
729 | * @throws Exception |
||
730 | */ |
||
731 | 63 | protected function insertNearInternal($forward) |
|
743 | |||
744 | /** |
||
745 | * @return int |
||
746 | */ |
||
747 | 12 | protected function deleteWithChildrenInternal() |
|
759 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.