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 | * @inheritdoc |
||
| 63 | * |
||
| 64 | * @param Task $task |
||
| 65 | * |
||
| 66 | * @return $this |
||
| 67 | */ |
||
| 68 | 1 | public function addTask(Task $task) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @inheritdoc |
||
| 77 | * |
||
| 78 | * @return bool |
||
| 79 | */ |
||
| 80 | 1 | public function tick() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Stops sibling processes of a task. |
||
| 137 | * |
||
| 138 | * @param Task $task |
||
| 139 | * |
||
| 140 | * @return $this |
||
| 141 | */ |
||
| 142 | private function stopSiblingTasks(Task $task) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Checks whether a new task can be run. |
||
| 159 | * |
||
| 160 | * @param Task $task |
||
| 161 | * |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | 1 | private function canRunTask(Task $task) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Gets the load profile related to a task. |
||
| 189 | * |
||
| 190 | * @param Task $task |
||
| 191 | * @param array $processes |
||
| 192 | * |
||
| 193 | * @return Profile |
||
| 194 | */ |
||
| 195 | 1 | private function getProfileForProcesses(Task $task, array $processes) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Gets processor and memory stats for a list of processes. |
||
| 220 | * |
||
| 221 | * @param Process[] $processes |
||
| 222 | * |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | 1 | private function getStatsForProcesses(array $processes) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Gets or creates a Shell instance. |
||
| 260 | * |
||
| 261 | * @return Shell |
||
| 262 | */ |
||
| 263 | 2 | public function getShell() |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param Shell $shell |
||
| 274 | * |
||
| 275 | * @return $this |
||
| 276 | */ |
||
| 277 | 1 | public function setShell(Shell $shell) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Creates a new Shell instance. |
||
| 286 | * |
||
| 287 | * @return Shell |
||
| 288 | */ |
||
| 289 | 2 | private function newShell() |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Creates a new Profile instance. |
||
| 296 | * |
||
| 297 | * @return Profile |
||
| 298 | */ |
||
| 299 | 1 | private function newProfile() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Gets or creates a new Rules instance. |
||
| 306 | * |
||
| 307 | * @return Rules |
||
| 308 | */ |
||
| 309 | 2 | public function getRules() |
|
| 317 | |||
| 318 | /** |
||
| 319 | * @param Rules $rules |
||
| 320 | * |
||
| 321 | * @return $this |
||
| 322 | */ |
||
| 323 | 1 | public function setRules(Rules $rules) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Creates a new Rules instance. |
||
| 332 | * |
||
| 333 | * @return Rules |
||
| 334 | */ |
||
| 335 | 2 | private function newRules() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @param string $binary |
||
| 342 | * |
||
| 343 | * @return $this |
||
| 344 | */ |
||
| 345 | public function setBinary($binary) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Gets the path of the PHP runtime. |
||
| 354 | * |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | 1 | public function getBinary() |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @param string $worker |
||
| 368 | * |
||
| 369 | * @return $this |
||
| 370 | */ |
||
| 371 | public function setWorker($worker) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Gets the path of the worker script. |
||
| 380 | * |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | 1 | public function getWorker() |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Gets the path to write stdout to. |
||
| 394 | * |
||
| 395 | * @return string |
||
| 396 | */ |
||
| 397 | 1 | private function getStdOut() |
|
| 405 | |||
| 406 | /** |
||
| 407 | * @return null|string |
||
| 408 | */ |
||
| 409 | 2 | public function getLogPath() |
|
| 413 | |||
| 414 | /** |
||
| 415 | * @param string $logPath |
||
| 416 | * |
||
| 417 | * @return $this |
||
| 418 | */ |
||
| 419 | 1 | public function setLogPath($logPath) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Gets the path to write stderr to. |
||
| 428 | * |
||
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | 1 | private function getStdErr() |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Gets a string representation of a task, to pass to the worker script. |
||
| 442 | * |
||
| 443 | * @param Task $task |
||
| 444 | * |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | 1 | private function getTaskString(Task $task) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Checks whether a task can be removed from the list of running processes. |
||
| 454 | * |
||
| 455 | * @param Task $task |
||
| 456 | * |
||
| 457 | * @return bool |
||
| 458 | */ |
||
| 459 | 1 | private function canRemoveTask(Task $task) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Check if the given task is expired |
||
| 492 | * |
||
| 493 | * @param Task $task |
||
| 494 | * |
||
| 495 | * @return boolean |
||
| 496 | */ |
||
| 497 | 1 | private function isTaskExpired(Task $task) |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Check if the given task is cancelled. |
||
| 513 | * |
||
| 514 | * @param Task $task |
||
| 515 | * |
||
| 516 | * @return bool |
||
| 517 | */ |
||
| 518 | 1 | private function isTaskCancelled(Task $task) |
|
| 526 | |||
| 527 | /** |
||
| 528 | * Revoke any background processes attached to this task. |
||
| 529 | * |
||
| 530 | * @param Task $task |
||
| 531 | * |
||
| 532 | * @return bool |
||
| 533 | */ |
||
| 534 | private function killTask(Task $task) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @param Rule $rule |
||
| 549 | * |
||
| 550 | * @return $this |
||
| 551 | */ |
||
| 552 | 1 | public function addRule(Rule $rule) |
|
| 558 | |||
| 559 | /** |
||
| 560 | * @param Rule $rule |
||
| 561 | * |
||
| 562 | * @return $this |
||
| 563 | */ |
||
| 564 | 1 | public function removeRule(Rule $rule) |
|
| 570 | |||
| 571 | public function __destruct() |
||
| 577 | } |
||
| 578 |