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:
| 1 | <?php declare(strict_types=1); |
||
| 34 | class ParallelExecutor |
||
| 35 | { |
||
| 36 | private $input; |
||
| 37 | private $output; |
||
| 38 | private $messenger; |
||
| 39 | private $console; |
||
| 40 | private $client; |
||
| 41 | private $config; |
||
| 42 | |||
| 43 | public function __construct( |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param Host[] $hosts |
||
| 62 | */ |
||
| 63 | private function connect(array $hosts) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param Task[] $tasks |
||
| 93 | * @param Host[] $hosts |
||
| 94 | */ |
||
| 95 | public function run(array $tasks, array $hosts): int |
||
| 96 | { |
||
| 97 | $this->persistHosts($hosts); |
||
| 98 | $this->connect($hosts); |
||
| 99 | |||
| 100 | $localhost = new Localhost(); |
||
|
|
|||
| 101 | $limit = (int)$this->input->getOption('limit') ?: count($hosts); |
||
| 102 | |||
| 103 | foreach ($tasks as $task) { |
||
| 104 | $this->messenger->startTask($task); |
||
| 105 | |||
| 106 | if ($limit === 1 || count($hosts) === 1) { |
||
| 107 | foreach ($hosts as $host) { |
||
| 108 | $host->getConfig()->getCollection()->load(); |
||
| 109 | $task->run(new Context($host, $this->input, $this->output)); |
||
| 110 | $host->getConfig()->getCollection()->flush(); |
||
| 111 | } |
||
| 112 | } else { |
||
| 113 | foreach (array_chunk($hosts, $limit) as $chunk) { |
||
| 114 | $exitCode = $this->runTask($chunk, $task); |
||
| 115 | if ($exitCode !== 0) { |
||
| 116 | return $exitCode; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | $this->messenger->endTask($task); |
||
| 122 | } |
||
| 123 | |||
| 124 | return 0; |
||
| 125 | } |
||
| 126 | |||
| 127 | private function runTask(array $hosts, Task $task): int |
||
| 161 | |||
| 162 | protected function getProcess(Host $host, Task $task): Process |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Start all of the processes. |
||
| 178 | * |
||
| 179 | * @param Process[] $processes |
||
| 180 | * @return void |
||
| 181 | */ |
||
| 182 | protected function startProcesses(array $processes) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Determine if any of the processes are running. |
||
| 191 | * |
||
| 192 | * @param Process[] $processes |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | protected function areRunning(array $processes): bool |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Gather the output from all of the processes. |
||
| 208 | * |
||
| 209 | * @param Process[] $processes |
||
| 210 | * @param callable $callback |
||
| 211 | * @return void |
||
| 212 | */ |
||
| 213 | protected function gatherOutput(array $processes, callable $callback) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Gather the cumulative exit code for the processes. |
||
| 230 | */ |
||
| 231 | protected function gatherExitCodes(array $processes): int |
||
| 241 | |||
| 242 | /** |
||
| 243 | * @param Host[] $hosts |
||
| 244 | */ |
||
| 245 | private function persistHosts(array $hosts) |
||
| 261 | } |
||
| 262 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.