| Conditions | 13 |
| Paths | 68 |
| Total Lines | 150 |
| Code Lines | 79 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 40 | public function beforeUpdate(Entity $entity, Space $space) |
||
| 41 | { |
||
| 42 | if ($this->isNested($space)) { |
||
| 43 | $repository = $space->getRepository(); |
||
| 44 | $spaceName = $space->getName(); |
||
|
|
|||
| 45 | |||
| 46 | $parent = $repository->findOne($entity->parent); |
||
| 47 | |||
| 48 | $client = $this->mapper->getClient(); |
||
| 49 | $map = $space->getTupleMap(); |
||
| 50 | |||
| 51 | $old_entity = $repository->getOriginal($entity); |
||
| 52 | |||
| 53 | foreach (['group'] as $field) { |
||
| 54 | if ($old_entity[$map->$field-1] != $entity->$field) { |
||
| 55 | throw new \Exception(ucfirst($field)." can't be changed"); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | if ($old_entity[$map->parent-1] != $entity->parent) { |
||
| 60 | $leftValue = $entity->left; |
||
| 61 | $rightValue = $entity->right; |
||
| 62 | $toRoot = $entity->parent == 0 ? 1 : 0; |
||
| 63 | |||
| 64 | $value = 0; |
||
| 65 | $delta = 0; |
||
| 66 | $depth = 0; |
||
| 67 | if ($entity->parent) { |
||
| 68 | $value = $parent->right; |
||
| 69 | $depth = $parent->depth - $entity->depth + 1; |
||
| 70 | $delta = $rightValue - $leftValue + 1; |
||
| 71 | } |
||
| 72 | |||
| 73 | $right_key_near = 0; |
||
| 74 | if (!$entity->parent) { |
||
| 75 | foreach ($repository->find(['group' => $entity->group]) as $node) { |
||
| 76 | if (!$node->parent && $node->right > $right_key_near) { |
||
| 77 | $right_key_near = $node->right; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | } |
||
| 81 | $skew_tree = $rightValue - $leftValue + 1; |
||
| 82 | $skew_edit = $right_key_near - $leftValue + 1; |
||
| 83 | |||
| 84 | $spaceName = $space->getName(); |
||
| 85 | |||
| 86 | if ($rightValue < $right_key_near) { |
||
| 87 | $skew_edit -= $skew_tree; |
||
| 88 | } |
||
| 89 | |||
| 90 | $result = $this->mapper->getClient()->evaluate(" |
||
| 91 | local result = {} |
||
| 92 | local updates = {} |
||
| 93 | local maxRightTuple = box.space.$spaceName.index.group_right:max(right); |
||
| 94 | local maxLeftTuple = box.space.$spaceName.index.group_left:max(left); |
||
| 95 | local maxValue = 100 |
||
| 96 | if maxRightTuple ~= nil then |
||
| 97 | maxValue = maxValue + maxRightTuple[$map->right] |
||
| 98 | end |
||
| 99 | if maxLeftTuple ~= nil then |
||
| 100 | maxValue = maxValue + maxLeftTuple[$map->left] |
||
| 101 | end |
||
| 102 | |||
| 103 | local leftValue = $entity->left |
||
| 104 | local rightValue = $entity->right |
||
| 105 | |||
| 106 | if $leftValue >= $value then |
||
| 107 | leftValue = leftValue + ($delta) |
||
| 108 | rightValue = rightValue + ($delta) |
||
| 109 | end |
||
| 110 | local left |
||
| 111 | local right |
||
| 112 | |||
| 113 | box.begin() |
||
| 114 | for i, node in box.space.$spaceName.index.group_right:pairs({{$entity->group}, 1}, 'ge') do |
||
| 115 | if node[$map->group] ~= $entity->group then |
||
| 116 | break |
||
| 117 | end |
||
| 118 | left = node[$map->left] |
||
| 119 | right = node[$map->right] |
||
| 120 | if $toRoot == 1 then |
||
| 121 | if left >= $leftValue and right <= $rightValue then |
||
| 122 | if node[$map->id] ~= $entity->id then |
||
| 123 | table.insert(updates, {node[$map->id], $map->left, left + 1 - $leftValue}) |
||
| 124 | table.insert(updates, {node[$map->id], $map->right, right + 1 - $leftValue}) |
||
| 125 | else |
||
| 126 | table.insert(updates, {node[$map->id], $map->right, right + $skew_edit}) |
||
| 127 | table.insert(updates, {node[$map->id], $map->left, left + $skew_edit}) |
||
| 128 | end |
||
| 129 | table.insert(updates, {node[$map->id], $map->depth, node[$map->depth] - $entity->depth}) |
||
| 130 | end |
||
| 131 | if left >= $rightValue + 1 then |
||
| 132 | table.insert(updates, {node[$map->id], $map->left, left + $leftValue - $rightValue - 1}) |
||
| 133 | end |
||
| 134 | if right >= $rightValue + 1 then |
||
| 135 | table.insert(updates, {node[$map->id], $map->right, right + $leftValue - $rightValue - 1}) |
||
| 136 | end |
||
| 137 | else |
||
| 138 | if left >= $value then |
||
| 139 | left = left + ($delta) |
||
| 140 | table.insert(updates, {node[$map->id], $map->left, left}) |
||
| 141 | end |
||
| 142 | if right >= $value then |
||
| 143 | right = right + ($delta) |
||
| 144 | table.insert(updates, {node[$map->id], $map->right, right}) |
||
| 145 | end |
||
| 146 | |||
| 147 | if left >= leftValue and right <= rightValue then |
||
| 148 | table.insert(updates, {node[$map->id], $map->depth, node[$map->depth] + $depth}) |
||
| 149 | end |
||
| 150 | |||
| 151 | if left >= leftValue and left <= rightValue then |
||
| 152 | left = left + $value - leftValue |
||
| 153 | table.insert(updates, {node[$map->id], $map->left, left}) |
||
| 154 | end |
||
| 155 | if right >= leftValue and right <= rightValue then |
||
| 156 | right = right + $value - leftValue |
||
| 157 | table.insert(updates, {node[$map->id], $map->right, right}) |
||
| 158 | end |
||
| 159 | if left >= rightValue + 1 then |
||
| 160 | left = left-($delta) |
||
| 161 | table.insert(updates, {node[$map->id], $map->left, left}) |
||
| 162 | end |
||
| 163 | if right >= rightValue + 1 then |
||
| 164 | right = right-($delta) |
||
| 165 | table.insert(updates, {node[$map->id], $map->right, right}) |
||
| 166 | end |
||
| 167 | end |
||
| 168 | end |
||
| 169 | for i, node in pairs(updates) do |
||
| 170 | box.space.$spaceName:update(node[1], {{'=', node[2], maxValue}}) |
||
| 171 | maxValue = maxValue + 1 |
||
| 172 | end |
||
| 173 | for i, node in pairs(updates) do |
||
| 174 | table.insert(result, node[1]) |
||
| 175 | box.space.$spaceName:update(node[1], {{'=', node[2], node[3]}}) |
||
| 176 | end |
||
| 177 | box.commit() |
||
| 178 | |||
| 179 | return result |
||
| 180 | ")->getData(); |
||
| 181 | |||
| 182 | foreach (array_unique($result[0]) as $id) { |
||
| 183 | $space->getRepository()->sync($id, ['left', 'right', 'depth']); |
||
| 184 | } |
||
| 185 | |||
| 186 | $space->getRepository()->flushCache(); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 328 |
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.