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 CFileBasedContent 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 CFileBasedContent, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class CFileBasedContent |
||
| 9 | { |
||
| 10 | use \Anax\TConfigure, |
||
| 11 | \Anax\DI\TInjectionAware; |
||
| 12 | |||
| 13 | |||
| 14 | |||
| 15 | /** |
||
| 16 | * Properties. |
||
| 17 | */ |
||
| 18 | private $index = null; |
||
| 19 | private $meta = null; |
||
| 20 | private $ignoreCache = false; |
||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | /** |
||
| 25 | * Set default values from configuration. |
||
| 26 | * |
||
| 27 | * @return this. |
||
|
|
|||
| 28 | */ |
||
| 29 | public function setDefaultsFromConfiguration() |
||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | /** |
||
| 41 | * Should the cache be used or ignored. |
||
| 42 | * |
||
| 43 | * @param boolean $use true to use the cache or false to ignore the cache |
||
| 44 | * |
||
| 45 | * @return this. |
||
| 46 | */ |
||
| 47 | public function useCache($use) |
||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | /** |
||
| 57 | * Get the index as an array. |
||
| 58 | * |
||
| 59 | * @return array as index. |
||
| 60 | */ |
||
| 61 | public function getIndex() |
||
| 65 | |||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * Create the index of all content into an array. |
||
| 70 | * |
||
| 71 | * @return array as index. |
||
| 72 | */ |
||
| 73 | View Code Duplication | private function loadIndex() |
|
| 89 | |||
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * Generate an index from the directory structure. |
||
| 94 | * |
||
| 95 | * @return array as table of content. |
||
| 96 | */ |
||
| 97 | private function createIndex() |
||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * Create the index of all meta content into an array. |
||
| 139 | * |
||
| 140 | * @return array as index. |
||
| 141 | */ |
||
| 142 | View Code Duplication | private function loadMetaIndex() |
|
| 158 | |||
| 159 | |||
| 160 | |||
| 161 | /** |
||
| 162 | * Generate an index for meta files. |
||
| 163 | * |
||
| 164 | * @return array as table of content. |
||
| 165 | */ |
||
| 166 | private function createMetaIndex() |
||
| 190 | |||
| 191 | |||
| 192 | |||
| 193 | /** |
||
| 194 | * Get a reference to meta data for specific route. |
||
| 195 | * |
||
| 196 | * @param string $route current route used to access page. |
||
| 197 | * |
||
| 198 | * @return array as table of content. |
||
| 199 | */ |
||
| 200 | private function getMetaForRoute($route) |
||
| 207 | |||
| 208 | |||
| 209 | |||
| 210 | /** |
||
| 211 | * Get the title of a document. |
||
| 212 | * |
||
| 213 | * @param string $file to get title from. |
||
| 214 | * |
||
| 215 | * @return string as the title. |
||
| 216 | */ |
||
| 217 | private function getTitle($file) |
||
| 227 | |||
| 228 | |||
| 229 | |||
| 230 | /** |
||
| 231 | * Create a table of content for routes at particular level. |
||
| 232 | * |
||
| 233 | * @param string $route base route to use. |
||
| 234 | * |
||
| 235 | * @return array as the toc. |
||
| 236 | */ |
||
| 237 | private function createBaseRouteToc($route) |
||
| 254 | |||
| 255 | |||
| 256 | |||
| 257 | /** |
||
| 258 | * Map the route to the correct entry in the index. |
||
| 259 | * |
||
| 260 | * @param string $route current route used to access page. |
||
| 261 | * |
||
| 262 | * @return array as the matched route. |
||
| 263 | */ |
||
| 264 | private function mapRoute2Index($route) |
||
| 276 | |||
| 277 | |||
| 278 | |||
| 279 | /** |
||
| 280 | * Get view by mergin information from meta and frontmatter. |
||
| 281 | * |
||
| 282 | * @param string $route current route used to access page. |
||
| 283 | * @param array $frontmatter for the content. |
||
| 284 | * @param string $key for the view to retrive. |
||
| 285 | * @param string $distinct how to merge the array. |
||
| 286 | * |
||
| 287 | * @return array with data to add as view. |
||
| 288 | */ |
||
| 289 | private function getView($route, $frontmatter, $key, $distinct = true) |
||
| 310 | |||
| 311 | |||
| 312 | |||
| 313 | /** |
||
| 314 | * Get details on extra views. |
||
| 315 | * |
||
| 316 | * @param string $route current route used to access page. |
||
| 317 | * @param array $frontmatter for the content. |
||
| 318 | * |
||
| 319 | * @return array with page data to send to view. |
||
| 320 | */ |
||
| 321 | private function getViews($route, $frontmatter) |
||
| 333 | |||
| 334 | |||
| 335 | |||
| 336 | /** |
||
| 337 | * Load extra info intro views based of meta information provided in each |
||
| 338 | * view. |
||
| 339 | * |
||
| 340 | * @param string $view with current settings. |
||
| 341 | * @param string $route to load view from. |
||
| 342 | * |
||
| 343 | * @return array with view details. |
||
| 344 | */ |
||
| 345 | private function getAdditionalViewDataForRoute($view, $route) |
||
| 358 | |||
| 359 | |||
| 360 | |||
| 361 | /** |
||
| 362 | * Load extra info intro views based of meta information provided in each |
||
| 363 | * view. |
||
| 364 | * |
||
| 365 | * @param array &$views array with all views. |
||
| 366 | * |
||
| 367 | * @throws NotFoundException when mapping can not be done. |
||
| 368 | * |
||
| 369 | * @return void. |
||
| 370 | */ |
||
| 371 | private function loadAdditionalContent(&$views) |
||
| 394 | |||
| 395 | |||
| 396 | |||
| 397 | /** |
||
| 398 | * Load extra info intro views based of meta information provided in each |
||
| 399 | * view. |
||
| 400 | * |
||
| 401 | * @param string $key array with all views. |
||
| 402 | * @param string $content array with all views. |
||
| 403 | * |
||
| 404 | * @throws NotFoundException when mapping can not be done. |
||
| 405 | * |
||
| 406 | * @return void. |
||
| 407 | */ |
||
| 408 | private function loadFileContent($key, $content) |
||
| 429 | |||
| 430 | |||
| 431 | |||
| 432 | /** |
||
| 433 | * Look up the route in the index and use that to retrieve the filtered |
||
| 434 | * content. |
||
| 435 | * |
||
| 436 | * @param string $route to look up. |
||
| 437 | * |
||
| 438 | * @return array with content and filtered version. |
||
| 439 | */ |
||
| 440 | public function mapRoute2Content($route) |
||
| 448 | |||
| 449 | |||
| 450 | |||
| 451 | /** |
||
| 452 | * Map url to content if such mapping can be done. |
||
| 453 | * |
||
| 454 | * @param string $route optional route to look up. |
||
| 455 | * |
||
| 456 | * @return object with content and filtered version. |
||
| 457 | */ |
||
| 458 | public function contentForRoute($route = null) |
||
| 491 | } |
||
| 492 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.