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:
Complex classes like JobManager 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 JobManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class JobManager extends PriorityJobManager |
||
22 | { |
||
23 | use JobIdTrait; |
||
24 | |||
25 | /** @var AMQPChannel */ |
||
26 | protected $channel; |
||
27 | |||
28 | /** @var AbstractConnection */ |
||
29 | protected $connection; |
||
30 | protected $queueArgs; |
||
31 | protected $exchangeArgs; |
||
32 | |||
33 | protected $channelSetup = false; |
||
34 | |||
35 | protected $hostname; |
||
36 | protected $pid; |
||
37 | |||
38 | 2 | public function __construct(RunManager $runManager, JobTimingManager $jobTimingManager, $jobClass) |
|
44 | |||
45 | /** |
||
46 | * @param string $exchange |
||
47 | * @param string $type |
||
48 | * @param bool $passive |
||
49 | * @param bool $durable |
||
50 | * @param bool $autoDelete |
||
51 | */ |
||
52 | 1 | public function setExchangeArgs($exchange, $type, $passive, $durable, $autoDelete) |
|
56 | |||
57 | /** |
||
58 | * @param string $queue |
||
59 | * @param bool $passive |
||
60 | * @param bool $durable |
||
61 | * @param bool $exclusive |
||
62 | * @param bool $autoDelete |
||
63 | * |
||
64 | * @throws PriorityException |
||
65 | */ |
||
66 | 1 | public function setQueueArgs($queue, $passive, $durable, $exclusive, $autoDelete) |
|
78 | |||
79 | 1 | public function setAMQPConnection(AbstractConnection $connection) |
|
84 | |||
85 | /** |
||
86 | * @return AMQPChannel |
||
87 | */ |
||
88 | public function getChannel() |
||
92 | |||
93 | /** |
||
94 | * @throws ArgumentsNotSetException |
||
95 | */ |
||
96 | 9 | protected function checkChannelArgs() |
|
105 | |||
106 | 1 | protected function performChannelSetup() |
|
116 | |||
117 | /** |
||
118 | * @throws ArgumentsNotSetException |
||
119 | */ |
||
120 | 9 | public function setupChannel() |
|
129 | |||
130 | /** |
||
131 | * @param \Dtc\QueueBundle\Model\Job $job |
||
132 | * |
||
133 | * @return \Dtc\QueueBundle\Model\Job |
||
134 | * |
||
135 | * @throws ClassNotSubclassException |
||
136 | * @throws PriorityException |
||
137 | * @throws ArgumentsNotSetException |
||
138 | */ |
||
139 | 8 | public function prioritySave(\Dtc\QueueBundle\Model\Job $job) |
|
154 | |||
155 | 8 | protected function publishJob(Job $job) |
|
162 | |||
163 | /** |
||
164 | * Sets the priority of the AMQPMessage. |
||
165 | * |
||
166 | * @param AMQPMessage $msg |
||
167 | * @param \Dtc\QueueBundle\Model\Job $job |
||
168 | */ |
||
169 | 8 | protected function setMsgPriority(AMQPMessage $msg, \Dtc\QueueBundle\Model\Job $job) |
|
176 | |||
177 | 8 | protected function calculatePriority($priority) |
|
186 | |||
187 | /** |
||
188 | * @param \Dtc\QueueBundle\Model\Job $job |
||
189 | * |
||
190 | * @throws PriorityException |
||
191 | * @throws ClassNotSubclassException |
||
192 | */ |
||
193 | 8 | View Code Duplication | protected function validateSaveable(\Dtc\QueueBundle\Model\Job $job) |
203 | |||
204 | /** |
||
205 | * @param string|null $workerName |
||
206 | * @param string|null $methodName |
||
207 | * @param bool $prioritize |
||
208 | * |
||
209 | * @throws UnsupportedException |
||
210 | */ |
||
211 | 7 | protected function verifyGetJobArgs($workerName = null, $methodName = null, $prioritize = true) |
|
217 | |||
218 | /** |
||
219 | * @param string|null $workerName |
||
220 | * @param string|null $methodName |
||
221 | * |
||
222 | * @throws UnsupportedException |
||
223 | * @throws ArgumentsNotSetException |
||
224 | */ |
||
225 | 7 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
237 | |||
238 | /** |
||
239 | * @param bool $expiredJob |
||
240 | * @param $runId |
||
241 | * |
||
242 | * @return Job|null |
||
243 | */ |
||
244 | 6 | protected function findJob(&$expiredJob, $runId) |
|
266 | |||
267 | 2 | View Code Duplication | protected function resetJob(RetryableJob $job) |
281 | |||
282 | // Save History get called upon completion of the job |
||
283 | 3 | protected function retryableSaveHistory(RetryableJob $job, $retry) |
|
293 | |||
294 | 2 | public function __destruct() |
|
298 | } |
||
299 |
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.