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 |
||
| 18 | class ModulePreferences implements SingletonInterface |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $preferences; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $tableName = 'tx_vidi_preference'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param string $key |
||
| 33 | * @param string $dataType |
||
| 34 | * @return mixed |
||
| 35 | */ |
||
| 36 | public function get($key, $dataType = '') |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Tell whether the module is loaded. |
||
| 53 | * |
||
| 54 | * @param string $dataType |
||
| 55 | * @return bool |
||
| 56 | */ |
||
| 57 | public function isLoaded($dataType) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param string $dataType |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | public function getAll($dataType = '') |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Get the md5 signature of the preferences. |
||
| 78 | * |
||
| 79 | * @param string $dataType |
||
| 80 | * @return bool |
||
| 81 | */ |
||
| 82 | public function getSignature($dataType = '') |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Load preferences. |
||
| 90 | * |
||
| 91 | * @param string $dataType |
||
| 92 | * @return void |
||
| 93 | */ |
||
| 94 | public function load($dataType) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Save preferences |
||
| 108 | * |
||
| 109 | * @param array $preferences |
||
| 110 | * @return void |
||
| 111 | */ |
||
| 112 | public function save($preferences) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param $dataType |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | public function fetchPreferencesFromDatabase($dataType) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Returns the module settings. |
||
| 163 | * |
||
| 164 | * @return array |
||
| 165 | */ |
||
| 166 | protected function fetchGlobalPreferencesFromTypoScript() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Returns the module settings. |
||
| 183 | * |
||
| 184 | * @param string $dataType |
||
| 185 | * @return array |
||
| 186 | */ |
||
| 187 | protected function fetchExtraPreferencesFromTypoScript($dataType) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Returns the module settings. |
||
| 208 | * |
||
| 209 | * @return array |
||
| 210 | */ |
||
| 211 | View Code Duplication | protected function getSettings() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @return object|DataService |
||
| 222 | */ |
||
| 223 | protected function getDataService(): DataService |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get the Vidi Module Loader. |
||
| 230 | * |
||
| 231 | * @return \Fab\Vidi\Module\ModuleLoader|object |
||
| 232 | */ |
||
| 233 | protected function getModuleLoader() |
||
| 237 | |||
| 238 | } |
||
| 239 |
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.