| 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 |
||
| 101 | private function updateState($params) |
||
| 102 | { |
||
| 103 | $params = [ |
||
| 104 | 'entity' => $params['entity'], |
||
| 105 | 'id' => $params['id'], |
||
| 106 | ]; |
||
| 107 | |||
| 108 | $timeaxis = []; |
||
| 109 | $changeaxis = []; |
||
| 110 | |||
| 111 | foreach ($this->mapper->find('_override', $params) as $i => $override) { |
||
| 112 | foreach (['begin', 'end'] as $field) { |
||
| 113 | if (!array_key_exists($override->$field, $timeaxis)) { |
||
| 114 | $timeaxis[$override->$field] = [ |
||
| 115 | 'begin' => $override->$field, |
||
| 116 | 'end' => $override->$field, |
||
| 117 | 'data' => [], |
||
| 118 | ]; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | if (!array_key_exists($override->timestamp, $changeaxis)) { |
||
| 123 | $changeaxis[$override->timestamp] = []; |
||
| 124 | } |
||
| 125 | $changeaxis[$override->timestamp][] = $override; |
||
| 126 | } |
||
| 127 | |||
| 128 | ksort($changeaxis); |
||
| 129 | ksort($timeaxis); |
||
| 130 | |||
| 131 | $nextSliceId = null; |
||
| 132 | foreach (array_reverse(array_keys($timeaxis)) as $timestamp) { |
||
| 133 | if ($nextSliceId) { |
||
| 134 | $timeaxis[$timestamp]['end'] = $nextSliceId; |
||
| 135 | } else { |
||
| 136 | $timeaxis[$timestamp]['end'] = 0; |
||
| 137 | } |
||
| 138 | $nextSliceId = $timestamp; |
||
| 139 | } |
||
| 140 | |||
| 141 | foreach ($this->mapper->find('_override_aggregate', $params) as $state) { |
||
| 142 | $this->mapper->remove($state); |
||
| 143 | } |
||
| 144 | |||
| 145 | $states = []; |
||
| 146 | foreach ($timeaxis as $state) { |
||
| 147 | foreach ($changeaxis as $overrides) { |
||
| 148 | foreach ($overrides as $override) { |
||
| 149 | if ($override->begin > $state['begin']) { |
||
| 150 | // future override |
||
| 151 | continue; |
||
| 152 | } |
||
| 153 | if ($override->end && ($override->end < $state['end'] || !$state['end'])) { |
||
| 154 | // complete override |
||
| 155 | continue; |
||
| 156 | } |
||
| 157 | |||
| 158 | $state['data'] = array_merge($state['data'], $override->data); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | if (count($state['data'])) { |
||
| 162 | $states[] = array_merge($state, $params); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | // merge states |
||
| 167 | $clean = false; |
||
| 168 | while (!$clean) { |
||
| 169 | $clean = true; |
||
| 170 | foreach ($states as $i => $state) { |
||
| 171 | if (array_key_exists($i+1, $states)) { |
||
| 172 | $next = $states[$i+1]; |
||
| 173 | if (!count(array_diff_assoc($state['data'], $next['data']))) { |
||
| 174 | $states[$i]['end'] = $next['end']; |
||
| 175 | unset($states[$i+1]); |
||
| 176 | $states = array_values($states); |
||
| 177 | $clean = false; |
||
| 178 | break; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | foreach ($states as $state) { |
||
| 185 | $this->mapper->create('_override_aggregate', $state); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 232 |