Complex classes like ParallelExecutor 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 ParallelExecutor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class ParallelExecutor implements ExecutorInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var InputInterface |
||
28 | */ |
||
29 | private $input; |
||
30 | |||
31 | /** |
||
32 | * @var OutputInterface |
||
33 | */ |
||
34 | private $output; |
||
35 | |||
36 | /** |
||
37 | * @var Informer |
||
38 | */ |
||
39 | private $informer; |
||
40 | |||
41 | /** |
||
42 | * @var Application |
||
43 | */ |
||
44 | private $console; |
||
45 | |||
46 | /** |
||
47 | * @param InputInterface $input |
||
48 | * @param OutputInterface $output |
||
49 | * @param Informer $informer |
||
50 | * @param Application $console |
||
51 | */ |
||
52 | 4 | public function __construct(InputInterface $input, OutputInterface $output, Informer $informer, Application $console) |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | 4 | public function run($tasks, $hosts) |
|
117 | |||
118 | /** |
||
119 | * Run task on hosts. |
||
120 | * |
||
121 | * @param Host[] $hosts |
||
122 | * @param Task $task |
||
123 | * @return int |
||
124 | */ |
||
125 | 4 | private function runTask(array $hosts, Task $task) |
|
155 | |||
156 | /** |
||
157 | * Get process for task on host. |
||
158 | * |
||
159 | * @param Host $host |
||
160 | * @param Task $task |
||
161 | * @return Process |
||
162 | */ |
||
163 | 4 | protected function getProcess($host, Task $task) |
|
187 | |||
188 | /** |
||
189 | * Start all of the processes. |
||
190 | * |
||
191 | * @param Process[] $processes |
||
192 | * @return void |
||
193 | */ |
||
194 | 4 | protected function startProcesses(array $processes) |
|
200 | |||
201 | /** |
||
202 | * Determine if any of the processes are running. |
||
203 | * |
||
204 | * @param Process[] $processes |
||
205 | * @return bool |
||
206 | */ |
||
207 | 4 | protected function areRunning(array $processes) |
|
216 | |||
217 | /** |
||
218 | * Gather the output from all of the processes. |
||
219 | * |
||
220 | * @param Process[] $processes |
||
221 | * @param callable $callback |
||
222 | */ |
||
223 | 4 | protected function gatherOutput(array $processes, callable $callback) |
|
238 | |||
239 | /** |
||
240 | * Gather the cumulative exit code for the processes. |
||
241 | * |
||
242 | * @param Process[] $processes |
||
243 | * @return int |
||
244 | */ |
||
245 | 4 | protected function gatherExitCodes(array $processes) |
|
255 | |||
256 | /** |
||
257 | * Generate options and arguments string. |
||
258 | * @return string |
||
259 | */ |
||
260 | 4 | private function generateOptions() |
|
289 | |||
290 | 4 | private function generateArguments(): string |
|
301 | } |
||
302 |