Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Aggregator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Aggregator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Aggregator |
||
| 9 | { |
||
| 10 | private $temporal; |
||
| 11 | |||
| 12 | public function __construct(Temporal $temporal) |
||
| 16 | |||
| 17 | public function getLeafs($link) |
||
| 31 | |||
| 32 | public function updateReferenceState($entity, $id, $target) |
||
| 92 | |||
| 93 | |||
| 94 | public function updateLinkAggregation(Entity $node) |
||
| 95 | { |
||
| 96 | $todo = [ |
||
| 97 | $this->temporal->entityIdToName($node->entity) => $node->entityId, |
||
| 98 | ]; |
||
| 99 | |||
| 100 | $current = $node; |
||
| 101 | while ($current->parent) { |
||
| 102 | $current = $this->temporal->getMapper()->findOne('_temporal_link', ['id' => $current->parent]); |
||
| 103 | $todo[$this->temporal->entityIdToName($current->entity)] = $current->entityId; |
||
| 104 | } |
||
| 105 | |||
| 106 | foreach ($todo as $entity => $id) { |
||
| 107 | $spaceId = $this->temporal->entityNameToId($entity); |
||
| 108 | $source = $this->temporal->getMapper()->find('_temporal_link', [ |
||
| 109 | 'entity' => $spaceId, |
||
| 110 | 'entityId' => $id, |
||
| 111 | ]); |
||
| 112 | |||
| 113 | $leafs = []; |
||
| 114 | foreach ($source as $node) { |
||
| 115 | foreach ($this->getLeafs($node) as $detail) { |
||
| 116 | $leafs[] = $detail; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | $changeaxis = []; |
||
| 121 | |||
| 122 | foreach ($leafs as $leaf) { |
||
| 123 | $current = $leaf; |
||
| 124 | $ref = []; |
||
| 125 | |||
| 126 | if (property_exists($leaf, 'idle') && $leaf->idle) { |
||
| 127 | continue; |
||
| 128 | } |
||
| 129 | |||
| 130 | while ($current) { |
||
| 131 | if ($current->entity != $spaceId) { |
||
| 132 | $ref[$current->entity] = $current->entityId; |
||
| 133 | } |
||
| 134 | if ($current->parent) { |
||
| 135 | $current = $this->temporal->getMapper()->findOne('_temporal_link', $current->parent); |
||
| 136 | } else { |
||
| 137 | $current = null; |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | $data = [$ref]; |
||
| 142 | if (property_exists($leaf, 'data') && $leaf->data) { |
||
| 143 | $data[] = $leaf->data; |
||
| 144 | } |
||
| 145 | |||
| 146 | if (!array_key_exists($leaf->timestamp, $changeaxis)) { |
||
| 147 | $changeaxis[$leaf->timestamp] = []; |
||
| 148 | } |
||
| 149 | $changeaxis[$leaf->timestamp][] = (object) [ |
||
| 150 | 'begin' => $leaf->begin, |
||
| 151 | 'end' => $leaf->end, |
||
| 152 | 'data' => $data |
||
| 153 | ]; |
||
| 154 | } |
||
| 155 | |||
| 156 | $params = [ |
||
| 157 | 'entity' => $spaceId, |
||
| 158 | 'id' => $id, |
||
| 159 | ]; |
||
| 160 | |||
| 161 | $timeaxis = []; |
||
| 162 | foreach ($changeaxis as $timestamp => $changes) { |
||
| 163 | View Code Duplication | foreach ($changes as $change) { |
|
| 164 | foreach (['begin', 'end'] as $field) { |
||
| 165 | if (!array_key_exists($change->$field, $timeaxis)) { |
||
| 166 | $timeaxis[$change->$field] = (object) [ |
||
| 167 | 'begin' => $change->$field, |
||
| 168 | 'end' => $change->$field, |
||
| 169 | 'data' => [], |
||
| 170 | ]; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | ksort($changeaxis); |
||
| 177 | ksort($timeaxis); |
||
| 178 | |||
| 179 | $nextSliceId = null; |
||
| 180 | View Code Duplication | foreach (array_reverse(array_keys($timeaxis)) as $timestamp) { |
|
| 181 | if ($nextSliceId) { |
||
| 182 | $timeaxis[$timestamp]->end = $nextSliceId; |
||
| 183 | } else { |
||
| 184 | $timeaxis[$timestamp]->end = 0; |
||
| 185 | } |
||
| 186 | $nextSliceId = $timestamp; |
||
| 187 | } |
||
| 188 | |||
| 189 | foreach ($this->temporal->getMapper()->find('_temporal_link_aggregate', $params) as $state) { |
||
| 190 | $this->temporal->getMapper()->remove($state); |
||
| 191 | } |
||
| 192 | |||
| 193 | $states = []; |
||
| 194 | foreach ($timeaxis as $state) { |
||
| 195 | View Code Duplication | foreach ($changeaxis as $changes) { |
|
| 196 | foreach ($changes as $change) { |
||
| 197 | if ($change->begin > $state->begin) { |
||
| 198 | // future override |
||
| 199 | continue; |
||
| 200 | } |
||
| 201 | if ($change->end && ($change->end < $state->end || !$state->end)) { |
||
| 202 | // complete override |
||
| 203 | continue; |
||
| 204 | } |
||
| 205 | $state->data[] = $change->data; |
||
| 206 | } |
||
| 207 | } |
||
| 208 | if (count($state->data)) { |
||
| 209 | $states[] = (object) array_merge(get_object_vars($state), $params); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | // merge states |
||
| 214 | $clean = false; |
||
| 215 | View Code Duplication | while (!$clean) { |
|
| 216 | $clean = true; |
||
| 217 | foreach ($states as $i => $state) { |
||
| 218 | if (array_key_exists($i+1, $states)) { |
||
| 219 | $next = $states[$i+1]; |
||
| 220 | if (json_encode($state->data) == json_encode($next->data)) { |
||
| 221 | $states[$i]->end = $next->end; |
||
| 222 | unset($states[$i+1]); |
||
| 223 | $states = array_values($states); |
||
| 224 | $clean = false; |
||
| 225 | break; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | foreach ($states as $state) { |
||
| 232 | $this->temporal->getMapper()->create('_temporal_link_aggregate', $state); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | public function updateOverrideAggregation($entity, $id) |
||
| 260 | |||
| 261 | private function generateStates($changes, $callback) |
||
| 331 | } |
||
| 332 |
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.