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:
Complex classes like Config often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Config, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Config implements \ArrayAccess |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Config. |
||
| 21 | * |
||
| 22 | * @var Data |
||
| 23 | */ |
||
| 24 | protected $data; |
||
| 25 | /** |
||
| 26 | * Source directory. |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $sourceDir; |
||
| 31 | /** |
||
| 32 | * Destination directory. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $destinationDir; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Config constructor. |
||
| 40 | * |
||
| 41 | * @param Config|array|null $config |
||
| 42 | */ |
||
| 43 | public function __construct($config = null) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Import array config to current config. |
||
| 81 | * |
||
| 82 | * @param array $config |
||
| 83 | */ |
||
| 84 | public function import($config) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Set config data. |
||
| 97 | * |
||
| 98 | * @param Data $data |
||
| 99 | * |
||
| 100 | * @return $this |
||
| 101 | */ |
||
| 102 | protected function setFromData(Data $data) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get config data. |
||
| 113 | * |
||
| 114 | * @return Data |
||
| 115 | */ |
||
| 116 | public function getAll() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get data as array. |
||
| 123 | * |
||
| 124 | * @return array |
||
| 125 | */ |
||
| 126 | public function getAllAsArray() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Return a config value. |
||
| 133 | * |
||
| 134 | * @param string $key |
||
| 135 | * @param string $default |
||
| 136 | * |
||
| 137 | * @return array|mixed|null |
||
| 138 | */ |
||
| 139 | public function get($key, $default = '') |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Implement ArrayAccess. |
||
| 146 | * |
||
| 147 | * @param mixed $offset |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | public function offsetExists($offset) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Implement ArrayAccess. |
||
| 158 | * |
||
| 159 | * @param mixed $offset |
||
| 160 | * |
||
| 161 | * @return null |
||
| 162 | */ |
||
| 163 | public function offsetGet($offset) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Implement ArrayAccess. |
||
| 170 | * |
||
| 171 | * @param mixed $offset |
||
| 172 | * @param mixed $value |
||
| 173 | */ |
||
| 174 | public function offsetSet($offset, $value) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Implement ArrayAccess. |
||
| 181 | * |
||
| 182 | * @param mixed $offset |
||
| 183 | */ |
||
| 184 | public function offsetUnset($offset) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Set source directory. |
||
| 191 | * |
||
| 192 | * @param null $sourceDir |
||
| 193 | * |
||
| 194 | * @throws Exception |
||
| 195 | * |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | View Code Duplication | public function setSourceDir($sourceDir = null) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Get source directory. |
||
| 213 | * |
||
| 214 | * @return string |
||
| 215 | */ |
||
| 216 | public function getSourceDir() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Set destination directory. |
||
| 223 | * |
||
| 224 | * @param null $destinationDir |
||
| 225 | * |
||
| 226 | * @throws Exception |
||
| 227 | * |
||
| 228 | * @return $this |
||
| 229 | */ |
||
| 230 | View Code Duplication | public function setDestinationDir($destinationDir = null) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Get destination directory. |
||
| 245 | * |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | public function getDestinationDir() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Path helpers. |
||
| 255 | */ |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Return content directory path. |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public function getContentPath() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Return templates directory path. |
||
| 269 | * |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | public function getLayoutsPath() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Return themes directory path. |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public function getThemesPath() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Return internal templates directory path. |
||
| 289 | * |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | public function getInternalLayoutsPath() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Return output directory path. |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | */ |
||
| 302 | public function getOutputPath() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Return static files directory path. |
||
| 309 | * |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | public function getStaticPath() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Themes helpers. |
||
| 319 | */ |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Return theme(s). |
||
| 323 | * |
||
| 324 | * @return array|null |
||
| 325 | */ |
||
| 326 | public function getTheme() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Has a (valid) theme(s)? |
||
| 339 | * |
||
| 340 | * @throws Exception |
||
| 341 | * |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | public function hasTheme() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Return the path of a specific theme's directory. |
||
| 365 | * |
||
| 366 | * @param string $theme |
||
| 367 | * @param string $dir |
||
| 368 | * |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | public function getThemeDirPath($theme, $dir = 'layouts') |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Return "clean" array output format array. |
||
| 378 | * |
||
| 379 | * @param string $format |
||
| 380 | * |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | public function getOutputFormat(string $format): array |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Return available languages. |
||
| 399 | * |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | public function getLanguages(): array |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Return default language key (ie: "en", "fr-fr", etc.). |
||
| 409 | * |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | public function getLanguageDefaultKey(): string |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Return (specified or default) language properties. |
||
| 429 | * |
||
| 430 | * @return array |
||
| 431 | */ |
||
| 432 | public function getLanguageProperties($key = null): array |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Return (specified or default) language property value. |
||
| 446 | * |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | public function getLanguageProperty($property, $key = null): string |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Language helper: return language key. |
||
| 473 | */ |
||
| 474 | public function getLang(): string |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Language helper: return language name. |
||
| 481 | */ |
||
| 482 | public function getLanguage(): string |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Language helper: return language locale. |
||
| 489 | */ |
||
| 490 | public function getLocale(): string |
||
| 494 | } |
||
| 495 |