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 |
||
| 18 | class JobManager extends PriorityJobManager |
||
| 19 | { |
||
| 20 | /** @var RedisInterface */ |
||
| 21 | protected $redis; |
||
| 22 | |||
| 23 | /** @var string */ |
||
| 24 | protected $cacheKeyPrefix; |
||
| 25 | |||
| 26 | protected $hostname; |
||
| 27 | protected $pid; |
||
| 28 | |||
| 29 | 2 | public function __construct(RunManager $runManager, JobTimingManager $jobTimingManager, $jobClass, $cacheKeyPrefix) |
|
| 37 | |||
| 38 | public function setRedis(RedisInterface $redis) |
||
| 42 | |||
| 43 | 18 | protected function getJobCacheKey($jobId) |
|
| 47 | |||
| 48 | 18 | protected function getJobCrcHashKey($jobCrc) |
|
| 52 | |||
| 53 | 14 | protected function getPriorityQueueCacheKey() |
|
| 57 | |||
| 58 | 18 | protected function getWhenQueueCacheKey() |
|
| 62 | |||
| 63 | 14 | protected function transferQueues() |
|
| 78 | |||
| 79 | 2 | protected function batchSave(\Dtc\QueueBundle\Redis\Job $job) |
|
| 80 | { |
||
| 81 | 2 | $crcHash = $job->getCrcHash(); |
|
| 82 | 2 | $crcCacheKey = $this->getJobCrcHashKey($crcHash); |
|
| 83 | 2 | $result = $this->redis->lrange($crcCacheKey, 0, 1000); |
|
| 84 | 2 | if (is_array($result)) { |
|
| 85 | 2 | foreach ($result as $jobId) { |
|
| 86 | 2 | $jobCacheKey1 = $this->getJobCacheKey($jobId); |
|
| 87 | 2 | if (!($foundJobMessage = $this->redis->get($jobCacheKey1))) { |
|
| 88 | $this->redis->lRem($crcCacheKey, 1, $jobCacheKey1); |
||
| 89 | continue; |
||
| 90 | } |
||
| 91 | |||
| 92 | /// There is one? |
||
| 93 | 2 | if ($foundJobMessage) { |
|
| 94 | 2 | $foundJob = $this->batchFoundJob($job, $jobCacheKey1, $foundJobMessage); |
|
| 95 | 2 | if ($foundJob) { |
|
| 96 | 2 | return $foundJob; |
|
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | return null; |
||
| 103 | } |
||
| 104 | |||
| 105 | 2 | protected function batchFoundJob(\Dtc\QueueBundle\Redis\Job $job, $foundJobCacheKey, $foundJobMessage) |
|
| 106 | { |
||
| 107 | 2 | $when = $job->getWhenUs(); |
|
| 108 | 2 | $crcHash = $job->getCrcHash(); |
|
| 109 | 2 | $crcCacheKey = $this->getJobCrcHashKey($crcHash); |
|
| 110 | |||
| 111 | 2 | $foundJob = new \Dtc\QueueBundle\Redis\Job(); |
|
| 112 | 2 | $foundJob->fromMessage($foundJobMessage); |
|
| 113 | 2 | $foundWhen = $foundJob->getWhenUs(); |
|
| 114 | |||
| 115 | // Fix this using bcmath |
||
| 116 | 2 | $curtimeU = Util::getMicrotimeDecimal(); |
|
| 117 | 2 | $newFoundWhen = null; |
|
| 118 | 2 | if (bccomp($foundWhen, $curtimeU) > 0 && bccomp($foundWhen, $when) >= 1) { |
|
| 119 | 2 | $newFoundWhen = $when; |
|
| 120 | 2 | } |
|
| 121 | 2 | $foundPriority = $foundJob->getPriority(); |
|
| 122 | 2 | $newFoundPriority = null; |
|
| 123 | 2 | if ($foundPriority > $job->getPriority()) { |
|
| 124 | 2 | $newFoundPriority = $job->getPriority(); |
|
| 125 | 2 | } |
|
| 126 | |||
| 127 | 2 | return $this->finishBatchFoundJob($foundJob, $foundJobCacheKey, $crcCacheKey, $newFoundWhen, $newFoundPriority); |
|
| 128 | } |
||
| 129 | |||
| 130 | 2 | protected function finishBatchFoundJob(Job $foundJob, $foundJobCacheKey, $crcCacheKey, $newFoundWhen, $newFoundPriority) |
|
| 131 | { |
||
| 132 | // Now how do we adjust this job's priority or time? |
||
| 133 | 2 | $adjust = false; |
|
| 134 | 2 | if (isset($newFoundWhen)) { |
|
| 135 | 2 | $foundJob->setWhenUs($newFoundWhen); |
|
| 136 | 2 | $adjust = true; |
|
| 137 | 2 | } |
|
| 138 | 2 | if (isset($newFoundPriority)) { |
|
| 139 | 2 | $foundJob->setPriority($newFoundPriority); |
|
| 140 | 2 | $adjust = true; |
|
| 141 | 2 | } |
|
| 142 | 2 | if (!$adjust) { |
|
| 143 | 2 | return $foundJob; |
|
| 144 | } |
||
| 145 | |||
| 146 | 2 | return $this->addFoundJob($adjust, $foundJob, $foundJobCacheKey, $crcCacheKey); |
|
| 147 | } |
||
| 148 | |||
| 149 | 2 | protected function addFoundJob($adjust, Job $foundJob, $foundJobCacheKey, $crcCacheKey) |
|
| 165 | |||
| 166 | 2 | private function adjustJob($adjust, $queue, Job $foundJob, $foundJobCacheKey, $crcCacheKey, $zScore) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 185 | * |
||
| 186 | * @return \Dtc\QueueBundle\Model\Job |
||
| 187 | * |
||
| 188 | * @throws ClassNotSubclassException |
||
| 189 | */ |
||
| 190 | 18 | public function prioritySave(\Dtc\QueueBundle\Model\Job $job) |
|
| 191 | { |
||
| 192 | 18 | if (!$job instanceof \Dtc\QueueBundle\Redis\Job) { |
|
| 193 | throw new \InvalidArgumentException('$job must be instance of '.\Dtc\QueueBundle\Redis\Job::class); |
||
| 194 | } |
||
| 195 | |||
| 196 | 18 | $this->validateSaveable($job); |
|
| 197 | 18 | $this->setJobId($job); |
|
| 198 | |||
| 199 | // Add to whenAt or priority queue? /// optimizaiton... |
||
| 200 | 18 | $whenUs = $job->getWhenUs(); |
|
| 201 | 18 | if (!$whenUs) { |
|
| 202 | $whenUs = Util::getMicrotimeDecimal(); |
||
| 203 | $job->setWhenUs($whenUs); |
||
| 204 | } |
||
| 205 | |||
| 206 | 18 | if (true === $job->getBatch()) { |
|
| 207 | // is there a CRC Hash already for this job |
||
| 208 | 2 | if ($oldJob = $this->batchSave($job)) { |
|
| 209 | 2 | return $oldJob; |
|
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | 18 | return $this->saveJob($job); |
|
| 214 | } |
||
| 215 | |||
| 216 | 18 | protected function saveJob(\Dtc\QueueBundle\Redis\Job $job) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * @param Job $job |
||
| 237 | * |
||
| 238 | * @return bool false if the job is already expired, true otherwise |
||
| 239 | */ |
||
| 240 | 18 | protected function insertJob(\Dtc\QueueBundle\Redis\Job $job) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Attach a unique id to a job since RabbitMQ will not. |
||
| 260 | * |
||
| 261 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 262 | */ |
||
| 263 | 18 | protected function setJobId(\Dtc\QueueBundle\Model\Job $job) |
|
| 264 | { |
||
| 265 | 18 | View Code Duplication | if (!$job->getId()) { |
| 266 | 18 | $job->setId(uniqid($this->hostname.'-'.$this->pid, true)); |
|
| 267 | 18 | } |
|
| 268 | 18 | } |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Returns the prioirty in DESCENDING order, except if maxPrioirty is null, then prioirty is 0. |
||
| 272 | */ |
||
| 273 | 18 | protected function calculatePriority($priority) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 290 | * |
||
| 291 | * @throws PriorityException |
||
| 292 | * @throws ClassNotSubclassException |
||
| 293 | */ |
||
| 294 | 18 | View Code Duplication | protected function validateSaveable(\Dtc\QueueBundle\Model\Job $job) |
| 304 | |||
| 305 | 16 | protected function verifyGetJobArgs($workerName = null, $methodName = null, $prioritize = true) |
|
| 311 | |||
| 312 | 2 | public function deleteJob(\Dtc\QueueBundle\Model\Job $job) |
|
| 313 | { |
||
| 314 | 2 | $jobId = $job->getId(); |
|
| 315 | 2 | $priorityQueue = $this->getPriorityQueueCacheKey(); |
|
| 316 | 2 | $whenQueue = $this->getWhenQueueCacheKey(); |
|
| 317 | |||
| 318 | 2 | $deleted = false; |
|
| 319 | 2 | if ($this->redis->zRem($priorityQueue, $jobId)) { |
|
| 320 | $deleted = true; |
||
| 321 | 2 | } elseif ($this->redis->zRem($whenQueue, $jobId)) { |
|
| 322 | 2 | $deleted = true; |
|
| 323 | 2 | } |
|
| 324 | |||
| 325 | 2 | if ($deleted) { |
|
| 326 | 2 | $this->redis->del([$this->getJobCacheKey($jobId)]); |
|
| 327 | 2 | $this->redis->lRem($this->getJobCrcHashKey($job->getCrcHash()), 1, $jobId); |
|
| 328 | 2 | } |
|
| 329 | 2 | } |
|
| 330 | |||
| 331 | /** |
||
| 332 | * @param string $workerName |
||
| 333 | */ |
||
| 334 | 16 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
| 335 | { |
||
| 336 | // First thing migrate any jobs from When queue to Prioirty queue |
||
| 337 | |||
| 338 | 16 | $this->verifyGetJobArgs($workerName, $methodName, $prioritize); |
|
| 339 | 14 | if (null !== $this->maxPriority) { |
|
| 340 | 14 | $this->transferQueues(); |
|
| 341 | 14 | $queue = $this->getPriorityQueueCacheKey(); |
|
| 342 | 14 | $jobId = $this->redis->zPop($queue); |
|
| 343 | 14 | } else { |
|
| 344 | $queue = $this->getWhenQueueCacheKey(); |
||
| 345 | $microtime = Util::getMicrotimeDecimal(); |
||
| 346 | $jobId = $this->redis->zPopByMaxScore($queue, $microtime); |
||
| 347 | } |
||
| 348 | |||
| 349 | 14 | if ($jobId) { |
|
| 350 | 12 | $jobMessage = $this->redis->get($this->getJobCacheKey($jobId)); |
|
| 351 | 12 | $job = new \Dtc\QueueBundle\Redis\Job(); |
|
| 352 | 12 | $job->fromMessage($jobMessage); |
|
| 353 | 12 | $crcCacheKey = $this->getJobCrcHashKey($job->getCrcHash()); |
|
| 354 | 12 | $this->redis->lRem($crcCacheKey, 1, $job->getId()); |
|
| 355 | 12 | $this->redis->del([$this->getJobCacheKey($job->getId())]); |
|
| 356 | |||
| 357 | 12 | return $job; |
|
| 358 | } |
||
| 359 | |||
| 360 | 10 | return null; |
|
| 361 | } |
||
| 362 | |||
| 363 | protected function getCurTime() |
||
| 369 | |||
| 370 | 4 | View Code Duplication | public function resetJob(RetryableJob $job) |
| 384 | |||
| 385 | 6 | public function retryableSaveHistory(RetryableJob $job, $retry) |
|
| 388 | } |
||
| 389 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: