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); |
||
20 | class ProcessExecutor |
||
21 | { |
||
22 | /** |
||
23 | * @var ProcessEnvironment |
||
24 | */ |
||
25 | private $environment; |
||
26 | |||
27 | /** |
||
28 | * @var TemplateEngine |
||
29 | */ |
||
30 | private $templateEngine; |
||
31 | |||
32 | /** |
||
33 | * @var Logger |
||
34 | */ |
||
35 | private $logger; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $applicationDirectory; |
||
41 | |||
42 | /** |
||
43 | * @var DeferredProcess[] |
||
44 | */ |
||
45 | private $deferredProcesses = []; |
||
46 | |||
47 | /** |
||
48 | * ProcessExecutor constructor. |
||
49 | * @param ProcessEnvironment $environment |
||
50 | * @param TemplateEngine $templateEngine |
||
51 | * @param Logger $logger |
||
52 | * @param string $applicationDirectory |
||
53 | */ |
||
54 | public function __construct( |
||
65 | |||
66 | /** |
||
67 | * @param Script $script |
||
68 | * @param Command[] $commands |
||
69 | */ |
||
70 | public function execute(Script $script, array $commands) |
||
86 | |||
87 | /** |
||
88 | * @param Command $command |
||
89 | * @param int $index |
||
90 | * @param int $totalCount |
||
91 | */ |
||
92 | private function executeCommand(Command $command, int $index, int $totalCount) |
||
142 | |||
143 | private function executeTemplateRendering() |
||
149 | |||
150 | /** |
||
151 | * @param ParsableCommand $command |
||
152 | * @return string |
||
153 | */ |
||
154 | private function getParsedShellCommand(ParsableCommand $command): string |
||
165 | |||
166 | /** |
||
167 | * @param Process $process |
||
168 | * @param ProcessCommand $command |
||
169 | */ |
||
170 | private function setProcessDefaults(Process $process, ProcessCommand $command) |
||
176 | |||
177 | /** |
||
178 | * @param Process $process |
||
179 | */ |
||
180 | private function runProcess(Process $process) |
||
186 | |||
187 | /** |
||
188 | * @param Process $process |
||
189 | * @param ProcessCommand $command |
||
190 | */ |
||
191 | private function testProcessResultValid(Process $process, ProcessCommand $command) |
||
197 | |||
198 | /** |
||
199 | * @param $template |
||
200 | */ |
||
201 | private function renderTemplate(Template $template) |
||
213 | |||
214 | private function waitForDeferredProcesses() |
||
244 | |||
245 | /** |
||
246 | * @param string $parsedCommand |
||
247 | * @param DeferredProcessCommand $command |
||
248 | * @param Process $process |
||
249 | */ |
||
250 | private function deferProcess(string $parsedCommand, DeferredProcessCommand $command, Process $process) |
||
260 | |||
261 | /** |
||
262 | * @param Process $process |
||
263 | * @param bool $ignoreError |
||
264 | * @return bool |
||
265 | */ |
||
266 | private function isProcessResultValid(Process $process, ProcessCommand $command): bool |
||
270 | |||
271 | /** |
||
272 | * @param WaitCommand $command |
||
273 | * @param int $index |
||
274 | * @param int $totalCount |
||
275 | */ |
||
276 | private function logWaitStart(WaitCommand $command, int $index, int $totalCount) |
||
287 | |||
288 | /** |
||
289 | * @param TemplateCommand $command |
||
290 | * @param int $index |
||
291 | * @param int $totalCount |
||
292 | * @param Template $template |
||
293 | */ |
||
294 | private function logTemplateStart(TemplateCommand $command, int $index, int $totalCount, Template $template) |
||
305 | |||
306 | /** |
||
307 | * @param DeferredProcessCommand $command |
||
308 | * @param int $index |
||
309 | * @param int $totalCount |
||
310 | * @param string $parsedCommand |
||
311 | */ |
||
312 | private function logDeferedStart(DeferredProcessCommand $command, int $index, int $totalCount, string $parsedCommand) |
||
323 | |||
324 | /** |
||
325 | * @param ProcessCommand $command |
||
326 | * @param int $index |
||
327 | * @param int $totalCount |
||
328 | * @param string $parsedCommand |
||
329 | */ |
||
330 | private function logSynchronousProcessStart(ProcessCommand $command, int $index, int $totalCount, string $parsedCommand) |
||
341 | |||
342 | /** |
||
343 | * @param BashCommand $command |
||
344 | * @param int $index |
||
345 | * @param int $totalCount |
||
346 | */ |
||
347 | private function logBashStart(BashCommand $command, int $index, int $totalCount) |
||
358 | |||
359 | /** |
||
360 | * @param DeferredProcess $deferredProcess |
||
361 | * @param $index |
||
362 | */ |
||
363 | private function logDeferredOutputStart(DeferredProcess $deferredProcess, $index) |
||
374 | } |
||
375 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.