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 |
||
26 | class Installer |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * Does some routine installation tasks so people don't have to. |
||
31 | * |
||
32 | * @param \Composer\Script\Event $event The composer event object. |
||
33 | * |
||
34 | * @return void |
||
35 | */ |
||
36 | public static function postInstall(Event $event) |
||
77 | |||
78 | /** |
||
79 | * Create the config/app.php file if it does not exist. |
||
80 | * |
||
81 | * @param string $dir The application's root directory. |
||
82 | * @param \Composer\IO\IOInterface $io IO interface to write to console. |
||
83 | * |
||
84 | * @return void |
||
85 | */ |
||
86 | View Code Duplication | public static function createAppConfig($dir, $io) |
|
95 | |||
96 | /** |
||
97 | * Create the config/database.php file if it does not exist. |
||
98 | * |
||
99 | * @param string $dir The application's root directory. |
||
100 | * @param \Composer\IO\IOInterface $io IO interface to write to console. |
||
101 | * |
||
102 | * @return void |
||
103 | */ |
||
104 | View Code Duplication | public static function createDatabaseConfig($dir, $io) |
|
105 | { |
||
106 | $databaseConfig = $dir . '/config/database.php'; |
||
107 | $defaultConfig = $dir . '/config/database.default.php'; |
||
108 | if (!file_exists($databaseConfig)) { |
||
109 | copy($defaultConfig, $databaseConfig); |
||
110 | $io->write('Created `config/database.php` file'); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Create the config/email.php file if it does not exist. |
||
116 | * |
||
117 | * @param string $dir The application's root directory. |
||
118 | * @param \Composer\IO\IOInterface $io IO interface to write to console. |
||
119 | * |
||
120 | * @return void |
||
121 | */ |
||
122 | View Code Duplication | public static function createEmailConfig($dir, $io) |
|
123 | { |
||
124 | $databaseConfig = $dir . '/config/email.php'; |
||
125 | $defaultConfig = $dir . '/config/email.default.php'; |
||
126 | if (!file_exists($databaseConfig)) { |
||
127 | copy($defaultConfig, $databaseConfig); |
||
128 | $io->write('Created `config/email.php` file'); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Create the `logs` and `tmp` directories. |
||
134 | * |
||
135 | * @param string $dir The application's root directory. |
||
136 | * @param \Composer\IO\IOInterface $io IO interface to write to console. |
||
137 | * @return void |
||
138 | */ |
||
139 | public static function createWritableDirectories($dir, $io) |
||
160 | |||
161 | /** |
||
162 | * Create the config/recaptcha.php file if it does not exist. |
||
163 | * |
||
164 | * @param string $dir The application's root directory. |
||
165 | * @param \Composer\IO\IOInterface $io IO interface to write to console. |
||
166 | * |
||
167 | * @return void |
||
168 | */ |
||
169 | View Code Duplication | public static function createRecaptchaConfig($dir, $io) |
|
178 | |||
179 | /** |
||
180 | * Set globally writable permissions on the "tmp" and "logs" directory. |
||
181 | * |
||
182 | * This is not the most secure default, but it gets people up and running quickly. |
||
183 | * |
||
184 | * @param string $dir The application's root directory. |
||
185 | * @param \Composer\IO\IOInterface $io IO interface to write to console. |
||
186 | * @return void |
||
187 | */ |
||
188 | public static function setFolderPermissions($dir, $io) |
||
225 | |||
226 | /** |
||
227 | * Set the datasources.default.database value in the application's config file. |
||
228 | * |
||
229 | * @param string $dir The application's root directory. |
||
230 | * @param \Composer\IO\IOInterface $io IO interface to write to console. |
||
231 | * |
||
232 | * @return void |
||
233 | */ |
||
234 | public static function setDatabaseName($dir, $io) |
||
265 | |||
266 | /** |
||
267 | * Set the security.salt value in the application's config file. |
||
268 | * |
||
269 | * @param string $dir The application's root directory. |
||
270 | * @param \Composer\IO\IOInterface $io IO interface to write to console. |
||
271 | * @return void |
||
272 | */ |
||
273 | public static function setSecuritySaltAndKey($dir, $io) |
||
298 | |||
299 | /** |
||
300 | * Set up the admin and member password for the database. |
||
301 | * |
||
302 | * @param string $dir The application's root directory. |
||
303 | * @param \Composer\IO\IOInterface $io IO interface to write to console. |
||
304 | * @param string $newKey The new security.salt. |
||
305 | * |
||
306 | * @return void |
||
307 | */ |
||
308 | public static function setAccountPassword($dir, $io, $newKey = null) |
||
352 | } |
||
353 |
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.