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 integer $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 integer $nanoSleep |
||
128 | * @param null|integer $maxCount |
||
129 | * @param null|integer $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 $maxCount |
||
214 | * @param integer|null $currentJob |
||
215 | * @param integer|null $duration |
||
|
|||
216 | * @param \DateTime|null $endTime |
||
217 | * @param boolean $noMoreJobsToRun |
||
218 | * |
||
219 | * @return bool |
||
220 | */ |
||
221 | protected function isFinished($maxCount, $endTime, $currentJob, $noMoreJobsToRun) |
||
239 | |||
240 | /** |
||
241 | * @param float $start |
||
242 | */ |
||
243 | protected function recordHeartbeat($start) |
||
249 | |||
250 | protected function persistRun($action = 'persist') |
||
258 | |||
259 | /** |
||
260 | * @param int $count |
||
261 | */ |
||
262 | protected function updateProcessed($count) |
||
267 | |||
268 | /** |
||
269 | * Sets up the runManager (document / entity persister) if appropriate. |
||
270 | * |
||
271 | * @param float $start |
||
272 | * @param int|null $maxCount |
||
273 | * @param int|null $duration |
||
274 | */ |
||
275 | protected function runStart($start, $maxCount = null, $duration = null) |
||
293 | |||
294 | /** |
||
295 | * @param int|null $start |
||
296 | */ |
||
297 | protected function runStop($start) |
||
308 | |||
309 | /** |
||
310 | * @param Job $job |
||
311 | */ |
||
312 | protected function reportJob(Job $job) |
||
322 | |||
323 | /** |
||
324 | * @param string $level |
||
325 | */ |
||
326 | public function log($level, $msg, array $context = []) |
||
343 | } |
||
344 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.