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 | |||
| 50 | /** @var string */ |
||
| 51 | private $theme; |
||
| 52 | |||
| 53 | /** @var Twig_Environment */ |
||
| 54 | private $twig; |
||
| 55 | |||
| 56 | 14 | public function __construct() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * @param string|false $template |
||
| 65 | */ |
||
| 66 | public function setRedirectTemplate($template) |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param Folder $folder |
||
| 73 | */ |
||
| 74 | 14 | public function setTargetFolder(Folder $folder) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @param PageView[][] $pageViews |
||
| 81 | * @param PageView[] $pageViewsFlattened |
||
| 82 | */ |
||
| 83 | 14 | public function setPageViews(array &$pageViews, array &$pageViewsFlattened) |
|
| 88 | |||
| 89 | public function setThemeName($themeName) |
||
| 93 | |||
| 94 | /// |
||
| 95 | // Twig parent templates |
||
| 96 | /// |
||
| 97 | |||
| 98 | public function isParentTemplate($filePath) |
||
| 102 | |||
| 103 | public function refreshParent($filePath) |
||
| 110 | |||
| 111 | /// |
||
| 112 | // IO Functionality |
||
| 113 | /// |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Compile all of the PageViews registered with the compiler. |
||
| 117 | * |
||
| 118 | * @since 0.1.0 |
||
| 119 | */ |
||
| 120 | 14 | public function compileAll() |
|
| 127 | |||
| 128 | public function compileSome($filter = array()) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Compile an individual PageView item. |
||
| 142 | * |
||
| 143 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
| 144 | * respective target file. |
||
| 145 | * |
||
| 146 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
||
| 147 | * |
||
| 148 | * @since 0.1.1 |
||
| 149 | */ |
||
| 150 | 14 | public function compilePageView(&$pageView) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Write the compiled output for a static PageView. |
||
| 178 | * |
||
| 179 | * @param PageView $pageView |
||
| 180 | * |
||
| 181 | * @since 0.1.1 |
||
| 182 | */ |
||
| 183 | 10 | private function compileStaticPageView(&$pageView) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Write the compiled output for a dynamic PageView. |
||
| 194 | * |
||
| 195 | * @param DynamicPageView $pageView |
||
| 196 | * |
||
| 197 | * @since 0.1.1 |
||
| 198 | */ |
||
| 199 | 2 | private function compileDynamicPageViews(&$pageView) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Write the compiled output for a repeater PageView. |
||
| 225 | * |
||
| 226 | * @param RepeaterPageView $pageView |
||
| 227 | * |
||
| 228 | * @since 0.1.1 |
||
| 229 | */ |
||
| 230 | 2 | View Code Duplication | private function compileRepeaterPageViews(&$pageView) |
| 247 | |||
| 248 | /** |
||
| 249 | * @deprecated |
||
| 250 | * |
||
| 251 | * @todo This function needs to be rewritten or removed. Something |
||
| 252 | * |
||
| 253 | * @param ContentItem $contentItem |
||
| 254 | */ |
||
| 255 | View Code Duplication | public function compileContentItem(&$contentItem) |
|
| 268 | |||
| 269 | /// |
||
| 270 | // Redirect handling |
||
| 271 | /// |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Write redirects for standard redirects. |
||
| 275 | * |
||
| 276 | * @param PageView $pageView |
||
| 277 | * |
||
| 278 | * @since 0.1.1 |
||
| 279 | */ |
||
| 280 | 1 | private function compileStandardRedirects(&$pageView) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Write redirects for expanded redirects. |
||
| 298 | * |
||
| 299 | * @param RepeaterPageView $pageView |
||
| 300 | * |
||
| 301 | * @since 0.1.1 |
||
| 302 | */ |
||
| 303 | private function compileExpandedRedirects(&$pageView) |
||
| 325 | |||
| 326 | /// |
||
| 327 | // Twig Functionality |
||
| 328 | /// |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
| 332 | * |
||
| 333 | * @param Twig_Template $template |
||
| 334 | * @param PageView $pageView |
||
| 335 | * @param ExpandedValue $expandedValue |
||
| 336 | * |
||
| 337 | * @since 0.1.1 |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get the compiled HTML for a specific ContentItem. |
||
| 358 | * |
||
| 359 | * @param Twig_Template $template |
||
| 360 | * @param PageView $pageView |
||
| 361 | * @param ContentItem $contentItem |
||
| 362 | * |
||
| 363 | * @since 0.1.1 |
||
| 364 | * |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | private function renderDynamicPageView(&$template, &$pageView, &$contentItem) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Get the compiled HTML for a static PageView. |
||
| 379 | * |
||
| 380 | * @param PageView $pageView |
||
| 381 | * |
||
| 382 | * @since 0.1.1 |
||
| 383 | * |
||
| 384 | * @throws \Exception |
||
| 385 | * @throws \Throwable |
||
| 386 | * @throws Twig_Error_Syntax |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | 10 | private function renderStaticPageView(&$pageView) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Create a Twig template that just needs an array to render. |
||
| 403 | * |
||
| 404 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
| 405 | * |
||
| 406 | * @since 0.1.1 |
||
| 407 | * |
||
| 408 | * @throws \Exception |
||
| 409 | * @throws \Throwable |
||
| 410 | * @throws Twig_Error_Syntax |
||
| 411 | * |
||
| 412 | * @return Twig_Template |
||
| 413 | */ |
||
| 414 | 14 | private function createTwigTemplate(&$pageView) |
|
| 447 | } |
||
| 448 |