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 MaterializedPathBehavior 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 MaterializedPathBehavior, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class MaterializedPathBehavior 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 |
||
36 | */ |
||
37 | public $pathAttribute = 'path'; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | public $depthAttribute = 'depth'; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | public $itemAttribute; |
||
48 | |||
49 | /** |
||
50 | * @var string|null |
||
51 | */ |
||
52 | public $treeAttribute; |
||
53 | |||
54 | /** |
||
55 | * @var array|false SortableBehavior config |
||
56 | */ |
||
57 | public $sortable = []; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | public $delimiter = '/'; |
||
63 | |||
64 | /** |
||
65 | * @var int Value of $depthAttribute for root node. |
||
66 | */ |
||
67 | public $rootDepthValue = 0; |
||
68 | |||
69 | /** |
||
70 | * @var int|null |
||
71 | */ |
||
72 | protected $operation; |
||
73 | |||
74 | /** |
||
75 | * @var ActiveRecord|self|null |
||
76 | */ |
||
77 | protected $node; |
||
78 | |||
79 | /** |
||
80 | * @var SortableBehavior |
||
81 | */ |
||
82 | protected $behavior; |
||
83 | |||
84 | /** |
||
85 | * @var bool |
||
86 | */ |
||
87 | protected $primaryKeyMode = false; |
||
88 | |||
89 | |||
90 | /** |
||
91 | * @inheritdoc |
||
92 | */ |
||
93 | 201 | public function events() |
|
94 | { |
||
95 | return [ |
||
96 | 201 | ActiveRecord::EVENT_BEFORE_INSERT => 'beforeSave', |
|
97 | ActiveRecord::EVENT_AFTER_INSERT => 'afterInsert', |
||
98 | ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeSave', |
||
99 | ActiveRecord::EVENT_AFTER_UPDATE => 'afterUpdate', |
||
100 | ActiveRecord::EVENT_BEFORE_DELETE => 'beforeDelete', |
||
101 | ActiveRecord::EVENT_AFTER_DELETE => 'afterDelete', |
||
102 | ]; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param ActiveRecord $owner |
||
107 | * @throws Exception |
||
108 | */ |
||
109 | 201 | public function attach($owner) |
|
110 | { |
||
111 | 201 | parent::attach($owner); |
|
112 | 201 | if ($this->itemAttribute === null) { |
|
113 | 201 | $primaryKey = $owner->primaryKey(); |
|
114 | 201 | if (!isset($primaryKey[0])) { |
|
115 | throw new Exception('"' . $owner->className() . '" must have a primary key.'); |
||
116 | } |
||
117 | 201 | $this->itemAttribute = $primaryKey[0]; |
|
118 | 201 | $this->primaryKeyMode = true; |
|
119 | } |
||
120 | 201 | if ($this->sortable !== false) { |
|
121 | 201 | $this->behavior = Yii::createObject(array_merge( |
|
122 | [ |
||
123 | 201 | 'class' => SortableBehavior::className(), |
|
124 | 'query' => function () { |
||
125 | 87 | return $this->getSortableQuery(); |
|
126 | 201 | }, |
|
127 | ], |
||
128 | 201 | $this->sortable |
|
129 | )); |
||
130 | 201 | $owner->attachBehavior('materialized-path-sortable', $this->behavior); |
|
131 | } |
||
132 | 201 | } |
|
133 | |||
134 | /** |
||
135 | * @param int|null $depth |
||
136 | * @return \yii\db\ActiveQuery |
||
137 | */ |
||
138 | 9 | public function getParents($depth = null) |
|
139 | { |
||
140 | 9 | $path = $this->getParentPath(); |
|
141 | 9 | if ($path !== null) { |
|
142 | 9 | $paths = explode($this->delimiter, $path); |
|
143 | 9 | if (!$this->primaryKeyMode) { |
|
144 | 9 | $path = null; |
|
145 | 9 | $paths = array_map( |
|
146 | 9 | function ($value) use (&$path) { |
|
147 | 9 | return $path = ($path !== null ? $path . $this->delimiter : '') . $value; |
|
148 | 9 | }, |
|
149 | 9 | $paths |
|
150 | ); |
||
151 | } |
||
152 | 9 | if ($depth !== null) { |
|
153 | 9 | $paths = array_slice($paths, -$depth); |
|
154 | } |
||
155 | } else { |
||
156 | 3 | $paths = []; |
|
157 | } |
||
158 | |||
159 | 9 | $tableName = $this->owner->tableName(); |
|
160 | 9 | $condition = ['and']; |
|
161 | 9 | if ($this->primaryKeyMode) { |
|
162 | 6 | $condition[] = ["{$tableName}.[[{$this->itemAttribute}]]" => $paths]; |
|
163 | } else { |
||
164 | 9 | $condition[] = ["{$tableName}.[[{$this->pathAttribute}]]" => $paths]; |
|
165 | } |
||
166 | |||
167 | 9 | $query = $this->owner->find() |
|
168 | 9 | ->andWhere($condition) |
|
169 | 9 | ->andWhere($this->treeCondition()) |
|
170 | 9 | ->addOrderBy(["{$tableName}.[[{$this->pathAttribute}]]" => SORT_ASC]); |
|
171 | 9 | $query->multiple = true; |
|
172 | |||
173 | 9 | return $query; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * @return \yii\db\ActiveQuery |
||
178 | */ |
||
179 | 6 | public function getParent() |
|
185 | |||
186 | /** |
||
187 | * @return \yii\db\ActiveQuery |
||
188 | */ |
||
189 | 3 | public function getRoot() |
|
201 | |||
202 | /** |
||
203 | * @param int|null $depth |
||
204 | * @param bool $andSelf |
||
205 | * @return \yii\db\ActiveQuery |
||
206 | */ |
||
207 | 27 | public function getDescendants($depth = null, $andSelf = false) |
|
208 | { |
||
209 | 27 | $tableName = $this->owner->tableName(); |
|
210 | 27 | $path = $this->owner->getAttribute($this->pathAttribute); |
|
211 | 27 | $query = $this->owner->find() |
|
212 | 27 | ->andWhere(['like', "{$tableName}.[[{$this->pathAttribute}]]", $this->getLike($path), false]); |
|
213 | |||
214 | 27 | if ($andSelf) { |
|
215 | 9 | $query->orWhere(["{$tableName}.[[{$this->pathAttribute}]]" => $path]); |
|
216 | } |
||
217 | |||
218 | 27 | if ($depth !== null) { |
|
219 | 21 | $query->andWhere(['<=', "{$tableName}.[[{$this->depthAttribute}]]", $this->owner->getAttribute($this->depthAttribute) + $depth]); |
|
220 | } |
||
221 | |||
222 | 27 | $orderBy = []; |
|
223 | 27 | $orderBy["{$tableName}.[[{$this->depthAttribute}]]"] = SORT_ASC; |
|
224 | 27 | View Code Duplication | if ($this->sortable !== false) { |
|
|||
225 | 27 | $orderBy["{$tableName}.[[{$this->behavior->sortAttribute}]]"] = SORT_ASC; |
|
226 | } |
||
227 | 27 | $orderBy["{$tableName}.[[{$this->itemAttribute}]]"] = SORT_ASC; |
|
228 | |||
229 | $query |
||
230 | 27 | ->andWhere($this->treeCondition()) |
|
231 | 27 | ->addOrderBy($orderBy); |
|
232 | 27 | $query->multiple = true; |
|
233 | |||
234 | 27 | return $query; |
|
235 | } |
||
236 | |||
237 | /** |
||
238 | * @return \yii\db\ActiveQuery |
||
239 | */ |
||
240 | 12 | public function getChildren() |
|
244 | |||
245 | /** |
||
246 | * @param int|null $depth |
||
247 | * @return \yii\db\ActiveQuery |
||
248 | */ |
||
249 | 3 | public function getLeaves($depth = null) |
|
250 | { |
||
251 | 3 | $tableName = $this->owner->tableName(); |
|
252 | $condition = [ |
||
253 | 3 | 'and', |
|
254 | 3 | ['like', "leaves.[[{$this->pathAttribute}]]", new Expression($this->concatExpression(["{$tableName}.[[{$this->pathAttribute}]]", ':delimiter']), [':delimiter' => $this->delimiter . '%'])], |
|
255 | ]; |
||
256 | |||
257 | 3 | if ($this->treeAttribute !== null) { |
|
258 | 3 | $condition[] = ["leaves.[[{$this->treeAttribute}]]" => new Expression("{$tableName}.[[{$this->treeAttribute}]]")]; |
|
259 | } |
||
260 | |||
261 | 3 | $query = $this->getDescendants($depth) |
|
262 | 3 | ->leftJoin("{$tableName} leaves", $condition) |
|
263 | 3 | ->andWhere(["leaves.[[{$this->pathAttribute}]]" => null]); |
|
264 | 3 | $query->multiple = true; |
|
265 | 3 | return $query; |
|
266 | } |
||
267 | |||
268 | /** |
||
269 | * @return \yii\db\ActiveQuery |
||
270 | * @throws NotSupportedException |
||
271 | */ |
||
272 | 3 | View Code Duplication | public function getPrev() |
288 | |||
289 | /** |
||
290 | * @return \yii\db\ActiveQuery |
||
291 | * @throws NotSupportedException |
||
292 | */ |
||
293 | 6 | View Code Duplication | public function getNext() |
309 | |||
310 | /** |
||
311 | * Returns all sibilings of node. |
||
312 | * |
||
313 | * @param bool $andSelf = false Include self node into result. |
||
314 | * @return \yii\db\ActiveQuery |
||
315 | */ |
||
316 | public function getSiblings($andSelf = false) |
||
317 | { |
||
318 | $tableName = $this->owner->tableName(); |
||
319 | $path = $this->getParentPath(); |
||
320 | $like = strtr($path . $this->delimiter, ['%' => '\%', '_' => '\_', '\\' => '\\\\']); |
||
321 | |||
322 | $query = $this->owner->find() |
||
323 | ->andWhere(['like', "{$tableName}.[[{$this->pathAttribute}]]", $like . '%', false]) |
||
324 | ->andWhere(['<=', "{$tableName}.[[{$this->depthAttribute}]]", $this->owner->{$this->depthAttribute}]); |
||
325 | |||
326 | if (!$andSelf) { |
||
327 | $query->andWhere(["!=", "{$tableName}.[[{$this->itemAttribute}]]", $this->owner->{$this->itemAttribute}]); |
||
328 | } |
||
329 | |||
330 | $orderBy = []; |
||
331 | $orderBy["{$tableName}.[[{$this->depthAttribute}]]"] = SORT_ASC; |
||
332 | View Code Duplication | if ($this->sortable !== false) { |
|
333 | $orderBy["{$tableName}.[[{$this->behavior->sortAttribute}]]"] = SORT_ASC; |
||
334 | } |
||
335 | $orderBy["{$tableName}.[[{$this->itemAttribute}]]"] = SORT_ASC; |
||
336 | |||
337 | $query |
||
338 | ->andWhere($this->treeCondition()) |
||
339 | ->addOrderBy($orderBy); |
||
340 | $query->multiple = true; |
||
341 | return $query; |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * @param bool $asArray = false |
||
346 | * @return null|string|array |
||
347 | */ |
||
348 | 66 | public function getParentPath($asArray = false) |
|
352 | |||
353 | /** |
||
354 | * Populate children relations for self and all descendants |
||
355 | * |
||
356 | * @param int $depth = null |
||
357 | * @param string|array $with = null |
||
358 | * @return static |
||
359 | */ |
||
360 | 3 | public function populateTree($depth = null, $with = null) |
|
361 | { |
||
362 | /** @var ActiveRecord[]|static[] $nodes */ |
||
363 | 3 | $query = $this->getDescendants($depth); |
|
364 | 3 | if ($with) { |
|
365 | $query->with($with); |
||
366 | } |
||
367 | 3 | $nodes = $query->all(); |
|
368 | |||
369 | 3 | $relates = []; |
|
370 | 3 | foreach ($nodes as $node) { |
|
371 | 3 | $path = $node->getParentPath(true); |
|
372 | 3 | $key = array_pop($path); |
|
373 | 3 | if (!isset($relates[$key])) { |
|
374 | 3 | $relates[$key] = []; |
|
375 | } |
||
376 | 3 | $relates[$key][] = $node; |
|
377 | } |
||
378 | |||
379 | 3 | $ownerDepth = $this->owner->getAttribute($this->depthAttribute); |
|
380 | 3 | $nodes[] = $this->owner; |
|
381 | 3 | foreach ($nodes as $node) { |
|
382 | 3 | $key = $node->getAttribute($this->itemAttribute); |
|
383 | 3 | if (isset($relates[$key])) { |
|
384 | 3 | $node->populateRelation('children', $relates[$key]); |
|
385 | 3 | } elseif ($depth === null || $ownerDepth + $depth > $node->getAttribute($this->depthAttribute)) { |
|
386 | 3 | $node->populateRelation('children', []); |
|
387 | } |
||
388 | } |
||
389 | |||
390 | 3 | return $this->owner; |
|
391 | } |
||
392 | |||
393 | /** |
||
394 | * @return bool |
||
395 | */ |
||
396 | 72 | public function isRoot() |
|
397 | { |
||
398 | 72 | return count(explode($this->delimiter, $this->owner->getAttribute($this->pathAttribute))) === 1; |
|
399 | } |
||
400 | |||
401 | /** |
||
402 | * @param ActiveRecord $node |
||
403 | * @return bool |
||
404 | */ |
||
405 | 96 | public function isChildOf($node) |
|
419 | |||
420 | /** |
||
421 | * @return bool |
||
422 | */ |
||
423 | 3 | public function isLeaf() |
|
424 | { |
||
425 | 3 | return count($this->owner->children) === 0; |
|
426 | } |
||
427 | |||
428 | /** |
||
429 | * @return ActiveRecord |
||
430 | */ |
||
431 | 6 | public function makeRoot() |
|
436 | |||
437 | /** |
||
438 | * @param ActiveRecord $node |
||
439 | * @return ActiveRecord |
||
440 | */ |
||
441 | 33 | public function prependTo($node) |
|
447 | |||
448 | /** |
||
449 | * @param ActiveRecord $node |
||
450 | * @return ActiveRecord |
||
451 | */ |
||
452 | 33 | public function appendTo($node) |
|
458 | |||
459 | /** |
||
460 | * @param ActiveRecord $node |
||
461 | * @return ActiveRecord |
||
462 | */ |
||
463 | 30 | public function insertBefore($node) |
|
469 | |||
470 | /** |
||
471 | * @param ActiveRecord $node |
||
472 | * @return ActiveRecord |
||
473 | */ |
||
474 | 30 | public function insertAfter($node) |
|
480 | |||
481 | /** |
||
482 | * Need for paulzi/auto-tree |
||
483 | */ |
||
484 | public function preDeleteWithChildren() |
||
488 | |||
489 | /** |
||
490 | * @return bool|int |
||
491 | * @throws \Exception |
||
492 | * @throws \yii\db\Exception |
||
493 | */ |
||
494 | 9 | public function deleteWithChildren() |
|
516 | |||
517 | /** |
||
518 | * @param bool $middle |
||
519 | * @return int |
||
520 | */ |
||
521 | 3 | public function reorderChildren($middle = true) |
|
531 | |||
532 | /** |
||
533 | * @throws Exception |
||
534 | * @throws NotSupportedException |
||
535 | */ |
||
536 | 138 | public function beforeSave() |
|
575 | |||
576 | /** |
||
577 | * @throws Exception |
||
578 | */ |
||
579 | 33 | public function afterInsert() |
|
614 | |||
615 | /** |
||
616 | * @param \yii\db\AfterSaveEvent $event |
||
617 | */ |
||
618 | 57 | public function afterUpdate($event) |
|
624 | |||
625 | /** |
||
626 | * @param \yii\base\ModelEvent $event |
||
627 | * @throws Exception |
||
628 | */ |
||
629 | 18 | public function beforeDelete($event) |
|
653 | |||
654 | /** |
||
655 | * |
||
656 | */ |
||
657 | 9 | public function afterDelete() |
|
672 | |||
673 | |||
674 | /** |
||
675 | * @return string |
||
676 | */ |
||
677 | 36 | protected function getPrimaryKeyValue() |
|
682 | |||
683 | /** |
||
684 | * @param bool $forInsertNear |
||
685 | * @throws Exception |
||
686 | */ |
||
687 | 126 | protected function checkNode($forInsertNear = false) |
|
704 | |||
705 | /** |
||
706 | * Make root operation internal handler |
||
707 | */ |
||
708 | 6 | protected function makeRootInternal() |
|
726 | |||
727 | /** |
||
728 | * Append to operation internal handler |
||
729 | * @param bool $append |
||
730 | * @throws Exception |
||
731 | */ |
||
732 | 66 | View Code Duplication | protected function insertIntoInternal($append) |
756 | |||
757 | /** |
||
758 | * Insert operation internal handler |
||
759 | * @param bool $forward |
||
760 | * @throws Exception |
||
761 | */ |
||
762 | 60 | View Code Duplication | protected function insertNearInternal($forward) |
786 | |||
787 | /** |
||
788 | * @return int |
||
789 | */ |
||
790 | 9 | protected function deleteWithChildrenInternal() |
|
800 | |||
801 | /** |
||
802 | * @param array $changedAttributes |
||
803 | * @throws Exception |
||
804 | */ |
||
805 | 57 | protected function moveNode($changedAttributes) |
|
843 | |||
844 | /** |
||
845 | * @param string $path |
||
846 | * @param string $delimiter |
||
847 | * @param bool $asArray = false |
||
848 | * @return null|string|array |
||
849 | */ |
||
850 | 66 | protected static function getParentPathInternal($path, $delimiter, $asArray = false) |
|
859 | |||
860 | /** |
||
861 | * @return array |
||
862 | */ |
||
863 | 123 | protected function treeCondition() |
|
872 | |||
873 | /** |
||
874 | * @return \yii\db\ActiveQuery |
||
875 | */ |
||
876 | 87 | protected function getSortableQuery() |
|
902 | |||
903 | /** |
||
904 | * @param string $path |
||
905 | * @return string |
||
906 | */ |
||
907 | 117 | protected function getLike($path) |
|
911 | |||
912 | /** |
||
913 | * @param array $items |
||
914 | * @return string |
||
915 | */ |
||
916 | 33 | protected function concatExpression($items) |
|
923 | |||
924 | 30 | protected function substringExpression($string, $from, $length) |
|
931 | } |
||
932 |
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.