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 |
||
| 35 | class Compiler extends BaseManager |
||
| 36 | { |
||
| 37 | /** @var string|false */ |
||
| 38 | private $redirectTemplate; |
||
| 39 | |||
| 40 | /** @var Twig_Template[] */ |
||
| 41 | private $templateDependencies; |
||
| 42 | |||
| 43 | /** @var PageView[] */ |
||
| 44 | private $pageViewsFlattened; |
||
| 45 | |||
| 46 | /** @var string[] */ |
||
| 47 | private $templateMapping; |
||
| 48 | |||
| 49 | /** @var PageView[][] */ |
||
| 50 | private $pageViews; |
||
| 51 | |||
| 52 | /** @var Folder */ |
||
| 53 | private $folder; |
||
| 54 | |||
| 55 | /** @var string */ |
||
| 56 | private $theme; |
||
| 57 | |||
| 58 | /** @var Twig_Environment */ |
||
| 59 | private $twig; |
||
| 60 | |||
| 61 | 14 | public function __construct() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * @param string|false $template |
||
| 71 | */ |
||
| 72 | public function setRedirectTemplate($template) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param Folder $folder |
||
| 79 | */ |
||
| 80 | 14 | public function setTargetFolder(Folder $folder) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param PageView[][] $pageViews |
||
| 87 | * @param PageView[] $pageViewsFlattened |
||
| 88 | */ |
||
| 89 | 14 | public function setPageViews(array &$pageViews, array &$pageViewsFlattened) |
|
| 94 | |||
| 95 | public function setThemeName($themeName) |
||
| 99 | |||
| 100 | /// |
||
| 101 | // Twig parent templates |
||
| 102 | /// |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Check whether a given file path is used as a parent template by a PageView |
||
| 106 | * |
||
| 107 | * @param string $filePath |
||
| 108 | * |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | public function isParentTemplate($filePath) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Rebuild all of the PageViews that used a given template as a parent |
||
| 118 | * |
||
| 119 | * @param string $filePath The file path to the parent Twig template |
||
| 120 | */ |
||
| 121 | 1 | public function refreshParent($filePath) |
|
| 128 | |||
| 129 | public function getTemplateMappings() |
||
| 133 | |||
| 134 | /// |
||
| 135 | // IO Functionality |
||
| 136 | /// |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Compile all of the PageViews registered with the compiler. |
||
| 140 | * |
||
| 141 | * @since 0.1.0 |
||
| 142 | */ |
||
| 143 | 14 | public function compileAll() |
|
| 150 | |||
| 151 | public function compileSome($filter = array()) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Compile an individual PageView item. |
||
| 165 | * |
||
| 166 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
| 167 | * respective target file. |
||
| 168 | * |
||
| 169 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
||
| 170 | * |
||
| 171 | * @since 0.1.1 |
||
| 172 | */ |
||
| 173 | 14 | public function compilePageView(&$pageView) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Write the compiled output for a static PageView. |
||
| 214 | * |
||
| 215 | * @param PageView $pageView |
||
| 216 | * |
||
| 217 | * @since 0.1.1 |
||
| 218 | */ |
||
| 219 | 10 | private function compileStaticPageView(&$pageView) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Write the compiled output for a dynamic PageView. |
||
| 230 | * |
||
| 231 | * @param DynamicPageView $pageView |
||
| 232 | * |
||
| 233 | * @since 0.1.1 |
||
| 234 | */ |
||
| 235 | 2 | private function compileDynamicPageViews(&$pageView) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Write the compiled output for a repeater PageView. |
||
| 261 | * |
||
| 262 | * @param RepeaterPageView $pageView |
||
| 263 | * |
||
| 264 | * @since 0.1.1 |
||
| 265 | */ |
||
| 266 | 2 | View Code Duplication | private function compileRepeaterPageViews(&$pageView) |
| 283 | |||
| 284 | /** |
||
| 285 | * @deprecated |
||
| 286 | * |
||
| 287 | * @todo This function needs to be rewritten or removed. Something |
||
| 288 | * |
||
| 289 | * @param ContentItem $contentItem |
||
| 290 | */ |
||
| 291 | View Code Duplication | public function compileContentItem(&$contentItem) |
|
| 304 | |||
| 305 | /// |
||
| 306 | // Redirect handling |
||
| 307 | /// |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Write redirects for standard redirects. |
||
| 311 | * |
||
| 312 | * @param PageView $pageView |
||
| 313 | * |
||
| 314 | * @since 0.1.1 |
||
| 315 | */ |
||
| 316 | 12 | private function compileStandardRedirects(&$pageView) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Write redirects for expanded redirects. |
||
| 334 | * |
||
| 335 | * @param RepeaterPageView $pageView |
||
| 336 | * |
||
| 337 | * @since 0.1.1 |
||
| 338 | */ |
||
| 339 | 2 | private function compileExpandedRedirects(&$pageView) |
|
| 361 | |||
| 362 | /// |
||
| 363 | // Twig Functionality |
||
| 364 | /// |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
| 368 | * |
||
| 369 | * @param Twig_Template $template |
||
| 370 | * @param PageView $pageView |
||
| 371 | * @param ExpandedValue $expandedValue |
||
| 372 | * |
||
| 373 | * @since 0.1.1 |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | 2 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Get the compiled HTML for a specific ContentItem. |
||
| 394 | * |
||
| 395 | * @param Twig_Template $template |
||
| 396 | * @param PageView $pageView |
||
| 397 | * @param ContentItem $contentItem |
||
| 398 | * |
||
| 399 | * @since 0.1.1 |
||
| 400 | * |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | 2 | private function renderDynamicPageView(&$template, &$pageView, &$contentItem) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Get the compiled HTML for a static PageView. |
||
| 415 | * |
||
| 416 | * @param PageView $pageView |
||
| 417 | * |
||
| 418 | * @since 0.1.1 |
||
| 419 | * |
||
| 420 | * @throws \Exception |
||
| 421 | * @throws \Throwable |
||
| 422 | * @throws Twig_Error_Syntax |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | 10 | private function renderStaticPageView(&$pageView) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Create a Twig template that just needs an array to render. |
||
| 439 | * |
||
| 440 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
| 441 | * |
||
| 442 | * @since 0.1.1 |
||
| 443 | * |
||
| 444 | * @throws \Exception |
||
| 445 | * @throws \Throwable |
||
| 446 | * @throws Twig_Error_Syntax |
||
| 447 | * |
||
| 448 | * @return Twig_Template |
||
| 449 | */ |
||
| 450 | 14 | private function createTwigTemplate(&$pageView) |
|
| 485 | } |
||
| 486 |