Complex classes like Job 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 Job, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | class Job |
||
| 6 | { |
||
| 7 | const STATUS_SUCCESS = 'success'; |
||
| 8 | const STATUS_ERROR = 'error'; |
||
| 9 | const STATUS_NEW = 'new'; |
||
| 10 | |||
| 11 | protected $id; |
||
| 12 | protected $workerName; |
||
| 13 | protected $className; |
||
| 14 | protected $method; |
||
| 15 | protected $args; |
||
| 16 | protected $batch; |
||
| 17 | protected $status; |
||
| 18 | protected $message; |
||
| 19 | protected $priority; |
||
| 20 | protected $crcHash; |
||
| 21 | protected $locked; |
||
| 22 | protected $lockedAt; |
||
| 23 | protected $when; |
||
| 24 | protected $expire; |
||
| 25 | protected $createdAt; |
||
| 26 | protected $updatedAt; |
||
| 27 | protected $delay; |
||
| 28 | protected $startedAt; |
||
| 29 | protected $finishedAt; |
||
| 30 | protected $maxDuration; |
||
| 31 | protected $elapsed; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var JobManagerInterface |
||
| 35 | */ |
||
| 36 | protected $jobManager; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @return |
||
| 40 | */ |
||
| 41 | public function getMessage() |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param $message |
||
| 48 | */ |
||
| 49 | public function setMessage($message) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @return $status |
||
|
|
|||
| 56 | */ |
||
| 57 | public function getStatus() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @return $locked |
||
| 64 | */ |
||
| 65 | public function getLocked() |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return $lockedAt |
||
| 72 | */ |
||
| 73 | public function getLockedAt() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @return $expire |
||
| 80 | */ |
||
| 81 | public function getExpire() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param $status |
||
| 88 | */ |
||
| 89 | public function setStatus($status) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param $locked |
||
| 96 | */ |
||
| 97 | public function setLocked($locked) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param $lockedAt |
||
| 104 | */ |
||
| 105 | public function setLockedAt($lockedAt) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param $expire |
||
| 112 | */ |
||
| 113 | public function setExpire($expire) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return $id |
||
| 120 | */ |
||
| 121 | public function getId() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @return $workerName |
||
| 128 | */ |
||
| 129 | public function getWorkerName() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return $className |
||
| 136 | */ |
||
| 137 | public function getClassName() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return $method |
||
| 144 | */ |
||
| 145 | public function getMethod() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | public function getArgs() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return $batch |
||
| 160 | */ |
||
| 161 | public function getBatch() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return $priority |
||
| 168 | */ |
||
| 169 | public function getPriority() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @return $crcHash |
||
| 176 | */ |
||
| 177 | public function getCrcHash() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @return $when |
||
| 184 | */ |
||
| 185 | public function getWhen() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return $createdAt |
||
| 192 | */ |
||
| 193 | public function getCreatedAt() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return $updatedAt |
||
| 200 | */ |
||
| 201 | public function getUpdatedAt() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @return $jobManager |
||
| 208 | */ |
||
| 209 | public function getJobManager() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param $id |
||
| 216 | */ |
||
| 217 | public function setId($id) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param $workerName |
||
| 224 | */ |
||
| 225 | public function setWorkerName($workerName) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param string $className |
||
| 232 | */ |
||
| 233 | public function setClassName($className) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param $method |
||
| 240 | */ |
||
| 241 | public function setMethod($method) |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return mixed |
||
| 248 | */ |
||
| 249 | public function getStartedAt() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param mixed $startedAt |
||
| 256 | */ |
||
| 257 | public function setStartedAt(\DateTime $startedAt) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return mixed |
||
| 264 | */ |
||
| 265 | public function getFinishedAt() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param mixed $finishedAt |
||
| 272 | */ |
||
| 273 | public function setFinishedAt($finishedAt) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @return mixed |
||
| 280 | */ |
||
| 281 | public function getMaxDuration() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param mixed $maxDuration |
||
| 288 | */ |
||
| 289 | public function setMaxDuration($maxDuration) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param $args |
||
| 296 | */ |
||
| 297 | public function setArgs($args) |
||
| 305 | |||
| 306 | protected function recursiveValidArgs($args) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param $batch |
||
| 323 | */ |
||
| 324 | public function setBatch($batch) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @param $priority |
||
| 331 | */ |
||
| 332 | public function setPriority($priority) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * @param $crcHash |
||
| 339 | */ |
||
| 340 | public function setCrcHash($crcHash) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param $when |
||
| 347 | */ |
||
| 348 | public function setWhen($when) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param $createdAt |
||
| 355 | */ |
||
| 356 | public function setCreatedAt($createdAt) |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param $updatedAt |
||
| 363 | */ |
||
| 364 | public function setUpdatedAt($updatedAt) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * @param $jobManager |
||
| 371 | */ |
||
| 372 | public function setJobManager($jobManager) |
||
| 376 | |||
| 377 | protected $worker; |
||
| 378 | |||
| 379 | public function __construct(Worker $worker = null, $batch = false, $priority = 10, \DateTime $when = null) |
||
| 393 | |||
| 394 | public function __call($method, $args) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @return $delay |
||
| 413 | */ |
||
| 414 | public function getDelay() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @return $worker |
||
| 421 | */ |
||
| 422 | public function getWorker() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @param $delay |
||
| 429 | */ |
||
| 430 | public function setDelay($delay) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param Worker $worker |
||
| 437 | */ |
||
| 438 | public function setWorker($worker) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @return $elapsed |
||
| 445 | */ |
||
| 446 | public function getElapsed() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param $elapsed |
||
| 453 | */ |
||
| 454 | public function setElapsed($elapsed) |
||
| 458 | |||
| 459 | public function toMessage() |
||
| 469 | |||
| 470 | public function fromMessage($message) |
||
| 477 | } |
||
| 478 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.