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 | 14 | protected function getPriorityQueueCacheKey() |
|
| 66 | |||
| 67 | 18 | protected function getWhenQueueCacheKey() |
|
| 71 | |||
| 72 | 14 | protected function transferQueues() |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @param Job $job |
||
| 90 | * |
||
| 91 | * @return Job|null |
||
| 92 | */ |
||
| 93 | 2 | protected function batchSave(Job $job) |
|
| 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 | } |
||
| 139 | 2 | $foundPriority = $foundJob->getPriority(); |
|
| 140 | 2 | $newFoundPriority = null; |
|
| 141 | 2 | if ($foundPriority > $job->getPriority()) { |
|
| 142 | 2 | $newFoundPriority = $job->getPriority(); |
|
| 143 | } |
||
| 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) |
|
| 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) |
|
| 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) |
|
| 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 | View Code Duplication | public function resetJob(RetryableJob $job) |
| 400 | { |
||
| 413 | |||
| 414 | 6 | public function retryableSaveHistory(RetryableJob $job, $retry) |
|
| 417 | } |
||
| 418 |
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: