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 ExceptionHandlerInterface |
||
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 $process; |
||
69 | |||
70 | /** |
||
71 | * Worker constructor. |
||
72 | * |
||
73 | * @param QueueManager $queueManager |
||
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 string $queue |
||
144 | * @param string $id |
||
145 | * @param Options $options |
||
146 | */ |
||
147 | public function runJobById(string $connectionName, string $queue, string $id, Options $options) |
||
173 | |||
174 | /** |
||
175 | * Make a Process for the Artisan command for the job id. |
||
176 | * |
||
177 | * @param JobContractInterface $job |
||
178 | * @param Options $options |
||
179 | */ |
||
180 | public function runInBackground(JobContractInterface $job, Options $options) |
||
186 | |||
187 | /** Process the given job from the queue. |
||
188 | * |
||
189 | * @param string $connectionName |
||
190 | * @param JobContractInterface $job |
||
191 | * @param Options $options |
||
192 | * |
||
193 | * @return void |
||
194 | * |
||
195 | * @throws \Throwable |
||
196 | */ |
||
197 | public function process(string $connectionName, JobContractInterface $job, Options $options) |
||
223 | |||
224 | /** |
||
225 | * Sleep the script for a given number of seconds. |
||
226 | * |
||
227 | * @param int $seconds |
||
228 | * |
||
229 | * @return void |
||
230 | */ |
||
231 | public function sleep(int $seconds) |
||
235 | |||
236 | /** |
||
237 | * Determine if the memory limit has been exceeded. |
||
238 | * |
||
239 | * @param int $memoryLimit |
||
240 | * |
||
241 | * @return bool |
||
242 | */ |
||
243 | public function memoryExceeded(int $memoryLimit) |
||
247 | |||
248 | /** |
||
249 | * Stop listening and bail out of the script. |
||
250 | */ |
||
251 | public function stop() |
||
257 | |||
258 | /** |
||
259 | * Mark the given job as failed if it has exceeded the maximum allowed attempts. |
||
260 | * |
||
261 | * This will likely be because the job previously exceeded a timeout. |
||
262 | * |
||
263 | * @param string $connectionName |
||
264 | * @param JobContractInterface $job |
||
265 | * @param int $maxTries |
||
266 | * |
||
267 | * @return void |
||
268 | */ |
||
269 | protected function markJobAsFailedIfAlreadyExceedsMaxAttempts(string $connectionName, JobContractInterface $job, int $maxTries) |
||
289 | |||
290 | /** |
||
291 | * Mark the given job as failed and raise the relevant event. |
||
292 | * |
||
293 | * @param string $connectionName |
||
294 | * @param JobContractInterface $job |
||
295 | * @param Exception $e |
||
296 | */ |
||
297 | protected function failJob(string $connectionName, JobContractInterface $job, Exception $e) |
||
315 | |||
316 | /** |
||
317 | * Handle an exception that occurred while the job was running. |
||
318 | * |
||
319 | * @param string $connectionName |
||
320 | * @param JobContractInterface $job |
||
321 | * @param Options $options |
||
322 | * @param Exception $e |
||
323 | * |
||
324 | * @return void |
||
325 | * |
||
326 | * @throws Exception |
||
327 | */ |
||
328 | protected function handleJobException(string $connectionName, JobContractInterface $job, Options $options, Exception $e) |
||
354 | |||
355 | /** |
||
356 | * Mark the given job as failed if it has exceeded the maximum allowed attempts. |
||
357 | * |
||
358 | * @param string $connectionName |
||
359 | * @param JobContractInterface $job |
||
360 | * @param int $maxTries |
||
361 | * @param Exception $e |
||
362 | * |
||
363 | * @return void |
||
364 | */ |
||
365 | protected function markJobAsFailedIfWillExceedMaxAttempts(string $connectionName, JobContractInterface $job, int $maxTries, Exception $e) |
||
377 | |||
378 | /** |
||
379 | * Get the next job from the queue connection. |
||
380 | * |
||
381 | * @param QueueInterface $connection |
||
382 | * @param string $queue |
||
383 | * |
||
384 | * @return JobContractInterface|null |
||
385 | */ |
||
386 | protected function getNextJob(QueueInterface $connection, string $queue): ?JobContractInterface |
||
402 | |||
403 | /** |
||
404 | * Raise the before queue job event. |
||
405 | * |
||
406 | * @param string $connectionName |
||
407 | * @param JobContractInterface $job |
||
408 | */ |
||
409 | protected function raiseBeforeJobEvent(string $connectionName, JobContractInterface $job) |
||
413 | |||
414 | /** |
||
415 | * Raise the after queue job event. |
||
416 | * |
||
417 | * @param string $connectionName |
||
418 | * @param JobContractInterface $job |
||
419 | */ |
||
420 | protected function raiseAfterJobEvent(string $connectionName, JobContractInterface $job) |
||
424 | |||
425 | /** |
||
426 | * Raise the exception occurred queue job event. |
||
427 | * |
||
428 | * @param string $connectionName |
||
429 | * @param JobContractInterface $job |
||
430 | * @param Exception $e |
||
431 | */ |
||
432 | protected function raiseExceptionOccurredJobEvent(string $connectionName, JobContractInterface $job, Exception $e) |
||
436 | |||
437 | /** |
||
438 | * Raise the failed queue job event. |
||
439 | * |
||
440 | * @param string $connectionName |
||
441 | * @param JobContractInterface $job |
||
442 | * @param Exception $e |
||
443 | */ |
||
444 | protected function raiseFailedJobEvent(string $connectionName, JobContractInterface $job, Exception $e) |
||
448 | } |
||
449 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.