Total Complexity | 47 |
Total Lines | 409 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like WorkflowInstance 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.
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 WorkflowInstance, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class WorkflowInstance |
||
10 | { |
||
11 | //@running_tasks - @queued_tasks > 0 |
||
12 | |||
13 | const STATUS_QUEUED = "QUEUED"; |
||
14 | const STATUS_RUNNING = "RUNNING"; |
||
15 | const STATUS_RETRYING = "RETRYING"; |
||
16 | const STATUS_FAILED = "FAILED"; |
||
17 | |||
18 | /** @var string|null $comment */ |
||
19 | private $comment; |
||
20 | |||
21 | /** @var \DateTime|null $endTime */ |
||
22 | private $endTime; |
||
23 | |||
24 | /** @var int|null $errors */ |
||
25 | private $errors; |
||
26 | |||
27 | /** @var string|null $host */ |
||
28 | private $host; |
||
29 | |||
30 | /** @var int $id */ |
||
31 | private $id; |
||
32 | |||
33 | /** @var string $name */ |
||
34 | private $name; |
||
35 | |||
36 | /** @var string|null $nodeName */ |
||
37 | private $nodeName; |
||
38 | |||
39 | /** @var int|null $scheduleId */ |
||
40 | private $scheduleId; |
||
41 | |||
42 | /** @var \DateTime $startTime */ |
||
43 | private $startTime; |
||
44 | |||
45 | /** @var string $status */ |
||
46 | private $status; |
||
47 | |||
48 | /** @var int|null $evqid */ |
||
49 | private $evqid; |
||
50 | |||
51 | /** @var int|null $queuedTasks */ |
||
52 | private $queuedTasks; |
||
53 | |||
54 | /** @var int|null $runningTasks */ |
||
55 | private $runningTasks; |
||
56 | |||
57 | /** @var int|null $retryingTasks */ |
||
58 | private $retryingTasks; |
||
59 | |||
60 | /** @var ArrayCollection */ |
||
61 | private $jobInstances; |
||
62 | |||
63 | /** |
||
64 | * WorkflowInstance constructor. |
||
65 | * |
||
66 | * @param array $params |
||
67 | * |
||
68 | * @throws \ReflectionException |
||
69 | */ |
||
70 | 33 | public function __construct(array $params) |
|
71 | { |
||
72 | 33 | $this->jobInstances = new ArrayCollection([]); |
|
73 | 33 | $thisObject = new \ReflectionClass($this); |
|
74 | 33 | $properties = $thisObject->getProperties(); |
|
75 | 33 | foreach ($properties as $property) { |
|
76 | 33 | $snakeProperty = $this->fromCamelCase($property->getName()); |
|
77 | 33 | if (isset($params[$snakeProperty])) { |
|
78 | 16 | $this->{$property->getName()} = $params[$snakeProperty]; |
|
79 | } |
||
80 | } |
||
81 | 33 | if (!is_null($this->getRunningTasks()) && !is_null($this->getQueuedTasks()) |
|
82 | 33 | && $this->getRunningTasks() - $this->getQueuedTasks() > 0) { |
|
83 | 1 | $this->setStatus(self::STATUS_RUNNING); |
|
84 | } |
||
85 | 33 | if (!is_null($this->getQueuedTasks()) && $this->getQueuedTasks() > 0) { |
|
86 | 2 | $this->setStatus(self::STATUS_QUEUED); |
|
87 | } |
||
88 | 33 | if (!is_null($this->getRetryingTasks()) && $this->getRetryingTasks() > 0) { |
|
89 | 2 | $this->setStatus(self::STATUS_RETRYING); |
|
90 | } |
||
91 | 33 | if (!is_null($this->getErrors()) && $this->getErrors() > 0) { |
|
92 | 2 | $this->setStatus(self::STATUS_FAILED); |
|
93 | } |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @return null|string |
||
98 | */ |
||
99 | 2 | public function getComment(): ?string |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param string $comment |
||
106 | * |
||
107 | * @return WorkflowInstance |
||
108 | */ |
||
109 | 1 | public function setComment(string $comment): WorkflowInstance |
|
110 | { |
||
111 | 1 | $this->comment = $comment; |
|
112 | 1 | return $this; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * @return null|\DateTime |
||
117 | */ |
||
118 | 2 | public function getEndTime(): ?\DateTime |
|
119 | { |
||
120 | 2 | return $this->endTime; |
|
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param \DateTime $endTime |
||
125 | * |
||
126 | * @return WorkflowInstance |
||
127 | */ |
||
128 | 1 | public function setEndTime(\DateTime $endTime): WorkflowInstance |
|
129 | { |
||
130 | 1 | $this->endTime = $endTime; |
|
131 | 1 | return $this; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * @return null|int |
||
136 | */ |
||
137 | 33 | public function getErrors(): ?int |
|
138 | { |
||
139 | 33 | return $this->errors; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param null|int $errors |
||
144 | * |
||
145 | * @return WorkflowInstance |
||
146 | */ |
||
147 | 1 | public function setErrors(?int $errors): WorkflowInstance |
|
148 | { |
||
149 | 1 | $this->errors = $errors; |
|
150 | 1 | return $this; |
|
151 | } |
||
152 | |||
153 | /** |
||
154 | * @return null|string |
||
155 | */ |
||
156 | 2 | public function getHost(): ?string |
|
157 | { |
||
158 | 2 | return $this->host; |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * @param string $host |
||
163 | * |
||
164 | * @return WorkflowInstance |
||
165 | */ |
||
166 | 1 | public function setHost(string $host): WorkflowInstance |
|
167 | { |
||
168 | 1 | $this->host = $host; |
|
169 | 1 | return $this; |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * @return int |
||
174 | */ |
||
175 | 2 | public function getId(): int |
|
176 | { |
||
177 | 2 | return $this->id; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param int $id |
||
182 | * |
||
183 | * @return WorkflowInstance |
||
184 | */ |
||
185 | 1 | public function setId(int $id): WorkflowInstance |
|
186 | { |
||
187 | 1 | $this->id = $id; |
|
188 | 1 | return $this; |
|
189 | } |
||
190 | |||
191 | /** |
||
192 | * @return string |
||
193 | */ |
||
194 | 2 | public function getName(): string |
|
195 | { |
||
196 | 2 | return $this->name; |
|
197 | } |
||
198 | |||
199 | /** |
||
200 | * @param string $name |
||
201 | * |
||
202 | * @return WorkflowInstance |
||
203 | */ |
||
204 | 1 | public function setName(string $name): WorkflowInstance |
|
205 | { |
||
206 | 1 | $this->name = $name; |
|
207 | 1 | return $this; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * @return null|string |
||
212 | */ |
||
213 | 2 | public function getNodeName(): ?string |
|
214 | { |
||
215 | 2 | return $this->nodeName; |
|
216 | } |
||
217 | |||
218 | /** |
||
219 | * @param string $nodeName |
||
220 | * |
||
221 | * @return WorkflowInstance |
||
222 | */ |
||
223 | 1 | public function setNodeName(string $nodeName): WorkflowInstance |
|
224 | { |
||
225 | 1 | $this->nodeName = $nodeName; |
|
226 | 1 | return $this; |
|
227 | } |
||
228 | |||
229 | /** |
||
230 | * @return null|int |
||
231 | */ |
||
232 | 2 | public function getScheduleId(): ?int |
|
233 | { |
||
234 | 2 | return $this->scheduleId; |
|
235 | } |
||
236 | |||
237 | /** |
||
238 | * @param int $scheduleId |
||
239 | * |
||
240 | * @return WorkflowInstance |
||
241 | */ |
||
242 | 1 | public function setScheduleId(int $scheduleId): WorkflowInstance |
|
243 | { |
||
244 | 1 | $this->scheduleId = $scheduleId; |
|
245 | 1 | return $this; |
|
246 | } |
||
247 | |||
248 | /** |
||
249 | * @return \DateTime |
||
250 | */ |
||
251 | 2 | public function getStartTime(): \DateTime |
|
252 | { |
||
253 | 2 | return $this->startTime; |
|
254 | } |
||
255 | |||
256 | /** |
||
257 | * @param \DateTime $startTime |
||
258 | * |
||
259 | * @return WorkflowInstance |
||
260 | */ |
||
261 | 1 | public function setStartTime(\DateTime $startTime): WorkflowInstance |
|
262 | { |
||
263 | 1 | $this->startTime = $startTime; |
|
264 | 1 | return $this; |
|
265 | } |
||
266 | |||
267 | /** |
||
268 | * @return string |
||
269 | */ |
||
270 | 3 | public function getStatus(): string |
|
271 | { |
||
272 | 3 | return $this->status; |
|
273 | } |
||
274 | |||
275 | /** |
||
276 | * @param string $status |
||
277 | * |
||
278 | * @return WorkflowInstance |
||
279 | */ |
||
280 | 5 | public function setStatus(string $status): WorkflowInstance |
|
281 | { |
||
282 | 5 | $this->status = $status; |
|
283 | 5 | return $this; |
|
284 | } |
||
285 | |||
286 | /** |
||
287 | * @return int|null |
||
288 | */ |
||
289 | 2 | public function getEvqid(): ?int |
|
290 | { |
||
291 | 2 | return $this->evqid; |
|
292 | } |
||
293 | |||
294 | /** |
||
295 | * @param int|null $evqid |
||
296 | * |
||
297 | * @return WorkflowInstance |
||
298 | */ |
||
299 | 1 | public function setEvqid(?int $evqid): WorkflowInstance |
|
300 | { |
||
301 | 1 | $this->evqid = $evqid; |
|
302 | 1 | return $this; |
|
303 | } |
||
304 | |||
305 | /** |
||
306 | * @return int|null |
||
307 | */ |
||
308 | 33 | public function getQueuedTasks(): ?int |
|
309 | { |
||
310 | 33 | return $this->queuedTasks; |
|
311 | } |
||
312 | |||
313 | /** |
||
314 | * @param int|null $queuedTasks |
||
315 | * |
||
316 | * @return WorkflowInstance |
||
317 | */ |
||
318 | 1 | public function setQueuedTasks(?int $queuedTasks): WorkflowInstance |
|
319 | { |
||
320 | 1 | $this->queuedTasks = $queuedTasks; |
|
321 | 1 | return $this; |
|
322 | } |
||
323 | |||
324 | /** |
||
325 | * @return int|null |
||
326 | */ |
||
327 | 33 | public function getRunningTasks(): ?int |
|
328 | { |
||
329 | 33 | return $this->runningTasks; |
|
330 | } |
||
331 | |||
332 | /** |
||
333 | * @param int|null $runningTasks |
||
334 | * |
||
335 | * @return WorkflowInstance |
||
336 | */ |
||
337 | 1 | public function setRunningTasks(?int $runningTasks): WorkflowInstance |
|
338 | { |
||
339 | 1 | $this->runningTasks = $runningTasks; |
|
340 | 1 | return $this; |
|
341 | } |
||
342 | |||
343 | /** |
||
344 | * @return int|null |
||
345 | */ |
||
346 | 33 | public function getRetryingTasks(): ?int |
|
347 | { |
||
348 | 33 | return $this->retryingTasks; |
|
349 | } |
||
350 | |||
351 | /** |
||
352 | * @param int|null $retryingTasks |
||
353 | * |
||
354 | * @return WorkflowInstance |
||
355 | */ |
||
356 | 1 | public function setRetryingTasks(?int $retryingTasks): WorkflowInstance |
|
360 | } |
||
361 | |||
362 | /** |
||
363 | * @return ArrayCollection |
||
364 | */ |
||
365 | 4 | public function getJobInstances(): ArrayCollection |
|
366 | { |
||
367 | 4 | return $this->jobInstances; |
|
368 | } |
||
369 | |||
370 | /** |
||
371 | * @param ArrayCollection $jobInstances |
||
372 | * |
||
373 | * @return WorkflowInstance |
||
374 | */ |
||
375 | 1 | public function setJobInstances(ArrayCollection $jobInstances): WorkflowInstance |
|
376 | { |
||
377 | $this->jobInstances = $jobInstances->filter(function(JobInstance $jobInstance) { |
||
1 ignored issue
–
show
|
|||
378 | 1 | return true; |
|
379 | 1 | }); |
|
380 | 1 | return $this; |
|
381 | } |
||
382 | |||
383 | /** |
||
384 | * @param JobInstance $jobInstance |
||
385 | * |
||
386 | * @return WorkflowInstance |
||
387 | */ |
||
388 | 2 | public function addJobInstance(JobInstance $jobInstance): WorkflowInstance |
|
389 | { |
||
390 | 2 | $this->jobInstances[] = $jobInstance; |
|
391 | 2 | return $this; |
|
392 | } |
||
393 | |||
394 | /** |
||
395 | * @param JobInstance $jobInstance |
||
396 | * |
||
397 | * @return WorkflowInstance |
||
398 | */ |
||
399 | 1 | public function removeJobInstance(JobInstance $jobInstance): WorkflowInstance |
|
400 | { |
||
401 | 1 | $this->jobInstances->removeElement($jobInstance); |
|
402 | 1 | return $this; |
|
403 | } |
||
404 | |||
405 | /** |
||
406 | * @param $input |
||
407 | * |
||
408 | * @return string |
||
409 | */ |
||
410 | 33 | private function fromCamelCase($input) |
|
418 | } |
||
419 | } |
||
420 |