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 File extends AbstractHandler |
||
| 17 | { |
||
| 18 | use UtilContainerAwareTrait; |
||
| 19 | |||
| 20 | |||
| 21 | /** |
||
| 22 | * Error in config check |
||
| 23 | * |
||
| 24 | * @var string[] |
||
| 25 | */ |
||
| 26 | protected $errorMessages = []; |
||
| 27 | |||
| 28 | |||
| 29 | /** |
||
| 30 | * Check if cache is ready for use |
||
| 31 | * |
||
| 32 | * @return boolean |
||
| 33 | */ |
||
| 34 | public function checkConfig() |
||
| 69 | |||
| 70 | |||
| 71 | /** |
||
| 72 | * Check config/cache store dir valid and writable |
||
| 73 | * |
||
| 74 | * If error, return error msg, else return empty str. |
||
| 75 | * |
||
| 76 | * @param string $dir |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | protected function checkFileDirConfig($dir) |
||
| 96 | |||
| 97 | |||
| 98 | /** |
||
| 99 | * Check cache rule exist and valid |
||
| 100 | * |
||
| 101 | * If error, return error msg, else return empty str. |
||
| 102 | * |
||
| 103 | * @param string $rule |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | View Code Duplication | protected function checkFileRuleConfig($rule) |
|
| 118 | |||
| 119 | |||
| 120 | /** |
||
| 121 | * {@inheritdoc} |
||
| 122 | */ |
||
| 123 | public function delete($key) |
||
| 136 | |||
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritdoc} |
||
| 140 | * |
||
| 141 | * File cache should check lifetime when get, return null when fail. |
||
| 142 | */ |
||
| 143 | public function get($key, $lifetime = null) |
||
| 157 | |||
| 158 | |||
| 159 | /** |
||
| 160 | * {@inheritdoc} |
||
| 161 | */ |
||
| 162 | protected function getDefaultConfigs() |
||
| 187 | |||
| 188 | |||
| 189 | /** |
||
| 190 | * @return string[] |
||
| 191 | */ |
||
| 192 | public function getErrorMessages() |
||
| 196 | |||
| 197 | |||
| 198 | /** |
||
| 199 | * Compute data file name of a key |
||
| 200 | * |
||
| 201 | * @param string $key |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | protected function getFileName($key) |
||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * Compute data file path of a key |
||
| 212 | * |
||
| 213 | * @param string $key |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | protected function getFilePath($key) |
||
| 236 | |||
| 237 | |||
| 238 | /** |
||
| 239 | * Compute path of a key by a single rule section |
||
| 240 | * |
||
| 241 | * @param string $rule |
||
| 242 | * @param string $key |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | protected function getFilePathSection($rule, $key) |
||
| 287 | |||
| 288 | |||
| 289 | /** |
||
| 290 | * {@inheritdoc} |
||
| 291 | * |
||
| 292 | * File cache does not keep lifetime in cache, so it need a lifetime from |
||
| 293 | * outside, or use default lifetime config. |
||
| 294 | */ |
||
| 295 | public function isExpired($key, $lifetime = null) |
||
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * {@inheritdoc} |
||
| 316 | */ |
||
| 317 | public function set($key, $val, $lifetime = null) |
||
| 335 | } |
||
| 336 |
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.