Complex classes like Loop 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 Loop, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Loop |
||
| 18 | { |
||
| 19 | /** @var Run $run */ |
||
| 20 | protected $run; |
||
| 21 | |||
| 22 | /** @var OutputInterface */ |
||
| 23 | protected $output; |
||
| 24 | |||
| 25 | /** @var LoggerInterface */ |
||
| 26 | protected $logger; |
||
| 27 | |||
| 28 | /** @var WorkerManager */ |
||
| 29 | protected $workerManager; |
||
| 30 | |||
| 31 | /** @var JobManagerInterface */ |
||
| 32 | protected $jobManager; |
||
| 33 | |||
| 34 | /** @var RunManager */ |
||
| 35 | protected $runManager; |
||
| 36 | |||
| 37 | /** @var int */ |
||
| 38 | protected $processTimeout; |
||
| 39 | |||
| 40 | 1 | public function __construct( |
|
| 49 | |||
| 50 | /** |
||
| 51 | * @return int |
||
| 52 | */ |
||
| 53 | public function getProcessTimeout() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param int $processTimeout |
||
| 60 | */ |
||
| 61 | public function setProcessTimeout(int $processTimeout) |
||
| 65 | |||
| 66 | public function setLogger(LoggerInterface $logger) |
||
| 70 | |||
| 71 | 1 | public function setOutput(OutputInterface $output) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * The current (last) run object. |
||
| 78 | * |
||
| 79 | * @return Run|null |
||
| 80 | */ |
||
| 81 | 1 | public function getRun() |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @param float $start |
||
| 88 | */ |
||
| 89 | public function runJobById($start, $jobId) |
||
| 90 | { |
||
| 91 | $this->runStart($start); |
||
| 92 | |||
| 93 | if (!$this->jobManager instanceof BaseJobManager) { |
||
| 94 | throw new \Exception("Can't get job by id when not using a database/datastore backed queue (such as mongodb or an RDBMS)"); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** @var Job $job */ |
||
| 98 | $job = $this->jobManager->getRepository()->find($jobId); |
||
| 99 | if (!$job) { |
||
| 100 | $this->log('error', "Job id is not found: {$jobId}"); |
||
| 101 | $this->runStop($start); |
||
| 102 | |||
| 103 | return; |
||
| 104 | } |
||
| 105 | |||
| 106 | $job = $this->workerManager->runJob($job); |
||
| 107 | $this->reportJob($job); |
||
| 108 | $this->run->setProcessed(1); |
||
| 109 | $this->runStop($start); |
||
| 110 | |||
| 111 | return; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param float $start |
||
| 116 | * @param int $nanoSleep |
||
| 117 | * @param null|int $maxCount |
||
| 118 | * @param null|int $duration |
||
| 119 | */ |
||
| 120 | 1 | public function runLoop($start, $workerName, $methodName, $maxCount, $duration = null, $nanoSleep = 500000000) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * @param int $nanoSleep |
||
| 147 | * @param null|int $maxCount |
||
| 148 | * @param null|int $duration |
||
| 149 | * |
||
| 150 | * @throws \Exception |
||
| 151 | */ |
||
| 152 | 1 | private function checkParameters(&$nanoSleep, &$maxCount, &$duration) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @param int|null $maxCount |
||
| 164 | * @param int|null $duration |
||
| 165 | * |
||
| 166 | * @throws \Exception |
||
| 167 | */ |
||
| 168 | 1 | protected function validateMaxCountDuration($maxCount, $duration) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @param int|null $nanoSleep |
||
| 180 | * |
||
| 181 | * @throws \Exception |
||
| 182 | */ |
||
| 183 | 1 | protected function validateNanoSleep($nanoSleep) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * @param int|null $duration |
||
| 192 | * |
||
| 193 | * @return null|\DateTime |
||
| 194 | */ |
||
| 195 | 1 | protected function getEndTime($duration) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @param Job|null $job |
||
| 209 | * @param bool $noMoreJobsToRun |
||
| 210 | * @param int $currentJob |
||
| 211 | * @param int|null $duration |
||
| 212 | * @param int $nanoSleep |
||
| 213 | */ |
||
| 214 | protected function runCurrentJob($job, &$noMoreJobsToRun, &$currentJob, $duration, $nanoSleep) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param $maxCount |
||
| 235 | * @param $duration |
||
| 236 | * @param $processTimeout |
||
| 237 | */ |
||
| 238 | public function checkMaxCountDuration(&$maxCount, &$duration, &$processTimeout) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Determine if the run loop is finished. |
||
| 252 | * |
||
| 253 | * @param int|null $maxCount |
||
| 254 | * @param int $currentJob |
||
| 255 | * @param \DateTime|null $endTime |
||
| 256 | * @param bool $noMoreJobsToRun |
||
| 257 | * |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | protected function isFinished($maxCount, $endTime, $currentJob, $noMoreJobsToRun) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param \DateTime|null $endTime |
||
| 274 | * @param bool $noMoreJobsToRun |
||
| 275 | * |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | protected function isFinishedJobs($endTime, $noMoreJobsToRun) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param \DateTime $endTime |
||
| 293 | * |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | protected function isFinishedEndTime(\DateTime $endTime) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param float $start |
||
| 308 | * @param Job|null $jobId |
||
| 309 | */ |
||
| 310 | protected function recordHeartbeat($start, $job) |
||
| 322 | |||
| 323 | protected function persistRun($action = 'persist') |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param int $count |
||
| 334 | */ |
||
| 335 | protected function updateProcessed($count) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Sets up the runManager (document / entity persister) if appropriate. |
||
| 343 | * |
||
| 344 | * @param float $start |
||
| 345 | * @param int|null $maxCount |
||
| 346 | * @param int|null $duration |
||
| 347 | */ |
||
| 348 | protected function runStart($start, $maxCount = null, $duration = null) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param int|null $start |
||
| 370 | */ |
||
| 371 | protected function runStop($start) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @param Job $job |
||
| 385 | */ |
||
| 386 | protected function reportJob(Job $job) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @param string $level |
||
| 401 | */ |
||
| 402 | public function log($level, $msg, array $context = []) |
||
| 419 | } |
||
| 420 |
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):