| Total Complexity | 47 |
| Total Lines | 402 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 4 | ||
| 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 | public function __construct(array $params) |
||
| 64 | { |
||
| 65 | $this->jobInstances = new ArrayCollection([]); |
||
|
|
|||
| 66 | $thisObject = new \ReflectionClass($this); |
||
| 67 | $properties = $thisObject->getProperties(); |
||
| 68 | foreach ($properties as $property) { |
||
| 69 | $snakeProperty = $this->fromCamelCase($property->getName()); |
||
| 70 | if (isset($params[$snakeProperty])) { |
||
| 71 | $this->{$property->getName()} = $params[$snakeProperty]; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | if (!is_null($this->getRunningTasks()) && !is_null($this->getQueuedTasks()) |
||
| 75 | && $this->getRunningTasks() - $this->getQueuedTasks() > 0) { |
||
| 76 | $this->setStatus(self::STATUS_RUNNING); |
||
| 77 | } |
||
| 78 | if (!is_null($this->getQueuedTasks()) && $this->getQueuedTasks() > 0) { |
||
| 79 | $this->setStatus(self::STATUS_QUEUED); |
||
| 80 | } |
||
| 81 | if (!is_null($this->getRetryingTasks()) && $this->getRetryingTasks() > 0) { |
||
| 82 | $this->setStatus(self::STATUS_RETRYING); |
||
| 83 | } |
||
| 84 | if (!is_null($this->getErrors()) && $this->getErrors() > 0) { |
||
| 85 | $this->setStatus(self::STATUS_FAILED); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | public function getComment(): string |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $comment |
||
| 99 | * |
||
| 100 | * @return WorkflowInstance |
||
| 101 | */ |
||
| 102 | public function setComment(string $comment): WorkflowInstance |
||
| 103 | { |
||
| 104 | $this->comment = $comment; |
||
| 105 | return $this; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return null|\DateTime |
||
| 110 | */ |
||
| 111 | public function getEndTime(): ?\DateTime |
||
| 112 | { |
||
| 113 | return $this->endTime; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param \DateTime $endTime |
||
| 118 | * |
||
| 119 | * @return WorkflowInstance |
||
| 120 | */ |
||
| 121 | public function setEndTime(\DateTime $endTime): WorkflowInstance |
||
| 122 | { |
||
| 123 | $this->endTime = $endTime; |
||
| 124 | return $this; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return null|int |
||
| 129 | */ |
||
| 130 | public function getErrors(): ?int |
||
| 131 | { |
||
| 132 | return $this->errors; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param null|int $errors |
||
| 137 | * |
||
| 138 | * @return WorkflowInstance |
||
| 139 | */ |
||
| 140 | public function setErrors(?int $errors): WorkflowInstance |
||
| 141 | { |
||
| 142 | $this->errors = $errors; |
||
| 143 | return $this; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return null|string |
||
| 148 | */ |
||
| 149 | public function getHost(): ?string |
||
| 150 | { |
||
| 151 | return $this->host; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param string $host |
||
| 156 | * |
||
| 157 | * @return WorkflowInstance |
||
| 158 | */ |
||
| 159 | public function setHost(string $host): WorkflowInstance |
||
| 160 | { |
||
| 161 | $this->host = $host; |
||
| 162 | return $this; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return int |
||
| 167 | */ |
||
| 168 | public function getId(): int |
||
| 169 | { |
||
| 170 | return $this->id; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param int $id |
||
| 175 | * |
||
| 176 | * @return WorkflowInstance |
||
| 177 | */ |
||
| 178 | public function setId(int $id): WorkflowInstance |
||
| 179 | { |
||
| 180 | $this->id = $id; |
||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function getName(): string |
||
| 188 | { |
||
| 189 | return $this->name; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $name |
||
| 194 | * |
||
| 195 | * @return WorkflowInstance |
||
| 196 | */ |
||
| 197 | public function setName(string $name): WorkflowInstance |
||
| 198 | { |
||
| 199 | $this->name = $name; |
||
| 200 | return $this; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return null|string |
||
| 205 | */ |
||
| 206 | public function getNodeName(): ?string |
||
| 207 | { |
||
| 208 | return $this->nodeName; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $nodeName |
||
| 213 | * |
||
| 214 | * @return WorkflowInstance |
||
| 215 | */ |
||
| 216 | public function setNodeName(string $nodeName): WorkflowInstance |
||
| 217 | { |
||
| 218 | $this->nodeName = $nodeName; |
||
| 219 | return $this; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @return null|int |
||
| 224 | */ |
||
| 225 | public function getScheduleId(): ?int |
||
| 226 | { |
||
| 227 | return $this->scheduleId; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param int $scheduleId |
||
| 232 | * |
||
| 233 | * @return WorkflowInstance |
||
| 234 | */ |
||
| 235 | public function setScheduleId(int $scheduleId): WorkflowInstance |
||
| 236 | { |
||
| 237 | $this->scheduleId = $scheduleId; |
||
| 238 | return $this; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return \DateTime |
||
| 243 | */ |
||
| 244 | public function getStartTime(): \DateTime |
||
| 245 | { |
||
| 246 | return $this->startTime; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param \DateTime $startTime |
||
| 251 | * |
||
| 252 | * @return WorkflowInstance |
||
| 253 | */ |
||
| 254 | public function setStartTime(\DateTime $startTime): WorkflowInstance |
||
| 255 | { |
||
| 256 | $this->startTime = $startTime; |
||
| 257 | return $this; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function getStatus(): string |
||
| 264 | { |
||
| 265 | return $this->status; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param string $status |
||
| 270 | * |
||
| 271 | * @return WorkflowInstance |
||
| 272 | */ |
||
| 273 | public function setStatus(string $status): WorkflowInstance |
||
| 274 | { |
||
| 275 | $this->status = $status; |
||
| 276 | return $this; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return int|null |
||
| 281 | */ |
||
| 282 | public function getEvqid(): ?int |
||
| 283 | { |
||
| 284 | return $this->evqid; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param int|null $evqid |
||
| 289 | * |
||
| 290 | * @return WorkflowInstance |
||
| 291 | */ |
||
| 292 | public function setEvqid(?int $evqid): WorkflowInstance |
||
| 293 | { |
||
| 294 | $this->evqid = $evqid; |
||
| 295 | return $this; |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @return int|null |
||
| 300 | */ |
||
| 301 | public function getQueuedTasks(): ?int |
||
| 302 | { |
||
| 303 | return $this->queuedTasks; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param int|null $queuedTasks |
||
| 308 | * |
||
| 309 | * @return WorkflowInstance |
||
| 310 | */ |
||
| 311 | public function setQueuedTasks(?int $queuedTasks): WorkflowInstance |
||
| 312 | { |
||
| 313 | $this->queuedTasks = $queuedTasks; |
||
| 314 | return $this; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @return int|null |
||
| 319 | */ |
||
| 320 | public function getRunningTasks(): ?int |
||
| 321 | { |
||
| 322 | return $this->runningTasks; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @param int|null $runningTasks |
||
| 327 | * |
||
| 328 | * @return WorkflowInstance |
||
| 329 | */ |
||
| 330 | public function setRunningTasks(?int $runningTasks): WorkflowInstance |
||
| 331 | { |
||
| 332 | $this->runningTasks = $runningTasks; |
||
| 333 | return $this; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @return int|null |
||
| 338 | */ |
||
| 339 | public function getRetryingTasks(): ?int |
||
| 340 | { |
||
| 341 | return $this->retryingTasks; |
||
| 342 | } |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @param int|null $retryingTasks |
||
| 346 | * |
||
| 347 | * @return WorkflowInstance |
||
| 348 | */ |
||
| 349 | public function setRetryingTasks(?int $retryingTasks): WorkflowInstance |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @return ArrayCollection |
||
| 357 | */ |
||
| 358 | public function getJobInstances(): ArrayCollection |
||
| 359 | { |
||
| 360 | return $this->jobInstances; |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param ArrayCollection $jobInstances |
||
| 365 | * |
||
| 366 | * @return WorkflowInstance |
||
| 367 | */ |
||
| 368 | public function setJobInstances(ArrayCollection $jobInstances): WorkflowInstance |
||
| 369 | { |
||
| 370 | $this->jobInstances = $jobInstances->filter(function(JobInstance $jobInstance) { |
||
|
1 ignored issue
–
show
|
|||
| 371 | return true; |
||
| 372 | }); |
||
| 373 | return $this; |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * @param JobInstance $jobInstance |
||
| 378 | * |
||
| 379 | * @return WorkflowInstance |
||
| 380 | */ |
||
| 381 | public function addJobInstance(JobInstance $jobInstance): WorkflowInstance |
||
| 382 | { |
||
| 383 | $this->jobInstances[] = $jobInstance; |
||
| 384 | return $this; |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param JobInstance $jobInstance |
||
| 389 | * |
||
| 390 | * @return WorkflowInstance |
||
| 391 | */ |
||
| 392 | public function removeJobInstance(JobInstance $jobInstance): WorkflowInstance |
||
| 393 | { |
||
| 394 | $this->jobInstances->removeElement($jobInstance); |
||
| 395 | return $this; |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @param $input |
||
| 400 | * |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | private function fromCamelCase($input) |
||
| 411 | } |
||
| 412 | } |
||
| 413 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..