Complex classes like Worker 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 Worker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Worker |
||
| 11 | { |
||
| 12 | private static $descriptorspec = [ |
||
| 13 | 0 => ['pipe', 'r'], |
||
| 14 | 1 => ['pipe', 'w'], |
||
| 15 | 2 => ['pipe', 'w'], |
||
| 16 | ]; |
||
| 17 | private $proc; |
||
| 18 | private $pipes; |
||
| 19 | private $inExecution = 0; |
||
| 20 | private $isRunning = false; |
||
| 21 | private $exitCode = null; |
||
| 22 | private $commands = []; |
||
| 23 | private $chunks = ''; |
||
| 24 | private $alreadyReadOutput = ''; |
||
| 25 | /** |
||
| 26 | * @var ExecutableTest |
||
| 27 | */ |
||
| 28 | private $currentlyExecuting; |
||
| 29 | |||
| 30 | 5 | public function start(string $wrapperBinary, $token = 1, $uniqueToken = null) |
|
| 46 | |||
| 47 | public function stdout() |
||
| 51 | |||
| 52 | 4 | public function execute(string $testCmd) |
|
| 59 | |||
| 60 | public function assign(ExecutableTest $test, string $phpunit, array $phpunitOptions) |
||
| 68 | |||
| 69 | public function printFeedback(ResultPrinter $printer) |
||
| 75 | |||
| 76 | public function reset() |
||
| 80 | |||
| 81 | 6 | public function isStarted(): bool |
|
| 85 | |||
| 86 | 4 | private function checkStarted() |
|
| 92 | |||
| 93 | 3 | public function stop() |
|
| 98 | |||
| 99 | /** |
||
| 100 | * This is an utility function for tests. |
||
| 101 | * Refactor or write it only in the test case. |
||
| 102 | */ |
||
| 103 | 2 | public function waitForFinishedJob() |
|
| 121 | |||
| 122 | 1 | public function isFree(): bool |
|
| 129 | |||
| 130 | /** |
||
| 131 | * @deprecated |
||
| 132 | * This function consumes a lot of CPU while waiting for |
||
| 133 | * the worker to finish. Use it only in testing paratest |
||
| 134 | * itself. |
||
| 135 | */ |
||
| 136 | 3 | public function waitForStop() |
|
| 144 | |||
| 145 | public function getCoverageFileName() |
||
| 151 | |||
| 152 | 4 | private function setExitCode(array $status) |
|
| 160 | |||
| 161 | 1 | public function isRunning(): bool |
|
| 168 | |||
| 169 | 3 | public function isCrashed(): bool |
|
| 188 | |||
| 189 | 2 | private function checkNotCrashed() |
|
| 202 | |||
| 203 | private function readAllStderr() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Have to read even incomplete lines to play nice with stream_select() |
||
| 210 | * Otherwise it would continue to non-block because there are bytes to be read, |
||
| 211 | * but fgets() won't pick them up. |
||
| 212 | */ |
||
| 213 | 3 | private function updateStateFromAvailableOutput() |
|
| 239 | } |
||
| 240 |