Complex classes like Worker 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 Worker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Worker |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * Events |
||
| 32 | */ |
||
| 33 | const EVENT_RAISE_BEFORE_JOB = 'job_queue_worker.raise_before_job'; |
||
| 34 | const EVENT_RAISE_AFTER_JOB = 'job_queue_worker.raise_after_job'; |
||
| 35 | const EVENT_RAISE_EXCEPTION_OCCURED_JOB = 'job_queue_worker.raise_exception_occurred_job'; |
||
| 36 | const EVENT_RAISE_FAILED_JOB = 'job_queue_worker.raise_failed_job'; |
||
| 37 | const EVENT_STOP = 'job_queue_worker.stop'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * QueueManager instance |
||
| 41 | * |
||
| 42 | * @var QueueManager |
||
| 43 | */ |
||
| 44 | private $queueManager; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Logger instance |
||
| 48 | * |
||
| 49 | * @var ExceptionHandler |
||
| 50 | */ |
||
| 51 | private $exceptions; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Failer instance |
||
| 55 | * |
||
| 56 | * @var FailedJobProviderInterface |
||
| 57 | */ |
||
| 58 | private $failer; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var EventDispatcherInterface |
||
| 62 | */ |
||
| 63 | private $dispatcher; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var JobProcess |
||
| 67 | */ |
||
| 68 | private $jobProcess; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Worker constructor. |
||
| 72 | * |
||
| 73 | * @param QueueInterface $queue |
||
|
|
|||
| 74 | * @param JobProcess $process |
||
| 75 | * @param FailedJobProviderInterface $failer |
||
| 76 | * @param ExceptionHandlerInterface $exceptions |
||
| 77 | * @param EventDispatcherInterface $dispatcher |
||
| 78 | */ |
||
| 79 | public function __construct(QueueManager $queueManager, |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Listen to the given queue in a loop. |
||
| 94 | * |
||
| 95 | * @param string $connectionName |
||
| 96 | * @param string $queue |
||
| 97 | * @param Options $options |
||
| 98 | */ |
||
| 99 | public function daemon(string $connectionName, string $queue, Options $options) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Process the next job on the queue. |
||
| 114 | * |
||
| 115 | * @param string $connectionName |
||
| 116 | * @param string $queue |
||
| 117 | * @param Options $options |
||
| 118 | * |
||
| 119 | * @return bool |
||
| 120 | */ |
||
| 121 | public function runNextJob(string $connectionName, string $queue, Options $options) |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Process the next job on the queue. |
||
| 141 | * |
||
| 142 | * @param string $connectionName |
||
| 143 | * @param $id |
||
| 144 | * @param Options $options |
||
| 145 | */ |
||
| 146 | public function runJobById(string $connectionName, $id, Options $options) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Make a Process for the Artisan command for the job id. |
||
| 175 | * |
||
| 176 | * @param JobContractInterface $job |
||
| 177 | * @param string $connectionName |
||
| 178 | */ |
||
| 179 | public function runInBackground(JobContractInterface $job, string $connectionName) |
||
| 185 | |||
| 186 | /** Process the given job from the queue. |
||
| 187 | * |
||
| 188 | * @param string $connectionName |
||
| 189 | * @param JobContractInterface $job |
||
| 190 | * @param Options $options |
||
| 191 | * |
||
| 192 | * @return void |
||
| 193 | * |
||
| 194 | * @throws \Throwable |
||
| 195 | */ |
||
| 196 | public function process(string $connectionName, JobContractInterface $job, Options $options) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Sleep the script for a given number of seconds. |
||
| 225 | * |
||
| 226 | * @param int $seconds |
||
| 227 | * |
||
| 228 | * @return void |
||
| 229 | */ |
||
| 230 | public function sleep(int $seconds) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Determine if the memory limit has been exceeded. |
||
| 237 | * |
||
| 238 | * @param int $memoryLimit |
||
| 239 | * |
||
| 240 | * @return bool |
||
| 241 | */ |
||
| 242 | public function memoryExceeded(int $memoryLimit) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Stop listening and bail out of the script. |
||
| 249 | * |
||
| 250 | * @param int $status |
||
| 251 | */ |
||
| 252 | public function stop(int $status = 0) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Mark the given job as failed if it has exceeded the maximum allowed attempts. |
||
| 261 | * |
||
| 262 | * This will likely be because the job previously exceeded a timeout. |
||
| 263 | * |
||
| 264 | * @param string $connectionName |
||
| 265 | * @param JobContractInterface $job |
||
| 266 | * @param int $maxTries |
||
| 267 | * |
||
| 268 | * @return void |
||
| 269 | */ |
||
| 270 | protected function markJobAsFailedIfAlreadyExceedsMaxAttempts(string $connectionName, JobContractInterface $job, int $maxTries) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Mark the given job as failed and raise the relevant event. |
||
| 293 | * |
||
| 294 | * @param string $connectionName |
||
| 295 | * @param JobContractInterface $job |
||
| 296 | * @param Exception $e |
||
| 297 | */ |
||
| 298 | protected function failJob(string $connectionName, JobContractInterface $job, Exception $e) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Handle an exception that occurred while the job was running. |
||
| 319 | * |
||
| 320 | * @param string $connectionName |
||
| 321 | * @param JobContractInterface $job |
||
| 322 | * @param Options $options |
||
| 323 | * @param Exception $e |
||
| 324 | * |
||
| 325 | * @return void |
||
| 326 | * |
||
| 327 | * @throws Exception |
||
| 328 | */ |
||
| 329 | protected function handleJobException(string $connectionName, JobContractInterface $job, Options $options, Exception $e) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Mark the given job as failed if it has exceeded the maximum allowed attempts. |
||
| 358 | * |
||
| 359 | * @param string $connectionName |
||
| 360 | * @param JobContractInterface $job |
||
| 361 | * @param int $maxTries |
||
| 362 | * @param Exception $e |
||
| 363 | * |
||
| 364 | * @return void |
||
| 365 | */ |
||
| 366 | protected function markJobAsFailedIfWillExceedMaxAttempts(string $connectionName, JobContractInterface $job, int $maxTries, Exception $e) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Get the next job from the queue connection. |
||
| 381 | * |
||
| 382 | * @param QueueInterface $connection |
||
| 383 | * @param string $queue |
||
| 384 | * |
||
| 385 | * @return JobContractInterface|null |
||
| 386 | */ |
||
| 387 | protected function getNextJob(QueueInterface $connection, string $queue): ?JobContractInterface |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Raise the before queue job event. |
||
| 406 | * |
||
| 407 | * @param string $connectionName |
||
| 408 | * @param JobContractInterface $job |
||
| 409 | */ |
||
| 410 | protected function raiseBeforeJobEvent(string $connectionName, JobContractInterface $job) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Raise the after queue job event. |
||
| 417 | * |
||
| 418 | * @param string $connectionName |
||
| 419 | * @param JobContractInterface $job |
||
| 420 | */ |
||
| 421 | protected function raiseAfterJobEvent(string $connectionName, JobContractInterface $job) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Raise the exception occurred queue job event. |
||
| 428 | * |
||
| 429 | * @param string $connectionName |
||
| 430 | * @param JobContractInterface $job |
||
| 431 | * @param Exception $e |
||
| 432 | */ |
||
| 433 | protected function raiseExceptionOccurredJobEvent(string $connectionName, JobContractInterface $job, Exception $e) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Raise the failed queue job event. |
||
| 440 | * |
||
| 441 | * @param string $connectionName |
||
| 442 | * @param JobContractInterface $job |
||
| 443 | * @param Exception $e |
||
| 444 | */ |
||
| 445 | protected function raiseFailedJobEvent(string $connectionName, JobContractInterface $job, Exception $e) |
||
| 449 | } |
||
| 450 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.