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); |
||
19 | class ProcessExecutor |
||
20 | { |
||
21 | /** |
||
22 | * @var ProcessEnvironment |
||
23 | */ |
||
24 | private $environment; |
||
25 | |||
26 | /** |
||
27 | * @var TemplateEngine |
||
28 | */ |
||
29 | private $templateEngine; |
||
30 | |||
31 | /** |
||
32 | * @var Logger |
||
33 | */ |
||
34 | private $logger; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $applicationDirectory; |
||
40 | |||
41 | /** |
||
42 | * @var DeferredProcess[] |
||
43 | */ |
||
44 | private $deferredProcesses = []; |
||
45 | |||
46 | /** |
||
47 | * ProcessExecutor constructor. |
||
48 | * @param ProcessEnvironment $environment |
||
49 | * @param TemplateEngine $templateEngine |
||
50 | * @param Logger $logger |
||
51 | * @param string $applicationDirectory |
||
52 | */ |
||
53 | public function __construct( |
||
64 | |||
65 | /** |
||
66 | * @param Script $script |
||
67 | * @param Command[] $commands |
||
68 | */ |
||
69 | public function execute(Script $script, array $commands) |
||
85 | |||
86 | /** |
||
87 | * @param Command $command |
||
88 | * @param int $index |
||
89 | * @param int $totalCount |
||
90 | */ |
||
91 | private function executeCommand(Command $command, int $index, int $totalCount) |
||
140 | |||
141 | private function executeTemplateRendering() |
||
147 | |||
148 | /** |
||
149 | * @param ProcessCommand $command |
||
150 | * @return string |
||
151 | */ |
||
152 | protected function getParsedShellCommand(ProcessCommand $command): string |
||
163 | |||
164 | /** |
||
165 | * @param Process $process |
||
166 | * @param bool $isTty |
||
167 | */ |
||
168 | private function setProcessDefaults(Process $process, bool $isTty) |
||
174 | |||
175 | /** |
||
176 | * @param Process $process |
||
177 | */ |
||
178 | private function runProcess(Process $process) |
||
184 | |||
185 | /** |
||
186 | * @param Process $process |
||
187 | * @param bool $ignoreError |
||
188 | */ |
||
189 | protected function testProcessResultValid(Process $process, bool $ignoreError) |
||
195 | |||
196 | /** |
||
197 | * @param $template |
||
198 | */ |
||
199 | private function renderTemplate(Template $template) |
||
211 | |||
212 | private function waitForDeferredProcesses() |
||
249 | |||
250 | /** |
||
251 | * @param string $parsedCommand |
||
252 | * @param DeferredProcessCommand $command |
||
253 | * @param Process $process |
||
254 | */ |
||
255 | private function deferProcess(string $parsedCommand, DeferredProcessCommand $command, Process $process) |
||
265 | |||
266 | /** |
||
267 | * @param Process $process |
||
268 | * @param bool $ignoreError |
||
269 | * @return bool |
||
270 | */ |
||
271 | protected function isProcessResultValid(Process $process, bool $ignoreError): bool |
||
275 | |||
276 | /** |
||
277 | * @param Command $command |
||
278 | * @param int $index |
||
279 | * @param int $totalCount |
||
280 | */ |
||
281 | private function logWaitStart(Command $command, int $index, int $totalCount) |
||
292 | |||
293 | /** |
||
294 | * @param Command $command |
||
295 | * @param int $index |
||
296 | * @param int $totalCount |
||
297 | * @param Template $template |
||
298 | */ |
||
299 | private function logTemplateStart(Command $command, int $index, int $totalCount, Template $template) |
||
310 | |||
311 | /** |
||
312 | * @param Command $command |
||
313 | * @param int $index |
||
314 | * @param int $totalCount |
||
315 | * @param string $parsedCommand |
||
316 | */ |
||
317 | private function logDeferedStart(Command $command, int $index, int $totalCount, string $parsedCommand) |
||
328 | |||
329 | /** |
||
330 | * @param Command $command |
||
331 | * @param int $index |
||
332 | * @param int $totalCount |
||
333 | * @param string $parsedCommand |
||
334 | */ |
||
335 | private function logSynchronousProcessStart(Command $command, int $index, int $totalCount, string $parsedCommand) |
||
346 | |||
347 | /** |
||
348 | * @param Command $command |
||
349 | * @param int $index |
||
350 | * @param int $totalCount |
||
351 | */ |
||
352 | private function logBashStart(Command $command, int $index, int $totalCount) |
||
363 | } |
||
364 |
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.