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 |
||
29 | class Worker |
||
30 | { |
||
31 | /** |
||
32 | * Events |
||
33 | */ |
||
34 | const EVENT_RAISE_BEFORE_JOB = 'raiseBeforeJobEvent'; |
||
35 | const EVENT_RAISE_AFTER_JOB = 'raiseAfterJobEvent'; |
||
36 | const EVENT_RAISE_EXCEPTION_OCCURED_JOB = 'raiseExceptionOccurredJobEvent'; |
||
37 | const EVENT_RAISE_FAILED_JOB = 'raiseFailedJobEvent'; |
||
38 | const EVENT_STOP = 'stop'; |
||
39 | /** |
||
40 | * The queue manager instance. |
||
41 | * |
||
42 | * @var \Illuminate\Queue\QueueManager |
||
43 | */ |
||
44 | protected $manager; |
||
45 | |||
46 | /** |
||
47 | * Failer instance |
||
48 | * |
||
49 | * @var FailedJobProviderInterface |
||
50 | */ |
||
51 | protected $failer; |
||
52 | |||
53 | /** |
||
54 | * The exception handler instance. |
||
55 | * |
||
56 | * @var \Illuminate\Foundation\Exceptions\Handler |
||
57 | */ |
||
58 | protected $exceptions; |
||
59 | |||
60 | protected $shouldQuit = false; |
||
61 | |||
62 | /** |
||
63 | * Create a new queue worker. |
||
64 | * |
||
65 | * @param QueueManager $manager |
||
66 | * @param FailedJobProviderInterface $failer |
||
67 | * @param ExceptionHandler $exceptions |
||
68 | */ |
||
69 | public function __construct(QueueManager $manager, |
||
77 | |||
78 | /** |
||
79 | * Listen to the given queue in a loop. |
||
80 | * |
||
81 | * @param string $connectionName |
||
82 | * @param string $queue |
||
83 | * @param WorkerOptions $options |
||
84 | * |
||
85 | * @return array |
||
86 | */ |
||
87 | public function daemon($connectionName, $queue, WorkerOptions $options) |
||
103 | |||
104 | /** |
||
105 | * Process the next job on the queue. |
||
106 | * |
||
107 | * @param string $connectionName |
||
108 | * @param string $queue |
||
109 | * @param \Illuminate\Queue\WorkerOptions $options |
||
110 | */ |
||
111 | public function runNextJob($connectionName, $queue, WorkerOptions $options) |
||
131 | |||
132 | /** |
||
133 | * Make a Process for the Artisan command for the job id. |
||
134 | * |
||
135 | * @param Job $job |
||
136 | * @param string $connectionName |
||
137 | */ |
||
138 | protected function runInBackground(Job $job, string $connectionName) |
||
144 | |||
145 | /** |
||
146 | * Process the given job from the queue. |
||
147 | * |
||
148 | * @param string $connectionName |
||
149 | * @param \Illuminate\Contracts\Queue\Job $job |
||
150 | * @param \Illuminate\Queue\WorkerOptions $options |
||
151 | * |
||
152 | * @throws \Throwable |
||
153 | */ |
||
154 | public function process($connectionName, $job, WorkerOptions $options) |
||
180 | |||
181 | /** |
||
182 | * Handle an exception that occurred while the job was running. |
||
183 | * |
||
184 | * @param string $connectionName |
||
185 | * @param \Illuminate\Contracts\Queue\Job $job |
||
186 | * @param WorkerOptions $options |
||
187 | * @param \Exception $e |
||
188 | * |
||
189 | * @throws \Exception |
||
190 | */ |
||
191 | protected function handleJobException($connectionName, $job, WorkerOptions $options, $e) |
||
217 | |||
218 | /** |
||
219 | * Mark the given job as failed if it has exceeded the maximum allowed attempts. |
||
220 | * |
||
221 | * This will likely be because the job previously exceeded a timeout. |
||
222 | * |
||
223 | * @param string $connectionName |
||
224 | * @param \Illuminate\Contracts\Queue\Job $job |
||
225 | * @param int $maxTries |
||
226 | */ |
||
227 | protected function markJobAsFailedIfAlreadyExceedsMaxAttempts($connectionName, $job, $maxTries) |
||
241 | |||
242 | /** |
||
243 | * Mark the given job as failed if it has exceeded the maximum allowed attempts. |
||
244 | * |
||
245 | * @param string $connectionName |
||
246 | * @param \Illuminate\Contracts\Queue\Job $job |
||
247 | * @param int $maxTries |
||
248 | * @param \Exception $e |
||
249 | */ |
||
250 | protected function markJobAsFailedIfWillExceedMaxAttempts($connectionName, $job, $maxTries, $e) |
||
258 | |||
259 | /** |
||
260 | * Get the next job from the queue connection. |
||
261 | * |
||
262 | * @param \Illuminate\Contracts\Queue\Queue $connection |
||
263 | * @param string $queue |
||
264 | * |
||
265 | * @return \Illuminate\Contracts\Queue\Job|null |
||
266 | */ |
||
267 | protected function getNextJob($connection, $queue) |
||
281 | |||
282 | /** |
||
283 | * Kill the process. |
||
284 | * |
||
285 | * @param int $status |
||
286 | */ |
||
287 | public function kill($status = 0) |
||
295 | |||
296 | /** |
||
297 | * Sleep the script for a given number of seconds. |
||
298 | * |
||
299 | * @param int $seconds |
||
300 | */ |
||
301 | public function sleep($seconds) |
||
305 | |||
306 | /** |
||
307 | * Process the next job on the queue. |
||
308 | * |
||
309 | * @param string $connectionName |
||
310 | * @param $id |
||
311 | * @param \Illuminate\Queue\WorkerOptions $options |
||
312 | */ |
||
313 | public function runJobById($connectionName, $id, WorkerOptions $options) |
||
332 | |||
333 | /** |
||
334 | * Mark the given job as failed and raise the relevant event. |
||
335 | * |
||
336 | * @param string $connectionName |
||
337 | * @param \Illuminate\Contracts\Queue\Job $job |
||
338 | * @param \Exception $e |
||
339 | */ |
||
340 | protected function failJob($connectionName, $job, $e) |
||
358 | |||
359 | /** |
||
360 | * Determine if the memory limit has been exceeded. |
||
361 | * |
||
362 | * @param int $memoryLimit |
||
363 | * |
||
364 | * @return bool |
||
365 | */ |
||
366 | public function memoryExceeded($memoryLimit) |
||
370 | |||
371 | /** |
||
372 | * Stop the worker if we have lost connection to a database. |
||
373 | * |
||
374 | * @param \Exception $e |
||
375 | */ |
||
376 | protected function stopWorkerIfLostConnection($e) |
||
399 | |||
400 | /** |
||
401 | * Raise the before queue job event. |
||
402 | * |
||
403 | * @param string $connectionName |
||
404 | * @param \Illuminate\Contracts\Queue\Job $job |
||
405 | */ |
||
406 | protected function raiseBeforeJobEvent($connectionName, $job) |
||
410 | |||
411 | /** |
||
412 | * Raise the after queue job event. |
||
413 | * |
||
414 | * @param string $connectionName |
||
415 | * @param \Illuminate\Contracts\Queue\Job $job |
||
416 | */ |
||
417 | protected function raiseAfterJobEvent($connectionName, $job) |
||
421 | |||
422 | /** |
||
423 | * Raise the exception occurred queue job event. |
||
424 | * |
||
425 | * @param string $connectionName |
||
426 | * @param \Illuminate\Contracts\Queue\Job $job |
||
427 | * @param \Exception $e |
||
428 | */ |
||
429 | protected function raiseExceptionOccurredJobEvent($connectionName, $job, $e) |
||
435 | |||
436 | /** |
||
437 | * Raise the failed queue job event. |
||
438 | * |
||
439 | * @param string $connectionName |
||
440 | * @param \Illuminate\Contracts\Queue\Job $job |
||
441 | * @param \Exception $e |
||
442 | */ |
||
443 | protected function raiseFailedJobEvent($connectionName, $job, $e) |
||
449 | |||
450 | /** |
||
451 | * Stop listening and bail out of the script. |
||
452 | */ |
||
453 | public function stop($status = 0) |
||
459 | } |
||
460 |
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..