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 |
||
| 33 | class Compiler extends BaseManager |
||
| 34 | { |
||
| 35 | /** @var string|false */ |
||
| 36 | private $redirectTemplate; |
||
| 37 | |||
| 38 | /** @var Twig_Template[] */ |
||
| 39 | private $templateDependencies; |
||
| 40 | |||
| 41 | /** @var PageView[] */ |
||
| 42 | private $pageViewsFlattened; |
||
| 43 | |||
| 44 | /** @var string[] */ |
||
| 45 | private $templateMapping; |
||
| 46 | |||
| 47 | /** @var PageView[][] */ |
||
| 48 | private $pageViews; |
||
| 49 | |||
| 50 | /** @var Folder */ |
||
| 51 | private $folder; |
||
| 52 | |||
| 53 | /** @var string */ |
||
| 54 | private $theme; |
||
| 55 | |||
| 56 | /** @var Twig_Environment */ |
||
| 57 | private $twig; |
||
| 58 | |||
| 59 | 14 | public function __construct() |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @param string|false $template |
||
| 69 | */ |
||
| 70 | public function setRedirectTemplate($template) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param Folder $folder |
||
| 77 | */ |
||
| 78 | 14 | public function setTargetFolder(Folder $folder) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * @param PageView[][] $pageViews |
||
| 85 | * @param PageView[] $pageViewsFlattened |
||
| 86 | */ |
||
| 87 | 14 | public function setPageViews(array &$pageViews, array &$pageViewsFlattened) |
|
| 92 | |||
| 93 | public function setThemeName($themeName) |
||
| 97 | |||
| 98 | /// |
||
| 99 | // Twig parent templates |
||
| 100 | /// |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Check whether a given file path is used as a parent template by a PageView |
||
| 104 | * |
||
| 105 | * @param string $filePath |
||
| 106 | * |
||
| 107 | * @return bool |
||
| 108 | */ |
||
| 109 | public function isParentTemplate($filePath) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Rebuild all of the PageViews that used a given template as a parent |
||
| 116 | * |
||
| 117 | * @param string $filePath The file path to the parent Twig template |
||
| 118 | */ |
||
| 119 | 1 | public function refreshParent($filePath) |
|
| 126 | |||
| 127 | public function getTemplateMappings() |
||
| 131 | |||
| 132 | /// |
||
| 133 | // IO Functionality |
||
| 134 | /// |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Compile all of the PageViews registered with the compiler. |
||
| 138 | * |
||
| 139 | * @since 0.1.0 |
||
| 140 | */ |
||
| 141 | 14 | public function compileAll() |
|
| 148 | |||
| 149 | public function compileSome($filter = array()) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Compile an individual PageView item. |
||
| 163 | * |
||
| 164 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
| 165 | * respective target file. |
||
| 166 | * |
||
| 167 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
||
| 168 | * |
||
| 169 | * @since 0.1.1 |
||
| 170 | */ |
||
| 171 | 14 | public function compilePageView(&$pageView) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Write the compiled output for a static PageView. |
||
| 199 | * |
||
| 200 | * @param PageView $pageView |
||
| 201 | * |
||
| 202 | * @since 0.1.1 |
||
| 203 | */ |
||
| 204 | 10 | private function compileStaticPageView(&$pageView) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Write the compiled output for a dynamic PageView. |
||
| 215 | * |
||
| 216 | * @param DynamicPageView $pageView |
||
| 217 | * |
||
| 218 | * @since 0.1.1 |
||
| 219 | */ |
||
| 220 | 2 | private function compileDynamicPageViews(&$pageView) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Write the compiled output for a repeater PageView. |
||
| 246 | * |
||
| 247 | * @param RepeaterPageView $pageView |
||
| 248 | * |
||
| 249 | * @since 0.1.1 |
||
| 250 | */ |
||
| 251 | 2 | View Code Duplication | private function compileRepeaterPageViews(&$pageView) |
| 268 | |||
| 269 | /** |
||
| 270 | * @deprecated |
||
| 271 | * |
||
| 272 | * @todo This function needs to be rewritten or removed. Something |
||
| 273 | * |
||
| 274 | * @param ContentItem $contentItem |
||
| 275 | */ |
||
| 276 | View Code Duplication | public function compileContentItem(&$contentItem) |
|
| 289 | |||
| 290 | /// |
||
| 291 | // Redirect handling |
||
| 292 | /// |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Write redirects for standard redirects. |
||
| 296 | * |
||
| 297 | * @param PageView $pageView |
||
| 298 | * |
||
| 299 | * @since 0.1.1 |
||
| 300 | */ |
||
| 301 | 12 | private function compileStandardRedirects(&$pageView) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Write redirects for expanded redirects. |
||
| 319 | * |
||
| 320 | * @param RepeaterPageView $pageView |
||
| 321 | * |
||
| 322 | * @since 0.1.1 |
||
| 323 | */ |
||
| 324 | 2 | private function compileExpandedRedirects(&$pageView) |
|
| 346 | |||
| 347 | /// |
||
| 348 | // Twig Functionality |
||
| 349 | /// |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
| 353 | * |
||
| 354 | * @param Twig_Template $template |
||
| 355 | * @param PageView $pageView |
||
| 356 | * @param ExpandedValue $expandedValue |
||
| 357 | * |
||
| 358 | * @since 0.1.1 |
||
| 359 | * |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | 2 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Get the compiled HTML for a specific ContentItem. |
||
| 379 | * |
||
| 380 | * @param Twig_Template $template |
||
| 381 | * @param PageView $pageView |
||
| 382 | * @param ContentItem $contentItem |
||
| 383 | * |
||
| 384 | * @since 0.1.1 |
||
| 385 | * |
||
| 386 | * @return string |
||
| 387 | */ |
||
| 388 | 2 | private function renderDynamicPageView(&$template, &$pageView, &$contentItem) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Get the compiled HTML for a static PageView. |
||
| 400 | * |
||
| 401 | * @param PageView $pageView |
||
| 402 | * |
||
| 403 | * @since 0.1.1 |
||
| 404 | * |
||
| 405 | * @throws \Exception |
||
| 406 | * @throws \Throwable |
||
| 407 | * @throws Twig_Error_Syntax |
||
| 408 | * |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | 10 | private function renderStaticPageView(&$pageView) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Create a Twig template that just needs an array to render. |
||
| 424 | * |
||
| 425 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
| 426 | * |
||
| 427 | * @since 0.1.1 |
||
| 428 | * |
||
| 429 | * @throws \Exception |
||
| 430 | * @throws \Throwable |
||
| 431 | * @throws Twig_Error_Syntax |
||
| 432 | * |
||
| 433 | * @return Twig_Template |
||
| 434 | */ |
||
| 435 | 14 | private function createTwigTemplate(&$pageView) |
|
| 470 | } |
||
| 471 |