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 |
||
16 | class Translator |
||
17 | { |
||
18 | /** |
||
19 | * Configuration Proxy |
||
20 | * |
||
21 | * @var \phpbu\Laravel\Configuration\Proxy |
||
22 | */ |
||
23 | private $proxy; |
||
24 | |||
25 | /** |
||
26 | * Configurable laravel backup types. |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | private $types = [ |
||
31 | 'directories' => 'directoryConfigToBackup', |
||
32 | 'databases' => 'databaseConfigToBackup', |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * Translates the laravel configuration to a phpbu configuration. |
||
37 | * |
||
38 | * @param \phpbu\Laravel\Configuration\Proxy $proxy |
||
39 | * @return \phpbu\App\Configuration |
||
40 | * @throws \phpbu\Laravel\Configuration\Exception |
||
41 | */ |
||
42 | 7 | public function translate(Proxy $proxy) |
|
53 | |||
54 | /** |
||
55 | * Translate and add all configured backups. |
||
56 | * |
||
57 | * @param \phpbu\App\Configuration $configuration |
||
58 | * @param array $laravelPhpbu |
||
59 | */ |
||
60 | 7 | protected function addBackups(Configuration $configuration, array $laravelPhpbu) |
|
70 | |||
71 | /** |
||
72 | * Translates a given laravel config type to a phpbu backup configuration. |
||
73 | * |
||
74 | * @param string $type |
||
75 | * @param array $conf |
||
76 | * @throws \phpbu\Laravel\Configuration\Exception |
||
77 | * @return \phpbu\App\Configuration\Backup |
||
78 | */ |
||
79 | 7 | public function translateBackup($type, array $conf) |
|
92 | |||
93 | /** |
||
94 | * Translate a laravel directory config to phpbu backup configuration. |
||
95 | * |
||
96 | * @param array $dir |
||
97 | * @return \phpbu\App\Configuration\Backup |
||
98 | * @throws \phpbu\Laravel\Configuration\Exception |
||
99 | */ |
||
100 | 7 | protected function directoryConfigToBackup(array $dir) |
|
115 | |||
116 | /** |
||
117 | * Translate a laravel db config to phpbu backup configuration. |
||
118 | * |
||
119 | * @param array $db |
||
120 | * @throws \phpbu\Laravel\Configuration\Exception |
||
121 | * @return \phpbu\App\Configuration\Backup |
||
122 | */ |
||
123 | 5 | protected function databaseConfigToBackup(array $db) |
|
146 | |||
147 | /** |
||
148 | * Get a database connection configuration. |
||
149 | * |
||
150 | * @param string $connection |
||
151 | * @return array |
||
152 | * @throws \Exception |
||
153 | */ |
||
154 | 5 | protected function getDatabaseConnectionConfig($connection) |
|
166 | |||
167 | /** |
||
168 | * Map database driver to phpbu source type. |
||
169 | * |
||
170 | * @param string $driver |
||
171 | * @return string |
||
172 | */ |
||
173 | 3 | protected function getDatabaseSourceType($driver) |
|
178 | |||
179 | /** |
||
180 | * Translate the target configuration. |
||
181 | * |
||
182 | * @param array $config |
||
183 | * @return Target |
||
184 | * @throws \Exception |
||
185 | */ |
||
186 | 7 | protected function translateTarget(array $config) |
|
200 | |||
201 | /** |
||
202 | * Adds a check configuration to the given backup configuration. |
||
203 | * |
||
204 | * @param \phpbu\App\Configuration\Backup $backup |
||
205 | * @param array $conf |
||
206 | */ |
||
207 | 5 | protected function addChecksIfConfigured(Configuration\Backup $backup, array $conf) |
|
218 | |||
219 | /** |
||
220 | * Adds a sync configuration to the given backup configuration. |
||
221 | * |
||
222 | * @param \phpbu\App\Configuration\Backup $backup |
||
223 | * @param array $conf |
||
224 | */ |
||
225 | 5 | protected function addSyncIfConfigured(Configuration\Backup $backup, array $conf) |
|
240 | |||
241 | /** |
||
242 | * Adds a cleanup configuration to the given backup configuration. |
||
243 | * |
||
244 | * @param \phpbu\App\Configuration\Backup $backup |
||
245 | * @param array $conf |
||
246 | */ |
||
247 | 5 | protected function addCleanupIfConfigured(Configuration\Backup $backup, array $conf) |
|
259 | |||
260 | /** |
||
261 | * Adds a encryption configuration to the given encryption configuration. |
||
262 | * |
||
263 | * @param \phpbu\App\Configuration\Backup $backup |
||
264 | * @param array $conf |
||
265 | */ |
||
266 | 5 | protected function addCryptIfConfigured(Configuration\Backup $backup, array $conf) |
|
278 | } |
||
279 |
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.