Complex classes like ProcessManager 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 ProcessManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | final class ProcessManager implements Manager |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var Task[] |
||
| 23 | */ |
||
| 24 | private $waiting = []; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var Task[] |
||
| 28 | */ |
||
| 29 | private $running = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var null|SplObjectStorage |
||
| 33 | */ |
||
| 34 | private $timings = null; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var null|string |
||
| 38 | */ |
||
| 39 | private $logPath; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var null|Rules |
||
| 43 | */ |
||
| 44 | private $rules; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var null|Shell |
||
| 48 | */ |
||
| 49 | protected $shell; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var null|string |
||
| 53 | */ |
||
| 54 | private $binary; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var null|string |
||
| 58 | */ |
||
| 59 | private $worker; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get a list of the waiting tasks. |
||
| 63 | * |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | public function getWaiting() |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Get a list of the running tasks. |
||
| 73 | * |
||
| 74 | * @return array |
||
| 75 | */ |
||
| 76 | public function getRunning() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @inheritdoc |
||
| 83 | * |
||
| 84 | * @param Task $task |
||
| 85 | * |
||
| 86 | * @return $this |
||
| 87 | */ |
||
| 88 | 1 | public function addTask(Task $task) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @inheritdoc |
||
| 97 | * |
||
| 98 | * @return bool |
||
| 99 | */ |
||
| 100 | 1 | public function tick() |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Stops sibling processes of a task. |
||
| 157 | * |
||
| 158 | * @param Task $task |
||
| 159 | * |
||
| 160 | * @return $this |
||
| 161 | */ |
||
| 162 | private function stopSiblingTasks(Task $task) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Checks whether a new task can be run. |
||
| 182 | * |
||
| 183 | * @param Task $task |
||
| 184 | * |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | 1 | private function canRunTask(Task $task) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Gets the load profile related to a task. |
||
| 212 | * |
||
| 213 | * @param Task $task |
||
| 214 | * @param array $processes |
||
| 215 | * |
||
| 216 | * @return Profile |
||
| 217 | */ |
||
| 218 | 1 | private function getProfileForProcesses(Task $task, array $processes) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Gets processor and memory stats for a list of processes. |
||
| 243 | * |
||
| 244 | * @param Process[] $processes |
||
| 245 | * |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | 1 | private function getStatsForProcesses(array $processes) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Gets or creates a Shell instance. |
||
| 283 | * |
||
| 284 | * @return Shell |
||
| 285 | */ |
||
| 286 | 2 | public function getShell() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @param Shell $shell |
||
| 297 | * |
||
| 298 | * @return $this |
||
| 299 | */ |
||
| 300 | 1 | public function setShell(Shell $shell) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Creates a new Shell instance. |
||
| 309 | * |
||
| 310 | * @return Shell |
||
| 311 | */ |
||
| 312 | 2 | private function newShell() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Creates a new Profile instance. |
||
| 319 | * |
||
| 320 | * @return Profile |
||
| 321 | */ |
||
| 322 | 1 | private function newProfile() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Gets or creates a new Rules instance. |
||
| 329 | * |
||
| 330 | * @return Rules |
||
| 331 | */ |
||
| 332 | 2 | public function getRules() |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @param Rules $rules |
||
| 343 | * |
||
| 344 | * @return $this |
||
| 345 | */ |
||
| 346 | 1 | public function setRules(Rules $rules) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Creates a new Rules instance. |
||
| 355 | * |
||
| 356 | * @return Rules |
||
| 357 | */ |
||
| 358 | 2 | private function newRules() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * @param string $binary |
||
| 365 | * |
||
| 366 | * @return $this |
||
| 367 | */ |
||
| 368 | public function setBinary($binary) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Gets the path of the PHP runtime. |
||
| 377 | * |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | 1 | public function getBinary() |
|
| 388 | |||
| 389 | /** |
||
| 390 | * @param string $worker |
||
| 391 | * |
||
| 392 | * @return $this |
||
| 393 | */ |
||
| 394 | public function setWorker($worker) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Gets the path of the worker script. |
||
| 403 | * |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | 1 | public function getWorker() |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Gets the path to write stdout to. |
||
| 417 | * |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | 1 | private function getStdOut() |
|
| 428 | |||
| 429 | /** |
||
| 430 | * @return null|string |
||
| 431 | */ |
||
| 432 | 2 | public function getLogPath() |
|
| 436 | |||
| 437 | /** |
||
| 438 | * @param string $logPath |
||
| 439 | * |
||
| 440 | * @return $this |
||
| 441 | */ |
||
| 442 | 1 | public function setLogPath($logPath) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Gets the path to write stderr to. |
||
| 451 | * |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | 1 | private function getStdErr() |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Gets a string representation of a task, to pass to the worker script. |
||
| 465 | * |
||
| 466 | * @param Task $task |
||
| 467 | * |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | 1 | private function getTaskString(Task $task) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Checks whether a task can be removed from the list of running processes. |
||
| 477 | * |
||
| 478 | * @param Task $task |
||
| 479 | * |
||
| 480 | * @return bool |
||
| 481 | */ |
||
| 482 | 1 | private function canRemoveTask(Task $task) |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Check if the given task is expired |
||
| 515 | * |
||
| 516 | * @param Task $task |
||
| 517 | * |
||
| 518 | * @return boolean |
||
| 519 | */ |
||
| 520 | 1 | private function isTaskExpired(Task $task) |
|
| 533 | |||
| 534 | /** |
||
| 535 | * Check if the given task is cancelled. |
||
| 536 | * |
||
| 537 | * @param Task $task |
||
| 538 | * |
||
| 539 | * @return bool |
||
| 540 | */ |
||
| 541 | 1 | private function isTaskCancelled(Task $task) |
|
| 549 | |||
| 550 | /** |
||
| 551 | * Revoke any background processes attached to this task. |
||
| 552 | * |
||
| 553 | * @param Task $task |
||
| 554 | * |
||
| 555 | * @return bool |
||
| 556 | */ |
||
| 557 | private function killTask(Task $task) |
||
| 572 | |||
| 573 | /** |
||
| 574 | * @param Rule $rule |
||
| 575 | * |
||
| 576 | * @return $this |
||
| 577 | */ |
||
| 578 | 1 | public function addRule(Rule $rule) |
|
| 584 | |||
| 585 | /** |
||
| 586 | * @param Rule $rule |
||
| 587 | * |
||
| 588 | * @return $this |
||
| 589 | */ |
||
| 590 | 1 | public function removeRule(Rule $rule) |
|
| 596 | |||
| 597 | public function __destruct() |
||
| 603 | } |
||
| 604 |