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 OutputInterface */ |
||
20 | protected $output; |
||
21 | |||
22 | /** @var LoggerInterface */ |
||
23 | protected $logger; |
||
24 | |||
25 | /** @var WorkerManager */ |
||
26 | protected $workerManager; |
||
27 | |||
28 | /** @var JobManagerInterface */ |
||
29 | protected $jobManager; |
||
30 | |||
31 | /** @var RunManager */ |
||
32 | protected $runManager; |
||
33 | |||
34 | /** @var int */ |
||
35 | protected $processTimeout; |
||
36 | |||
37 | /** @var Run */ |
||
38 | protected $lastRun; |
||
39 | |||
40 | 3 | public function __construct( |
|
49 | |||
50 | /** |
||
51 | * @return Run|null |
||
52 | */ |
||
53 | 3 | public function getLastRun() |
|
57 | |||
58 | /** |
||
59 | * @return int |
||
60 | */ |
||
61 | public function getProcessTimeout() |
||
65 | |||
66 | /** |
||
67 | * @param int $processTimeout |
||
68 | */ |
||
69 | public function setProcessTimeout(int $processTimeout) |
||
73 | |||
74 | public function setLogger(LoggerInterface $logger) |
||
78 | |||
79 | 1 | public function setOutput(OutputInterface $output) |
|
83 | |||
84 | /** |
||
85 | * @param float $start |
||
86 | */ |
||
87 | 2 | public function runJobById($start, $jobId) |
|
113 | |||
114 | /** |
||
115 | * @param float $start |
||
116 | * @param int $nanoSleep |
||
117 | * @param null|int $maxCount |
||
118 | * @param null|int $duration |
||
119 | */ |
||
120 | 3 | public function runLoop($start, $workerName, $methodName, $maxCount, $duration = null, $nanoSleep = 500000000) |
|
146 | |||
147 | /** |
||
148 | * @param int $nanoSleep |
||
149 | * @param null|int $maxCount |
||
150 | * @param null|int $duration |
||
151 | * |
||
152 | * @throws \InvalidArgumentException |
||
153 | */ |
||
154 | 3 | private function checkParameters(&$nanoSleep, &$maxCount, &$duration) |
|
163 | |||
164 | /** |
||
165 | * @param int|null $maxCount |
||
166 | * @param int|null $duration |
||
167 | * |
||
168 | * @throws \InvalidArgumentException |
||
169 | */ |
||
170 | 3 | protected function validateMaxCountDuration($maxCount, $duration) |
|
179 | |||
180 | /** |
||
181 | * @param int|null $nanoSleep |
||
182 | * |
||
183 | * @throws \InvalidArgumentException |
||
184 | */ |
||
185 | 3 | protected function validateNanoSleep($nanoSleep) |
|
191 | |||
192 | /** |
||
193 | * @param int|null $duration |
||
194 | * |
||
195 | * @return null|\DateTime |
||
196 | */ |
||
197 | 3 | protected function getEndTime(Run $run, $duration) |
|
208 | |||
209 | /** |
||
210 | * @param Run $run |
||
211 | * @param Job|null $job |
||
212 | * @param bool $noMoreJobsToRun |
||
213 | * @param int $currentJob |
||
214 | * @param int|null $duration |
||
215 | * @param int $nanoSleep |
||
216 | */ |
||
217 | protected function runCurrentJob($run, $job, &$noMoreJobsToRun, &$currentJob, $duration, $nanoSleep) |
||
235 | |||
236 | /** |
||
237 | * @param $maxCount |
||
238 | * @param $duration |
||
239 | * @param $processTimeout |
||
240 | */ |
||
241 | public function checkMaxCountDuration(&$maxCount, &$duration, &$processTimeout) |
||
252 | |||
253 | /** |
||
254 | * Determine if the run loop is finished. |
||
255 | * |
||
256 | * @param int|null $maxCount |
||
257 | * @param int $currentJob |
||
258 | * @param \DateTime|null $endTime |
||
259 | * @param bool $noMoreJobsToRun |
||
260 | * |
||
261 | * @return bool |
||
262 | */ |
||
263 | protected function isFinished($maxCount, $endTime, $currentJob, $noMoreJobsToRun) |
||
274 | |||
275 | /** |
||
276 | * @param \DateTime|null $endTime |
||
277 | * @param bool $noMoreJobsToRun |
||
278 | * |
||
279 | * @return bool |
||
280 | */ |
||
281 | protected function isFinishedJobs($endTime, $noMoreJobsToRun) |
||
293 | |||
294 | /** |
||
295 | * @param \DateTime $endTime |
||
296 | * |
||
297 | * @return bool |
||
298 | */ |
||
299 | protected function isFinishedEndTime(\DateTime $endTime) |
||
308 | |||
309 | /** |
||
310 | * @param Job $job |
||
311 | */ |
||
312 | protected function reportJob(Job $job) |
||
324 | |||
325 | /** |
||
326 | * @param string $level |
||
327 | */ |
||
328 | public function log($level, $msg, array $context = []) |
||
345 | } |
||
346 |
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):