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 |
||
| 39 | class Runner { |
||
| 40 | |||
| 41 | |||
| 42 | const TICK_TTL = 300; |
||
| 43 | const TICK_MINIMUM = 2; |
||
| 44 | const MEMORY_INFO_UPDATE = 5; |
||
| 45 | |||
| 46 | const RESULT_TYPE_SUCCESS = 1; |
||
| 47 | const RESULT_TYPE_WARNING = 4; |
||
| 48 | const RESULT_TYPE_FAIL = 9; |
||
| 49 | |||
| 50 | /** @var RunningService */ |
||
| 51 | private $runningService; |
||
| 52 | |||
| 53 | /** @var string */ |
||
| 54 | private $source; |
||
| 55 | |||
| 56 | /** @var bool */ |
||
| 57 | private $strict = false; |
||
| 58 | |||
| 59 | /** @var int */ |
||
| 60 | private $tickId; |
||
| 61 | |||
| 62 | /** @var ExtendedBase */ |
||
| 63 | private $commandBase = null; |
||
| 64 | |||
| 65 | /** @var OutputInterface */ |
||
| 66 | private $outputInterface = null; |
||
| 67 | |||
| 68 | /** @var array */ |
||
| 69 | private $info = []; |
||
| 70 | |||
| 71 | /** @var int */ |
||
| 72 | private $oldTick = 0; |
||
| 73 | |||
| 74 | /** @var string */ |
||
| 75 | private $oldAction = ''; |
||
| 76 | |||
| 77 | /** @var int */ |
||
| 78 | private $ramTick = 0; |
||
| 79 | |||
| 80 | /** @var array */ |
||
| 81 | private $methodOnKeyPress = []; |
||
| 82 | |||
| 83 | /** @var array */ |
||
| 84 | private $methodOnInfoUpdate = []; |
||
| 85 | |||
| 86 | /** @var array */ |
||
| 87 | private $methodOnIndexError = []; |
||
| 88 | |||
| 89 | /** @var array */ |
||
| 90 | private $methodOnIndexResult = []; |
||
| 91 | |||
| 92 | /** @var bool */ |
||
| 93 | private $paused = false; |
||
| 94 | |||
| 95 | /** @var bool */ |
||
| 96 | private $pauseRunning = false; |
||
| 97 | |||
| 98 | /** @var array */ |
||
| 99 | private $keys = ['nextStep' => 'n']; |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
| 103 | * Runner constructor. |
||
| 104 | * |
||
| 105 | * @param RunningService $runningService |
||
| 106 | * @param string $source |
||
| 107 | * @param array $keys |
||
| 108 | */ |
||
| 109 | public function __construct(RunningService $runningService, $source, $keys = []) { |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * @param bool $strict |
||
| 121 | * |
||
| 122 | * @throws RunnerAlreadyUpException |
||
| 123 | */ |
||
| 124 | public function start($strict = false) { |
||
| 128 | |||
| 129 | |||
| 130 | /** |
||
| 131 | * @param string $action |
||
| 132 | * @param bool $force |
||
| 133 | * |
||
| 134 | * @return string |
||
|
|
|||
| 135 | * @throws InterruptException |
||
| 136 | * @throws TickDoesNotExistException |
||
| 137 | */ |
||
| 138 | public function updateAction($action = '', $force = false) { |
||
| 195 | |||
| 196 | |||
| 197 | /** |
||
| 198 | * @param string $info |
||
| 199 | * @param string $value |
||
| 200 | * @param int $type |
||
| 201 | */ |
||
| 202 | public function setInfo($info, $value, $type = 0) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param array $data |
||
| 210 | */ |
||
| 211 | public function setInfoArray($data) { |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * @param string $info |
||
| 224 | * @param int $level |
||
| 225 | */ |
||
| 226 | public function setInfoColored($info, $level) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | public function getInfoAll() { |
||
| 261 | |||
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $k |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function getInfo($k) { |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * @param array $method |
||
| 275 | */ |
||
| 276 | public function onKeyPress($method) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param $key |
||
| 282 | */ |
||
| 283 | public function keyPressed($key) { |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * @param array $method |
||
| 292 | */ |
||
| 293 | public function onInfoUpdate($method) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param $key |
||
| 299 | */ |
||
| 300 | public function infoUpdated() { |
||
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * @param array $method |
||
| 309 | */ |
||
| 310 | public function onNewIndexError($method) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param Index $index |
||
| 316 | * @param string $message |
||
| 317 | * @param string $class |
||
| 318 | * @param int $sev |
||
| 319 | */ |
||
| 320 | public function newIndexError($index, $message, $class = '', $sev = 3) { |
||
| 332 | |||
| 333 | |||
| 334 | /** |
||
| 335 | * @param array $method |
||
| 336 | */ |
||
| 337 | public function onNewIndexResult($method) { |
||
| 340 | |||
| 341 | |||
| 342 | /** |
||
| 343 | * @param Index $index |
||
| 344 | * @param string $message |
||
| 345 | * @param string $status |
||
| 346 | * @param int $type |
||
| 347 | */ |
||
| 348 | public function newIndexResult($index, $message, $status, $type) { |
||
| 360 | |||
| 361 | |||
| 362 | /** |
||
| 363 | * @throws InterruptException |
||
| 364 | */ |
||
| 365 | private function hasBeenInterrupted() { |
||
| 371 | |||
| 372 | |||
| 373 | /** |
||
| 374 | * @param $tick |
||
| 375 | */ |
||
| 376 | private function updateTick($tick) { |
||
| 384 | |||
| 385 | |||
| 386 | /** |
||
| 387 | * @deprecated - verifier l'interet !? |
||
| 388 | * |
||
| 389 | * @param $reason |
||
| 390 | * @param $stop |
||
| 391 | */ |
||
| 392 | public function exception($reason, $stop) { |
||
| 399 | |||
| 400 | |||
| 401 | /** |
||
| 402 | * @throws TickDoesNotExistException |
||
| 403 | */ |
||
| 404 | public function stop() { |
||
| 407 | |||
| 408 | |||
| 409 | /** |
||
| 410 | * @param ExtendedBase $base |
||
| 411 | * @param OutputInterface $output |
||
| 412 | */ |
||
| 413 | public function sourceIsCommandLine(ExtendedBase $base, OutputInterface $output) { |
||
| 417 | |||
| 418 | |||
| 419 | /** |
||
| 420 | * @param bool $pause |
||
| 421 | */ |
||
| 422 | public function pause($pause) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return bool |
||
| 429 | */ |
||
| 430 | public function isPaused() { |
||
| 433 | |||
| 434 | |||
| 435 | /** |
||
| 436 | * @param bool $running |
||
| 437 | */ |
||
| 438 | public function pauseRunning($running) { |
||
| 442 | |||
| 443 | |||
| 444 | public function isPauseRunning() { |
||
| 447 | |||
| 448 | |||
| 449 | /** |
||
| 450 | * @return bool |
||
| 451 | */ |
||
| 452 | public function isStrict() { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * @param string $line |
||
| 458 | */ |
||
| 459 | public function output($line) { |
||
| 466 | |||
| 467 | |||
| 468 | } |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.