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 |
||
| 23 | class JobManager extends PriorityJobManager |
||
| 24 | { |
||
| 25 | use JobIdTrait; |
||
| 26 | use VerifyTrait; |
||
| 27 | use SaveableTrait; |
||
| 28 | |||
| 29 | /** @var AMQPChannel */ |
||
| 30 | protected $channel; |
||
| 31 | |||
| 32 | /** @var AbstractConnection */ |
||
| 33 | protected $connection; |
||
| 34 | protected $queueArgs; |
||
| 35 | protected $exchangeArgs; |
||
| 36 | |||
| 37 | protected $channelSetup = false; |
||
| 38 | |||
| 39 | protected $hostname; |
||
| 40 | protected $pid; |
||
| 41 | |||
| 42 | 2 | public function __construct(RunManager $runManager, JobTimingManager $jobTimingManager, $jobClass) |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @param string $exchange |
||
| 51 | * @param string $type |
||
| 52 | * @param bool $passive |
||
| 53 | * @param bool $durable |
||
| 54 | * @param bool $autoDelete |
||
| 55 | */ |
||
| 56 | 1 | public function setExchangeArgs($exchange, $type, $passive, $durable, $autoDelete) |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $queue |
||
| 63 | * @param bool $passive |
||
| 64 | * @param bool $durable |
||
| 65 | * @param bool $exclusive |
||
| 66 | * @param bool $autoDelete |
||
| 67 | * |
||
| 68 | * @throws PriorityException |
||
| 69 | */ |
||
| 70 | 1 | public function setQueueArgs($queue, $passive, $durable, $exclusive, $autoDelete) |
|
| 82 | |||
| 83 | 1 | public function setAMQPConnection(AbstractConnection $connection) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * @return AMQPChannel |
||
| 91 | */ |
||
| 92 | 1 | public function getChannel() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @throws ArgumentsNotSetException |
||
| 99 | */ |
||
| 100 | 11 | protected function checkChannelArgs() |
|
| 109 | |||
| 110 | 1 | protected function performChannelSetup() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @throws ArgumentsNotSetException |
||
| 123 | */ |
||
| 124 | 11 | public function setupChannel() |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 136 | * |
||
| 137 | * @return \Dtc\QueueBundle\Model\Job |
||
| 138 | * |
||
| 139 | * @throws ClassNotSubclassException |
||
| 140 | * @throws PriorityException |
||
| 141 | * @throws ArgumentsNotSetException |
||
| 142 | */ |
||
| 143 | 9 | public function prioritySave(\Dtc\QueueBundle\Model\Job $job) |
|
| 158 | |||
| 159 | 9 | protected function publishJob(Job $job) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Sets the priority of the AMQPMessage. |
||
| 169 | * |
||
| 170 | * @param AMQPMessage $msg |
||
| 171 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 172 | */ |
||
| 173 | 9 | protected function setMsgPriority(AMQPMessage $msg, \Dtc\QueueBundle\Model\Job $job) |
|
| 180 | |||
| 181 | 9 | protected function calculatePriority($priority) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @param string|null $workerName |
||
| 193 | * @param string|null $methodName |
||
| 194 | * |
||
| 195 | * @throws UnsupportedException |
||
| 196 | * @throws ArgumentsNotSetException |
||
| 197 | */ |
||
| 198 | 9 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @param bool $expiredJob |
||
| 213 | * @param $runId |
||
| 214 | * |
||
| 215 | * @return Job|null |
||
| 216 | */ |
||
| 217 | 8 | protected function findJob(&$expiredJob, $runId) |
|
| 239 | |||
| 240 | 2 | View Code Duplication | protected function resetJob(RetryableJob $job) |
| 255 | |||
| 256 | // Save History get called upon completion of the job |
||
| 257 | 3 | protected function retryableSaveHistory(RetryableJob $job, $retry) |
|
| 269 | |||
| 270 | 3 | public function getWaitingJobCount($workerName = null, $methodName = null) |
|
| 285 | |||
| 286 | 2 | public function __destruct() |
|
| 290 | } |
||
| 291 |
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.