| Conditions | 21 |
| Paths | 4620 |
| Total Lines | 87 |
| Code Lines | 52 |
| 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 |
||
| 132 | private function updateState($params) |
||
| 133 | { |
||
| 134 | $params = [ |
||
| 135 | 'entity' => $params['entity'], |
||
| 136 | 'id' => $params['id'], |
||
| 137 | ]; |
||
| 138 | |||
| 139 | $timeaxis = []; |
||
| 140 | $changeaxis = []; |
||
| 141 | |||
| 142 | foreach ($this->mapper->find('_override', $params) as $i => $override) { |
||
| 143 | foreach (['begin', 'end'] as $field) { |
||
| 144 | if (!array_key_exists($override->$field, $timeaxis)) { |
||
| 145 | $timeaxis[$override->$field] = [ |
||
| 146 | 'begin' => $override->$field, |
||
| 147 | 'end' => $override->$field, |
||
| 148 | 'data' => [], |
||
| 149 | ]; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | if (!array_key_exists($override->timestamp, $changeaxis)) { |
||
| 154 | $changeaxis[$override->timestamp] = []; |
||
| 155 | } |
||
| 156 | $changeaxis[$override->timestamp][] = $override; |
||
| 157 | } |
||
| 158 | |||
| 159 | ksort($changeaxis); |
||
| 160 | ksort($timeaxis); |
||
| 161 | |||
| 162 | $nextSliceId = null; |
||
| 163 | foreach (array_reverse(array_keys($timeaxis)) as $timestamp) { |
||
| 164 | if ($nextSliceId) { |
||
| 165 | $timeaxis[$timestamp]['end'] = $nextSliceId; |
||
| 166 | } else { |
||
| 167 | $timeaxis[$timestamp]['end'] = 0; |
||
| 168 | } |
||
| 169 | $nextSliceId = $timestamp; |
||
| 170 | } |
||
| 171 | |||
| 172 | foreach ($this->mapper->find('_override_aggregate', $params) as $state) { |
||
| 173 | $this->mapper->remove($state); |
||
| 174 | } |
||
| 175 | |||
| 176 | $states = []; |
||
| 177 | foreach ($timeaxis as $state) { |
||
| 178 | foreach ($changeaxis as $overrides) { |
||
| 179 | foreach ($overrides as $override) { |
||
| 180 | if ($override->begin > $state['begin']) { |
||
| 181 | // future override |
||
| 182 | continue; |
||
| 183 | } |
||
| 184 | if ($override->end && ($override->end < $state['end'] || !$state['end'])) { |
||
| 185 | // complete override |
||
| 186 | continue; |
||
| 187 | } |
||
| 188 | |||
| 189 | $state['data'] = array_merge($state['data'], $override->data); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | if (count($state['data'])) { |
||
| 193 | $states[] = array_merge($state, $params); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | // merge states |
||
| 198 | $clean = false; |
||
| 199 | while (!$clean) { |
||
| 200 | $clean = true; |
||
| 201 | foreach ($states as $i => $state) { |
||
| 202 | if (array_key_exists($i+1, $states)) { |
||
| 203 | $next = $states[$i+1]; |
||
| 204 | if (!count(array_diff_assoc($state['data'], $next['data']))) { |
||
| 205 | $states[$i]['end'] = $next['end']; |
||
| 206 | unset($states[$i+1]); |
||
| 207 | $states = array_values($states); |
||
| 208 | $clean = false; |
||
| 209 | break; |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | foreach ($states as $state) { |
||
| 216 | $this->mapper->create('_override_aggregate', $state); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 228 |