| Conditions | 34 |
| Paths | > 20000 |
| Total Lines | 142 |
| Code Lines | 86 |
| 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 |
||
| 93 | public function updateLinkAggregation(Entity $node) |
||
| 94 | { |
||
| 95 | $todo = [ |
||
| 96 | $this->temporal->entityIdToName($node->entity) => $node->entityId, |
||
| 97 | ]; |
||
| 98 | |||
| 99 | $current = $node; |
||
| 100 | while ($current->parent) { |
||
| 101 | $current = $this->temporal->getMapper()->findOne('_temporal_link', ['id' => $current->parent]); |
||
| 102 | $todo[$this->temporal->entityIdToName($current->entity)] = $current->entityId; |
||
| 103 | } |
||
| 104 | |||
| 105 | foreach ($todo as $entity => $id) { |
||
| 106 | $spaceId = $this->temporal->entityNameToId($entity); |
||
| 107 | $source = $this->temporal->getMapper()->find('_temporal_link', [ |
||
| 108 | 'entity' => $spaceId, |
||
| 109 | 'entityId' => $id, |
||
| 110 | ]); |
||
| 111 | |||
| 112 | $leafs = []; |
||
| 113 | foreach ($source as $node) { |
||
| 114 | foreach ($this->getLeafs($node) as $detail) { |
||
| 115 | $leafs[] = $detail; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | $changeaxis = []; |
||
| 120 | |||
| 121 | foreach ($leafs as $leaf) { |
||
| 122 | $current = $leaf; |
||
| 123 | $ref = []; |
||
| 124 | |||
| 125 | if (property_exists($leaf, 'idle') && $leaf->idle) { |
||
| 126 | continue; |
||
| 127 | } |
||
| 128 | |||
| 129 | while ($current) { |
||
| 130 | if ($current->entity != $spaceId) { |
||
| 131 | $ref[$current->entity] = $current->entityId; |
||
| 132 | } |
||
| 133 | if ($current->parent) { |
||
| 134 | $current = $this->temporal->getMapper()->findOne('_temporal_link', $current->parent); |
||
| 135 | } else { |
||
| 136 | $current = null; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | $data = [$ref]; |
||
| 141 | if (property_exists($leaf, 'data') && $leaf->data) { |
||
| 142 | $data[] = $leaf->data; |
||
| 143 | } |
||
| 144 | |||
| 145 | if (!array_key_exists($leaf->timestamp, $changeaxis)) { |
||
| 146 | $changeaxis[$leaf->timestamp] = []; |
||
| 147 | } |
||
| 148 | $changeaxis[$leaf->timestamp][] = (object) [ |
||
| 149 | 'begin' => $leaf->begin, |
||
| 150 | 'end' => $leaf->end, |
||
| 151 | 'data' => $data |
||
| 152 | ]; |
||
| 153 | } |
||
| 154 | |||
| 155 | $params = [ |
||
| 156 | 'entity' => $spaceId, |
||
| 157 | 'id' => $id, |
||
| 158 | ]; |
||
| 159 | |||
| 160 | $timeaxis = []; |
||
| 161 | foreach ($changeaxis as $timestamp => $changes) { |
||
| 162 | foreach ($changes as $change) { |
||
| 163 | foreach (['begin', 'end'] as $field) { |
||
| 164 | if (!array_key_exists($change->$field, $timeaxis)) { |
||
| 165 | $timeaxis[$change->$field] = (object) [ |
||
| 166 | 'begin' => $change->$field, |
||
| 167 | 'end' => $change->$field, |
||
| 168 | 'data' => [], |
||
| 169 | ]; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | ksort($changeaxis); |
||
| 176 | ksort($timeaxis); |
||
| 177 | |||
| 178 | $nextSliceId = null; |
||
| 179 | foreach (array_reverse(array_keys($timeaxis)) as $timestamp) { |
||
| 180 | if ($nextSliceId) { |
||
| 181 | $timeaxis[$timestamp]->end = $nextSliceId; |
||
| 182 | } else { |
||
| 183 | $timeaxis[$timestamp]->end = 0; |
||
| 184 | } |
||
| 185 | $nextSliceId = $timestamp; |
||
| 186 | } |
||
| 187 | |||
| 188 | foreach ($this->temporal->getMapper()->find('_temporal_link_aggregate', $params) as $state) { |
||
| 189 | $this->temporal->getMapper()->remove($state); |
||
| 190 | } |
||
| 191 | |||
| 192 | $states = []; |
||
| 193 | foreach ($timeaxis as $state) { |
||
| 194 | foreach ($changeaxis as $changes) { |
||
| 195 | foreach ($changes as $change) { |
||
| 196 | if ($change->begin > $state->begin) { |
||
| 197 | // future override |
||
| 198 | continue; |
||
| 199 | } |
||
| 200 | if ($change->end && ($change->end < $state->end || !$state->end)) { |
||
| 201 | // complete override |
||
| 202 | continue; |
||
| 203 | } |
||
| 204 | $state->data[] = $change->data; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | if (count($state->data)) { |
||
| 208 | $states[] = (object) array_merge(get_object_vars($state), $params); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | // merge states |
||
| 213 | $clean = false; |
||
| 214 | while (!$clean) { |
||
| 215 | $clean = true; |
||
| 216 | foreach ($states as $i => $state) { |
||
| 217 | if (array_key_exists($i+1, $states)) { |
||
| 218 | $next = $states[$i+1]; |
||
| 219 | if (json_encode($state->data) == json_encode($next->data)) { |
||
| 220 | $states[$i]->end = $next->end; |
||
| 221 | unset($states[$i+1]); |
||
| 222 | $states = array_values($states); |
||
| 223 | $clean = false; |
||
| 224 | break; |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | foreach ($states as $state) { |
||
| 231 | $this->temporal->getMapper()->create('_temporal_link_aggregate', $state); |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 359 |
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.