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) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Get process for task on host. |
||
| 157 | * |
||
| 158 | * @param Host $host |
||
| 159 | * @param Task $task |
||
| 160 | * @return Process |
||
| 161 | */ |
||
| 162 | 4 | protected function getProcess($host, Task $task) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Start all of the processes. |
||
| 189 | * |
||
| 190 | * @param Process[] $processes |
||
| 191 | * @return void |
||
| 192 | */ |
||
| 193 | 4 | protected function startProcesses(array $processes) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Determine if any of the processes are running. |
||
| 202 | * |
||
| 203 | * @param Process[] $processes |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | 4 | protected function areRunning(array $processes) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Gather the output from all of the processes. |
||
| 218 | * |
||
| 219 | * @param Process[] $processes |
||
| 220 | * @param callable $callback |
||
| 221 | */ |
||
| 222 | 4 | protected function gatherOutput(array $processes, callable $callback) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Gather the cumulative exit code for the processes. |
||
| 240 | * |
||
| 241 | * @param Process[] $processes |
||
| 242 | * @return int |
||
| 243 | */ |
||
| 244 | 4 | protected function gatherExitCodes(array $processes) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Generate options and arguments string. |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | 4 | private function generateOptions() |
|
| 288 | |||
| 289 | 4 | private function generateArguments(): string |
|
| 300 | } |
||
| 301 |