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 |
||
| 17 | class Config |
||
| 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 | * Default data. |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected static $defaultData = [ |
||
| 43 | 'site' => [ |
||
| 44 | 'title' => 'My Webiste', |
||
| 45 | 'baseline' => 'An amazing static website!', |
||
| 46 | 'baseurl' => 'http://localhost:8000/', |
||
| 47 | 'canonicalurl' => false, |
||
| 48 | 'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', |
||
| 49 | 'taxonomies' => [ |
||
| 50 | 'tags' => 'tag', |
||
| 51 | 'categories' => 'category', |
||
| 52 | ], |
||
| 53 | 'paginate' => [ |
||
| 54 | 'max' => 5, |
||
| 55 | 'path' => 'page', |
||
| 56 | ], |
||
| 57 | 'date' => [ |
||
| 58 | 'format' => 'j F Y', |
||
| 59 | 'timezone' => 'Europe/Paris', |
||
| 60 | ], |
||
| 61 | 'fmpages' => [ |
||
| 62 | 'robotstxt' => [ |
||
| 63 | 'title' => 'Robots.txt', |
||
| 64 | 'layout' => 'robots.txt', |
||
| 65 | 'permalink' => 'robots.txt', |
||
| 66 | ], |
||
| 67 | 'sitemap' => [ |
||
| 68 | 'title' => 'XML sitemap', |
||
| 69 | 'layout' => 'sitemap.xml', |
||
| 70 | 'permalink' => 'sitemap.xml', |
||
| 71 | 'changefreq' => 'monthly', |
||
| 72 | 'priority' => '0.5', |
||
| 73 | ], |
||
| 74 | '404' => [ |
||
| 75 | 'title' => '404 page', |
||
| 76 | 'layout' => '404.html', |
||
| 77 | 'permalink' => '404.html', |
||
| 78 | ], |
||
| 79 | 'rss' => [ |
||
| 80 | 'title' => 'RSS file', |
||
| 81 | 'layout' => 'rss.xml', |
||
| 82 | 'permalink' => 'rss.xml', |
||
| 83 | 'section' => 'blog', |
||
| 84 | ], |
||
| 85 | ], |
||
| 86 | ], |
||
| 87 | 'content' => [ |
||
| 88 | 'dir' => 'content', |
||
| 89 | 'ext' => ['md', 'markdown', 'mdown', 'mkdn', 'mkd', 'text', 'txt'], |
||
| 90 | ], |
||
| 91 | 'frontmatter' => [ |
||
| 92 | 'format' => 'yaml', |
||
| 93 | ], |
||
| 94 | 'body' => [ |
||
| 95 | 'format' => 'md', |
||
| 96 | ], |
||
| 97 | 'static' => [ |
||
| 98 | 'dir' => 'static', |
||
| 99 | ], |
||
| 100 | 'layouts' => [ |
||
| 101 | 'dir' => 'layouts', |
||
| 102 | 'internal' => [ |
||
| 103 | 'dir' => 'res/layouts', |
||
| 104 | ], |
||
| 105 | ], |
||
| 106 | 'output' => [ |
||
| 107 | 'dir' => '_site', |
||
| 108 | 'filename' => 'index.html', |
||
| 109 | ], |
||
| 110 | 'themes' => [ |
||
| 111 | 'dir' => 'themes', |
||
| 112 | ], |
||
| 113 | 'generators' => [ |
||
| 114 | 10 => 'Cecil\Generator\Section', |
||
| 115 | 20 => 'Cecil\Generator\Taxonomy', |
||
| 116 | 30 => 'Cecil\Generator\Homepage', |
||
| 117 | 40 => 'Cecil\Generator\Pagination', |
||
| 118 | 50 => 'Cecil\Generator\Alias', |
||
| 119 | 35 => 'Cecil\Generator\ExternalBody', |
||
| 120 | 36 => 'Cecil\Generator\PagesFromConfig', |
||
| 121 | 60 => 'Cecil\Generator\Redirect', |
||
| 122 | ], |
||
| 123 | ]; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Config constructor. |
||
| 127 | * |
||
| 128 | * @param Config|array|null $config |
||
| 129 | */ |
||
| 130 | public function __construct($config = null) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Import array config to current config. |
||
| 168 | * |
||
| 169 | * @param array $config |
||
| 170 | */ |
||
| 171 | public function import($config) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Set config data. |
||
| 184 | * |
||
| 185 | * @param Data $data |
||
| 186 | * |
||
| 187 | * @return $this |
||
| 188 | */ |
||
| 189 | protected function setFromData(Data $data) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Get config data. |
||
| 200 | * |
||
| 201 | * @return Data |
||
| 202 | */ |
||
| 203 | public function getAll() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get data as array. |
||
| 210 | * |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | public function getAllAsArray() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Return a config value. |
||
| 220 | * |
||
| 221 | * @param string $key |
||
| 222 | * @param string $default |
||
| 223 | * |
||
| 224 | * @return array|mixed|null |
||
| 225 | */ |
||
| 226 | public function get($key, $default = '') |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Set source directory. |
||
| 233 | * |
||
| 234 | * @param null $sourceDir |
||
| 235 | * |
||
| 236 | * @throws Exception |
||
| 237 | * |
||
| 238 | * @return $this |
||
| 239 | */ |
||
| 240 | View Code Duplication | public function setSourceDir($sourceDir = null) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Get source directory. |
||
| 255 | * |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | public function getSourceDir() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Set destination directory. |
||
| 265 | * |
||
| 266 | * @param null $destinationDir |
||
| 267 | * |
||
| 268 | * @throws Exception |
||
| 269 | * |
||
| 270 | * @return $this |
||
| 271 | */ |
||
| 272 | View Code Duplication | public function setDestinationDir($destinationDir = null) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Get destination directory. |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | public function getDestinationDir() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Path helpers. |
||
| 297 | */ |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Return content directory path. |
||
| 301 | * |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | public function getContentPath() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Return templates directory path. |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | public function getLayoutsPath() |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Return themes directory path. |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function getThemesPath() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Return internal templates directory path. |
||
| 331 | * |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | public function getInternalLayoutsPath() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Return output directory path. |
||
| 341 | * |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | public function getOutputPath() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Return static files directory path. |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | public function getStaticPath() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Themes helpers. |
||
| 361 | */ |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Return theme(s). |
||
| 365 | * |
||
| 366 | * @return array|null |
||
| 367 | */ |
||
| 368 | public function getTheme() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Has a (valid) theme(s)? |
||
| 381 | * |
||
| 382 | * @throws Exception |
||
| 383 | * |
||
| 384 | * @return bool |
||
| 385 | */ |
||
| 386 | public function hasTheme() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Return the path of a specific theme's directory. |
||
| 407 | * |
||
| 408 | * @param string $theme |
||
| 409 | * @param string $dir |
||
| 410 | * |
||
| 411 | * @return string |
||
| 412 | */ |
||
| 413 | public function getThemeDirPath($theme, $dir = 'layouts') |
||
| 417 | } |
||
| 418 |