| Conditions | 42 |
| Paths | > 20000 |
| Total Lines | 171 |
| Code Lines | 99 |
| 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 |
||
| 32 | public function updateReferenceState($entity, $id) |
||
| 33 | { |
||
| 34 | $mapper = $this->temporal->getMapper(); |
||
| 35 | |||
| 36 | $params = [ |
||
| 37 | 'entity' => $this->temporal->entityNameToId($entity), |
||
| 38 | 'id' => $id, |
||
| 39 | ]; |
||
| 40 | |||
| 41 | // collect changes |
||
| 42 | |||
| 43 | $states = []; |
||
| 44 | $changeaxis = []; |
||
| 45 | foreach ($mapper->find('_temporal_reference', $params) as $i => $reference) { |
||
| 46 | if (property_exists($reference, 'idle') && $reference->idle) { |
||
| 47 | continue; |
||
| 48 | } |
||
| 49 | if (!array_key_exists($reference->target, $changeaxis)) { |
||
| 50 | $changeaxis[$reference->target] = []; |
||
| 51 | } |
||
| 52 | $changeaxis[$reference->target][] = get_object_vars($reference); |
||
| 53 | } |
||
| 54 | |||
| 55 | // calculate states |
||
| 56 | |||
| 57 | $timeaxis = []; |
||
|
|
|||
| 58 | foreach ($changeaxis as $target => $references) { |
||
| 59 | $targetStates = []; |
||
| 60 | foreach ($references as $reference) { |
||
| 61 | foreach (['begin', 'end'] as $field) { |
||
| 62 | if (!array_key_exists($reference[$field], $targetStates)) { |
||
| 63 | $targetStates[$reference[$field]] = [ |
||
| 64 | 'begin' => $reference[$field], |
||
| 65 | 'end' => $reference[$field], |
||
| 66 | ]; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 | ksort($targetStates); |
||
| 71 | |||
| 72 | $nextSliceId = null; |
||
| 73 | foreach (array_reverse(array_keys($targetStates)) as $timestamp) { |
||
| 74 | if ($nextSliceId) { |
||
| 75 | $targetStates[$timestamp]['end'] = $nextSliceId; |
||
| 76 | } else { |
||
| 77 | $targetStates[$timestamp]['end'] = 0; |
||
| 78 | } |
||
| 79 | $nextSliceId = $timestamp; |
||
| 80 | } |
||
| 81 | |||
| 82 | foreach ($targetStates as $state) { |
||
| 83 | foreach ($changeaxis[$target] as $reference) { |
||
| 84 | if ($reference['begin'] > $state['begin']) { |
||
| 85 | // future reference |
||
| 86 | continue; |
||
| 87 | } |
||
| 88 | if ($reference['end'] && ($reference['end'] < $state['end'] || !$state['end'])) { |
||
| 89 | // complete reference |
||
| 90 | continue; |
||
| 91 | } |
||
| 92 | $state['targetId'] = $reference['targetId']; |
||
| 93 | } |
||
| 94 | if (array_key_exists('targetId', $state)) { |
||
| 95 | $states[] = array_merge($state, $params, ['target' => $target]); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | // merge reference states |
||
| 101 | |||
| 102 | $clean = false; |
||
| 103 | while (!$clean) { |
||
| 104 | $clean = true; |
||
| 105 | foreach ($states as $i => $state) { |
||
| 106 | if (array_key_exists($i+1, $states)) { |
||
| 107 | $next = $states[$i+1]; |
||
| 108 | if ($state['target'] == $next['target'] && $state['targetId'] == $next['targetId']) { |
||
| 109 | $states[$i]['end'] = $next['end']; |
||
| 110 | unset($states[$i+1]); |
||
| 111 | $states = array_values($states); |
||
| 112 | $clean = false; |
||
| 113 | break; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | // update states in database |
||
| 120 | |||
| 121 | $affected = []; |
||
| 122 | foreach ($mapper->find('_temporal_reference_state', $params) as $state) { |
||
| 123 | $mapper->remove($state); |
||
| 124 | } |
||
| 125 | foreach ($states as $state) { |
||
| 126 | if (!in_array([$state['target'], $state['targetId']], $affected)) { |
||
| 127 | $affected[] = [$state['target'], $state['targetId']]; |
||
| 128 | } |
||
| 129 | $mapper->create('_temporal_reference_state', $state); |
||
| 130 | } |
||
| 131 | |||
| 132 | |||
| 133 | // update reference aggregation for affected targets |
||
| 134 | foreach ($affected as [$entity, $entityId]) { |
||
| 135 | // collect changes |
||
| 136 | $changeaxis = $mapper->find('_temporal_reference_state', [ |
||
| 137 | 'target' => $entity, |
||
| 138 | 'targetId' => $entityId, |
||
| 139 | 'entity' => $params['entity'], |
||
| 140 | ]); |
||
| 141 | $changeaxis = array_map('get_object_vars', $changeaxis); |
||
| 142 | // generate states |
||
| 143 | $targetStates = []; |
||
| 144 | foreach ($changeaxis as $state) { |
||
| 145 | foreach (['begin', 'end'] as $field) { |
||
| 146 | if (!array_key_exists($state[$field], $targetStates)) { |
||
| 147 | $targetStates[$state[$field]] = [ |
||
| 148 | 'begin' => $state[$field], |
||
| 149 | 'end' => $state[$field], |
||
| 150 | 'data' => [], |
||
| 151 | ]; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 | ksort($targetStates); |
||
| 156 | $nextSliceId = null; |
||
| 157 | foreach (array_reverse(array_keys($targetStates)) as $timestamp) { |
||
| 158 | if ($nextSliceId) { |
||
| 159 | $targetStates[$timestamp]['end'] = $nextSliceId; |
||
| 160 | } else { |
||
| 161 | $targetStates[$timestamp]['end'] = 0; |
||
| 162 | } |
||
| 163 | $nextSliceId = $timestamp; |
||
| 164 | } |
||
| 165 | |||
| 166 | $aggregateParams = [ |
||
| 167 | 'entity' => $entity, |
||
| 168 | 'id' => $entityId, |
||
| 169 | 'source' => $params['entity'] |
||
| 170 | ]; |
||
| 171 | |||
| 172 | // calculate aggregates |
||
| 173 | $aggregates = []; |
||
| 174 | foreach ($targetStates as $state) { |
||
| 175 | foreach ($changeaxis as $reference) { |
||
| 176 | if ($reference['begin'] > $state['begin']) { |
||
| 177 | // future reference |
||
| 178 | continue; |
||
| 179 | } |
||
| 180 | if ($reference['end'] && ($reference['end'] < $state['end'] || !$state['end'])) { |
||
| 181 | // complete reference |
||
| 182 | continue; |
||
| 183 | } |
||
| 184 | if (!in_array($reference['id'], $state['data'])) { |
||
| 185 | $state['data'][] = $reference['id']; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | if (count($state['data'])) { |
||
| 189 | $aggregates[] = array_merge($state, $aggregateParams); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | // sync aggregates |
||
| 194 | foreach ($mapper->find('_temporal_reference_aggregate', $aggregateParams) as $aggregate) { |
||
| 195 | $mapper->remove($aggregate); |
||
| 196 | } |
||
| 197 | |||
| 198 | foreach ($aggregates as $aggregate) { |
||
| 199 | $mapper->create('_temporal_reference_aggregate', $aggregate); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 386 |
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.