Conditions | 9 |
Paths | 14 |
Total Lines | 78 |
Code Lines | 61 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
54 | public function reorder($oldPosition = null, $position = null, $siblings = []) |
||
55 | { |
||
56 | if (null === $oldPosition || null === $position || true === empty($siblings)) { |
||
57 | return ['info' => 'nothing to change']; |
||
58 | } |
||
59 | $nodeId = $siblings[$position]; |
||
60 | $class = $this->className; |
||
61 | $lr = $workWith = []; |
||
62 | $nodeOperator = $siblingsOperator = ''; |
||
63 | if ($oldPosition > $position) { |
||
64 | //change next |
||
65 | $nodeOperator = '-'; |
||
66 | $siblingsOperator = '+'; |
||
67 | $workWith = array_slice($siblings, $position, $oldPosition - $position + 1); |
||
68 | } else if ($oldPosition < $position) { |
||
69 | //change previous |
||
70 | $nodeOperator = '+'; |
||
71 | $siblingsOperator = '-'; |
||
72 | $workWith = array_slice($siblings, $oldPosition, $position - $oldPosition + 1); |
||
73 | } else { |
||
74 | return ['info' => 'nothing to change']; |
||
75 | } |
||
76 | if (true === empty($workWith)) { |
||
77 | return ['info' => 'nothing to change']; |
||
78 | } |
||
79 | $lr = $workWithLr = $this->getLr($workWith); |
||
80 | if (true === empty($lr)) { |
||
81 | return ['info' => 'nothing to change']; |
||
82 | } |
||
83 | unset($workWithLr[$nodeId]); |
||
84 | $lft = array_column($workWithLr, $this->leftAttribute); |
||
85 | $lft = min($lft); |
||
86 | $rgt = array_column($workWithLr, $this->rightAttribute); |
||
87 | $rgt = max($rgt); |
||
88 | $nodeCondition = [ |
||
89 | 'and', |
||
90 | ['>=', $this->leftAttribute, $lft], |
||
91 | ['<=', $this->rightAttribute, $rgt] |
||
92 | ]; |
||
93 | $this->applyRootCondition($nodeCondition); |
||
94 | $nodeDelta = $this->getCount($nodeCondition); |
||
95 | $nodeDelta *= 2; |
||
96 | $siblingsCondition = [ |
||
97 | 'and', |
||
98 | ['>=', $this->leftAttribute, $lr[$nodeId][$this->leftAttribute]], |
||
99 | ['<=', $this->rightAttribute, $lr[$nodeId][$this->rightAttribute]] |
||
100 | ]; |
||
101 | $this->applyRootCondition($siblingsCondition); |
||
102 | $nodeChildren = $this->getChildIds($siblingsCondition); |
||
103 | $siblingsDelta = count($nodeChildren) * 2; |
||
104 | $db = Yii::$app->getDb(); |
||
105 | $transaction = $db->beginTransaction(); |
||
106 | try { |
||
107 | //updating necessary node siblings |
||
108 | $db->createCommand()->update( |
||
109 | $class::tableName(), |
||
110 | [ |
||
111 | $this->leftAttribute => new Expression($this->leftAttribute . sprintf('%s%d', $siblingsOperator, $siblingsDelta)), |
||
112 | $this->rightAttribute => new Expression($this->rightAttribute . sprintf('%s%d', $siblingsOperator, $siblingsDelta)), |
||
113 | ], |
||
114 | $nodeCondition |
||
115 | )->execute(); |
||
116 | //updating node |
||
117 | $db->createCommand()->update( |
||
118 | $class::tableName(), |
||
119 | [ |
||
120 | $this->leftAttribute => new Expression($this->leftAttribute . sprintf('%s%d', $nodeOperator, $nodeDelta)), |
||
121 | $this->rightAttribute => new Expression($this->rightAttribute . sprintf('%s%d', $nodeOperator, $nodeDelta)), |
||
122 | ], |
||
123 | ['id' => $nodeChildren] |
||
124 | )->execute(); |
||
125 | $transaction->commit(); |
||
126 | } catch (\Exception $e) { |
||
127 | $transaction->rollBack(); |
||
128 | return ['error', $e->getMessage()]; |
||
129 | } |
||
130 | return ['siblingsDelta' => $siblingsDelta, 'nodeDelta' => $nodeDelta, 'workWith' => $workWith]; |
||
131 | } |
||
132 | |||
209 | } |
This check looks for the
else
branches ofif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
else
branches can be removed.could be turned into
This is much more concise to read.