| Conditions | 10 |
| Paths | 37 |
| Total Lines | 123 |
| Code Lines | 68 |
| 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 | |||
| 45 | $parent = $repository->findOne($entity->parent); |
||
|
|
|||
| 46 | $level_up = $parent ? $parent->depth+1 : 0; |
||
| 47 | |||
| 48 | $left_key = $entity->left; |
||
| 49 | $right_key = $entity->right; |
||
| 50 | |||
| 51 | $client = $this->mapper->getClient(); |
||
| 52 | $map = $space->getTupleMap(); |
||
| 53 | |||
| 54 | $old_entity = $client->getSpace($space->getId())->select([$entity->id])->getData()[0]; |
||
| 55 | $old_parent = $repository->findOne($old_entity[$map->parent-1]); |
||
| 56 | $old_parent_id = $old_parent ? $old_parent->id : 0; |
||
| 57 | |||
| 58 | if ($old_parent_id != $entity->parent) { |
||
| 59 | $right_key_near = 0; |
||
| 60 | if (!$entity->parent) { |
||
| 61 | foreach ($repository->find(['parent' => 0]) as $node) { |
||
| 62 | if ($node->right > $right_key_near) { |
||
| 63 | $right_key_near = $node->right; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | } else { |
||
| 67 | $right_key_near = $parent->left; |
||
| 68 | } |
||
| 69 | |||
| 70 | $skew_tree = $right_key - $left_key + 1; |
||
| 71 | $skew_edit = $right_key_near - $left_key + 1; |
||
| 72 | $skew_level = $level_up - $entity->depth; |
||
| 73 | |||
| 74 | $spaceName = $space->getName(); |
||
| 75 | |||
| 76 | if ($right_key < $right_key_near) { |
||
| 77 | $skew_edit -= $skew_tree; |
||
| 78 | } |
||
| 79 | $result = $this->mapper->getClient()->evaluate(" |
||
| 80 | local result = {} |
||
| 81 | local updates = {} |
||
| 82 | local leftKeys = {} |
||
| 83 | local rightKeys = {} |
||
| 84 | local maxRightTuple = box.space.$spaceName.index.group_right:max(right); |
||
| 85 | local maxLeftTuple = box.space.$spaceName.index.group_left:max(left); |
||
| 86 | local maxRight = 100 |
||
| 87 | if maxRightTuple ~= nil then |
||
| 88 | maxRight = maxRightTuple[$map->right]+100 |
||
| 89 | end |
||
| 90 | local maxLeft = 100 |
||
| 91 | if maxLeftTuple ~= nil then |
||
| 92 | maxLeft = maxLeftTuple[$map->left]+100 |
||
| 93 | end |
||
| 94 | box.begin() |
||
| 95 | for i, node in box.space.$spaceName:pairs() do |
||
| 96 | if $right_key < $right_key_near then |
||
| 97 | if node[$map->right] > $left_key and node[$map->left] <= $right_key_near then |
||
| 98 | if node[$map->right] <= $right_key then |
||
| 99 | table.insert(updates, {node[$map->id], $map->left, node[$map->left]+$skew_edit}) |
||
| 100 | box.space.$spaceName:update(node[$map->id], {{'=', $map->left, maxLeft}}) |
||
| 101 | maxLeft = maxLeft+1 |
||
| 102 | elseif node[$map->left] > $right_key then |
||
| 103 | table.insert(updates, {node[$map->id], $map->left, node[$map->left]-$skew_tree}) |
||
| 104 | box.space.$spaceName:update(node[$map->id], {{'=', $map->left, maxLeft}}) |
||
| 105 | maxLeft = maxLeft+1 |
||
| 106 | end |
||
| 107 | if node[$map->right] <= $right_key then |
||
| 108 | table.insert(updates, {node[$map->id], $map->depth, node[$map->depth]+$skew_level}) |
||
| 109 | end |
||
| 110 | if node[$map->right] <= $right_key then |
||
| 111 | table.insert(updates, {node[$map->id], $map->right, node[$map->right]+$skew_edit}) |
||
| 112 | box.space.$spaceName:update(node[$map->id], {{'=', $map->right, maxRight}}) |
||
| 113 | maxRight = maxRight+1 |
||
| 114 | elseif node[$map->right] <= $right_key_near then |
||
| 115 | table.insert(updates, {node[$map->id], $map->right, node[$map->right]-$skew_tree}) |
||
| 116 | box.space.$spaceName:update(node[$map->id], {{'=', $map->right, maxRight}}) |
||
| 117 | maxRight = maxRight+1 |
||
| 118 | end |
||
| 119 | end |
||
| 120 | else |
||
| 121 | if node[$map->right] > $right_key_near and node[$map->left] < $right_key then |
||
| 122 | if node[$map->left] >= $left_key then |
||
| 123 | table.insert(updates, {node[$map->id], $map->right, node[$map->right]+$skew_edit}) |
||
| 124 | box.space.$spaceName:update(node[$map->id], {{'=', $map->right, maxRight}}) |
||
| 125 | maxRight = maxRight+1 |
||
| 126 | elseif node[$map->right] < $left_key then |
||
| 127 | table.insert(updates, {node[$map->id], $map->right, node[$map->right]+$skew_tree}) |
||
| 128 | box.space.$spaceName:update(node[$map->id], {{'=', $map->right, maxRight}}) |
||
| 129 | maxRight = maxRight+1 |
||
| 130 | end |
||
| 131 | if node[$map->left] >= $left_key then |
||
| 132 | table.insert(updates, {node[$map->id], $map->depth, node[$map->depth]+$skew_level}) |
||
| 133 | end |
||
| 134 | if node[$map->left] >= $left_key then |
||
| 135 | table.insert(updates, {node[$map->id], $map->left, node[$map->left]+$skew_edit}) |
||
| 136 | box.space.$spaceName:update(node[$map->id], {{'=', $map->left, maxLeft}}) |
||
| 137 | maxLeft = maxLeft+1 |
||
| 138 | elseif node[$map->left] > $right_key_near then |
||
| 139 | table.insert(updates, {node[$map->id], $map->left, node[$map->left]+$skew_tree}) |
||
| 140 | box.space.$spaceName:update(node[$map->id], {{'=', $map->left, maxLeft}}) |
||
| 141 | maxLeft = maxLeft+1 |
||
| 142 | end |
||
| 143 | end |
||
| 144 | end |
||
| 145 | end |
||
| 146 | for i, node in pairs(updates) do |
||
| 147 | table.insert(result, node[1]) |
||
| 148 | box.space.$spaceName:update(node[1], {{'=', node[2], node[3]}}) |
||
| 149 | end |
||
| 150 | box.commit() |
||
| 151 | |||
| 152 | return result |
||
| 153 | ")->getData(); |
||
| 154 | |||
| 155 | foreach (array_unique($result[0]) as $id) { |
||
| 156 | $space->getRepository()->sync($id); |
||
| 157 | } |
||
| 158 | |||
| 159 | $space->getRepository()->flushCache(); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 301 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.