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 |
||
24 | class NestedSetsBehavior extends Behavior |
||
25 | { |
||
26 | const OPERATION_MAKE_ROOT = 1; |
||
27 | const OPERATION_PREPEND_TO = 2; |
||
28 | const OPERATION_APPEND_TO = 3; |
||
29 | const OPERATION_INSERT_BEFORE = 4; |
||
30 | const OPERATION_INSERT_AFTER = 5; |
||
31 | const OPERATION_DELETE_ALL = 6; |
||
32 | |||
33 | |||
34 | /** |
||
35 | * @var string|null |
||
36 | */ |
||
37 | public $treeAttribute; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | public $leftAttribute = 'lft'; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | public $rightAttribute = 'rgt'; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | public $depthAttribute = 'depth'; |
||
53 | |||
54 | /** |
||
55 | * @var string|null |
||
56 | */ |
||
57 | protected $operation; |
||
58 | |||
59 | /** |
||
60 | * @var ActiveRecord|self|null |
||
61 | */ |
||
62 | protected $node; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $treeChange; |
||
68 | |||
69 | |||
70 | /** |
||
71 | * @inheritdoc |
||
72 | */ |
||
73 | 198 | public function events() |
|
74 | { |
||
75 | return [ |
||
76 | 198 | ActiveRecord::EVENT_BEFORE_INSERT => 'beforeInsert', |
|
77 | 198 | ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert', |
|
78 | 198 | ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeUpdate', |
|
79 | 198 | ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate', |
|
80 | 198 | ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete', |
|
81 | 198 | ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete', |
|
82 | ]; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param int|null $depth |
||
87 | * @return \yii\db\ActiveQuery |
||
88 | */ |
||
89 | 6 | public function getParents($depth = null) |
|
90 | { |
||
91 | 6 | $tableName = $this->owner->tableName(); |
|
92 | $condition = [ |
||
93 | 6 | 'and', |
|
94 | 6 | ['<', "{$tableName}.[[{$this->leftAttribute}]]", $this->owner->getAttribute($this->leftAttribute)], |
|
95 | 6 | ['>', "{$tableName}.[[{$this->rightAttribute}]]", $this->owner->getAttribute($this->rightAttribute)], |
|
96 | ]; |
||
97 | 6 | View Code Duplication | if ($depth !== null) { |
|
|||
98 | 6 | $condition[] = ['>=', "{$tableName}.[[{$this->depthAttribute}]]", $this->owner->getAttribute($this->depthAttribute) - $depth]; |
|
99 | } |
||
100 | |||
101 | 6 | $query = $this->owner->find() |
|
102 | 6 | ->andWhere($condition) |
|
103 | 6 | ->andWhere($this->treeCondition()) |
|
104 | 6 | ->addOrderBy(["{$tableName}.[[{$this->leftAttribute}]]" => SORT_ASC]); |
|
105 | 6 | $query->multiple = true; |
|
106 | |||
107 | 6 | return $query; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return \yii\db\ActiveQuery |
||
112 | */ |
||
113 | 3 | View Code Duplication | public function getParent() |
122 | |||
123 | /** |
||
124 | * @return \yii\db\ActiveQuery |
||
125 | */ |
||
126 | 3 | View Code Duplication | public function getRoot() |
136 | |||
137 | /** |
||
138 | * @param int|null $depth |
||
139 | * @param bool $andSelf |
||
140 | * @param bool $backOrder |
||
141 | * @return \yii\db\ActiveQuery |
||
142 | */ |
||
143 | 78 | public function getDescendants($depth = null, $andSelf = false, $backOrder = false) |
|
144 | { |
||
145 | 78 | $tableName = $this->owner->tableName(); |
|
146 | 78 | $attribute = $backOrder ? $this->rightAttribute : $this->leftAttribute; |
|
147 | $condition = [ |
||
148 | 78 | 'and', |
|
149 | 78 | [$andSelf ? '>=' : '>', "{$tableName}.[[{$attribute}]]", $this->owner->getAttribute($this->leftAttribute)], |
|
150 | 78 | [$andSelf ? '<=' : '<', "{$tableName}.[[{$attribute}]]", $this->owner->getAttribute($this->rightAttribute)], |
|
151 | ]; |
||
152 | |||
153 | 78 | View Code Duplication | if ($depth !== null) { |
154 | 12 | $condition[] = ['<=', "{$tableName}.[[{$this->depthAttribute}]]", $this->owner->getAttribute($this->depthAttribute) + $depth]; |
|
155 | } |
||
156 | |||
157 | 78 | $query = $this->owner->find() |
|
158 | 78 | ->andWhere($condition) |
|
159 | 78 | ->andWhere($this->treeCondition()) |
|
160 | 78 | ->addOrderBy(["{$tableName}.[[{$attribute}]]" => $backOrder ? SORT_DESC : SORT_ASC]); |
|
161 | 78 | $query->multiple = true; |
|
162 | |||
163 | 78 | return $query; |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * @return \yii\db\ActiveQuery |
||
168 | */ |
||
169 | 3 | public function getChildren() |
|
173 | |||
174 | /** |
||
175 | * @param int|null $depth |
||
176 | * @return \yii\db\ActiveQuery |
||
177 | */ |
||
178 | 3 | public function getLeaves($depth = null) |
|
186 | |||
187 | /** |
||
188 | * @return \yii\db\ActiveQuery |
||
189 | */ |
||
190 | 3 | View Code Duplication | public function getPrev() |
200 | |||
201 | /** |
||
202 | * @return \yii\db\ActiveQuery |
||
203 | */ |
||
204 | 3 | View Code Duplication | public function getNext() |
214 | |||
215 | /** |
||
216 | * Populate children relations for self and all descendants |
||
217 | * @param int $depth = null |
||
218 | * @return static |
||
219 | */ |
||
220 | 3 | public function populateTree($depth = null) |
|
221 | { |
||
222 | /** @var ActiveRecord[]|static[] $nodes */ |
||
223 | 3 | if ($depth === null) { |
|
224 | 3 | $nodes = $this->owner->descendants; |
|
225 | } else { |
||
226 | 3 | $nodes = $this->getDescendants($depth)->all(); |
|
227 | } |
||
228 | |||
229 | 3 | $key = $this->owner->getAttribute($this->leftAttribute); |
|
230 | 3 | $relates = []; |
|
231 | 3 | $parents = [$key]; |
|
232 | 3 | $prev = $this->owner->getAttribute($this->depthAttribute); |
|
233 | 3 | foreach($nodes as $node) |
|
234 | { |
||
235 | 3 | $level = $node->getAttribute($this->depthAttribute); |
|
236 | 3 | if ($level <= $prev) { |
|
237 | 3 | $parents = array_slice($parents, 0, $level - $prev - 1); |
|
238 | } |
||
239 | |||
240 | 3 | $key = end($parents); |
|
241 | 3 | if (!isset($relates[$key])) { |
|
242 | 3 | $relates[$key] = []; |
|
243 | } |
||
244 | 3 | $relates[$key][] = $node; |
|
245 | |||
246 | 3 | $parents[] = $node->getAttribute($this->leftAttribute); |
|
247 | 3 | $prev = $level; |
|
248 | } |
||
249 | |||
250 | 3 | $ownerDepth = $this->owner->getAttribute($this->depthAttribute); |
|
251 | 3 | $nodes[] = $this->owner; |
|
252 | 3 | foreach ($nodes as $node) { |
|
253 | 3 | $key = $node->getAttribute($this->leftAttribute); |
|
254 | 3 | if (isset($relates[$key])) { |
|
255 | 3 | $node->populateRelation('children', $relates[$key]); |
|
256 | 3 | } elseif ($depth === null || $ownerDepth + $depth > $node->getAttribute($this->depthAttribute)) { |
|
257 | 3 | $node->populateRelation('children', []); |
|
258 | } |
||
259 | } |
||
260 | |||
261 | 3 | return $this->owner; |
|
262 | } |
||
263 | |||
264 | /** |
||
265 | * @return bool |
||
266 | */ |
||
267 | 66 | public function isRoot() |
|
271 | |||
272 | /** |
||
273 | * @param ActiveRecord $node |
||
274 | * @return bool |
||
275 | */ |
||
276 | 69 | public function isChildOf($node) |
|
277 | { |
||
278 | 69 | $result = $this->owner->getAttribute($this->leftAttribute) > $node->getAttribute($this->leftAttribute) |
|
279 | 69 | && $this->owner->getAttribute($this->rightAttribute) < $node->getAttribute($this->rightAttribute); |
|
280 | |||
281 | 69 | View Code Duplication | if ($result && $this->treeAttribute !== null) { |
282 | 6 | $result = $this->owner->getAttribute($this->treeAttribute) === $node->getAttribute($this->treeAttribute); |
|
283 | } |
||
284 | |||
285 | 69 | return $result; |
|
286 | } |
||
287 | |||
288 | /** |
||
289 | * @return bool |
||
290 | */ |
||
291 | 6 | public function isLeaf() |
|
295 | |||
296 | /** |
||
297 | * @return ActiveRecord |
||
298 | */ |
||
299 | 9 | public function makeRoot() |
|
304 | |||
305 | /** |
||
306 | * @param ActiveRecord $node |
||
307 | * @return ActiveRecord |
||
308 | */ |
||
309 | 33 | public function prependTo($node) |
|
315 | |||
316 | /** |
||
317 | * @param ActiveRecord $node |
||
318 | * @return ActiveRecord |
||
319 | */ |
||
320 | 33 | public function appendTo($node) |
|
326 | |||
327 | /** |
||
328 | * @param ActiveRecord $node |
||
329 | * @return ActiveRecord |
||
330 | */ |
||
331 | 27 | public function insertBefore($node) |
|
337 | |||
338 | /** |
||
339 | * @param ActiveRecord $node |
||
340 | * @return ActiveRecord |
||
341 | */ |
||
342 | 30 | public function insertAfter($node) |
|
348 | |||
349 | /** |
||
350 | * Need for paulzi/auto-tree |
||
351 | */ |
||
352 | public function preDeleteWithChildren() |
||
356 | |||
357 | /** |
||
358 | * @return bool|int |
||
359 | * @throws \Exception |
||
360 | * @throws \yii\db\Exception |
||
361 | */ |
||
362 | 9 | public function deleteWithChildren() |
|
363 | { |
||
364 | 9 | $this->operation = self::OPERATION_DELETE_ALL; |
|
365 | 9 | if (!$this->owner->isTransactional(ActiveRecord::OP_DELETE)) { |
|
366 | $transaction = $this->owner->getDb()->beginTransaction(); |
||
367 | try { |
||
368 | $result = $this->deleteWithChildrenInternal(); |
||
369 | if ($result === false) { |
||
370 | $transaction->rollBack(); |
||
371 | } else { |
||
372 | $transaction->commit(); |
||
373 | } |
||
374 | return $result; |
||
375 | } catch (\Exception $e) { |
||
376 | $transaction->rollBack(); |
||
377 | throw $e; |
||
378 | } |
||
379 | } else { |
||
380 | 9 | $result = $this->deleteWithChildrenInternal(); |
|
381 | } |
||
382 | 6 | return $result; |
|
383 | } |
||
384 | |||
385 | /** |
||
386 | * @throws Exception |
||
387 | * @throws NotSupportedException |
||
388 | */ |
||
389 | 48 | public function beforeInsert() |
|
390 | { |
||
391 | 48 | if ($this->node !== null && !$this->node->getIsNewRecord()) { |
|
392 | 27 | $this->node->refresh(); |
|
393 | } |
||
394 | 48 | switch ($this->operation) { |
|
395 | 48 | case self::OPERATION_MAKE_ROOT: |
|
396 | 6 | $condition = array_merge([$this->leftAttribute => 1], $this->treeCondition()); |
|
397 | 6 | if ($this->owner->find()->andWhere($condition)->one() !== null) { |
|
398 | 3 | throw new Exception('Can not create more than one root.'); |
|
399 | } |
||
400 | 3 | $this->owner->setAttribute($this->leftAttribute, 1); |
|
401 | 3 | $this->owner->setAttribute($this->rightAttribute, 2); |
|
402 | 3 | $this->owner->setAttribute($this->depthAttribute, 0); |
|
403 | 3 | break; |
|
404 | |||
405 | 42 | case self::OPERATION_PREPEND_TO: |
|
406 | 9 | $this->insertNode($this->node->getAttribute($this->leftAttribute) + 1, 1); |
|
407 | 6 | break; |
|
408 | |||
409 | 33 | case self::OPERATION_APPEND_TO: |
|
410 | 9 | $this->insertNode($this->node->getAttribute($this->rightAttribute), 1); |
|
411 | 6 | break; |
|
412 | |||
413 | 24 | case self::OPERATION_INSERT_BEFORE: |
|
414 | 9 | $this->insertNode($this->node->getAttribute($this->leftAttribute), 0); |
|
415 | 6 | break; |
|
416 | |||
417 | 15 | case self::OPERATION_INSERT_AFTER: |
|
418 | 12 | $this->insertNode($this->node->getAttribute($this->rightAttribute) + 1, 0); |
|
419 | 6 | break; |
|
420 | |||
421 | default: |
||
422 | 3 | throw new NotSupportedException('Method "'. $this->owner->className() . '::insert" is not supported for inserting new nodes.'); |
|
423 | } |
||
424 | 27 | } |
|
425 | |||
426 | /** |
||
427 | * @throws Exception |
||
428 | */ |
||
429 | 27 | public function afterInsert() |
|
430 | { |
||
431 | 27 | if ($this->operation === self::OPERATION_MAKE_ROOT && $this->treeAttribute !== null && $this->owner->getAttribute($this->treeAttribute) === null) { |
|
432 | 3 | $id = $this->owner->getPrimaryKey(); |
|
433 | 3 | $this->owner->setAttribute($this->treeAttribute, $id); |
|
434 | |||
435 | 3 | $primaryKey = $this->owner->primaryKey(); |
|
436 | 3 | if (!isset($primaryKey[0])) { |
|
437 | throw new Exception('"' . $this->owner->className() . '" must have a primary key.'); |
||
438 | } |
||
439 | |||
440 | 3 | $this->owner->updateAll([$this->treeAttribute => $id], [$primaryKey[0] => $id]); |
|
441 | } |
||
442 | 27 | $this->operation = null; |
|
443 | 27 | $this->node = null; |
|
444 | 27 | } |
|
445 | |||
446 | /** |
||
447 | * @throws Exception |
||
448 | */ |
||
449 | 90 | public function beforeUpdate() |
|
450 | { |
||
451 | 90 | if ($this->node !== null && !$this->node->getIsNewRecord()) { |
|
452 | 78 | $this->node->refresh(); |
|
453 | } |
||
454 | |||
455 | 90 | switch ($this->operation) { |
|
456 | 90 | case self::OPERATION_MAKE_ROOT: |
|
457 | 3 | if ($this->treeAttribute === null) { |
|
458 | throw new Exception('Can not move a node as the root when "treeAttribute" is not set.'); |
||
459 | } |
||
460 | 3 | if ($this->owner->getOldAttribute($this->treeAttribute) !== $this->owner->getAttribute($this->treeAttribute)) { |
|
461 | 3 | $this->treeChange = $this->owner->getAttribute($this->treeAttribute); |
|
462 | 3 | $this->owner->setAttribute($this->treeAttribute, $this->owner->getOldAttribute($this->treeAttribute)); |
|
463 | } |
||
464 | 3 | break; |
|
465 | |||
466 | 87 | case self::OPERATION_INSERT_BEFORE: |
|
467 | 69 | case self::OPERATION_INSERT_AFTER: |
|
468 | 36 | if ($this->node->isRoot()) { |
|
469 | throw new Exception('Can not move a node before/after root.'); |
||
470 | } |
||
471 | |||
472 | 51 | case self::OPERATION_PREPEND_TO: |
|
473 | 27 | case self::OPERATION_APPEND_TO: |
|
474 | 84 | if ($this->node->getIsNewRecord()) { |
|
475 | 6 | throw new Exception('Can not move a node when the target node is new record.'); |
|
476 | } |
||
477 | |||
478 | 78 | if ($this->owner->equals($this->node)) { |
|
479 | 12 | throw new Exception('Can not move a node when the target node is same.'); |
|
480 | } |
||
481 | |||
482 | 66 | if ($this->node->isChildOf($this->owner)) { |
|
483 | 12 | throw new Exception('Can not move a node when the target node is child.'); |
|
484 | } |
||
485 | } |
||
486 | 60 | } |
|
487 | |||
488 | /** |
||
489 | * |
||
490 | */ |
||
491 | 60 | public function afterUpdate() |
|
492 | { |
||
493 | 60 | switch ($this->operation) { |
|
494 | 60 | case self::OPERATION_MAKE_ROOT: |
|
495 | 3 | $this->moveNodeAsRoot(); |
|
496 | 3 | break; |
|
497 | |||
498 | 57 | case self::OPERATION_PREPEND_TO: |
|
499 | 15 | $this->moveNode($this->node->getAttribute($this->leftAttribute) + 1, 1); |
|
500 | 15 | break; |
|
501 | |||
502 | 42 | case self::OPERATION_APPEND_TO: |
|
503 | 15 | $this->moveNode($this->node->getAttribute($this->rightAttribute), 1); |
|
504 | 15 | break; |
|
505 | |||
506 | 27 | case self::OPERATION_INSERT_BEFORE: |
|
507 | 12 | $this->moveNode($this->node->getAttribute($this->leftAttribute), 0); |
|
508 | 12 | break; |
|
509 | |||
510 | 15 | case self::OPERATION_INSERT_AFTER: |
|
511 | 12 | $this->moveNode($this->node->getAttribute($this->rightAttribute) + 1, 0); |
|
512 | 12 | break; |
|
513 | } |
||
514 | 60 | $this->operation = null; |
|
515 | 60 | $this->node = null; |
|
516 | 60 | $this->treeChange = null; |
|
517 | 60 | } |
|
518 | |||
519 | /** |
||
520 | * @throws Exception |
||
521 | */ |
||
522 | 18 | public function beforeDelete() |
|
532 | |||
533 | /** |
||
534 | * |
||
535 | */ |
||
536 | 9 | public function afterDelete() |
|
537 | { |
||
538 | 9 | $left = $this->owner->getAttribute($this->leftAttribute); |
|
539 | 9 | $right = $this->owner->getAttribute($this->rightAttribute); |
|
540 | 9 | if ($this->operation === static::OPERATION_DELETE_ALL || $this->isLeaf()) { |
|
541 | 6 | $this->shift($right + 1, null, $left - $right - 1); |
|
542 | } else { |
||
543 | 3 | $this->owner->updateAll( |
|
544 | [ |
||
545 | 3 | $this->leftAttribute => new Expression("[[{$this->leftAttribute}]] - 1"), |
|
546 | 3 | $this->rightAttribute => new Expression("[[{$this->rightAttribute}]] - 1"), |
|
547 | 3 | $this->depthAttribute => new Expression("[[{$this->depthAttribute}]] - 1"), |
|
548 | ], |
||
549 | 3 | $this->getDescendants()->where |
|
550 | ); |
||
551 | 3 | $this->shift($right + 1, null, -2); |
|
552 | } |
||
553 | 9 | $this->operation = null; |
|
554 | 9 | $this->node = null; |
|
555 | 9 | } |
|
556 | |||
557 | /** |
||
558 | * @return int |
||
559 | */ |
||
560 | 9 | protected function deleteWithChildrenInternal() |
|
570 | |||
571 | /** |
||
572 | * @param int $to |
||
573 | * @param int $depth |
||
574 | * @throws Exception |
||
575 | */ |
||
576 | 39 | protected function insertNode($to, $depth = 0) |
|
577 | { |
||
578 | 39 | if ($this->node->getIsNewRecord()) { |
|
579 | 12 | throw new Exception('Can not create a node when the target node is new record.'); |
|
580 | } |
||
581 | |||
582 | 27 | if ($depth === 0 && $this->node->isRoot()) { |
|
583 | 3 | throw new Exception('Can not insert a node before/after root.'); |
|
584 | } |
||
585 | 24 | $this->owner->setAttribute($this->leftAttribute, $to); |
|
586 | 24 | $this->owner->setAttribute($this->rightAttribute, $to + 1); |
|
587 | 24 | $this->owner->setAttribute($this->depthAttribute, $this->node->getAttribute($this->depthAttribute) + $depth); |
|
588 | 24 | if ($this->treeAttribute !== null) { |
|
589 | 24 | $this->owner->setAttribute($this->treeAttribute, $this->node->getAttribute($this->treeAttribute)); |
|
590 | } |
||
591 | 24 | $this->shift($to, null, 2); |
|
592 | 24 | } |
|
593 | |||
594 | /** |
||
595 | * @param int $to |
||
596 | * @param int $depth |
||
597 | * @throws Exception |
||
598 | */ |
||
599 | 54 | protected function moveNode($to, $depth = 0) |
|
600 | { |
||
601 | 54 | $left = $this->owner->getAttribute($this->leftAttribute); |
|
602 | 54 | $right = $this->owner->getAttribute($this->rightAttribute); |
|
603 | 54 | $depth = $this->owner->getAttribute($this->depthAttribute) - $this->node->getAttribute($this->depthAttribute) - $depth; |
|
604 | 54 | if ($this->treeAttribute === null || $this->owner->getAttribute($this->treeAttribute) === $this->node->getAttribute($this->treeAttribute)) { |
|
605 | // same root |
||
606 | 42 | $this->owner->updateAll( |
|
607 | 42 | [$this->depthAttribute => new Expression("-[[{$this->depthAttribute}]]" . sprintf('%+d', $depth))], |
|
608 | 42 | $this->getDescendants(null, true)->where |
|
609 | ); |
||
610 | 42 | $delta = $right - $left + 1; |
|
611 | 42 | if ($left >= $to) { |
|
612 | 24 | $this->shift($to, $left - 1, $delta); |
|
613 | 24 | $delta = $to - $left; |
|
614 | } else { |
||
615 | 18 | $this->shift($right + 1, $to - 1, -$delta); |
|
616 | 18 | $delta = $to - $right - 1; |
|
617 | } |
||
618 | 42 | $this->owner->updateAll( |
|
619 | [ |
||
620 | 42 | $this->leftAttribute => new Expression("[[{$this->leftAttribute}]]" . sprintf('%+d', $delta)), |
|
621 | 42 | $this->rightAttribute => new Expression("[[{$this->rightAttribute}]]" . sprintf('%+d', $delta)), |
|
622 | 42 | $this->depthAttribute => new Expression("-[[{$this->depthAttribute}]]"), |
|
623 | ], |
||
624 | [ |
||
625 | 42 | 'and', |
|
626 | 42 | $this->getDescendants(null, true)->where, |
|
627 | 42 | ['<', $this->depthAttribute, 0], |
|
628 | ] |
||
629 | ); |
||
630 | } else { |
||
631 | // move from other root |
||
632 | 12 | $tree = $this->node->getAttribute($this->treeAttribute); |
|
633 | 12 | $this->shift($to, null, $right - $left + 1, $tree); |
|
634 | 12 | $delta = $to - $left; |
|
635 | 12 | $this->owner->updateAll( |
|
636 | [ |
||
637 | 12 | $this->leftAttribute => new Expression("[[{$this->leftAttribute}]]" . sprintf('%+d', $delta)), |
|
638 | 12 | $this->rightAttribute => new Expression("[[{$this->rightAttribute}]]" . sprintf('%+d', $delta)), |
|
639 | 12 | $this->depthAttribute => new Expression("[[{$this->depthAttribute}]]" . sprintf('%+d', -$depth)), |
|
640 | 12 | $this->treeAttribute => $tree, |
|
641 | ], |
||
642 | 12 | $this->getDescendants(null, true)->where |
|
643 | ); |
||
644 | 12 | $this->shift($right + 1, null, $left - $right - 1); |
|
645 | } |
||
646 | 54 | } |
|
647 | |||
648 | /** |
||
649 | * |
||
650 | */ |
||
651 | 3 | protected function moveNodeAsRoot() |
|
652 | { |
||
653 | 3 | $left = $this->owner->getAttribute($this->leftAttribute); |
|
654 | 3 | $right = $this->owner->getAttribute($this->rightAttribute); |
|
655 | 3 | $depth = $this->owner->getAttribute($this->depthAttribute); |
|
656 | 3 | $tree = $this->treeChange ? $this->treeChange : $this->owner->getPrimaryKey(); |
|
657 | |||
658 | 3 | $this->owner->updateAll( |
|
659 | [ |
||
660 | 3 | $this->leftAttribute => new Expression("[[{$this->leftAttribute}]]" . sprintf('%+d', 1 - $left)), |
|
661 | 3 | $this->rightAttribute => new Expression("[[{$this->rightAttribute}]]" . sprintf('%+d', 1 - $left)), |
|
662 | 3 | $this->depthAttribute => new Expression("[[{$this->depthAttribute}]]" . sprintf('%+d', -$depth)), |
|
663 | 3 | $this->treeAttribute => $tree, |
|
664 | ], |
||
665 | 3 | $this->getDescendants(null, true)->where |
|
666 | ); |
||
667 | 3 | $this->shift($right + 1, null, $left - $right - 1); |
|
668 | 3 | } |
|
669 | |||
670 | |||
671 | |||
672 | /** |
||
673 | * @param int $from |
||
674 | * @param int $to |
||
675 | * @param int $delta |
||
676 | * @param int|null $tree |
||
677 | */ |
||
678 | 90 | protected function shift($from, $to, $delta, $tree = null) |
|
679 | { |
||
680 | 90 | if ($delta !== 0 && ($to === null || $to >= $from)) { |
|
681 | 78 | View Code Duplication | if ($this->treeAttribute !== null && $tree === null) { |
682 | 78 | $tree = $this->owner->getAttribute($this->treeAttribute); |
|
683 | } |
||
684 | 78 | foreach ([$this->leftAttribute, $this->rightAttribute] as $i => $attribute) { |
|
685 | 78 | $this->owner->updateAll( |
|
686 | 78 | [$attribute => new Expression("[[{$attribute}]]" . sprintf('%+d', $delta))], |
|
687 | [ |
||
688 | 78 | 'and', |
|
689 | 78 | $to === null ? ['>=', $attribute, $from] : ['between', $attribute, $from, $to], |
|
690 | 78 | $this->treeAttribute !== null ? [$this->treeAttribute => $tree] : [], |
|
691 | ] |
||
692 | ); |
||
693 | } |
||
694 | } |
||
695 | 90 | } |
|
696 | |||
697 | /** |
||
698 | * @return array |
||
699 | */ |
||
700 | 99 | protected function treeCondition() |
|
709 | } |
||
710 |
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.