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 NestedSetsBehavior 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 NestedSetsBehavior, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class NestedSetsBehavior extends Behavior |
||
24 | { |
||
25 | const OPERATION_MAKE_ROOT = 'makeRoot'; |
||
26 | const OPERATION_PREPEND_TO = 'prependTo'; |
||
27 | const OPERATION_APPEND_TO = 'appendTo'; |
||
28 | const OPERATION_INSERT_BEFORE = 'insertBefore'; |
||
29 | const OPERATION_INSERT_AFTER = 'insertAfter'; |
||
30 | const OPERATION_DELETE_WITH_CHILDREN = 'deleteWithChildren'; |
||
31 | |||
32 | /** |
||
33 | * @var string|false |
||
34 | */ |
||
35 | public $treeAttribute = false; |
||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | public $leftAttribute = 'lft'; |
||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | public $rightAttribute = 'rgt'; |
||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | public $depthAttribute = 'depth'; |
||
48 | /** |
||
49 | * @var string|null |
||
50 | */ |
||
51 | protected $operation; |
||
52 | /** |
||
53 | * @var ActiveRecord|null |
||
54 | */ |
||
55 | protected $node; |
||
56 | |||
57 | /** |
||
58 | * @inheritdoc |
||
59 | */ |
||
60 | 116 | public function events() |
|
71 | |||
72 | /** |
||
73 | * Creates the root node if the active record is new or moves it |
||
74 | * as the root node. |
||
75 | * @param boolean $runValidation |
||
76 | * @param array $attributes |
||
77 | * @return boolean |
||
78 | */ |
||
79 | 10 | public function makeRoot($runValidation = true, $attributes = null) |
|
85 | |||
86 | /** |
||
87 | * Creates a node as the first child of the target node if the active |
||
88 | * record is new or moves it as the first child of the target node. |
||
89 | * @param ActiveRecord $node |
||
90 | * @param boolean $runValidation |
||
91 | * @param array $attributes |
||
92 | * @return boolean |
||
93 | */ |
||
94 | 16 | View Code Duplication | public function prependTo($node, $runValidation = true, $attributes = null) |
101 | |||
102 | /** |
||
103 | * Creates a node as the last child of the target node if the active |
||
104 | * record is new or moves it as the last child of the target node. |
||
105 | * @param ActiveRecord $node |
||
106 | * @param boolean $runValidation |
||
107 | * @param array $attributes |
||
108 | * @return boolean |
||
109 | */ |
||
110 | 16 | View Code Duplication | public function appendTo($node, $runValidation = true, $attributes = null) |
117 | |||
118 | /** |
||
119 | * Creates a node as the previous sibling of the target node if the active |
||
120 | * record is new or moves it as the previous sibling of the target node. |
||
121 | * @param ActiveRecord $node |
||
122 | * @param boolean $runValidation |
||
123 | * @param array $attributes |
||
124 | * @return boolean |
||
125 | */ |
||
126 | 20 | View Code Duplication | public function insertBefore($node, $runValidation = true, $attributes = null) |
133 | |||
134 | /** |
||
135 | * Creates a node as the next sibling of the target node if the active |
||
136 | * record is new or moves it as the next sibling of the target node. |
||
137 | * @param ActiveRecord $node |
||
138 | * @param boolean $runValidation |
||
139 | * @param array $attributes |
||
140 | * @return boolean |
||
141 | */ |
||
142 | 20 | View Code Duplication | public function insertAfter($node, $runValidation = true, $attributes = null) |
149 | |||
150 | /** |
||
151 | * Deletes a node and its children. |
||
152 | * @return integer|false the number of rows deleted or false if |
||
153 | * the deletion is unsuccessful for some reason. |
||
154 | * @throws \Exception |
||
155 | */ |
||
156 | 4 | public function deleteWithChildren() |
|
157 | { |
||
158 | 4 | $this->operation = self::OPERATION_DELETE_WITH_CHILDREN; |
|
159 | |||
160 | 4 | if (!$this->owner->isTransactional(ActiveRecord::OP_DELETE)) { |
|
161 | return $this->deleteWithChildrenInternal(); |
||
162 | } |
||
163 | |||
164 | 4 | $transaction = $this->owner->getDb()->beginTransaction(); |
|
165 | |||
166 | try { |
||
167 | 4 | $result = $this->deleteWithChildrenInternal(); |
|
168 | |||
169 | 2 | if ($result === false) { |
|
170 | $transaction->rollBack(); |
||
171 | } else { |
||
172 | 2 | $transaction->commit(); |
|
173 | } |
||
174 | |||
175 | 2 | return $result; |
|
176 | 2 | } catch (\Exception $e) { |
|
177 | 2 | $transaction->rollBack(); |
|
178 | 2 | throw $e; |
|
179 | } |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @return integer|false the number of rows deleted or false if |
||
184 | * the deletion is unsuccessful for some reason. |
||
185 | */ |
||
186 | 4 | protected function deleteWithChildrenInternal() |
|
187 | { |
||
188 | 4 | if (!$this->owner->beforeDelete()) { |
|
189 | return false; |
||
190 | } |
||
191 | |||
192 | $condition = [ |
||
193 | 2 | 'and', |
|
194 | 2 | ['>=', $this->leftAttribute, $this->owner->getAttribute($this->leftAttribute)], |
|
195 | 2 | ['<=', $this->rightAttribute, $this->owner->getAttribute($this->rightAttribute)] |
|
196 | 2 | ]; |
|
197 | |||
198 | 2 | $this->applyTreeAttributeCondition($condition); |
|
199 | 2 | $result = $this->owner->deleteAll($condition); |
|
200 | 2 | $this->owner->setOldAttributes(null); |
|
201 | 2 | $this->owner->afterDelete(); |
|
202 | |||
203 | 2 | return $result; |
|
204 | } |
||
205 | |||
206 | /** |
||
207 | * Gets the parents of the node. |
||
208 | * @param integer|null $depth the depth |
||
209 | * @return \yii\db\ActiveQuery |
||
210 | */ |
||
211 | 2 | View Code Duplication | public function parents($depth = null) |
212 | { |
||
213 | $condition = [ |
||
214 | 2 | 'and', |
|
215 | 2 | ['<', $this->leftAttribute, $this->owner->getAttribute($this->leftAttribute)], |
|
216 | 2 | ['>', $this->rightAttribute, $this->owner->getAttribute($this->rightAttribute)], |
|
217 | 2 | ]; |
|
218 | |||
219 | 2 | if ($depth !== null) { |
|
220 | 2 | $condition[] = ['>=', $this->depthAttribute, $this->owner->getAttribute($this->depthAttribute) - $depth]; |
|
221 | 2 | } |
|
222 | |||
223 | 2 | $this->applyTreeAttributeCondition($condition); |
|
224 | |||
225 | 2 | return $this->owner->find()->andWhere($condition)->addOrderBy([$this->leftAttribute => SORT_ASC]); |
|
226 | } |
||
227 | |||
228 | /** |
||
229 | * Gets the children of the node. |
||
230 | * @param integer|null $depth the depth |
||
231 | * @return \yii\db\ActiveQuery |
||
232 | */ |
||
233 | 2 | View Code Duplication | public function children($depth = null) |
249 | |||
250 | /** |
||
251 | * Gets the leaves of the node. |
||
252 | * @return \yii\db\ActiveQuery |
||
253 | */ |
||
254 | 2 | public function leaves() |
|
267 | |||
268 | /** |
||
269 | * Gets the previous sibling of the node. |
||
270 | * @return \yii\db\ActiveQuery |
||
271 | */ |
||
272 | 2 | View Code Duplication | public function prev() |
279 | |||
280 | /** |
||
281 | * Gets the next sibling of the node. |
||
282 | * @return \yii\db\ActiveQuery |
||
283 | */ |
||
284 | 2 | View Code Duplication | public function next() |
291 | |||
292 | /** |
||
293 | * Determines whether the node is root. |
||
294 | * @return boolean whether the node is root |
||
295 | */ |
||
296 | 48 | public function isRoot() |
|
300 | |||
301 | /** |
||
302 | * Determines whether the node is child of the parent node. |
||
303 | * @param ActiveRecord $node the parent node |
||
304 | * @return boolean whether the node is child of the parent node |
||
305 | */ |
||
306 | 34 | public function isChildOf($node) |
|
307 | { |
||
308 | 34 | $result = $this->owner->getAttribute($this->leftAttribute) > $node->getAttribute($this->leftAttribute) |
|
309 | 34 | && $this->owner->getAttribute($this->rightAttribute) < $node->getAttribute($this->rightAttribute); |
|
310 | |||
311 | 34 | if ($result && $this->treeAttribute !== false) { |
|
312 | 2 | $result = $this->owner->getAttribute($this->treeAttribute) === $node->getAttribute($this->treeAttribute); |
|
313 | 2 | } |
|
314 | |||
315 | 34 | return $result; |
|
316 | } |
||
317 | |||
318 | /** |
||
319 | * Determines whether the node is leaf. |
||
320 | * @return boolean whether the node is leaf |
||
321 | */ |
||
322 | 6 | public function isLeaf() |
|
326 | |||
327 | /** |
||
328 | * @throws NotSupportedException |
||
329 | */ |
||
330 | 26 | public function beforeInsert() |
|
356 | |||
357 | /** |
||
358 | * @throws Exception |
||
359 | */ |
||
360 | 4 | protected function beforeInsertRootNode() |
|
370 | |||
371 | /** |
||
372 | * @param integer $value |
||
373 | * @param integer $depth |
||
374 | * @throws Exception |
||
375 | */ |
||
376 | 20 | protected function beforeInsertNode($value, $depth) |
|
396 | |||
397 | /** |
||
398 | * @throws Exception |
||
399 | */ |
||
400 | 10 | public function afterInsert() |
|
419 | |||
420 | /** |
||
421 | * @throws Exception |
||
422 | */ |
||
423 | 60 | public function beforeUpdate() |
|
424 | { |
||
425 | 60 | if ($this->node !== null && !$this->node->getIsNewRecord()) { |
|
426 | 44 | $this->node->refresh(); |
|
427 | 44 | } |
|
428 | |||
429 | 60 | switch ($this->operation) { |
|
430 | 60 | case self::OPERATION_MAKE_ROOT: |
|
431 | 6 | if ($this->treeAttribute === false) { |
|
432 | 2 | throw new Exception('Can not move a node as the root when "treeAttribute" is false.'); |
|
433 | } |
||
434 | |||
435 | 4 | if ($this->owner->isRoot()) { |
|
436 | 2 | throw new Exception('Can not move the root node as the root.'); |
|
437 | } |
||
438 | |||
439 | 2 | break; |
|
440 | 54 | case self::OPERATION_INSERT_BEFORE: |
|
441 | 54 | case self::OPERATION_INSERT_AFTER: |
|
442 | 28 | if ($this->node->isRoot()) { |
|
443 | 4 | throw new Exception('Can not move a node when the target node is root.'); |
|
444 | } |
||
445 | 50 | case self::OPERATION_PREPEND_TO: |
|
446 | 50 | case self::OPERATION_APPEND_TO: |
|
447 | 48 | if ($this->node->getIsNewRecord()) { |
|
448 | 8 | throw new Exception('Can not move a node when the target node is new record.'); |
|
449 | } |
||
450 | |||
451 | 40 | if ($this->owner->equals($this->node)) { |
|
452 | 8 | throw new Exception('Can not move a node when the target node is same.'); |
|
453 | } |
||
454 | |||
455 | 32 | if ($this->node->isChildOf($this->owner)) { |
|
456 | 8 | throw new Exception('Can not move a node when the target node is child.'); |
|
457 | } |
||
458 | 28 | } |
|
459 | 28 | } |
|
460 | |||
461 | /** |
||
462 | * @return void |
||
463 | */ |
||
464 | 28 | public function afterUpdate() |
|
465 | { |
||
466 | 28 | switch ($this->operation) { |
|
467 | 28 | case self::OPERATION_MAKE_ROOT: |
|
468 | 2 | $this->moveNodeAsRoot(); |
|
469 | 2 | break; |
|
470 | 26 | case self::OPERATION_PREPEND_TO: |
|
471 | 6 | $this->moveNode($this->node->getAttribute($this->leftAttribute) + 1, 1); |
|
472 | 6 | break; |
|
473 | 20 | case self::OPERATION_APPEND_TO: |
|
474 | 6 | $this->moveNode($this->node->getAttribute($this->rightAttribute), 1); |
|
475 | 6 | break; |
|
476 | 14 | case self::OPERATION_INSERT_BEFORE: |
|
477 | 6 | $this->moveNode($this->node->getAttribute($this->leftAttribute), 0); |
|
478 | 6 | break; |
|
479 | 8 | case self::OPERATION_INSERT_AFTER: |
|
480 | 6 | $this->moveNode($this->node->getAttribute($this->rightAttribute) + 1, 0); |
|
481 | 6 | break; |
|
482 | 2 | default: |
|
483 | 2 | return; |
|
484 | 28 | } |
|
485 | |||
486 | 26 | $this->operation = null; |
|
487 | 26 | $this->node = null; |
|
488 | 26 | } |
|
489 | |||
490 | /** |
||
491 | * @return void |
||
492 | */ |
||
493 | 2 | protected function moveNodeAsRoot() |
|
521 | |||
522 | /** |
||
523 | * @param integer $value |
||
524 | * @param integer $depth |
||
525 | */ |
||
526 | 24 | protected function moveNode($value, $depth) |
|
596 | |||
597 | /** |
||
598 | * @throws Exception |
||
599 | * @throws NotSupportedException |
||
600 | */ |
||
601 | 10 | public function beforeDelete() |
|
602 | { |
||
603 | 10 | if ($this->owner->getIsNewRecord()) { |
|
604 | 4 | throw new Exception('Can not delete a node when it is new record.'); |
|
605 | } |
||
606 | |||
607 | 6 | if ($this->owner->isRoot() && $this->operation !== self::OPERATION_DELETE_WITH_CHILDREN) { |
|
608 | 2 | throw new NotSupportedException('Method "'. get_class($this->owner) . '::delete" is not supported for deleting root nodes.'); |
|
609 | } |
||
610 | |||
611 | 4 | $this->owner->refresh(); |
|
612 | 4 | } |
|
613 | |||
614 | /** |
||
615 | * @return void |
||
616 | */ |
||
617 | 42 | public function afterDelete() |
|
649 | |||
650 | /** |
||
651 | * @param integer $value |
||
652 | * @param integer $delta |
||
653 | */ |
||
654 | 38 | protected function shiftLeftRightAttribute($value, $delta) |
|
668 | |||
669 | /** |
||
670 | * @param array $condition |
||
671 | */ |
||
672 | 48 | protected function applyTreeAttributeCondition(&$condition) |
|
683 | } |
||
684 |
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.