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 namespace Laravel\Installer\Console; |
||
10 | class DownloadCommand extends Command { |
||
11 | |||
12 | private $output; |
||
13 | |||
14 | private $progress = 0; |
||
15 | |||
16 | /** |
||
17 | * Configure the command options. |
||
18 | * |
||
19 | * @return void |
||
20 | */ |
||
21 | protected function configure() |
||
26 | |||
27 | /** |
||
28 | * Execute the command. |
||
29 | * |
||
30 | * @param InputInterface $input |
||
31 | * @param OutputInterface $output |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | protected function execute(InputInterface $input, OutputInterface $output) |
||
45 | |||
46 | /** |
||
47 | * Download the temporary Zip to the given file. |
||
48 | * |
||
49 | * @return $this |
||
50 | */ |
||
51 | protected function download() |
||
75 | |||
76 | /** |
||
77 | * Clean-up the Zip file. |
||
78 | * |
||
79 | * @param the zip file to remove |
||
80 | * |
||
81 | * @return $this |
||
82 | */ |
||
83 | protected function cleanUp($zipFile) |
||
90 | |||
91 | |||
92 | /** |
||
93 | * Download the nukacode build files and display progress bar. |
||
94 | * |
||
95 | * @param $buildUrl |
||
96 | * @param $zipFile |
||
97 | */ |
||
98 | View Code Duplication | protected function downloadFileWithProgressBar($buildUrl, $zipFile) |
|
126 | |||
127 | /** |
||
128 | * Check if the server has a newer version of the nukacode build. |
||
129 | * |
||
130 | * @param string $md5CheckPath The url to check for file md5 hash |
||
131 | * @param string $zipFile The zip file we are checking |
||
132 | * |
||
133 | * @return bool |
||
134 | */ |
||
135 | protected function checkIfServerHasNewerBuild($md5CheckPath, $zipFile) |
||
149 | } |
||
150 |
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.