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 | 3 | public function __construct( |
|
46 | |||
47 | public function setLogger(LoggerInterface $logger) |
||
51 | |||
52 | 1 | public function setOutput(OutputInterface $output) |
|
56 | |||
57 | /** |
||
58 | * The current (last) run object. |
||
59 | * |
||
60 | * @return Run|null |
||
61 | */ |
||
62 | 3 | public function getRun() |
|
66 | |||
67 | /** |
||
68 | * @param float $start |
||
69 | */ |
||
70 | 2 | public function runJobById($start, $jobId) |
|
94 | |||
95 | /** |
||
96 | * @param float $start |
||
97 | * @param int $nanoSleep |
||
98 | * @param null|int $maxCount |
||
99 | * @param null|int $duration |
||
100 | */ |
||
101 | 3 | public function runLoop($start, $workerName, $methodName, $maxCount, $duration = null, $nanoSleep = 500000000) |
|
125 | |||
126 | /** |
||
127 | * @param int $nanoSleep |
||
128 | * @param null|int $maxCount |
||
129 | * @param null|int $duration |
||
130 | * |
||
131 | * @throws \Exception |
||
132 | */ |
||
133 | 3 | private function checkParameters(&$nanoSleep, &$maxCount, &$duration) |
|
149 | |||
150 | /** |
||
151 | * @param int|null $duration |
||
152 | * |
||
153 | * @return null|\DateTime |
||
154 | */ |
||
155 | 3 | protected function getEndTime($duration) |
|
166 | |||
167 | /** |
||
168 | * @param Job|null $job |
||
169 | * @param bool $noMoreJobsToRun |
||
170 | * @param int $currentJob |
||
171 | * @param int|null $duration |
||
172 | * @param int $nanoSleep |
||
173 | */ |
||
174 | protected function runCurrentJob($job, &$noMoreJobsToRun, &$currentJob, $duration, $nanoSleep) |
||
192 | |||
193 | /** |
||
194 | * @param $maxCount |
||
195 | * @param $duration |
||
196 | * @param $processTimeout |
||
197 | */ |
||
198 | public function checkMaxCountDuration(&$maxCount, &$duration, &$processTimeout) |
||
209 | |||
210 | /** |
||
211 | * Determine if the run loop is finished. |
||
212 | * |
||
213 | * @param int|null $maxCount |
||
214 | * @param int $currentJob |
||
215 | * @param \DateTime|null $endTime |
||
216 | * @param bool $noMoreJobsToRun |
||
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | protected function isFinished($maxCount, $endTime, $currentJob, $noMoreJobsToRun) |
||
238 | |||
239 | /** |
||
240 | * @param float $start |
||
241 | */ |
||
242 | protected function recordHeartbeat($start) |
||
248 | |||
249 | protected function persistRun($action = 'persist') |
||
257 | |||
258 | /** |
||
259 | * @param int $count |
||
260 | */ |
||
261 | protected function updateProcessed($count) |
||
266 | |||
267 | /** |
||
268 | * Sets up the runManager (document / entity persister) if appropriate. |
||
269 | * |
||
270 | * @param float $start |
||
271 | * @param int|null $maxCount |
||
272 | * @param int|null $duration |
||
273 | */ |
||
274 | protected function runStart($start, $maxCount = null, $duration = null) |
||
292 | |||
293 | /** |
||
294 | * @param int|null $start |
||
295 | */ |
||
296 | protected function runStop($start) |
||
307 | |||
308 | /** |
||
309 | * @param Job $job |
||
310 | */ |
||
311 | protected function reportJob(Job $job) |
||
323 | |||
324 | /** |
||
325 | * @param string $level |
||
326 | */ |
||
327 | public function log($level, $msg, array $context = []) |
||
344 | } |
||
345 |