Complex classes like Temporal 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 Temporal, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Temporal extends Plugin |
||
17 | { |
||
18 | private $actor; |
||
19 | private $timestamps = []; |
||
20 | private $aggregator; |
||
21 | |||
22 | public function __construct(Mapper $mapper) |
||
23 | { |
||
24 | $this->mapper = $mapper; |
||
25 | $this->schema = new Schema($mapper); |
||
|
|||
26 | $this->aggregator = new Aggregator($this); |
||
27 | } |
||
28 | |||
29 | public function getReference($entity, $id, $target, $date) |
||
30 | { |
||
31 | if (!$this->mapper->getSchema()->hasSpace('_temporal_reference_state')) { |
||
32 | return; |
||
33 | } |
||
34 | |||
35 | $entity = $this->entityNameToId($entity); |
||
36 | $target = $this->entityNameToId($target); |
||
37 | $date = $this->getTimestamp($date); |
||
38 | |||
39 | $rows = $this->mapper->getClient()->getSpace('_temporal_reference_state') |
||
40 | ->select(Criteria::key([$entity, $id, $target, $date])->andLimit(1)->andLeIterator()); |
||
41 | |||
42 | if (count($rows)) { |
||
43 | $row = $rows[0]; |
||
44 | if ([$entity, $id, $target] == [$row[0], $row[1], $row[2]]) { |
||
45 | $state = $this->mapper->findOne('_temporal_reference_state', [ |
||
46 | 'entity' => $entity, |
||
47 | 'id' => $id, |
||
48 | 'target' => $target, |
||
49 | 'begin' => $row[3] |
||
50 | ]); |
||
51 | if (!$state->end || $state->end >= $date) { |
||
52 | return $state->targetId; |
||
53 | } |
||
54 | } |
||
55 | } |
||
56 | } |
||
57 | |||
58 | public function getAggregator() |
||
59 | { |
||
60 | return $this->aggregator; |
||
61 | } |
||
62 | |||
63 | public function getReferenceLog($entity, $id, $target) |
||
64 | { |
||
65 | if (!$this->mapper->getSchema()->hasSpace('_temporal_reference')) { |
||
66 | return []; |
||
67 | } |
||
68 | $log = []; |
||
69 | $params = [ |
||
70 | 'entity' => $this->entityNameToId($entity), |
||
71 | 'id' => $id, |
||
72 | 'target' => $this->entityNameToId($target), |
||
73 | ]; |
||
74 | foreach ($this->mapper->find('_temporal_reference', $params) as $reference) { |
||
75 | $log[] = $reference; |
||
76 | } |
||
77 | return $log; |
||
78 | } |
||
79 | |||
80 | public function getReferenceStates($entity, $entityId, $target, $begin, $end) |
||
81 | { |
||
82 | if (!$this->mapper->getSchema()->hasSpace('_temporal_reference_state')) { |
||
83 | return; |
||
84 | } |
||
85 | |||
86 | $states = $this->mapper->find('_temporal_reference_state', [ |
||
87 | 'entity' => $this->entityNameToId($entity), |
||
88 | 'id' => $entityId, |
||
89 | 'target' => $this->entityNameToId($target), |
||
90 | ]); |
||
91 | |||
92 | $begin = $this->getTimestamp($begin); |
||
93 | $end = $this->getTimestamp($end); |
||
94 | |||
95 | $slices = []; |
||
96 | foreach ($states as $state) { |
||
97 | if ($state->begin < $end && ($begin < $state->end || !$state->end)) { |
||
98 | $slices[] = [ |
||
99 | 'begin' => +date('Ymd', max($state->begin, $begin)), |
||
100 | 'end' => +date('Ymd', min($state->end ?: $end, $end)), |
||
101 | 'value' => $state->targetId, |
||
102 | ]; |
||
103 | } |
||
104 | } |
||
105 | |||
106 | return $slices; |
||
107 | } |
||
108 | |||
109 | public function getReferences($target, $targetId, $source, $date) |
||
110 | { |
||
111 | if (!$this->mapper->getSchema()->hasSpace('_temporal_reference_aggregate')) { |
||
112 | return []; |
||
113 | } |
||
114 | |||
115 | $target = $this->entityNameToId($target); |
||
116 | $source = $this->entityNameToId($source); |
||
117 | $date = $this->getTimestamp($date); |
||
118 | |||
119 | $rows = $this->mapper->getClient()->getSpace('_temporal_reference_aggregate') |
||
120 | ->select(Criteria::key([$target, $targetId, $source, $date])->andLimit(1)->andLeIterator()); |
||
121 | |||
122 | if (count($rows)) { |
||
123 | $row = $rows[0]; |
||
124 | if ([$target, $targetId, $source] == [$row[0], $row[1], $row[2]]) { |
||
125 | $state = $this->mapper->findOne('_temporal_reference_aggregate', [ |
||
126 | 'entity' => $target, |
||
127 | 'id' => $targetId, |
||
128 | 'source' => $source, |
||
129 | 'begin' => $row[3] |
||
130 | ]); |
||
131 | |||
132 | if (!$state->end || $state->end > $date) { |
||
133 | return $state->data; |
||
134 | } |
||
135 | } |
||
136 | } |
||
137 | return []; |
||
138 | } |
||
139 | |||
140 | public function reference(array $reference) |
||
141 | { |
||
142 | $reference = $this->parseConfig($reference); |
||
143 | |||
144 | foreach ($reference as $k => $v) { |
||
145 | if (!in_array($k, ['entity', 'id', 'begin', 'end', 'data'])) { |
||
146 | $reference['entity'] = $k; |
||
147 | $reference['id'] = $v; |
||
148 | unset($reference[$k]); |
||
149 | } |
||
150 | } |
||
151 | |||
152 | if (!array_key_exists('entity', $reference)) { |
||
153 | throw new Exception("no entity defined"); |
||
154 | } |
||
155 | |||
156 | if (count($reference['data']) != 1) { |
||
157 | throw new Exception("Invalid reference configuration"); |
||
158 | } |
||
159 | |||
160 | [$targetName] = array_keys($reference['data']); |
||
161 | $reference['target'] = $this->entityNameToId($targetName); |
||
162 | $reference['targetId'] = $reference['data'][$targetName]; |
||
163 | |||
164 | |||
165 | // set entity id |
||
166 | $entityName = $reference['entity']; |
||
167 | $reference['entity'] = $this->entityNameToId($entityName); |
||
168 | $reference['actor'] = $this->actor; |
||
169 | $reference['timestamp'] = Carbon::now()->timestamp; |
||
170 | |||
171 | $this->schema->init('reference'); |
||
172 | $this->mapper->create('_temporal_reference', $reference); |
||
173 | |||
174 | $this->aggregator->updateReferenceState($entityName, $reference['id'], $targetName); |
||
175 | } |
||
176 | |||
177 | public function getLinksLog($entity, $entityId, $filter = []) |
||
178 | { |
||
179 | if (!$this->mapper->getSchema()->hasSpace('_temporal_link')) { |
||
180 | return []; |
||
181 | } |
||
182 | |||
183 | $entity = $this->entityNameToId($entity); |
||
184 | |||
185 | $nodes = $this->mapper->find('_temporal_link', [ |
||
186 | 'entity' => $entity, |
||
187 | 'entityId' => $entityId, |
||
188 | ]); |
||
189 | |||
190 | $links = []; |
||
191 | |||
192 | foreach ($nodes as $node) { |
||
193 | foreach ($this->aggregator->getLeafs($node) as $leaf) { |
||
194 | $entityName = $this->entityIdToName($leaf->entity); |
||
195 | $link = [ |
||
196 | $entityName => $leaf->entityId, |
||
197 | 'id' => $leaf->id, |
||
198 | 'begin' => $leaf->begin, |
||
199 | 'end' => $leaf->end, |
||
200 | 'timestamp' => $leaf->timestamp, |
||
201 | 'actor' => $leaf->actor, |
||
202 | 'idle' => property_exists($leaf, 'idle') ? $leaf->idle : 0, |
||
203 | ]; |
||
204 | |||
205 | $current = $leaf; |
||
206 | while ($current->parent) { |
||
207 | $current = $this->mapper->findOne('_temporal_link', $current->parent); |
||
208 | $entityName = $this->entityIdToName($current->entity); |
||
209 | $link[$entityName] = $current->entityId; |
||
210 | } |
||
211 | |||
212 | if (count($filter)) { |
||
213 | foreach ($filter as $required) { |
||
214 | if (!array_key_exists($required, $link)) { |
||
215 | continue 2; |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | $links[] = $link; |
||
220 | } |
||
221 | } |
||
222 | |||
223 | return $links; |
||
224 | } |
||
225 | |||
226 | public function getLinks($entity, $id, $date) |
||
227 | { |
||
228 | if (!$this->mapper->getSchema()->hasSpace('_temporal_link_aggregate')) { |
||
229 | return []; |
||
230 | } |
||
231 | |||
232 | $links = $this->getData($entity, $id, $date, '_temporal_link_aggregate'); |
||
233 | foreach ($links as $i => $source) { |
||
234 | $link = array_key_exists(1, $source) ? ['data' => $source[1]] : []; |
||
235 | foreach ($source[0] as $spaceId => $entityId) { |
||
236 | $spaceName = $this->mapper->findOne('_temporal_entity', $spaceId)->name; |
||
237 | $link[$spaceName] = $entityId; |
||
238 | } |
||
239 | $links[$i] = $link; |
||
240 | } |
||
241 | return $links; |
||
242 | } |
||
243 | |||
244 | public function getState($entity, $id, $date) |
||
245 | { |
||
246 | if (!$this->mapper->getSchema()->hasSpace('_temporal_override_aggregate')) { |
||
247 | return []; |
||
248 | } |
||
249 | |||
250 | return $this->getData($entity, $id, $date, '_temporal_override_aggregate'); |
||
251 | } |
||
252 | |||
253 | private function getData($entity, $id, $date, $space) |
||
254 | { |
||
255 | $entity = $this->entityNameToId($entity); |
||
256 | $date = $this->getTimestamp($date); |
||
257 | |||
258 | $rows = $this->mapper->getClient()->getSpace($space) |
||
259 | ->select(Criteria::key([$entity, $id, $date])->andLimit(1)->andLeIterator()); |
||
260 | |||
261 | if (count($rows) && $rows[0][0] == $entity && $rows[0][1] == $id) { |
||
262 | $state = $this->mapper->findOne($space, [ |
||
263 | 'entity' => $entity, |
||
264 | 'id' => $id, |
||
265 | 'begin' => $rows[0][2] |
||
266 | ]); |
||
267 | if (!$state->end || $state->end >= $date) { |
||
268 | return $state->data; |
||
269 | } |
||
270 | } |
||
271 | |||
272 | return []; |
||
273 | } |
||
274 | |||
275 | public function getOverrides(string $entityName, int $id) : array |
||
286 | |||
287 | public function override(array $override) |
||
288 | { |
||
289 | $override = $this->parseConfig($override); |
||
290 | |||
291 | foreach ($override as $k => $v) { |
||
292 | if (!in_array($k, ['entity', 'id', 'begin', 'end', 'data'])) { |
||
293 | $override['entity'] = $k; |
||
294 | $override['id'] = $v; |
||
295 | unset($override[$k]); |
||
296 | } |
||
297 | } |
||
298 | |||
299 | if (!array_key_exists('entity', $override)) { |
||
300 | throw new Exception("no entity defined"); |
||
301 | } |
||
302 | |||
303 | // set entity id |
||
304 | $entityName = $override['entity']; |
||
305 | $override['entity'] = $this->entityNameToId($entityName); |
||
306 | $override['actor'] = $this->actor; |
||
307 | $override['timestamp'] = Carbon::now()->timestamp; |
||
308 | |||
309 | $this->schema->init('override'); |
||
310 | $this->mapper->create('_temporal_override', $override); |
||
311 | |||
312 | $this->aggregator->updateOverrideAggregation($entityName, $override['id']); |
||
313 | } |
||
314 | |||
315 | public function setLinkIdle($id, $flag) |
||
324 | |||
325 | public function toggleLinkIdle(Entity $link) |
||
336 | |||
337 | public function setReferenceIdle($entity, $id, $target, $targetId, $begin, $actor, $timestamp, $flag) |
||
353 | |||
354 | public function toggleReferenceIdle($entity, $id, $target, $targetId, $begin, $actor, $timestamp) |
||
375 | |||
376 | public function setOverrideIdle($entity, $id, $begin, $actor, $timestamp, $flag) |
||
390 | |||
391 | public function toggleOverrideIdle($entity, $id, $begin, $actor, $timestamp) |
||
410 | |||
411 | public function setReferenceEnd($entity, $id, $target, $targetId, $begin, $actor, $timestamp, $end) |
||
412 | { |
||
413 | $reference = $this->mapper->findOrFail('_temporal_reference', [ |
||
414 | 'entity' => $this->entityNameToId($entity), |
||
415 | 'id' => $id, |
||
416 | 'target' => $this->entityNameToId($target), |
||
417 | 'targetId' => $targetId, |
||
418 | 'begin' => $begin, |
||
419 | 'actor' => $actor, |
||
420 | 'timestamp' => $timestamp, |
||
421 | ]); |
||
422 | if ($reference->end != $end) { |
||
428 | |||
429 | public function setOverrideEnd($entity, $id, $begin, $actor, $timestamp, $end) |
||
444 | |||
445 | |||
446 | public function link(array $link) |
||
493 | |||
494 | public function getActor() |
||
498 | |||
499 | public function setActor($actor) |
||
504 | |||
505 | private function getTimestamp($string) |
||
520 | |||
521 | private function parseConfig(array $data) |
||
545 | |||
546 | public function entityNameToId($name) |
||
564 | |||
565 | public function entityIdToName($id) |
||
569 | } |
||
570 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: