Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 declare(strict_types=1); |
||
31 | class ParallelExecutor |
||
32 | { |
||
33 | private $input; |
||
34 | private $output; |
||
35 | private $messenger; |
||
36 | private $client; |
||
37 | private $config; |
||
38 | |||
39 | 8 | public function __construct( |
|
53 | |||
54 | /** |
||
55 | * @param Host[] $hosts |
||
56 | */ |
||
57 | 8 | private function connect(array $hosts) |
|
84 | |||
85 | /** |
||
86 | * @param Task[] $tasks |
||
87 | * @param Host[] $hosts |
||
88 | * @param Planner|null $plan |
||
89 | * @return int |
||
90 | */ |
||
91 | 8 | public function run(array $tasks, array $hosts, $plan = null): int |
|
92 | { |
||
93 | 8 | $plan || $this->connect($hosts); |
|
94 | |||
95 | 8 | $globalLimit = (int)$this->input->getOption('limit') ?: count($hosts); |
|
96 | |||
97 | 8 | foreach ($tasks as $task) { |
|
98 | 8 | $plan || $this->messenger->startTask($task); |
|
99 | |||
100 | 8 | $limit = min($globalLimit, $task->getLimit() ?? $globalLimit); |
|
101 | 8 | $plannedHosts = $hosts; |
|
102 | 8 | if ($task->isOnce()) { |
|
103 | 4 | $plannedHosts = []; |
|
104 | 4 | foreach ($hosts as $host) { |
|
105 | 4 | if (Selector::apply($task->getSelector(), $host)) { |
|
106 | 4 | $plannedHosts[] = $host; |
|
107 | 4 | break; |
|
108 | } |
||
109 | } |
||
110 | } |
||
111 | |||
112 | 8 | if ($limit === 1 || count($plannedHosts) === 1) { |
|
113 | 8 | foreach ($plannedHosts as $host) { |
|
114 | 8 | if (!Selector::apply($task->getSelector(), $host)) { |
|
115 | if ($plan) { |
||
116 | $plan->commit([], $task); |
||
117 | } |
||
118 | continue; |
||
119 | } |
||
120 | |||
121 | 8 | if ($plan) { |
|
122 | $plan->commit([$host], $task); |
||
123 | continue; |
||
124 | } |
||
125 | |||
126 | try { |
||
127 | 8 | $host->getConfig()->load(); |
|
128 | 8 | Exception::setTaskSourceLocation($task->getSourceLocation()); |
|
129 | |||
130 | 8 | $task->run(new Context($host, $this->input, $this->output)); |
|
131 | |||
132 | 8 | $host->getConfig()->save(); |
|
133 | 1 | } catch (GracefulShutdownException $exception) { |
|
134 | $this->messenger->renderException($exception, $host); |
||
135 | return GracefulShutdownException::EXIT_CODE; |
||
136 | 1 | } catch (\Throwable $exception) { |
|
137 | 1 | $this->messenger->renderException($exception, $host); |
|
138 | 1 | return 1; |
|
139 | } |
||
140 | } |
||
141 | } else { |
||
142 | 1 | foreach (array_chunk($hosts, $limit) as $chunk) { |
|
143 | 1 | $exitCode = $this->runTask($chunk, $task, $plan); |
|
144 | 1 | if ($exitCode !== 0) { |
|
145 | return $exitCode; |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | |||
150 | 8 | $this->messenger->endTask($task); |
|
151 | } |
||
152 | |||
153 | 8 | return 0; |
|
154 | } |
||
155 | |||
156 | 1 | private function runTask(array $hosts, Task $task, Planner $plan = null): int |
|
195 | |||
196 | 1 | protected function getProcess(Host $host, Task $task): Process |
|
209 | |||
210 | /** |
||
211 | * @param Process[] $processes |
||
212 | */ |
||
213 | 1 | protected function startProcesses(array $processes) |
|
219 | |||
220 | /** |
||
221 | * @param Process[] $processes |
||
222 | */ |
||
223 | 1 | protected function areRunning(array $processes): bool |
|
233 | |||
234 | /** |
||
235 | * @param Process[] $processes |
||
236 | */ |
||
237 | 1 | protected function gatherOutput(array $processes, callable $callback) |
|
251 | |||
252 | /** |
||
253 | * Gather the cumulative exit code for the processes. |
||
254 | */ |
||
255 | 1 | protected function cumulativeExitCode(array $processes): int |
|
265 | } |
||
266 |