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 PageView[][] */ |
||
| 45 | private $pageViews; |
||
| 46 | |||
| 47 | /** @var Folder */ |
||
| 48 | private $folder; |
||
| 49 | 14 | ||
| 50 | /** @var string */ |
||
| 51 | 14 | private $theme; |
|
| 52 | |||
| 53 | 14 | /** @var Twig_Environment */ |
|
| 54 | 14 | private $twig; |
|
| 55 | |||
| 56 | public function __construct() |
||
| 57 | { |
||
| 58 | parent::__construct(); |
||
| 59 | |||
| 60 | $this->twig = TwigManager::getInstance(); |
||
| 61 | $this->theme = ''; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param string|false $template |
||
| 66 | */ |
||
| 67 | 14 | public function setRedirectTemplate($template) |
|
| 68 | { |
||
| 69 | 14 | $this->redirectTemplate = $template; |
|
| 70 | 14 | } |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param Folder $folder |
||
| 74 | */ |
||
| 75 | public function setTargetFolder(Folder $folder) |
||
| 76 | 14 | { |
|
| 77 | $this->folder = $folder; |
||
| 78 | 14 | } |
|
| 79 | 14 | ||
| 80 | 14 | /** |
|
| 81 | * @param PageView[][] $pageViews |
||
| 82 | * @param PageView[] $pageViewsFlattened |
||
| 83 | */ |
||
| 84 | public function setPageViews(array &$pageViews, array &$pageViewsFlattened) |
||
| 85 | { |
||
| 86 | $this->pageViews = &$pageViews; |
||
| 87 | $this->pageViewsFlattened = &$pageViewsFlattened; |
||
| 88 | } |
||
| 89 | |||
| 90 | public function setThemeName($themeName) |
||
| 91 | 14 | { |
|
| 92 | $this->theme = $themeName; |
||
| 93 | 14 | } |
|
| 94 | |||
| 95 | 14 | /// |
|
| 96 | 14 | // Twig parent templates |
|
| 97 | 14 | /// |
|
| 98 | |||
| 99 | public function isParentTemplate($filePath) |
||
| 103 | |||
| 104 | public function refreshParent($filePath) |
||
| 105 | { |
||
| 106 | foreach ($this->templateDependencies[$filePath] as &$parentTemplate) |
||
|
|
|||
| 107 | { |
||
| 108 | $this->compilePageView($parentTemplate); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | /// |
||
| 113 | // IO Functionality |
||
| 114 | /// |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Compile all of the PageViews registered with the compiler. |
||
| 118 | * |
||
| 119 | * @since 0.1.0 |
||
| 120 | */ |
||
| 121 | 14 | public function compileAll() |
|
| 128 | 14 | ||
| 129 | public function compileSome($filter = array()) |
||
| 130 | 14 | { |
|
| 131 | 10 | /** @var PageView $pageView */ |
|
| 132 | 10 | foreach ($this->pageViewsFlattened as &$pageView) |
|
| 133 | 10 | { |
|
| 134 | if ($pageView->hasTwigDependency($filter['namespace'], $filter['dependency'])) |
||
| 140 | 2 | ||
| 141 | 2 | /** |
|
| 142 | 2 | * Compile an individual PageView item. |
|
| 143 | 2 | * |
|
| 144 | 14 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
|
| 145 | 14 | * respective target file. |
|
| 146 | * |
||
| 147 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
||
| 148 | * |
||
| 149 | * @since 0.1.1 |
||
| 150 | */ |
||
| 151 | public function compilePageView(&$pageView) |
||
| 176 | |||
| 177 | 2 | /** |
|
| 178 | 2 | * Write the compiled output for a static PageView. |
|
| 179 | 1 | * |
|
| 180 | 1 | * @param PageView $pageView |
|
| 181 | 1 | * |
|
| 182 | * @since 0.1.1 |
||
| 183 | 1 | */ |
|
| 184 | private function compileStaticPageView(&$pageView) |
||
| 192 | 2 | ||
| 193 | /** |
||
| 194 | * Write the compiled output for a dynamic PageView. |
||
| 195 | * |
||
| 196 | * @param DynamicPageView $pageView |
||
| 197 | * |
||
| 198 | * @since 0.1.1 |
||
| 199 | */ |
||
| 200 | private function compileDynamicPageViews(&$pageView) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Write the compiled output for a repeater PageView. |
||
| 226 | * |
||
| 227 | * @param RepeaterPageView $pageView |
||
| 228 | * |
||
| 229 | * @since 0.1.1 |
||
| 230 | */ |
||
| 231 | View Code Duplication | private function compileRepeaterPageViews(&$pageView) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @deprecated |
||
| 251 | 12 | * |
|
| 252 | * @todo This function needs to be rewritten or removed. Something |
||
| 253 | 12 | * |
|
| 254 | * @param ContentItem $contentItem |
||
| 255 | 12 | */ |
|
| 256 | View Code Duplication | public function compileContentItem(&$contentItem) |
|
| 269 | |||
| 270 | /// |
||
| 271 | // Redirect handling |
||
| 272 | /// |
||
| 273 | |||
| 274 | 2 | /** |
|
| 275 | * Write redirects for standard redirects. |
||
| 276 | 2 | * |
|
| 277 | * @param PageView $pageView |
||
| 278 | * |
||
| 279 | 2 | * @since 0.1.1 |
|
| 280 | */ |
||
| 281 | private function compileStandardRedirects(&$pageView) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Write redirects for expanded redirects. |
||
| 299 | * |
||
| 300 | * @param RepeaterPageView $pageView |
||
| 301 | * |
||
| 302 | * @since 0.1.1 |
||
| 303 | */ |
||
| 304 | private function compileExpandedRedirects(&$pageView) |
||
| 326 | |||
| 327 | /// |
||
| 328 | // Twig Functionality |
||
| 329 | /// |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
| 333 | * |
||
| 334 | * @param Twig_Template $template |
||
| 335 | * @param PageView $pageView |
||
| 336 | * @param ExpandedValue $expandedValue |
||
| 337 | * |
||
| 338 | 2 | * @since 0.1.1 |
|
| 339 | * |
||
| 340 | 2 | * @return string |
|
| 341 | */ |
||
| 342 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Get the compiled HTML for a specific ContentItem. |
||
| 359 | * |
||
| 360 | * @param Twig_Template $template |
||
| 361 | 10 | * @param PageView $pageView |
|
| 362 | * @param ContentItem $contentItem |
||
| 363 | 10 | * |
|
| 364 | * @since 0.1.1 |
||
| 365 | 10 | * |
|
| 366 | 10 | * @return string |
|
| 367 | 10 | */ |
|
| 368 | 10 | private function renderDynamicPageView(&$template, &$pageView, &$contentItem) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Get the compiled HTML for a static PageView. |
||
| 380 | * |
||
| 381 | * @param PageView $pageView |
||
| 382 | * |
||
| 383 | * @since 0.1.1 |
||
| 384 | * |
||
| 385 | 14 | * @throws \Exception |
|
| 386 | * @throws \Throwable |
||
| 387 | * @throws Twig_Error_Syntax |
||
| 388 | * |
||
| 389 | 14 | * @return string |
|
| 390 | */ |
||
| 391 | private function renderStaticPageView(&$pageView) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Create a Twig template that just needs an array to render. |
||
| 404 | * |
||
| 405 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
| 406 | * |
||
| 407 | * @since 0.1.1 |
||
| 408 | * |
||
| 409 | * @throws \Exception |
||
| 410 | * @throws \Throwable |
||
| 411 | * @throws Twig_Error_Syntax |
||
| 412 | * |
||
| 413 | * @return Twig_Template |
||
| 414 | */ |
||
| 415 | private function createTwigTemplate(&$pageView) |
||
| 448 | } |
||
| 449 |