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 |
||
| 9 | class Worker |
||
| 10 | { |
||
| 11 | private static $descriptorspec = [ |
||
| 12 | 0 => ['pipe', 'r'], |
||
| 13 | 1 => ['pipe', 'w'], |
||
| 14 | 2 => ['pipe', 'w'], |
||
| 15 | ]; |
||
| 16 | private $proc; |
||
| 17 | private $pipes; |
||
| 18 | private $inExecution = 0; |
||
| 19 | private $isRunning = false; |
||
| 20 | private $exitCode = null; |
||
| 21 | private $commands = []; |
||
| 22 | private $chunks = ''; |
||
| 23 | private $alreadyReadOutput = ''; |
||
| 24 | /** |
||
| 25 | * @var ExecutableTest |
||
| 26 | */ |
||
| 27 | private $currentlyExecuting; |
||
| 28 | |||
| 29 | 5 | public function start(string $wrapperBinary, $token = 1, $uniqueToken = null) |
|
| 44 | |||
| 45 | public function stdout() |
||
| 49 | |||
| 50 | 4 | public function execute(string $testCmd) |
|
| 57 | |||
| 58 | public function assign(ExecutableTest $test, string $phpunit, array $phpunitOptions) |
||
| 66 | |||
| 67 | public function printFeedback(ResultPrinter $printer) |
||
| 73 | |||
| 74 | public function reset() |
||
| 78 | |||
| 79 | 6 | public function isStarted(): bool |
|
| 83 | |||
| 84 | 4 | private function checkStarted() |
|
| 90 | |||
| 91 | 3 | public function stop() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * This is an utility function for tests. |
||
| 99 | * Refactor or write it only in the test case. |
||
| 100 | */ |
||
| 101 | 2 | public function waitForFinishedJob() |
|
| 119 | |||
| 120 | 1 | public function isFree(): bool |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @deprecated |
||
| 130 | * This function consumes a lot of CPU while waiting for |
||
| 131 | * the worker to finish. Use it only in testing paratest |
||
| 132 | * itself. |
||
| 133 | */ |
||
| 134 | 3 | public function waitForStop() |
|
| 142 | |||
| 143 | public function getCoverageFileName() |
||
| 149 | |||
| 150 | 4 | private function setExitCode(array $status) |
|
| 158 | |||
| 159 | 1 | public function isRunning(): bool |
|
| 166 | |||
| 167 | 3 | public function isCrashed(): bool |
|
| 186 | |||
| 187 | 2 | private function checkNotCrashed() |
|
| 200 | |||
| 201 | private function readAllStderr() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Have to read even incomplete lines to play nice with stream_select() |
||
| 208 | * Otherwise it would continue to non-block because there are bytes to be read, |
||
| 209 | * but fgets() won't pick them up. |
||
| 210 | */ |
||
| 211 | 3 | private function updateStateFromAvailableOutput() |
|
| 237 | } |
||
| 238 |