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 |
||
| 47 | class ConfigService { |
||
| 48 | |||
| 49 | const FILES_LOCAL = 'files_local'; |
||
| 50 | const FILES_EXTERNAL = 'files_external'; |
||
| 51 | const FILES_GROUP_FOLDERS = 'files_group_folders'; |
||
| 52 | const FILES_EXCLUDED = 'files_excluded'; |
||
| 53 | const FILES_ENCRYPTED = 'files_encrypted'; |
||
| 54 | const FILES_FEDERATED = 'files_federated'; |
||
| 55 | const FILES_SIZE = 'files_size'; |
||
| 56 | const FILES_OFFICE = 'files_office'; |
||
| 57 | const FILES_PDF = 'files_pdf'; |
||
| 58 | const FILES_ZIP = 'files_zip'; |
||
| 59 | const FILES_IMAGE = 'files_image'; |
||
| 60 | const FILES_AUDIO = 'files_audio'; |
||
| 61 | |||
| 62 | public $defaults = [ |
||
| 63 | self::FILES_LOCAL => '1', |
||
| 64 | self::FILES_EXTERNAL => '0', |
||
| 65 | self::FILES_GROUP_FOLDERS => '0', |
||
| 66 | self::FILES_EXCLUDED => '', |
||
| 67 | self::FILES_ENCRYPTED => '0', |
||
| 68 | self::FILES_FEDERATED => '0', |
||
| 69 | self::FILES_SIZE => '20', |
||
| 70 | self::FILES_PDF => '1', |
||
| 71 | self::FILES_OFFICE => '1', |
||
| 72 | self::FILES_IMAGE => '0', |
||
| 73 | self::FILES_AUDIO => '0' |
||
| 74 | ]; |
||
| 75 | |||
| 76 | |||
| 77 | /** @var IConfig */ |
||
| 78 | private $config; |
||
| 79 | |||
| 80 | /** @var string */ |
||
| 81 | private $userId; |
||
| 82 | |||
| 83 | /** @var MiscService */ |
||
| 84 | private $miscService; |
||
| 85 | |||
| 86 | /** @var array */ |
||
| 87 | private $excludedFilesCache; |
||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * ConfigService constructor. |
||
| 92 | * |
||
| 93 | * @param IConfig $config |
||
| 94 | * @param string $userId |
||
| 95 | * @param MiscService $miscService |
||
| 96 | */ |
||
| 97 | public function __construct(IConfig $config, $userId, MiscService $miscService) { |
||
| 98 | $this->config = $config; |
||
| 99 | $this->userId = $userId; |
||
| 100 | $this->miscService = $miscService; |
||
| 101 | } |
||
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | public function getConfig(): array { |
||
| 108 | $keys = array_keys($this->defaults); |
||
| 109 | $data = []; |
||
| 110 | |||
| 111 | foreach ($keys as $k) { |
||
| 112 | $data[$k] = $this->getAppValue($k); |
||
| 113 | } |
||
| 114 | |||
| 115 | return $data; |
||
| 116 | } |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * @param array $save |
||
| 121 | */ |
||
| 122 | public function setConfig(array $save) { |
||
| 123 | $keys = array_keys($this->defaults); |
||
| 124 | |||
| 125 | foreach ($keys as $k) { |
||
| 126 | if (array_key_exists($k, $save)) { |
||
| 127 | $this->setAppValue($k, $save[$k]); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | |||
| 133 | /** |
||
| 134 | * Get a value by key |
||
| 135 | * |
||
| 136 | * @param string $key |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function getAppValue(string $key): string { |
|
|
|
|||
| 141 | $defaultValue = null; |
||
| 142 | if (array_key_exists($key, $this->defaults)) { |
||
| 143 | $defaultValue = $this->defaults[$key]; |
||
| 144 | } |
||
| 145 | |||
| 146 | return (string)$this->config->getAppValue(Application::APP_NAME, $key, $defaultValue); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Set a value by key |
||
| 151 | * |
||
| 152 | * @param string $key |
||
| 153 | * @param string $value |
||
| 154 | */ |
||
| 155 | public function setAppValue(string $key, string $value) { |
||
| 156 | $this->config->setAppValue(Application::APP_NAME, $key, $value); |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * remove a key |
||
| 161 | * |
||
| 162 | * @param string $key |
||
| 163 | */ |
||
| 164 | public function deleteAppValue(string $key) { |
||
| 165 | $this->config->deleteAppValue(Application::APP_NAME, $key); |
||
| 166 | } |
||
| 167 | |||
| 168 | |||
| 169 | /** |
||
| 170 | * return if option is enabled. |
||
| 171 | * |
||
| 172 | * @param string $key |
||
| 173 | * |
||
| 174 | * @return bool |
||
| 175 | */ |
||
| 176 | public function optionIsSelected(string $key): bool { |
||
| 177 | return ($this->getAppValue($key) === '1'); |
||
| 178 | } |
||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * Get a user value by key |
||
| 183 | * |
||
| 184 | * @param string $key |
||
| 185 | * |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | View Code Duplication | public function getUserValue(string $key): string { |
|
| 189 | $defaultValue = null; |
||
| 190 | if (array_key_exists($key, $this->defaults)) { |
||
| 191 | $defaultValue = $this->defaults[$key]; |
||
| 192 | } |
||
| 193 | |||
| 194 | return $this->config->getUserValue( |
||
| 195 | $this->userId, Application::APP_NAME, $key, $defaultValue |
||
| 196 | ); |
||
| 197 | } |
||
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * Get a user value by key and user |
||
| 202 | * |
||
| 203 | * @param string $userId |
||
| 204 | * @param string $key |
||
| 205 | * |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | public function getValueForUser(string $userId, string $key): string { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Set a user value by key |
||
| 214 | * |
||
| 215 | * @param string $userId |
||
| 216 | * @param string $key |
||
| 217 | * @param string $value |
||
| 218 | * |
||
| 219 | * @throws PreConditionNotMetException |
||
| 220 | */ |
||
| 221 | public function setValueForUser(string $userId, string $key, string $value) { |
||
| 224 | |||
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $key |
||
| 228 | * |
||
| 229 | * @param string $default |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function getSystemValue(string $key, string $default = ''): string { |
||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * @param FilesDocument $document |
||
| 240 | * @param string $option |
||
| 241 | */ |
||
| 242 | public function setDocumentIndexOption(FilesDocument $document, string $option) { |
||
| 246 | |||
| 247 | |||
| 248 | /** |
||
| 249 | * @param IIndex $index |
||
| 250 | * |
||
| 251 | * @return bool |
||
| 252 | */ |
||
| 253 | public function compareIndexOptions(IIndex $index): bool { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * return the cloud version. |
||
| 269 | * |
||
| 270 | * @return int |
||
| 271 | */ |
||
| 272 | public function getFullCloudVersion(): int { |
||
| 277 | |||
| 278 | |||
| 279 | /** |
||
| 280 | * @param $major |
||
| 281 | * @param $sub |
||
| 282 | * @param $minor |
||
| 283 | * |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | public function isCloudVersionAtLeast($major, $sub, $minor): bool { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Check if the path is excluded via configuration |
||
| 296 | * and therefore the file shouldn't be indexed |
||
| 297 | * |
||
| 298 | * @param $filePath The file to be indexed |
||
| 299 | * |
||
| 300 | * @return bool |
||
| 301 | */ |
||
| 302 | public function isPathExcluded($filePath): bool { |
||
| 312 | |||
| 313 | private function getExcludedPaths(): array { |
||
| 325 | |||
| 326 | private function isPathMatch($filePath, $excludePath): bool { |
||
| 345 | } |
||
| 346 | |||
| 347 |
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.