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 |
||
| 30 | class Worker |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * Events |
||
| 34 | */ |
||
| 35 | const EVENT_RAISE_BEFORE_JOB = 'job_queue_worker.raise_before_job'; |
||
| 36 | const EVENT_RAISE_AFTER_JOB = 'job_queue_worker.raise_after_job'; |
||
| 37 | const EVENT_RAISE_EXCEPTION_OCCURED_JOB = 'job_queue_worker.raise_exception_occurred_job'; |
||
| 38 | const EVENT_RAISE_FAILED_JOB = 'job_queue_worker.raise_failed_job'; |
||
| 39 | const EVENT_STOP = 'job_queue_worker.stop'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var QueueManager |
||
| 43 | */ |
||
| 44 | private $manager; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Logger instance |
||
| 48 | * |
||
| 49 | * @var ExceptionHandler |
||
| 50 | */ |
||
| 51 | private $exceptions; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Failer instance |
||
| 55 | * |
||
| 56 | * @var MongoFailedJobProvider |
||
| 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 | * Create a new queue worker. |
||
| 72 | * |
||
| 73 | * @param QueueManager $manager |
||
|
|
|||
| 74 | * @param MongoFailedJobProvider $failer |
||
| 75 | * @param ExceptionHandler $exceptions |
||
| 76 | */ |
||
| 77 | public function __construct(JobQueue $queue, |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Listen to the given queue in a loop. |
||
| 92 | * |
||
| 93 | * @param string $connectionName |
||
| 94 | * @param string $queue |
||
| 95 | * @param Options $options |
||
| 96 | */ |
||
| 97 | public function daemon($connectionName, $queue, Options $options) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Process the next job on the queue. |
||
| 112 | * |
||
| 113 | * @param string $connectionName |
||
| 114 | * @param string $queue |
||
| 115 | * @param Options $options |
||
| 116 | * |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | public function runNextJob($connectionName, $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($connectionName, $id, Options $options) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Make a Process for the Artisan command for the job id. |
||
| 177 | * |
||
| 178 | * @param Job $job |
||
| 179 | * @param string $connectionName |
||
| 180 | */ |
||
| 181 | public function runInBackground(Job $job, string $connectionName) |
||
| 187 | |||
| 188 | /** Process the given job from the queue. |
||
| 189 | * |
||
| 190 | * @param string $connectionName |
||
| 191 | * @param \Illuminate\Contracts\Queue\Job $job |
||
| 192 | * @param Options $options |
||
| 193 | * |
||
| 194 | * @return void |
||
| 195 | * |
||
| 196 | * @throws \Throwable |
||
| 197 | */ |
||
| 198 | public function process($connectionName, $job, Options $options) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Sleep the script for a given number of seconds. |
||
| 227 | * |
||
| 228 | * @param int $seconds |
||
| 229 | * |
||
| 230 | * @return void |
||
| 231 | */ |
||
| 232 | public function sleep($seconds) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Determine if the memory limit has been exceeded. |
||
| 239 | * |
||
| 240 | * @param int $memoryLimit |
||
| 241 | * |
||
| 242 | * @return bool |
||
| 243 | */ |
||
| 244 | public function memoryExceeded($memoryLimit) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Stop listening and bail out of the script. |
||
| 251 | * |
||
| 252 | * @param int $status |
||
| 253 | */ |
||
| 254 | public function stop($status = 0) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Mark the given job as failed if it has exceeded the maximum allowed attempts. |
||
| 263 | * |
||
| 264 | * This will likely be because the job previously exceeded a timeout. |
||
| 265 | * |
||
| 266 | * @param string $connectionName |
||
| 267 | * @param \Illuminate\Contracts\Queue\Job $job |
||
| 268 | * @param int $maxTries |
||
| 269 | * |
||
| 270 | * @return void |
||
| 271 | */ |
||
| 272 | protected function markJobAsFailedIfAlreadyExceedsMaxAttempts($connectionName, $job, $maxTries) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Mark the given job as failed and raise the relevant event. |
||
| 295 | * |
||
| 296 | * @param string $connectionName |
||
| 297 | * @param \Illuminate\Contracts\Queue\Job $job |
||
| 298 | * @param \Exception $e |
||
| 299 | */ |
||
| 300 | protected function failJob($connectionName, $job, $e) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Handle an exception that occurred while the job was running. |
||
| 321 | * |
||
| 322 | * @param string $connectionName |
||
| 323 | * @param \Illuminate\Contracts\Queue\Job $job |
||
| 324 | * @param Options $options |
||
| 325 | * @param \Exception $e |
||
| 326 | * |
||
| 327 | * @return void |
||
| 328 | * |
||
| 329 | * @throws \Exception |
||
| 330 | */ |
||
| 331 | protected function handleJobException($connectionName, $job, Options $options, $e) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Mark the given job as failed if it has exceeded the maximum allowed attempts. |
||
| 360 | * |
||
| 361 | * @param string $connectionName |
||
| 362 | * @param \Illuminate\Contracts\Queue\Job $job |
||
| 363 | * @param int $maxTries |
||
| 364 | * @param \Exception $e |
||
| 365 | * |
||
| 366 | * @return void |
||
| 367 | */ |
||
| 368 | protected function markJobAsFailedIfWillExceedMaxAttempts($connectionName, $job, $maxTries, $e) |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Get the next job from the queue connection. |
||
| 383 | * |
||
| 384 | * @param \Illuminate\Contracts\Queue\Queue $connection |
||
| 385 | * @param string $queue |
||
| 386 | * |
||
| 387 | * @return \Illuminate\Contracts\Queue\Job|null |
||
| 388 | */ |
||
| 389 | protected function getNextJob($connection, $queue) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Raise the before queue job event. |
||
| 406 | * |
||
| 407 | * @param string $connectionName |
||
| 408 | * @param \Illuminate\Contracts\Queue\Job $job |
||
| 409 | */ |
||
| 410 | protected function raiseBeforeJobEvent($connectionName, $job) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Raise the after queue job event. |
||
| 417 | * |
||
| 418 | * @param string $connectionName |
||
| 419 | * @param \Illuminate\Contracts\Queue\Job $job |
||
| 420 | */ |
||
| 421 | protected function raiseAfterJobEvent($connectionName, $job) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Raise the exception occurred queue job event. |
||
| 428 | * |
||
| 429 | * @param string $connectionName |
||
| 430 | * @param \Illuminate\Contracts\Queue\Job $job |
||
| 431 | * @param \Exception $e |
||
| 432 | */ |
||
| 433 | protected function raiseExceptionOccurredJobEvent($connectionName, $job, $e) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Raise the failed queue job event. |
||
| 440 | * |
||
| 441 | * @param string $connectionName |
||
| 442 | * @param \Illuminate\Contracts\Queue\Job $job |
||
| 443 | * @param \Exception $e |
||
| 444 | */ |
||
| 445 | protected function raiseFailedJobEvent($connectionName, $job, $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.