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 |
||
| 7 | class Resque implements EnqueueInterface |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var array |
||
| 11 | */ |
||
| 12 | private $kernelOptions; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var array |
||
| 16 | */ |
||
| 17 | private $redisConfiguration; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | private $globalRetryStrategy = array(); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | private $jobRetryStrategy = array(); |
||
| 28 | |||
| 29 | public function __construct(array $kernelOptions) |
||
| 33 | |||
| 34 | public function setPrefix($prefix) |
||
| 38 | |||
| 39 | public function setRedisConfiguration($host, $port, $database) |
||
| 50 | |||
| 51 | public function setGlobalRetryStrategy($strategy) |
||
| 55 | |||
| 56 | public function setJobRetryStrategy($strategy) |
||
| 60 | |||
| 61 | public function getRedisConfiguration() |
||
| 65 | |||
| 66 | public function enqueue(Job $job, $trackStatus = false) |
||
| 82 | |||
| 83 | public function enqueueOnce(Job $job, $trackStatus = false) |
||
| 98 | |||
| 99 | View Code Duplication | public function enqueueAt($at, Job $job) |
|
| 111 | |||
| 112 | View Code Duplication | public function enqueueIn($in, Job $job) |
|
| 124 | |||
| 125 | View Code Duplication | public function removedDelayed(Job $job) |
|
| 135 | |||
| 136 | View Code Duplication | public function removeFromTimestamp($at, Job $job) |
|
| 146 | |||
| 147 | public function getQueues() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param $queue |
||
| 156 | * @return Queue |
||
| 157 | */ |
||
| 158 | public function getQueue($queue) |
||
| 162 | |||
| 163 | public function getWorkers() |
||
| 169 | |||
| 170 | public function getWorker($id) |
||
| 180 | |||
| 181 | public function pruneDeadWorkers() |
||
| 188 | |||
| 189 | public function getDelayedJobTimestamps() |
||
| 201 | |||
| 202 | public function getFirstDelayedJobTimestamp() |
||
| 211 | |||
| 212 | public function getNumberOfDelayedJobs() |
||
| 216 | |||
| 217 | View Code Duplication | public function getJobsForTimestamp($timestamp) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @param $queue |
||
| 230 | * @return int |
||
| 231 | */ |
||
| 232 | public function clearQueue($queue) |
||
| 239 | |||
| 240 | View Code Duplication | public function getFailedJobs($start = -100, $count = 100) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Attach any applicable retry strategy to the job. |
||
| 255 | * |
||
| 256 | * @param Job $job |
||
| 257 | */ |
||
| 258 | protected function attachRetryStrategy($job) |
||
| 271 | } |
||
| 272 |