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 |
||
| 36 | class ConfigService { |
||
| 37 | |||
| 38 | const FILES_LOCAL = 'files_local'; |
||
| 39 | const FILES_EXTERNAL = 'files_external'; |
||
| 40 | const FILES_GROUP_FOLDERS = 'files_group_folders'; |
||
| 41 | const FILES_ENCRYPTED = 'files_encrypted'; |
||
| 42 | const FILES_FEDERATED = 'files_federated'; |
||
| 43 | const FILES_SIZE = 'files_size'; |
||
| 44 | const FILES_OFFICE = 'files_office'; |
||
| 45 | const FILES_PDF = 'files_pdf'; |
||
| 46 | const FILES_IMAGE = 'files_image'; |
||
| 47 | const FILES_AUDIO = 'files_audio'; |
||
| 48 | |||
| 49 | public $defaults = [ |
||
| 50 | self::FILES_LOCAL => '1', |
||
| 51 | self::FILES_EXTERNAL => '0', |
||
| 52 | self::FILES_GROUP_FOLDERS => '0', |
||
| 53 | self::FILES_ENCRYPTED => '0', |
||
| 54 | self::FILES_FEDERATED => '0', |
||
| 55 | self::FILES_SIZE => '20', |
||
| 56 | self::FILES_PDF => '1', |
||
| 57 | self::FILES_OFFICE => '1', |
||
| 58 | self::FILES_IMAGE => '0', |
||
| 59 | self::FILES_AUDIO => '0' |
||
| 60 | ]; |
||
| 61 | |||
| 62 | |||
| 63 | /** @var IConfig */ |
||
| 64 | private $config; |
||
| 65 | |||
| 66 | /** @var string */ |
||
| 67 | private $userId; |
||
| 68 | |||
| 69 | /** @var MiscService */ |
||
| 70 | private $miscService; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * ConfigService constructor. |
||
| 74 | * |
||
| 75 | * @param IConfig $config |
||
| 76 | * @param string $userId |
||
| 77 | * @param MiscService $miscService |
||
| 78 | */ |
||
| 79 | public function __construct(IConfig $config, $userId, MiscService $miscService) { |
||
| 84 | |||
| 85 | |||
| 86 | /** |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | public function getConfig() { |
||
| 99 | |||
| 100 | |||
| 101 | /** |
||
| 102 | * @param array $save |
||
| 103 | */ |
||
| 104 | public function setConfig($save) { |
||
| 113 | |||
| 114 | |||
| 115 | /** |
||
| 116 | * Get a value by key |
||
| 117 | * |
||
| 118 | * @param string $key |
||
| 119 | * |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | View Code Duplication | public function getAppValue($key) { |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Set a value by key |
||
| 133 | * |
||
| 134 | * @param string $key |
||
| 135 | * @param string $value |
||
| 136 | * |
||
| 137 | * @return void |
||
| 138 | */ |
||
| 139 | public function setAppValue($key, $value) { |
||
| 142 | |||
| 143 | /** |
||
| 144 | * remove a key |
||
| 145 | * |
||
| 146 | * @param string $key |
||
| 147 | * |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function deleteAppValue($key) { |
||
| 153 | |||
| 154 | |||
| 155 | /** |
||
| 156 | * return if option is enabled. |
||
| 157 | * |
||
| 158 | * @param $key |
||
| 159 | * |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | public function optionIsSelected($key) { |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Get a user value by key |
||
| 169 | * |
||
| 170 | * @param string $key |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | View Code Duplication | public function getUserValue($key) { |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Set a user value by key |
||
| 187 | * |
||
| 188 | * @param string $key |
||
| 189 | * @param string $value |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | * @throws PreConditionNotMetException |
||
| 193 | */ |
||
| 194 | public function setUserValue($key, $value) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get a user value by key and user |
||
| 200 | * |
||
| 201 | * @param string $userId |
||
| 202 | * @param string $key |
||
| 203 | * |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function getValueForUser($userId, $key) { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Set a user value by key |
||
| 212 | * |
||
| 213 | * @param string $userId |
||
| 214 | * @param string $key |
||
| 215 | * @param string $value |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | * @throws PreConditionNotMetException |
||
| 219 | */ |
||
| 220 | public function setValueForUser($userId, $key, $value) { |
||
| 223 | |||
| 224 | |||
| 225 | /** |
||
| 226 | * @param string $key |
||
| 227 | * |
||
| 228 | * @return mixed |
||
| 229 | */ |
||
| 230 | public function getSystemValue($key) { |
||
| 233 | |||
| 234 | |||
| 235 | /** |
||
| 236 | * @param FilesDocument $document |
||
| 237 | * @param string $option |
||
| 238 | */ |
||
| 239 | public function setDocumentIndexOption(FilesDocument $document, $option) { |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * @param Index $index |
||
| 247 | * |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | public function compareIndexOptions(Index $index) { |
||
| 261 | |||
| 262 | /** |
||
| 263 | * return the cloud version. |
||
| 264 | * if $complete is true, return a string x.y.z |
||
| 265 | * |
||
| 266 | * @param boolean $complete |
||
| 267 | * |
||
| 268 | * @return string|integer |
||
| 269 | */ |
||
| 270 | public function getCloudVersion($complete = false) { |
||
| 279 | } |
||
| 280 |
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.