| Conditions | 10 |
| Paths | 100 |
| Total Lines | 137 |
| Code Lines | 106 |
| Lines | 10 |
| Ratio | 7.3 % |
| Changes | 2 | ||
| 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 |
||
| 133 | private function move($position = null, $siblings = [], $oldParentId) |
||
| 134 | { |
||
| 135 | $class = $this->className; |
||
| 136 | if (null === $oldParent = $class::findOne($oldParentId)) { |
||
| 137 | return ['error' => "Old parent with id '{$oldParentId}' not found"]; |
||
| 138 | } |
||
| 139 | $nodeCountCondition = [ |
||
| 140 | 'and', |
||
| 141 | ['>=', $this->leftAttribute, $this->node{$this->leftAttribute}], |
||
| 142 | ['<=', $this->rightAttribute, $this->node{$this->rightAttribute}] |
||
| 143 | ]; |
||
| 144 | $this->applyRootCondition($nodeCountCondition); |
||
| 145 | $nodeChildren = $this->getChildIds($nodeCountCondition); |
||
| 146 | $siblingsDelta = count($nodeChildren) * 2; |
||
| 147 | if ($position == 0) { |
||
| 148 | $compareRight = $this->parent->{$this->leftAttribute} + 1; |
||
| 149 | } else { |
||
| 150 | if (false === isset($siblings[$position - 1])) { |
||
| 151 | return ['error' => 'New previous sibling not exists']; |
||
| 152 | } |
||
| 153 | $newPrevSiblingId = $siblings[$position - 1]; |
||
| 154 | $newPrevSiblingData = $this->getLr($newPrevSiblingId); |
||
| 155 | $compareRight = $newPrevSiblingData[$newPrevSiblingId][$this->rightAttribute]; |
||
| 156 | } |
||
| 157 | |||
| 158 | if ($this->node->{$this->leftAttribute} > $compareRight) { |
||
| 159 | //move node up |
||
| 160 | View Code Duplication | if ($position == 0) { |
|
| 161 | $leftFrom = $this->parent->{$this->leftAttribute} + 1; |
||
| 162 | } else { |
||
| 163 | $leftFrom = $newPrevSiblingData[$newPrevSiblingId][$this->rightAttribute] + 1; |
||
| 164 | } |
||
| 165 | $rightTo = $this->node->{$this->leftAttribute}; |
||
| 166 | $nodeDelta = $this->node->{$this->leftAttribute} - $leftFrom; |
||
| 167 | $nodeOperator = '-'; |
||
| 168 | $parentOperator = $siblingsOperator = '+'; |
||
| 169 | $newParentUpdateField = $this->rightAttribute; |
||
| 170 | $oldParentUpdateField = $this->leftAttribute; |
||
| 171 | } else if ($this->node->{$this->leftAttribute} < $compareRight) { |
||
| 172 | //move node down |
||
| 173 | $leftFrom = $this->node->{$this->rightAttribute}; |
||
| 174 | View Code Duplication | if ($position == 0) { |
|
| 175 | $rightTo = $this->parent->{$this->leftAttribute}; |
||
| 176 | } else { |
||
| 177 | $rightTo = $newPrevSiblingData[$newPrevSiblingId][$this->rightAttribute]; |
||
| 178 | } |
||
| 179 | $nodeOperator = '+'; |
||
| 180 | $parentOperator = $siblingsOperator = '-'; |
||
| 181 | $nodeDelta = $rightTo - $siblingsDelta + 1 - $this->node->{$this->leftAttribute}; |
||
| 182 | $newParentUpdateField = $this->leftAttribute; |
||
| 183 | $oldParentUpdateField = $this->rightAttribute; |
||
| 184 | } else { |
||
| 185 | return ['error' => 'There are two nodes with same "left" value. This should not be.']; |
||
| 186 | } |
||
| 187 | $siblingsCondition = [ |
||
| 188 | 'and', |
||
| 189 | ['>=', $this->leftAttribute, $leftFrom], |
||
| 190 | ['<=', $this->rightAttribute, $rightTo] |
||
| 191 | ]; |
||
| 192 | $this->applyRootCondition($siblingsCondition); |
||
| 193 | $db = Yii::$app->getDb(); |
||
| 194 | $transaction = $db->beginTransaction(); |
||
| 195 | $oldParentDepth = $oldParent->{$this->depthAttribute}; |
||
| 196 | $newParentDepth = $this->parent->{$this->depthAttribute}; |
||
| 197 | if ($newParentDepth < $oldParentDepth) { |
||
| 198 | $depthOperator = '-'; |
||
| 199 | $depthDelta = $oldParentDepth - $newParentDepth; |
||
| 200 | } else { |
||
| 201 | $depthOperator = '+'; |
||
| 202 | $depthDelta = $newParentDepth - $oldParentDepth; |
||
| 203 | } |
||
| 204 | $commonParentsCondition = [ |
||
| 205 | 'and', |
||
| 206 | ['<', $this->leftAttribute, $leftFrom], |
||
| 207 | ['>', $this->rightAttribute, $rightTo] |
||
| 208 | ]; |
||
| 209 | $this->applyRootCondition($commonParentsCondition); |
||
| 210 | $commonParentsIds = $class::find()->select('id')->where($commonParentsCondition)->column(); |
||
| 211 | $commonCondition = [ |
||
| 212 | ['!=', $this->depthAttribute, 0], |
||
| 213 | ['not in', 'id', $commonParentsIds], |
||
| 214 | ]; |
||
| 215 | $this->applyRootCondition($commonCondition); |
||
| 216 | $newParentCondition = array_merge([ |
||
| 217 | 'and', |
||
| 218 | ['<=', $this->leftAttribute, $this->parent->{$this->leftAttribute}], |
||
| 219 | ['>=', $this->rightAttribute, $this->parent->{$this->rightAttribute}], |
||
| 220 | ], $commonCondition); |
||
| 221 | $oldParentsCondition = array_merge([ |
||
| 222 | 'and', |
||
| 223 | ['<', $this->leftAttribute, $this->node->{$this->leftAttribute}], |
||
| 224 | ['>', $this->rightAttribute, $this->node->{$this->rightAttribute}], |
||
| 225 | ], $commonCondition); |
||
| 226 | try { |
||
| 227 | //updating necessary node siblings |
||
| 228 | $db->createCommand()->update( |
||
| 229 | $class::tableName(), |
||
| 230 | [ |
||
| 231 | $this->leftAttribute => new Expression($this->leftAttribute . sprintf('%s%d', $siblingsOperator, $siblingsDelta)), |
||
| 232 | $this->rightAttribute => new Expression($this->rightAttribute . sprintf('%s%d', $siblingsOperator, $siblingsDelta)), |
||
| 233 | ], |
||
| 234 | $siblingsCondition |
||
| 235 | )->execute(); |
||
| 236 | //updating old parents |
||
| 237 | $db->createCommand()->update( |
||
| 238 | $class::tableName(), |
||
| 239 | [ |
||
| 240 | //down - right |
||
| 241 | $oldParentUpdateField => new Expression($oldParentUpdateField . sprintf('%s%d', $parentOperator, $siblingsDelta)), |
||
| 242 | ], |
||
| 243 | $oldParentsCondition |
||
| 244 | )->execute(); |
||
| 245 | //updating new parents |
||
| 246 | $db->createCommand()->update( |
||
| 247 | $class::tableName(), |
||
| 248 | [ |
||
| 249 | //down - left |
||
| 250 | $newParentUpdateField => new Expression($newParentUpdateField . sprintf('%s%d', $parentOperator, $siblingsDelta)), |
||
| 251 | ], |
||
| 252 | $newParentCondition |
||
| 253 | )->execute(); |
||
| 254 | //updating node with children |
||
| 255 | $db->createCommand()->update( |
||
| 256 | $class::tableName(), |
||
| 257 | [ |
||
| 258 | $this->leftAttribute => new Expression($this->leftAttribute . sprintf('%s%d', $nodeOperator, $nodeDelta)), |
||
| 259 | $this->rightAttribute => new Expression($this->rightAttribute . sprintf('%s%d', $nodeOperator, $nodeDelta)), |
||
| 260 | $this->depthAttribute => new Expression($this->depthAttribute . sprintf('%s%d', $depthOperator, $depthDelta)), |
||
| 261 | ], |
||
| 262 | ['id' => $nodeChildren] |
||
| 263 | )->execute(); |
||
| 264 | $transaction->commit(); |
||
| 265 | } catch (\Exception $e) { |
||
| 266 | $transaction->rollBack(); |
||
| 267 | return ['error', $e->getMessage()]; |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 320 | } |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.