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 NestedIntervalsBehavior 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 NestedIntervalsBehavior, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class NestedIntervalsBehavior extends Behavior |
||
25 | { |
||
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|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 int[] |
||
56 | */ |
||
57 | public $range = [0, 2147483647]; |
||
58 | |||
59 | /** |
||
60 | * @var int|int[] Average amount of children in each level |
||
61 | */ |
||
62 | public $amountOptimize = 10; |
||
63 | |||
64 | /** |
||
65 | * @var float |
||
66 | */ |
||
67 | public $reserveFactor = 1; |
||
68 | |||
69 | /** |
||
70 | * @var bool |
||
71 | */ |
||
72 | public $noPrepend = false; |
||
73 | |||
74 | /** |
||
75 | * @var bool |
||
76 | */ |
||
77 | public $noAppend = false; |
||
78 | |||
79 | /** |
||
80 | * @var bool |
||
81 | */ |
||
82 | public $noInsert = false; |
||
83 | |||
84 | /** |
||
85 | * @var string|null |
||
86 | */ |
||
87 | protected $operation; |
||
88 | |||
89 | /** |
||
90 | * @var ActiveRecord|self|null |
||
91 | */ |
||
92 | protected $node; |
||
93 | |||
94 | /** |
||
95 | * @var string |
||
96 | */ |
||
97 | protected $treeChange; |
||
98 | |||
99 | |||
100 | /** |
||
101 | * @inheritdoc |
||
102 | */ |
||
103 | 299 | public function events() |
|
104 | { |
||
105 | return [ |
||
106 | 299 | ActiveRecord::EVENT_BEFORE_INSERT => 'beforeInsert', |
|
107 | 299 | ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert', |
|
108 | 299 | ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeUpdate', |
|
109 | 299 | ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate', |
|
110 | 299 | ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete', |
|
111 | 299 | ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete', |
|
112 | 299 | ]; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param int|null $depth |
||
117 | * @return \yii\db\ActiveQuery |
||
118 | */ |
||
119 | 73 | public function getParents($depth = null) |
|
120 | { |
||
121 | 73 | $tableName = $this->owner->tableName(); |
|
122 | $condition = [ |
||
123 | 73 | 'and', |
|
124 | 73 | ['<', "{$tableName}.[[{$this->leftAttribute}]]", $this->owner->getAttribute($this->leftAttribute)], |
|
125 | 73 | ['>', "{$tableName}.[[{$this->rightAttribute}]]", $this->owner->getAttribute($this->rightAttribute)], |
|
126 | 73 | ]; |
|
127 | 73 | View Code Duplication | if ($depth !== null) { |
|
|||
128 | 73 | $condition[] = ['>=', "{$tableName}.[[{$this->depthAttribute}]]", $this->owner->getAttribute($this->depthAttribute) - $depth]; |
|
129 | 73 | } |
|
130 | |||
131 | 73 | $query = $this->owner->find() |
|
132 | 73 | ->andWhere($condition) |
|
133 | 73 | ->andWhere($this->treeCondition()) |
|
134 | 73 | ->addOrderBy(["{$tableName}.[[{$this->leftAttribute}]]" => SORT_ASC]); |
|
135 | 73 | $query->multiple = true; |
|
136 | |||
137 | 73 | return $query; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * @return \yii\db\ActiveQuery |
||
142 | */ |
||
143 | 68 | public function getParent() |
|
144 | { |
||
145 | 68 | $tableName = $this->owner->tableName(); |
|
146 | 68 | $query = $this->getParents(1) |
|
147 | 68 | ->orderBy(["{$tableName}.[[{$this->leftAttribute}]]" => SORT_DESC]) |
|
148 | 68 | ->limit(1); |
|
149 | 68 | $query->multiple = false; |
|
150 | 68 | return $query; |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * @return \yii\db\ActiveQuery |
||
155 | */ |
||
156 | 21 | public function getRoot() |
|
157 | { |
||
158 | 21 | $tableName = $this->owner->tableName(); |
|
159 | 21 | $query = $this->owner->find() |
|
160 | 21 | ->andWhere(["{$tableName}.[[{$this->leftAttribute}]]" => $this->range[0]]) |
|
161 | 21 | ->andWhere($this->treeCondition()) |
|
162 | 21 | ->limit(1); |
|
163 | 21 | $query->multiple = false; |
|
164 | 21 | return $query; |
|
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param int|null $depth |
||
169 | * @param bool $andSelf |
||
170 | * @param bool $backOrder |
||
171 | * @return \yii\db\ActiveQuery |
||
172 | */ |
||
173 | 174 | public function getDescendants($depth = null, $andSelf = false, $backOrder = false) |
|
174 | { |
||
175 | 174 | $tableName = $this->owner->tableName(); |
|
176 | 174 | $attribute = $backOrder ? $this->rightAttribute : $this->leftAttribute; |
|
177 | $condition = [ |
||
178 | 174 | 'and', |
|
179 | 174 | [$andSelf ? '>=' : '>', "{$tableName}.[[{$attribute}]]", $this->owner->getAttribute($this->leftAttribute)], |
|
180 | 174 | [$andSelf ? '<=' : '<', "{$tableName}.[[{$attribute}]]", $this->owner->getAttribute($this->rightAttribute)], |
|
181 | 174 | ]; |
|
182 | |||
183 | 174 | View Code Duplication | if ($depth !== null) { |
184 | 129 | $condition[] = ['<=', "{$tableName}.[[{$this->depthAttribute}]]", $this->owner->getAttribute($this->depthAttribute) + $depth]; |
|
185 | 129 | } |
|
186 | |||
187 | 174 | $query = $this->owner->find() |
|
188 | 174 | ->andWhere($condition) |
|
189 | 174 | ->andWhere($this->treeCondition()) |
|
190 | 174 | ->addOrderBy(["{$tableName}.[[{$attribute}]]" => $backOrder ? SORT_DESC : SORT_ASC]); |
|
191 | 174 | $query->multiple = true; |
|
192 | |||
193 | 174 | return $query; |
|
194 | 1 | } |
|
195 | |||
196 | /** |
||
197 | * @return \yii\db\ActiveQuery |
||
198 | */ |
||
199 | 114 | public function getChildren() |
|
203 | |||
204 | /** |
||
205 | * @param int|null $depth |
||
206 | * @return \yii\db\ActiveQuery |
||
207 | */ |
||
208 | 5 | public function getLeaves($depth = null) |
|
209 | { |
||
210 | 5 | $tableName = $this->owner->tableName(); |
|
211 | $condition = [ |
||
212 | 5 | 'and', |
|
213 | 5 | ['>', "leaves.[[{$this->leftAttribute}]]", new Expression("{$tableName}.[[{$this->leftAttribute}]]")], |
|
214 | 5 | ['<', "leaves.[[{$this->leftAttribute}]]", new Expression("{$tableName}.[[{$this->rightAttribute}]]")], |
|
215 | 5 | ]; |
|
216 | |||
217 | 5 | if ($this->treeAttribute !== null) { |
|
218 | 5 | $condition[] = ["leaves.[[{$this->treeAttribute}]]" => new Expression("{$tableName}.[[{$this->treeAttribute}]]")]; |
|
219 | 5 | } |
|
220 | |||
221 | 5 | $query = $this->getDescendants($depth) |
|
222 | 5 | ->leftJoin("{$tableName} leaves", $condition) |
|
223 | 5 | ->andWhere(["leaves.[[{$this->leftAttribute}]]" => null]); |
|
224 | 5 | $query->multiple = true; |
|
225 | 5 | return $query; |
|
226 | } |
||
227 | |||
228 | /** |
||
229 | * @return \yii\db\ActiveQuery |
||
230 | */ |
||
231 | 30 | View Code Duplication | public function getPrev() |
232 | { |
||
233 | 30 | $tableName = $this->owner->tableName(); |
|
234 | 30 | $query = $this->owner->find() |
|
235 | 30 | ->andWhere([ |
|
236 | 30 | 'and', |
|
237 | 30 | ['<', "{$tableName}.[[{$this->rightAttribute}]]", $this->owner->getAttribute($this->leftAttribute)], |
|
238 | 30 | ['>', "{$tableName}.[[{$this->rightAttribute}]]", $this->getParent()->select([$this->leftAttribute])], |
|
239 | 30 | ]) |
|
240 | 30 | ->andWhere($this->treeCondition()) |
|
241 | 30 | ->orderBy(["{$tableName}.[[{$this->rightAttribute}]]" => SORT_DESC]) |
|
242 | 30 | ->limit(1); |
|
243 | 30 | $query->multiple = false; |
|
244 | 30 | return $query; |
|
245 | } |
||
246 | |||
247 | /** |
||
248 | * @return \yii\db\ActiveQuery |
||
249 | */ |
||
250 | 33 | View Code Duplication | public function getNext() |
251 | { |
||
252 | 33 | $tableName = $this->owner->tableName(); |
|
253 | 33 | $query = $this->owner->find() |
|
254 | 33 | ->andWhere([ |
|
255 | 33 | 'and', |
|
256 | 33 | ['>', "{$tableName}.[[{$this->leftAttribute}]]", $this->owner->getAttribute($this->rightAttribute)], |
|
257 | 33 | ['<', "{$tableName}.[[{$this->leftAttribute}]]", $this->getParent()->select([$this->rightAttribute])], |
|
258 | 33 | ]) |
|
259 | 33 | ->andWhere($this->treeCondition()) |
|
260 | 33 | ->orderBy(["{$tableName}.[[{$this->leftAttribute}]]" => SORT_ASC]) |
|
261 | 33 | ->limit(1); |
|
262 | 33 | $query->multiple = false; |
|
263 | 33 | return $query; |
|
264 | } |
||
265 | |||
266 | /** |
||
267 | * Populate children relations for self and all descendants |
||
268 | * @param int $depth = null |
||
269 | * @return static |
||
270 | */ |
||
271 | 5 | public function populateTree($depth = null) |
|
272 | { |
||
273 | /** @var ActiveRecord[]|static[] $nodes */ |
||
274 | 5 | if ($depth === null) { |
|
275 | 5 | $nodes = $this->owner->descendants; |
|
276 | 5 | } else { |
|
277 | 5 | $nodes = $this->getDescendants($depth)->all(); |
|
278 | } |
||
279 | |||
280 | 5 | $relates = []; |
|
281 | 5 | $parents = [$this->owner->getAttribute($this->leftAttribute)]; |
|
282 | 5 | $prev = $this->owner->getAttribute($this->depthAttribute); |
|
283 | 5 | foreach($nodes as $node) |
|
284 | { |
||
285 | 5 | $depth = $node->getAttribute($this->depthAttribute); |
|
286 | 5 | if ($depth <= $prev) { |
|
287 | 5 | $parents = array_slice($parents, 0, $depth - $prev - 1); |
|
288 | 5 | } |
|
289 | |||
290 | 5 | $key = end($parents); |
|
291 | 5 | if (!isset($relates[$key])) { |
|
292 | 5 | $relates[$key] = []; |
|
293 | 5 | } |
|
294 | 5 | $relates[$key][] = $node; |
|
295 | |||
296 | 5 | $parents[] = $node->getAttribute($this->leftAttribute); |
|
297 | 5 | $prev = $depth; |
|
298 | 5 | } |
|
299 | |||
300 | 5 | $nodes[] = $this->owner; |
|
301 | 5 | foreach ($nodes as $node) { |
|
302 | 5 | $key = $node->getAttribute($this->leftAttribute); |
|
303 | 5 | if (isset($relates[$key])) { |
|
304 | 5 | $node->populateRelation('children', $relates[$key]); |
|
305 | 5 | } elseif ($depth === null) { |
|
306 | 5 | $node->populateRelation('children', []); |
|
307 | 5 | } |
|
308 | 5 | } |
|
309 | |||
310 | 5 | return $this->owner; |
|
311 | } |
||
312 | |||
313 | /** |
||
314 | * @return bool |
||
315 | */ |
||
316 | 80 | public function isRoot() |
|
320 | |||
321 | /** |
||
322 | * @param ActiveRecord $node |
||
323 | * @return bool |
||
324 | */ |
||
325 | 83 | public function isChildOf($node) |
|
336 | |||
337 | /** |
||
338 | * @return bool |
||
339 | */ |
||
340 | 5 | public function isLeaf() |
|
344 | |||
345 | /** |
||
346 | * @return ActiveRecord |
||
347 | */ |
||
348 | 13 | public function makeRoot() |
|
349 | { |
||
350 | 13 | $this->operation = self::OPERATION_MAKE_ROOT; |
|
351 | 13 | return $this->owner; |
|
352 | } |
||
353 | |||
354 | /** |
||
355 | * @param ActiveRecord $node |
||
356 | * @return ActiveRecord |
||
357 | */ |
||
358 | 61 | public function prependTo($node) |
|
364 | |||
365 | /** |
||
366 | * @param ActiveRecord $node |
||
367 | * @return ActiveRecord |
||
368 | */ |
||
369 | 61 | public function appendTo($node) |
|
375 | |||
376 | /** |
||
377 | * @param ActiveRecord $node |
||
378 | * @return ActiveRecord |
||
379 | */ |
||
380 | 31 | public function insertBefore($node) |
|
386 | |||
387 | /** |
||
388 | * @param ActiveRecord $node |
||
389 | * @return ActiveRecord |
||
390 | */ |
||
391 | 34 | public function insertAfter($node) |
|
397 | |||
398 | /** |
||
399 | * Need for paulzi/auto-tree |
||
400 | */ |
||
401 | public function preDeleteWithChildren() |
||
405 | |||
406 | /** |
||
407 | * @return bool|int |
||
408 | * @throws \Exception |
||
409 | * @throws \yii\db\Exception |
||
410 | */ |
||
411 | 11 | public function deleteWithChildren() |
|
433 | |||
434 | /** |
||
435 | * @return ActiveRecord |
||
436 | * @throws \Exception |
||
437 | * @throws \yii\db\Exception |
||
438 | */ |
||
439 | 5 | public function optimize() |
|
455 | |||
456 | /** |
||
457 | * @throws Exception |
||
458 | * @throws NotSupportedException |
||
459 | */ |
||
460 | 102 | public function beforeInsert() |
|
500 | |||
501 | /** |
||
502 | * @throws Exception |
||
503 | */ |
||
504 | 81 | public function afterInsert() |
|
514 | |||
515 | /** |
||
516 | * @throws Exception |
||
517 | */ |
||
518 | 104 | public function beforeUpdate() |
|
556 | |||
557 | /** |
||
558 | * |
||
559 | */ |
||
560 | 74 | public function afterUpdate() |
|
598 | |||
599 | /** |
||
600 | * @throws Exception |
||
601 | */ |
||
602 | 22 | public function beforeDelete() |
|
612 | |||
613 | /** |
||
614 | * |
||
615 | */ |
||
616 | 13 | public function afterDelete() |
|
624 | |||
625 | /** |
||
626 | * @return mixed |
||
627 | * @throws Exception |
||
628 | */ |
||
629 | 31 | protected function getPrimaryKey() |
|
637 | |||
638 | /** |
||
639 | * @return self |
||
640 | */ |
||
641 | 157 | public function getNodeBehavior() |
|
650 | |||
651 | /** |
||
652 | * @return int |
||
653 | */ |
||
654 | 11 | protected function deleteWithChildrenInternal() |
|
664 | |||
665 | /** |
||
666 | * @param int $left |
||
667 | * @param int $right |
||
668 | * @param int $depth |
||
669 | * @param bool $forward |
||
670 | * @param array|null $parent |
||
671 | * @throws Exception |
||
672 | */ |
||
673 | 91 | protected function insertNode($left, $right, $depth = 0, $forward = true, $parent = null) |
|
750 | |||
751 | /** |
||
752 | * @param int $left |
||
753 | * @param int $right |
||
754 | * @param int $depth |
||
755 | * @throws Exception |
||
756 | */ |
||
757 | 54 | protected function moveNode($left, $right, $depth = 0) |
|
810 | |||
811 | /** |
||
812 | * |
||
813 | */ |
||
814 | 5 | protected function moveNodeAsRoot() |
|
843 | |||
844 | /** |
||
845 | * @throws Exception |
||
846 | */ |
||
847 | 5 | protected function optimizeInternal() |
|
858 | |||
859 | /** |
||
860 | * @param \yii\db\ActiveQuery $query |
||
861 | * @param string $attribute |
||
862 | * @param int $from |
||
863 | * @param int $size |
||
864 | * @param int $offset |
||
865 | * @param int|null $freeFrom |
||
866 | * @param int $freeSize |
||
867 | * @param int|null $targetDepth |
||
868 | * @param array $additional |
||
869 | * @return int|null |
||
870 | * @throws Exception |
||
871 | * @throws \yii\db\Exception |
||
872 | */ |
||
873 | 21 | protected function optimizeAttribute($query, $attribute, $from, $size, $offset = 0, $freeFrom = null, $freeSize = 0, $targetDepth = null, $additional = []) |
|
994 | |||
995 | /** |
||
996 | * @param int $left |
||
997 | * @param int $right |
||
998 | */ |
||
999 | 52 | View Code Duplication | protected function findPrependRange(&$left, &$right) |
1012 | |||
1013 | /** |
||
1014 | * @param int $left |
||
1015 | * @param int $right |
||
1016 | */ |
||
1017 | 52 | View Code Duplication | protected function findAppendRange(&$left, &$right) |
1030 | |||
1031 | /** |
||
1032 | * @param int $left |
||
1033 | * @param int $right |
||
1034 | * @return array|null |
||
1035 | */ |
||
1036 | 25 | View Code Duplication | protected function findInsertBeforeRange(&$left, &$right) |
1053 | |||
1054 | /** |
||
1055 | * @param int $left |
||
1056 | * @param int $right |
||
1057 | * @return array|null |
||
1058 | */ |
||
1059 | 28 | View Code Duplication | protected function findInsertAfterRange(&$left, &$right) |
1076 | |||
1077 | /** |
||
1078 | * @param int $value |
||
1079 | * @param string $attribute |
||
1080 | * @param bool $forward |
||
1081 | * @return bool|int |
||
1082 | */ |
||
1083 | 32 | protected function findUnallocated($value, $attribute, $forward = true) |
|
1115 | |||
1116 | /** |
||
1117 | * @param int $left |
||
1118 | * @param int $right |
||
1119 | * @return bool|int |
||
1120 | */ |
||
1121 | 32 | protected function findUnallocatedAll($left, $right) |
|
1137 | |||
1138 | /** |
||
1139 | * @param int $from |
||
1140 | * @param int $to |
||
1141 | * @param int $delta |
||
1142 | */ |
||
1143 | 70 | protected function shift($from, $to, $delta) |
|
1158 | |||
1159 | /** |
||
1160 | * @return array |
||
1161 | */ |
||
1162 | 238 | protected function treeCondition() |
|
1171 | } |
||
1172 |
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.