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() |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param string|false $template |
||
| 66 | */ |
||
| 67 | 14 | public function setRedirectTemplate($template) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param Folder $folder |
||
| 74 | */ |
||
| 75 | public function setTargetFolder(Folder $folder) |
||
| 79 | 14 | ||
| 80 | 14 | /** |
|
| 81 | * @param PageView[][] $pageViews |
||
| 82 | * @param PageView[] $pageViewsFlattened |
||
| 83 | */ |
||
| 84 | public function setPageViews(array &$pageViews, array &$pageViewsFlattened) |
||
| 89 | |||
| 90 | public function setThemeName($themeName) |
||
| 94 | |||
| 95 | 14 | /// |
|
| 96 | 14 | // Twig parent templates |
|
| 97 | 14 | /// |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Check whether a given file path is used as a parent template by a PageView |
||
| 101 | * |
||
| 102 | * @param string $filePath |
||
| 103 | * |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | public function isParentTemplate($filePath) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Rebuild all of the PageViews that used a given template as a parent |
||
| 113 | * |
||
| 114 | * @param string $filePath The file path to the parent Twig template |
||
| 115 | */ |
||
| 116 | public function refreshParent($filePath) |
||
| 123 | 14 | ||
| 124 | 14 | /// |
|
| 125 | 14 | // IO Functionality |
|
| 126 | 14 | /// |
|
| 127 | |||
| 128 | 14 | /** |
|
| 129 | * Compile all of the PageViews registered with the compiler. |
||
| 130 | 14 | * |
|
| 131 | 10 | * @since 0.1.0 |
|
| 132 | 10 | */ |
|
| 133 | 10 | public function compileAll() |
|
| 140 | 2 | ||
| 141 | 2 | public function compileSome($filter = array()) |
|
| 152 | |||
| 153 | /** |
||
| 154 | 10 | * Compile an individual PageView item. |
|
| 155 | * |
||
| 156 | 10 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
|
| 157 | 10 | * respective target file. |
|
| 158 | * |
||
| 159 | 10 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
|
| 160 | 10 | * |
|
| 161 | 10 | * @since 0.1.1 |
|
| 162 | */ |
||
| 163 | public function compilePageView(&$pageView) |
||
| 188 | |||
| 189 | 2 | /** |
|
| 190 | 2 | * Write the compiled output for a static PageView. |
|
| 191 | 2 | * |
|
| 192 | 2 | * @param PageView $pageView |
|
| 193 | * |
||
| 194 | * @since 0.1.1 |
||
| 195 | */ |
||
| 196 | private function compileStaticPageView(&$pageView) |
||
| 204 | |||
| 205 | 2 | /** |
|
| 206 | 2 | * Write the compiled output for a dynamic PageView. |
|
| 207 | * |
||
| 208 | 2 | * @param DynamicPageView $pageView |
|
| 209 | * |
||
| 210 | 2 | * @since 0.1.1 |
|
| 211 | 2 | */ |
|
| 212 | 2 | private function compileDynamicPageViews(&$pageView) |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Write the compiled output for a repeater PageView. |
||
| 238 | * |
||
| 239 | * @param RepeaterPageView $pageView |
||
| 240 | * |
||
| 241 | * @since 0.1.1 |
||
| 242 | */ |
||
| 243 | View Code Duplication | private function compileRepeaterPageViews(&$pageView) |
|
| 260 | 4 | ||
| 261 | 4 | /** |
|
| 262 | * @deprecated |
||
| 263 | 4 | * |
|
| 264 | 12 | * @todo This function needs to be rewritten or removed. Something |
|
| 265 | 12 | * |
|
| 266 | * @param ContentItem $contentItem |
||
| 267 | */ |
||
| 268 | View Code Duplication | public function compileContentItem(&$contentItem) |
|
| 281 | |||
| 282 | /// |
||
| 283 | // Redirect handling |
||
| 284 | /// |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Write redirects for standard redirects. |
||
| 288 | * |
||
| 289 | * @param PageView $pageView |
||
| 290 | * |
||
| 291 | * @since 0.1.1 |
||
| 292 | */ |
||
| 293 | private function compileStandardRedirects(&$pageView) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Write redirects for expanded redirects. |
||
| 311 | * |
||
| 312 | 2 | * @param RepeaterPageView $pageView |
|
| 313 | * |
||
| 314 | 2 | * @since 0.1.1 |
|
| 315 | */ |
||
| 316 | 2 | private function compileExpandedRedirects(&$pageView) |
|
| 338 | 2 | ||
| 339 | /// |
||
| 340 | 2 | // Twig Functionality |
|
| 341 | /// |
||
| 342 | |||
| 343 | 2 | /** |
|
| 344 | 2 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
|
| 345 | 2 | * |
|
| 346 | * @param Twig_Template $template |
||
| 347 | * @param PageView $pageView |
||
| 348 | * @param ExpandedValue $expandedValue |
||
| 349 | * |
||
| 350 | * @since 0.1.1 |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
||
| 368 | 10 | ||
| 369 | 10 | /** |
|
| 370 | * Get the compiled HTML for a specific ContentItem. |
||
| 371 | * |
||
| 372 | * @param Twig_Template $template |
||
| 373 | * @param PageView $pageView |
||
| 374 | * @param ContentItem $contentItem |
||
| 375 | * |
||
| 376 | * @since 0.1.1 |
||
| 377 | * |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | private function renderDynamicPageView(&$template, &$pageView, &$contentItem) |
||
| 389 | 14 | ||
| 390 | /** |
||
| 391 | * Get the compiled HTML for a static PageView. |
||
| 392 | * |
||
| 393 | * @param PageView $pageView |
||
| 394 | * |
||
| 395 | * @since 0.1.1 |
||
| 396 | * |
||
| 397 | * @throws \Exception |
||
| 398 | * @throws \Throwable |
||
| 399 | * @throws Twig_Error_Syntax |
||
| 400 | * |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | private function renderStaticPageView(&$pageView) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Create a Twig template that just needs an array to render. |
||
| 416 | * |
||
| 417 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
| 418 | * |
||
| 419 | * @since 0.1.1 |
||
| 420 | * |
||
| 421 | * @throws \Exception |
||
| 422 | * @throws \Throwable |
||
| 423 | * @throws Twig_Error_Syntax |
||
| 424 | * |
||
| 425 | * @return Twig_Template |
||
| 426 | */ |
||
| 427 | private function createTwigTemplate(&$pageView) |
||
| 460 | } |
||
| 461 |