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 |
||
| 31 | class Compiler extends BaseManager |
||
| 32 | { |
||
| 33 | /** @var string|false */ |
||
| 34 | private $redirectTemplate; |
||
| 35 | |||
| 36 | /** @var PageView[] */ |
||
| 37 | private $pageViews; |
||
| 38 | |||
| 39 | /** @var Folder */ |
||
| 40 | private $folder; |
||
| 41 | |||
| 42 | /** @var Twig_Environment */ |
||
| 43 | private $twig; |
||
| 44 | |||
| 45 | 9 | public function __construct() |
|
| 51 | |||
| 52 | /** |
||
| 53 | * @param string|false $template |
||
| 54 | */ |
||
| 55 | public function setRedirectTemplate($template) |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param Folder $folder |
||
| 62 | */ |
||
| 63 | 9 | public function setTargetFolder(Folder $folder) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @param PageView[] $pageViews |
||
| 70 | */ |
||
| 71 | 9 | public function setPageViews(array &$pageViews) |
|
| 75 | |||
| 76 | /// |
||
| 77 | // IO Functionality |
||
| 78 | /// |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Compile all of the PageViews registered with the compiler. |
||
| 82 | * |
||
| 83 | * @since 0.1.0 |
||
| 84 | */ |
||
| 85 | 9 | public function compileAll() |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Compile an individual PageView item. |
||
| 100 | * |
||
| 101 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
| 102 | * respective target file. |
||
| 103 | * |
||
| 104 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
||
| 105 | * |
||
| 106 | * @since 0.1.1 |
||
| 107 | */ |
||
| 108 | 9 | private function compilePageView(&$pageView) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Write the compiled output for a static PageView. |
||
| 131 | * |
||
| 132 | * @param PageView $pageView |
||
| 133 | * |
||
| 134 | * @since 0.1.1 |
||
| 135 | */ |
||
| 136 | 9 | private function compileStaticPageView(&$pageView) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Write the compiled output for a dynamic PageView. |
||
| 147 | * |
||
| 148 | * @param DynamicPageView $pageView |
||
| 149 | * |
||
| 150 | * @since 0.1.1 |
||
| 151 | */ |
||
| 152 | View Code Duplication | private function compileDynamicPageViews(&$pageView) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Write the compiled output for a repeater PageView. |
||
| 169 | * |
||
| 170 | * @param RepeaterPageView $pageView |
||
| 171 | * |
||
| 172 | * @since 0.1.1 |
||
| 173 | */ |
||
| 174 | View Code Duplication | private function compileRepeaterPageViews(&$pageView) |
|
| 191 | |||
| 192 | /// |
||
| 193 | // Redirect handling |
||
| 194 | /// |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Write redirects for standard redirects. |
||
| 198 | * |
||
| 199 | * @param PageView $pageView |
||
| 200 | * |
||
| 201 | * @since 0.1.1 |
||
| 202 | */ |
||
| 203 | 9 | private function compileStandardRedirects(&$pageView) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Write redirects for expanded redirects. |
||
| 221 | * |
||
| 222 | * @param RepeaterPageView $pageView |
||
| 223 | * |
||
| 224 | * @since 0.1.1 |
||
| 225 | */ |
||
| 226 | private function compileExpandedRedirects(&$pageView) |
||
| 248 | |||
| 249 | /// |
||
| 250 | // Twig Functionality |
||
| 251 | /// |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
| 255 | * |
||
| 256 | * @param Twig_Template $template |
||
| 257 | * @param PageView $pageView |
||
| 258 | * @param ExpandedValue $expandedValue |
||
| 259 | * |
||
| 260 | * @since 0.1.1 |
||
| 261 | * |
||
| 262 | * @return string |
||
| 263 | */ |
||
| 264 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Get the compiled HTML for a specific ContentItem. |
||
| 281 | * |
||
| 282 | * @param Twig_Template $template |
||
| 283 | * @param PageView $pageView |
||
| 284 | * @param ContentItem $contentItem |
||
| 285 | * |
||
| 286 | * @since 0.1.1 |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | private function renderDynamicPageView(&$template, &$pageView, &$contentItem) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get the compiled HTML for a static PageView. |
||
| 302 | * |
||
| 303 | * @param PageView $pageView |
||
| 304 | * |
||
| 305 | * @since 0.1.1 |
||
| 306 | * |
||
| 307 | * @throws \Exception |
||
| 308 | * @throws \Throwable |
||
| 309 | * @throws Twig_Error_Syntax |
||
| 310 | * |
||
| 311 | * @return string |
||
| 312 | */ |
||
| 313 | 9 | private function renderStaticPageView(&$pageView) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Create a Twig template that just needs an array to render. |
||
| 326 | * |
||
| 327 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
| 328 | * |
||
| 329 | * @since 0.1.1 |
||
| 330 | * |
||
| 331 | * @throws \Exception |
||
| 332 | * @throws \Throwable |
||
| 333 | * @throws Twig_Error_Syntax |
||
| 334 | * |
||
| 335 | * @return Twig_Template |
||
| 336 | */ |
||
| 337 | 9 | private function createTwigTemplate(&$pageView) |
|
| 355 | } |
||
| 356 |