Complex classes like Runner 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 Runner, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class Runner { |
||
39 | |||
40 | |||
41 | const TICK_TTL = 300; |
||
42 | const TICK_MINIMUM = 2; |
||
43 | const INFO_UPDATE = 10; |
||
44 | |||
45 | /** @var RunningService */ |
||
46 | private $runningService; |
||
47 | |||
48 | /** @var string */ |
||
49 | private $source; |
||
50 | |||
51 | /** @var bool */ |
||
52 | private $strict = false; |
||
53 | |||
54 | /** @var int */ |
||
55 | private $tickId; |
||
56 | |||
57 | /** @var ExtendedBase */ |
||
58 | private $commandBase = null; |
||
59 | |||
60 | /** @var OutputInterface */ |
||
61 | private $outputInterface = null; |
||
62 | |||
63 | /** @var array */ |
||
64 | private $info = []; |
||
65 | |||
66 | /** @var int */ |
||
67 | private $oldTick = 0; |
||
68 | |||
69 | /** @var string */ |
||
70 | private $oldAction = ''; |
||
71 | |||
72 | /** @var int */ |
||
73 | private $ramTick = 0; |
||
74 | |||
75 | /** @var array */ |
||
76 | private $methodOnKeyPress = []; |
||
77 | |||
78 | /** @var array */ |
||
79 | private $methodOnNewAction = []; |
||
80 | |||
81 | /** @var array */ |
||
82 | private $methodOnInfoUpdate = []; |
||
83 | |||
84 | /** @var array */ |
||
85 | private $methodOnIndexError = []; |
||
86 | |||
87 | /** @var bool */ |
||
88 | private $paused = false; |
||
89 | |||
90 | /** @var bool */ |
||
91 | private $pauseRunning = false; |
||
92 | |||
93 | /** @var array */ |
||
94 | private $keys = ['nextStep' => 'n']; |
||
95 | |||
96 | |||
97 | /** |
||
98 | * Runner constructor. |
||
99 | * |
||
100 | * @param RunningService $runningService |
||
101 | * @param string $source |
||
102 | * @param array $keys |
||
103 | */ |
||
104 | public function __construct(RunningService $runningService, $source, $keys = []) { |
||
112 | |||
113 | |||
114 | /** |
||
115 | * @param bool $strict |
||
116 | * |
||
117 | * @throws RunnerAlreadyUpException |
||
118 | */ |
||
119 | public function start($strict = false) { |
||
123 | |||
124 | |||
125 | /** |
||
126 | * @param string $action |
||
127 | * @param bool $force |
||
128 | * |
||
129 | * @return string |
||
|
|||
130 | * @throws InterruptException |
||
131 | * @throws TickDoesNotExistException |
||
132 | */ |
||
133 | public function updateAction($action = '', $force = false) { |
||
191 | |||
192 | |||
193 | /** |
||
194 | * @param string $info |
||
195 | * @param string $value |
||
196 | * @param string $colored |
||
197 | */ |
||
198 | public function setInfo($info, $value, $colored = '') { |
||
203 | |||
204 | /** |
||
205 | * @param array $data |
||
206 | */ |
||
207 | public function setInfoArray($data) { |
||
216 | |||
217 | private function setInfoColored($info, $value, $colored) { |
||
243 | |||
244 | /** |
||
245 | * @return array |
||
246 | */ |
||
247 | public function getInfo() { |
||
250 | |||
251 | |||
252 | /** |
||
253 | * @param array $method |
||
254 | */ |
||
255 | public function onKeyPress($method) { |
||
258 | |||
259 | /** |
||
260 | * @param $key |
||
261 | */ |
||
262 | public function keyPressed($key) { |
||
267 | |||
268 | |||
269 | /** |
||
270 | * @param array $method |
||
271 | */ |
||
272 | public function onNewAction($method) { |
||
275 | |||
276 | /** |
||
277 | * @param string $action |
||
278 | */ |
||
279 | public function newAction($action) { |
||
284 | |||
285 | |||
286 | /** |
||
287 | * @param array $method |
||
288 | */ |
||
289 | public function onInfoUpdate($method) { |
||
292 | |||
293 | /** |
||
294 | * @param $key |
||
295 | */ |
||
296 | public function infoUpdated() { |
||
301 | |||
302 | |||
303 | /** |
||
304 | * @param array $method |
||
305 | */ |
||
306 | public function onNewIndexError($method) { |
||
309 | |||
310 | /** |
||
311 | * @param Index $index |
||
312 | * @param string $message |
||
313 | * @param string $class |
||
314 | * @param int $sev |
||
315 | */ |
||
316 | public function newIndexError($index, $message, $class = '', $sev = 3) { |
||
328 | |||
329 | |||
330 | /** |
||
331 | * @throws InterruptException |
||
332 | */ |
||
333 | private function hasBeenInterrupted() { |
||
339 | |||
340 | |||
341 | /** |
||
342 | * @param $tick |
||
343 | */ |
||
344 | private function updateTick($tick) { |
||
352 | |||
353 | |||
354 | /** |
||
355 | * @deprecated - verifier l'interet !? |
||
356 | * @param $reason |
||
357 | * @param $stop |
||
358 | */ |
||
359 | public function exception($reason, $stop) { |
||
366 | |||
367 | |||
368 | /** |
||
369 | * @throws TickDoesNotExistException |
||
370 | */ |
||
371 | public function stop() { |
||
374 | |||
375 | |||
376 | /** |
||
377 | * @param ExtendedBase $base |
||
378 | * @param OutputInterface $output |
||
379 | */ |
||
380 | public function sourceIsCommandLine(ExtendedBase $base, OutputInterface $output) { |
||
384 | |||
385 | |||
386 | /** |
||
387 | * @param bool $pause |
||
388 | */ |
||
389 | public function pause($pause) { |
||
393 | |||
394 | /** |
||
395 | * @return bool |
||
396 | */ |
||
397 | public function isPaused() { |
||
400 | |||
401 | |||
402 | /** |
||
403 | * @param bool $running |
||
404 | */ |
||
405 | public function pauseRunning($running) { |
||
409 | |||
410 | |||
411 | public function isPauseRunning() { |
||
414 | |||
415 | |||
416 | /** |
||
417 | * @return bool |
||
418 | */ |
||
419 | public function isStrict() { |
||
422 | |||
423 | /** |
||
424 | * @param string $line |
||
425 | */ |
||
426 | public function output($line) { |
||
433 | |||
434 | |||
435 | } |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.