Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
12 | class JobManager extends RetryableJobManager |
||
13 | { |
||
14 | const DEFAULT_RESERVE_TIMEOUT = 5; // seconds |
||
15 | |||
16 | /** @var Pheanstalk */ |
||
17 | protected $beanstalkd; |
||
18 | |||
19 | protected $tube; |
||
20 | |||
21 | protected $reserveTimeout = self::DEFAULT_RESERVE_TIMEOUT; |
||
22 | |||
23 | 2 | public function setBeanstalkd(Pheanstalk $beanstalkd) |
|
27 | |||
28 | public function setTube($tube) |
||
32 | |||
33 | public function setReserveTimeout($timeout) |
||
37 | |||
38 | 7 | public function retryableSave(RetryableJob $job) |
|
46 | |||
47 | 7 | protected function putJob(Job $job) |
|
48 | { |
||
49 | /** @var Job $job */ |
||
50 | 7 | $message = $job->toMessage(); |
|
51 | 7 | $arguments = [$message, $job->getPriority(), $job->getDelay(), $job->getTtr()]; |
|
52 | 7 | $method = 'put'; |
|
53 | 7 | if ($this->tube) { |
|
54 | array_unshift($arguments, $this->tube); |
||
55 | $method .= 'InTube'; |
||
56 | } |
||
57 | 7 | $jobId = call_user_func_array([$this->beanstalkd, $method], $arguments); |
|
58 | 7 | $job->setId($jobId); |
|
59 | |||
60 | // Ideally we should get this from beanstalk, but to save the roundtrip time, we do this here |
||
61 | 7 | $job->setBeanJob($this->getBeanJob($jobId, $message)); |
|
62 | |||
63 | 7 | return $job; |
|
64 | } |
||
65 | |||
66 | 3 | View Code Duplication | protected function resetJob(RetryableJob $job) |
80 | |||
81 | 7 | public function getBeanJob($jobId, $data) |
|
85 | |||
86 | /** |
||
87 | * @param string|null $workerName |
||
88 | * @param string|null $methodName |
||
89 | * @param bool $prioritize |
||
90 | * @param int|string|null $runId |
||
91 | * |
||
92 | * @return Job|null |
||
93 | * |
||
94 | * @throws UnsupportedException |
||
95 | */ |
||
96 | 6 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
97 | { |
||
98 | 6 | if (null !== $methodName) { |
|
99 | throw new UnsupportedException('Unsupported'); |
||
100 | } |
||
101 | 6 | if (null !== $workerName) { |
|
102 | 1 | throw new UnsupportedException('Unsupported'); |
|
103 | } |
||
104 | |||
105 | 5 | $beanstalkd = $this->beanstalkd; |
|
106 | 5 | if ($this->tube) { |
|
107 | $beanstalkd = $this->beanstalkd->watch($this->tube); |
||
108 | } |
||
109 | |||
110 | do { |
||
111 | 5 | $expiredJob = false; |
|
112 | 5 | $job = $this->findJob($beanstalkd, $expiredJob, $runId); |
|
113 | 5 | } while ($expiredJob); |
|
114 | |||
115 | 5 | return $job; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param Pheanstalk $beanstalkd |
||
120 | * @param bool $expiredJob |
||
121 | * @param int|string|null $runId |
||
122 | * |
||
123 | * @return Job|null |
||
124 | */ |
||
125 | 5 | protected function findJob(Pheanstalk $beanstalkd, &$expiredJob, $runId) |
|
147 | |||
148 | 2 | public function deleteJob(\Dtc\QueueBundle\Model\Job $job) |
|
153 | |||
154 | // Save History get called upon completion of the job |
||
155 | 3 | public function retryableSaveHistory(RetryableJob $job, $retry) |
|
156 | { |
||
157 | 3 | if (!$retry) { |
|
158 | 3 | $this->beanstalkd |
|
159 | 3 | ->delete($job); |
|
160 | 3 | } |
|
161 | 3 | } |
|
162 | |||
163 | 1 | public function getStats() |
|
167 | } |
||
168 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.