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 |
||
24 | final class Composer |
||
25 | { |
||
26 | /** |
||
27 | * The filesystem instance. |
||
28 | * |
||
29 | * @var \Illuminate\Filesystem\Filesystem |
||
30 | */ |
||
31 | private $files; |
||
32 | |||
33 | /** |
||
34 | * The working path to execute Composer from. |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | private $workingPath; |
||
39 | |||
40 | /** |
||
41 | * The logging path to store Composer logs. |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | private $loggingPath; |
||
46 | |||
47 | /** |
||
48 | * Create a new Composer manager instance. |
||
49 | * |
||
50 | * @param \Illuminate\Filesystem\Filesystem $files |
||
51 | * @param string $workingPath |
||
52 | * @param string $loggingPath |
||
53 | */ |
||
54 | public function __construct(Filesystem $files, string $workingPath, string $loggingPath) |
||
60 | |||
61 | /** |
||
62 | * Regenerate the Composer autoloader files. |
||
63 | * |
||
64 | * @param string[] $extra |
||
65 | * @return void |
||
66 | */ |
||
67 | public function dumpAutoload(array $extra = []): void |
||
73 | |||
74 | /** |
||
75 | * Regenerate the optimized Composer autoloader files. |
||
76 | * |
||
77 | * @return void |
||
78 | */ |
||
79 | public function dumpOptimized(): void |
||
83 | |||
84 | /** |
||
85 | * Install Composer requirement. |
||
86 | * |
||
87 | * @param \Cog\Contracts\Paket\Requirement\Entities\Requirement $requirement |
||
88 | * @param \Cog\Contracts\Paket\Job\Entities\Job $job |
||
89 | * @return void |
||
90 | * |
||
91 | * @throws \Cog\Contracts\Paket\Job\Exceptions\JobFailed |
||
92 | */ |
||
93 | View Code Duplication | public function install(RequirementContract $requirement, JobContract $job): void |
|
112 | |||
113 | /** |
||
114 | * Uninstall Composer requirement. |
||
115 | * |
||
116 | * @param \Cog\Contracts\Paket\Requirement\Entities\Requirement $requirement |
||
117 | * @param \Cog\Contracts\Paket\Job\Entities\Job $job |
||
118 | * @return void |
||
119 | * |
||
120 | * @throws \Cog\Contracts\Paket\Job\Exceptions\JobFailed |
||
121 | */ |
||
122 | View Code Duplication | public function uninstall(RequirementContract $requirement, JobContract $job): void |
|
141 | |||
142 | /** |
||
143 | * Builds full command, executes it and logs process output. |
||
144 | * |
||
145 | * @param \Cog\Contracts\Paket\Job\Entities\Job $job |
||
146 | * @param string $command |
||
147 | * |
||
148 | * @throws \Cog\Contracts\Paket\Job\Exceptions\JobFailed |
||
149 | */ |
||
150 | private function executeCommand(JobContract $job, string $command): void |
||
180 | |||
181 | private function getComposerExecutable(): array |
||
189 | |||
190 | private function getPhpBinary(): string |
||
194 | |||
195 | private function getProcess(array $command): Process |
||
199 | |||
200 | private function addLineToLogFile(string $jobLogFile, string $line): void |
||
204 | } |
||
205 |
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.