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 Resque 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 Resque, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Resque implements EnqueueInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var array |
||
15 | */ |
||
16 | private $kernelOptions; |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | private $redisConfiguration; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $globalRetryStrategy = []; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private $jobRetryStrategy = []; |
||
32 | |||
33 | /** |
||
34 | * Resque constructor. |
||
35 | * @param array $kernelOptions |
||
36 | */ |
||
37 | public function __construct(array $kernelOptions) |
||
41 | |||
42 | /** |
||
43 | * @param $prefix |
||
44 | */ |
||
45 | public function setPrefix($prefix) |
||
49 | |||
50 | /** |
||
51 | * @param $strategy |
||
52 | */ |
||
53 | public function setGlobalRetryStrategy($strategy) |
||
57 | |||
58 | /** |
||
59 | * @param $strategy |
||
60 | */ |
||
61 | public function setJobRetryStrategy($strategy) |
||
65 | |||
66 | /** |
||
67 | * @return array |
||
68 | */ |
||
69 | public function getRedisConfiguration() |
||
73 | |||
74 | /** |
||
75 | * @param $host |
||
76 | * @param $port |
||
77 | * @param $database |
||
78 | */ |
||
79 | public function setRedisConfiguration($host, $port, $database) |
||
90 | |||
91 | /** |
||
92 | * @param Job $job |
||
93 | * @param bool $trackStatus |
||
94 | * @return null|\Resque_Job_Status |
||
95 | */ |
||
96 | public function enqueueOnce(Job $job, $trackStatus = FALSE) |
||
111 | |||
112 | /** |
||
113 | * @param Job $job |
||
114 | * @param bool $trackStatus |
||
115 | * @return null|\Resque_Job_Status |
||
116 | */ |
||
117 | public function enqueue(Job $job, $trackStatus = FALSE) |
||
133 | |||
134 | /** |
||
135 | * Attach any applicable retry strategy to the job. |
||
136 | * |
||
137 | * @param Job $job |
||
138 | */ |
||
139 | protected function attachRetryStrategy($job) |
||
152 | |||
153 | /** |
||
154 | * @param $at |
||
155 | * @param Job $job |
||
156 | * @return null |
||
157 | */ |
||
158 | View Code Duplication | public function enqueueAt($at, Job $job) |
|
170 | |||
171 | /** |
||
172 | * @param $in |
||
173 | * @param Job $job |
||
174 | * @return null |
||
175 | */ |
||
176 | View Code Duplication | public function enqueueIn($in, Job $job) |
|
188 | |||
189 | /** |
||
190 | * @param Job $job |
||
191 | * @return mixed |
||
192 | */ |
||
193 | View Code Duplication | public function removedDelayed(Job $job) |
|
203 | |||
204 | /** |
||
205 | * @param $at |
||
206 | * @param Job $job |
||
207 | * @return mixed |
||
208 | */ |
||
209 | View Code Duplication | public function removeFromTimestamp($at, Job $job) |
|
219 | |||
220 | /** |
||
221 | * @return array |
||
222 | */ |
||
223 | public function getQueues() |
||
229 | |||
230 | /** |
||
231 | * @param $queue |
||
232 | * @return Queue |
||
233 | */ |
||
234 | public function getQueue($queue) |
||
238 | |||
239 | /** |
||
240 | * @return array |
||
241 | */ |
||
242 | public function getWorkers() |
||
248 | |||
249 | /** |
||
250 | * @param $id |
||
251 | * @return Worker|null |
||
252 | */ |
||
253 | public function getWorker($id) |
||
263 | |||
264 | /** |
||
265 | * @todo - Clean this up, for now, prune dead workers, just in case |
||
266 | */ |
||
267 | public function pruneDeadWorkers() |
||
273 | |||
274 | /** |
||
275 | * @return array|mixed |
||
276 | */ |
||
277 | public function getFirstDelayedJobTimestamp() |
||
286 | |||
287 | /** |
||
288 | * @return array |
||
289 | */ |
||
290 | public function getDelayedJobTimestamps() |
||
302 | |||
303 | /** |
||
304 | * @return mixed |
||
305 | */ |
||
306 | public function getNumberOfDelayedJobs() |
||
310 | |||
311 | /** |
||
312 | * @param $timestamp |
||
313 | * @return array |
||
314 | */ |
||
315 | View Code Duplication | public function getJobsForTimestamp($timestamp) |
|
325 | |||
326 | /** |
||
327 | * @param $queue |
||
328 | * @return int |
||
329 | */ |
||
330 | public function clearQueue($queue) |
||
337 | |||
338 | /** |
||
339 | * @param int $start |
||
340 | * @param int $count |
||
341 | * @return array |
||
342 | */ |
||
343 | View Code Duplication | public function getFailedJobs($start = -100, $count = 100) |
|
355 | } |
||
356 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.