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 |
||
| 19 | class JobManager extends PriorityJobManager |
||
| 20 | { |
||
| 21 | use JobIdTrait; |
||
| 22 | |||
| 23 | /** @var RedisInterface */ |
||
| 24 | protected $redis; |
||
| 25 | |||
| 26 | /** @var string */ |
||
| 27 | protected $cacheKeyPrefix; |
||
| 28 | |||
| 29 | protected $hostname; |
||
| 30 | protected $pid; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param string $cacheKeyPrefix |
||
| 34 | */ |
||
| 35 | 2 | public function __construct(RunManager $runManager, JobTimingManager $jobTimingManager, $jobClass, $cacheKeyPrefix) |
|
| 43 | |||
| 44 | public function setRedis(RedisInterface $redis) |
||
| 48 | |||
| 49 | 18 | protected function getJobCacheKey($jobId) |
|
| 53 | |||
| 54 | /** |
||
| 55 | * @param string $jobCrc |
||
| 56 | */ |
||
| 57 | 18 | protected function getJobCrcHashKey($jobCrc) |
|
| 61 | |||
| 62 | 16 | protected function getPriorityQueueCacheKey() |
|
| 66 | |||
| 67 | 20 | protected function getWhenQueueCacheKey() |
|
| 71 | |||
| 72 | 14 | protected function transferQueues() |
|
| 73 | { |
||
| 74 | // Drains from WhenAt queue into Prioirty Queue |
||
| 75 | 14 | $whenQueue = $this->getWhenQueueCacheKey(); |
|
| 76 | 14 | $priorityQueue = $this->getPriorityQueueCacheKey(); |
|
| 77 | 14 | $microtime = Util::getMicrotimeDecimal(); |
|
| 78 | 14 | while ($jobId = $this->redis->zPopByMaxScore($whenQueue, $microtime)) { |
|
| 79 | 12 | $jobMessage = $this->redis->get($this->getJobCacheKey($jobId)); |
|
| 80 | 12 | if ($jobMessage) { |
|
| 81 | 12 | $job = new Job(); |
|
| 82 | 12 | $job->fromMessage($jobMessage); |
|
|
|
|||
| 83 | 12 | $this->redis->zAdd($priorityQueue, $job->getPriority(), $job->getId()); |
|
| 84 | 12 | } |
|
| 85 | 12 | } |
|
| 86 | 14 | } |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @param Job $job |
||
| 90 | * |
||
| 91 | * @return Job|null |
||
| 92 | */ |
||
| 93 | 2 | protected function batchSave(Job $job) |
|
| 94 | { |
||
| 95 | 2 | $crcHash = $job->getCrcHash(); |
|
| 96 | 2 | $crcCacheKey = $this->getJobCrcHashKey($crcHash); |
|
| 97 | 2 | $result = $this->redis->lrange($crcCacheKey, 0, 1000); |
|
| 98 | 2 | if (is_array($result)) { |
|
| 99 | 2 | foreach ($result as $jobId) { |
|
| 100 | 2 | $jobCacheKey1 = $this->getJobCacheKey($jobId); |
|
| 101 | 2 | if (!($foundJobMessage = $this->redis->get($jobCacheKey1))) { |
|
| 102 | $this->redis->lRem($crcCacheKey, 1, $jobCacheKey1); |
||
| 103 | continue; |
||
| 104 | } |
||
| 105 | |||
| 106 | /// There is one? |
||
| 107 | 2 | if ($foundJobMessage) { |
|
| 108 | 2 | $foundJob = $this->batchFoundJob($job, $jobCacheKey1, $foundJobMessage); |
|
| 109 | 2 | if ($foundJob) { |
|
| 110 | 2 | return $foundJob; |
|
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | return null; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param string $foundJobCacheKey |
||
| 121 | * @param bool $foundJobMessage |
||
| 122 | */ |
||
| 123 | 2 | protected function batchFoundJob(Job $job, $foundJobCacheKey, $foundJobMessage) |
|
| 124 | { |
||
| 125 | 2 | $when = $job->getWhenUs(); |
|
| 126 | 2 | $crcHash = $job->getCrcHash(); |
|
| 127 | 2 | $crcCacheKey = $this->getJobCrcHashKey($crcHash); |
|
| 128 | |||
| 129 | 2 | $foundJob = new Job(); |
|
| 130 | 2 | $foundJob->fromMessage($foundJobMessage); |
|
| 131 | 2 | $foundWhen = $foundJob->getWhenUs(); |
|
| 132 | |||
| 133 | // Fix this using bcmath |
||
| 134 | 2 | $curtimeU = Util::getMicrotimeDecimal(); |
|
| 135 | 2 | $newFoundWhen = null; |
|
| 136 | 2 | if (bccomp($foundWhen, $curtimeU) > 0 && bccomp($foundWhen, $when) >= 1) { |
|
| 137 | 2 | $newFoundWhen = $when; |
|
| 138 | 2 | } |
|
| 139 | 2 | $foundPriority = $foundJob->getPriority(); |
|
| 140 | 2 | $newFoundPriority = null; |
|
| 141 | 2 | if ($foundPriority > $job->getPriority()) { |
|
| 142 | 2 | $newFoundPriority = $job->getPriority(); |
|
| 143 | 2 | } |
|
| 144 | |||
| 145 | 2 | return $this->finishBatchFoundJob($foundJob, $foundJobCacheKey, $crcCacheKey, $newFoundWhen, $newFoundPriority); |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string $crcCacheKey |
||
| 150 | * @param int|null $newFoundPriority |
||
| 151 | */ |
||
| 152 | 2 | protected function finishBatchFoundJob(Job $foundJob, $foundJobCacheKey, $crcCacheKey, $newFoundWhen, $newFoundPriority) |
|
| 153 | { |
||
| 154 | // Now how do we adjust this job's priority or time? |
||
| 155 | 2 | $adjust = false; |
|
| 156 | 2 | if (isset($newFoundWhen)) { |
|
| 157 | 2 | $foundJob->setWhenUs($newFoundWhen); |
|
| 158 | 2 | $adjust = true; |
|
| 159 | 2 | } |
|
| 160 | 2 | if (isset($newFoundPriority)) { |
|
| 161 | 2 | $foundJob->setPriority($newFoundPriority); |
|
| 162 | 2 | $adjust = true; |
|
| 163 | 2 | } |
|
| 164 | 2 | if (!$adjust) { |
|
| 165 | 2 | return $foundJob; |
|
| 166 | } |
||
| 167 | |||
| 168 | 2 | return $this->addFoundJob($adjust, $foundJob, $foundJobCacheKey, $crcCacheKey); |
|
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param bool $adjust |
||
| 173 | */ |
||
| 174 | 2 | protected function addFoundJob($adjust, Job $foundJob, $foundJobCacheKey, $crcCacheKey) |
|
| 175 | { |
||
| 176 | 2 | $whenQueue = $this->getWhenQueueCacheKey(); |
|
| 177 | 2 | $result = $this->adjustJob($adjust, $whenQueue, $foundJob, $foundJobCacheKey, $crcCacheKey, $foundJob->getWhenUs()); |
|
| 178 | 2 | if (null !== $result) { |
|
| 179 | 2 | return $result; |
|
| 180 | } |
||
| 181 | if (null === $this->maxPriority) { |
||
| 182 | return false; |
||
| 183 | } |
||
| 184 | |||
| 185 | $priorityQueue = $this->getPriorityQueueCacheKey(); |
||
| 186 | $result = $this->adjustJob($adjust, $priorityQueue, $foundJob, $foundJobCacheKey, $crcCacheKey, $foundJob->getPriority()); |
||
| 187 | |||
| 188 | return $result ?: false; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param string $queue |
||
| 193 | * @param bool $adjust |
||
| 194 | */ |
||
| 195 | 2 | private function adjustJob($adjust, $queue, Job $foundJob, $foundJobCacheKey, $crcCacheKey, $zScore) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 214 | * |
||
| 215 | * @return Job|null |
||
| 216 | * |
||
| 217 | * @throws ClassNotSubclassException |
||
| 218 | * @throws PriorityException |
||
| 219 | */ |
||
| 220 | 18 | public function prioritySave(\Dtc\QueueBundle\Model\Job $job) |
|
| 221 | { |
||
| 222 | 18 | if (!$job instanceof Job) { |
|
| 223 | throw new \InvalidArgumentException('$job must be instance of '.Job::class); |
||
| 224 | } |
||
| 225 | |||
| 226 | 18 | $this->validateSaveable($job); |
|
| 227 | 18 | $this->setJobId($job); |
|
| 228 | |||
| 229 | // Add to whenAt or priority queue? /// optimizaiton... |
||
| 230 | 18 | $whenUs = $job->getWhenUs(); |
|
| 231 | 18 | if (!$whenUs) { |
|
| 232 | $whenUs = Util::getMicrotimeDecimal(); |
||
| 233 | $job->setWhenUs($whenUs); |
||
| 234 | } |
||
| 235 | |||
| 236 | 18 | if (true === $job->getBatch()) { |
|
| 237 | // is there a CRC Hash already for this job |
||
| 238 | 2 | if ($oldJob = $this->batchSave($job)) { |
|
| 239 | 2 | return $oldJob; |
|
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | 18 | return $this->saveJob($job); |
|
| 244 | } |
||
| 245 | |||
| 246 | 18 | protected function saveJob(Job $job) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @param Job $job |
||
| 267 | * |
||
| 268 | * @return bool false if the job is already expired, true otherwise |
||
| 269 | */ |
||
| 270 | 18 | protected function insertJob(Job $job) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Returns the prioirty in DESCENDING order, except if maxPrioirty is null, then prioirty is 0. |
||
| 290 | * |
||
| 291 | * @param int|null $priority |
||
| 292 | * |
||
| 293 | * @return int |
||
| 294 | */ |
||
| 295 | 18 | protected function calculatePriority($priority) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * @param \Dtc\QueueBundle\Model\Job $job |
||
| 312 | * |
||
| 313 | * @throws PriorityException |
||
| 314 | * @throws ClassNotSubclassException |
||
| 315 | */ |
||
| 316 | 18 | View Code Duplication | protected function validateSaveable(\Dtc\QueueBundle\Model\Job $job) |
| 326 | |||
| 327 | /** |
||
| 328 | * @param string|null $workerName |
||
| 329 | * @param string|null $methodName |
||
| 330 | * @param bool $prioritize |
||
| 331 | * |
||
| 332 | * @throws UnsupportedException |
||
| 333 | */ |
||
| 334 | 16 | protected function verifyGetJobArgs($workerName = null, $methodName = null, $prioritize = true) |
|
| 340 | |||
| 341 | 2 | public function deleteJob(\Dtc\QueueBundle\Model\Job $job) |
|
| 342 | { |
||
| 343 | 2 | $jobId = $job->getId(); |
|
| 344 | 2 | $priorityQueue = $this->getPriorityQueueCacheKey(); |
|
| 345 | 2 | $whenQueue = $this->getWhenQueueCacheKey(); |
|
| 346 | |||
| 359 | |||
| 360 | /** |
||
| 361 | * @param string|null $workerName |
||
| 362 | * @param string|null $methodName |
||
| 363 | * @param bool $prioritize |
||
| 364 | * @param mixed $runId |
||
| 365 | * |
||
| 366 | * @throws UnsupportedException |
||
| 367 | * |
||
| 368 | * @return Job|null |
||
| 369 | */ |
||
| 370 | 16 | public function getJob($workerName = null, $methodName = null, $prioritize = true, $runId = null) |
|
| 398 | |||
| 399 | 4 | public function getWaitingJobCount($workerName = null, $methodName = null) |
|
| 410 | |||
| 411 | 4 | View Code Duplication | public function resetJob(RetryableJob $job) |
| 425 | |||
| 426 | 6 | public function retryableSaveHistory(RetryableJob $job, $retry) |
|
| 429 | } |
||
| 430 |
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: