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 |
||
6 | class Worker |
||
7 | { |
||
8 | private static $descriptorspec = [ |
||
9 | 0 => ["pipe", "r"], |
||
10 | 1 => ["pipe", "w"], |
||
11 | 2 => ["pipe", "w"] |
||
12 | ]; |
||
13 | private $proc; |
||
14 | private $pipes; |
||
15 | private $inExecution = 0; |
||
16 | private $isRunning = false; |
||
17 | private $exitCode = null; |
||
18 | private $commands = []; |
||
19 | private $chunks = ''; |
||
20 | private $alreadyReadOutput = ''; |
||
21 | /** |
||
22 | * @var ExecutableTest |
||
23 | */ |
||
24 | private $currentlyExecuting; |
||
25 | |||
26 | 5 | public function start($wrapperBinary, $token = 1, $uniqueToken = null) |
|
41 | |||
42 | public function stdout() |
||
46 | |||
47 | 4 | public function execute($testCmd) |
|
54 | |||
55 | public function assign(ExecutableTest $test, $phpunit, $phpunitOptions) |
||
63 | |||
64 | public function printFeedback($printer) |
||
70 | |||
71 | public function reset() |
||
75 | |||
76 | 6 | public function isStarted() |
|
80 | |||
81 | 4 | private function checkStarted() |
|
87 | |||
88 | 3 | public function stop() |
|
93 | |||
94 | /** |
||
95 | * This is an utility function for tests. |
||
96 | * Refactor or write it only in the test case. |
||
97 | */ |
||
98 | 2 | public function waitForFinishedJob() |
|
116 | |||
117 | 1 | public function isFree() |
|
123 | |||
124 | /** |
||
125 | * @deprecated |
||
126 | * This function consumes a lot of CPU while waiting for |
||
127 | * the worker to finish. Use it only in testing paratest |
||
128 | * itself. |
||
129 | */ |
||
130 | 3 | public function waitForStop() |
|
138 | |||
139 | public function getCoverageFileName() |
||
147 | |||
148 | 4 | private function setExitCode($status) |
|
156 | |||
157 | 1 | public function isRunning() |
|
163 | |||
164 | 3 | public function isCrashed() |
|
182 | |||
183 | 2 | private function checkNotCrashed() |
|
196 | |||
197 | private function readAllStderr() |
||
201 | |||
202 | /** |
||
203 | * Have to read even incomplete lines to play nice with stream_select() |
||
204 | * Otherwise it would continue to non-block because there are bytes to be read, |
||
205 | * but fgets() won't pick them up. |
||
206 | */ |
||
207 | 3 | private function updateStateFromAvailableOutput() |
|
233 | } |
||
234 |