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 |
||
| 40 | class Compiler |
||
| 41 | { |
||
| 42 | /** @var string|false */ |
||
| 43 | private $redirectTemplate; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * All of the PageViews handled by this Compiler instance indexed by their file paths relative to the site root. |
||
| 47 | * |
||
| 48 | * ``` |
||
| 49 | * array['_pages/index.html.twig'] = &PageView; |
||
| 50 | * ``` |
||
| 51 | * |
||
| 52 | * @var BasePageView[] |
||
| 53 | */ |
||
| 54 | private $pageViewsFlattened; |
||
| 55 | |||
| 56 | /** @var string[] */ |
||
| 57 | private $templateMapping; |
||
| 58 | |||
| 59 | /** @var Folder */ |
||
| 60 | private $folder; |
||
| 61 | |||
| 62 | /** @var string */ |
||
| 63 | private $theme; |
||
| 64 | |||
| 65 | private $templateBridge; |
||
| 66 | private $pageManager; |
||
| 67 | private $eventDispatcher; |
||
| 68 | private $configuration; |
||
| 69 | |||
| 70 | 12 | public function __construct( |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @param Folder $folder |
||
| 100 | */ |
||
| 101 | 12 | public function setTargetFolder(Folder $folder) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * @param string $themeName |
||
| 108 | */ |
||
| 109 | public function setThemeName($themeName) |
||
| 113 | |||
| 114 | /// |
||
| 115 | // Twig parent templates |
||
| 116 | /// |
||
| 117 | |||
| 118 | public function getTemplateMappings() |
||
| 122 | |||
| 123 | /// |
||
| 124 | // Rendering HTML Functionality |
||
| 125 | /// |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Get the HTML for a Static PageView. |
||
| 129 | * |
||
| 130 | * This function just **renders** the HTML but does not write it to the filesystem. Use `compilePageView()` for that |
||
| 131 | * instead. |
||
| 132 | * |
||
| 133 | * @param StaticPageView $pageView |
||
| 134 | * |
||
| 135 | * @throws TemplateErrorInterface |
||
| 136 | * |
||
| 137 | * @return string The HTML for a Static PageView. |
||
| 138 | */ |
||
| 139 | 10 | public function renderStaticPageView(StaticPageView $pageView) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Get the HTML for a Dynamic PageView and ContentItem. |
||
| 148 | * |
||
| 149 | * This function just **renders** the HTML but does not write it to the filesystem. Use `compileDynamicPageView()` |
||
| 150 | * for that instead. |
||
| 151 | * |
||
| 152 | * @param DynamicPageView $pageView |
||
| 153 | * @param TemplateReadyDocument $contentItem |
||
| 154 | * |
||
| 155 | * @throws TemplateErrorInterface |
||
| 156 | * |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | public function renderDynamicPageView(DynamicPageView $pageView, TemplateReadyDocument $contentItem) |
||
| 165 | |||
| 166 | /// |
||
| 167 | // IO Functionality |
||
| 168 | /// |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Compile all of the PageViews registered with the compiler. |
||
| 172 | * |
||
| 173 | * @since 0.1.0 |
||
| 174 | */ |
||
| 175 | 12 | public function compileAll() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Compile an individual PageView item. |
||
| 185 | * |
||
| 186 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
| 187 | * respective target file. |
||
| 188 | * |
||
| 189 | * @param DynamicPageView|RepeaterPageView|StaticPageView $pageView The PageView that needs to be compiled |
||
| 190 | * |
||
| 191 | * @since 0.1.1 |
||
| 192 | */ |
||
| 193 | 12 | public function compilePageView(BasePageView &$pageView) |
|
| 194 | { |
||
| 195 | 12 | $this->templateBridge->setGlobalVariable('__currentTemplate', $pageView->getAbsoluteFilePath()); |
|
| 196 | 12 | $this->logger->debug('Compiling {type} PageView: {pageview}', [ |
|
| 197 | 12 | 'pageview' => $pageView->getRelativeFilePath(), |
|
| 198 | 12 | 'type' => $pageView->getType(), |
|
| 199 | ]); |
||
| 200 | |||
| 201 | try |
||
| 202 | { |
||
| 203 | 12 | switch ($pageView->getType()) |
|
| 204 | { |
||
| 205 | 12 | case BasePageView::STATIC_TYPE: |
|
| 206 | 10 | $this->compileStaticPageView($pageView); |
|
| 207 | 10 | $this->compileStandardRedirects($pageView); |
|
| 208 | 10 | break; |
|
| 209 | |||
| 210 | 3 | case BasePageView::DYNAMIC_TYPE: |
|
| 211 | $this->compileDynamicPageView($pageView); |
||
| 212 | $this->compileStandardRedirects($pageView); |
||
| 213 | break; |
||
| 214 | |||
| 215 | 3 | case BasePageView::REPEATER_TYPE: |
|
| 216 | 3 | $this->compileRepeaterPageView($pageView); |
|
| 217 | 3 | $this->compileExpandedRedirects($pageView); |
|
| 218 | 12 | break; |
|
| 219 | } |
||
| 220 | } |
||
| 221 | catch (TemplateErrorInterface $e) |
||
| 222 | { |
||
| 223 | throw new FileAwareException( |
||
| 224 | $e->getMessage(), |
||
| 225 | $e->getCode(), |
||
| 226 | $e, |
||
| 227 | $pageView->getRelativeFilePath(), |
||
| 228 | $e->getTemplateLine() + $pageView->getLineOffset() |
||
| 229 | ); |
||
| 230 | } |
||
| 231 | 12 | } |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Write the compiled output for a static PageView. |
||
| 235 | * |
||
| 236 | * @since 0.1.1 |
||
| 237 | * |
||
| 238 | * @throws TemplateErrorInterface |
||
| 239 | */ |
||
| 240 | 10 | private function compileStaticPageView(StaticPageView &$pageView) |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Write the compiled output for a dynamic PageView. |
||
| 251 | * |
||
| 252 | * @param DynamicPageView $pageView |
||
| 253 | * |
||
| 254 | * @since 0.1.1 |
||
| 255 | * |
||
| 256 | * @throws TemplateErrorInterface |
||
| 257 | */ |
||
| 258 | private function compileDynamicPageView(DynamicPageView &$pageView) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Write the compiled output for a repeater PageView. |
||
| 286 | * |
||
| 287 | * @param RepeaterPageView $pageView |
||
| 288 | * |
||
| 289 | * @since 0.1.1 |
||
| 290 | * |
||
| 291 | * @throws TemplateErrorInterface |
||
| 292 | */ |
||
| 293 | 3 | private function compileRepeaterPageView(RepeaterPageView &$pageView) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * Write the given $output to the $targetFile as a $fileType PageView. |
||
| 314 | * |
||
| 315 | * @param string $targetFile |
||
| 316 | * @param string $output |
||
| 317 | * @param string $fileType |
||
| 318 | */ |
||
| 319 | 12 | private function writeToFilesystem($targetFile, $output, $fileType) |
|
| 327 | |||
| 328 | /// |
||
| 329 | // Redirect handling |
||
| 330 | /// |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Write redirects for standard redirects. |
||
| 334 | * |
||
| 335 | * @throws TemplateErrorInterface |
||
| 336 | * |
||
| 337 | * @since 0.1.1 |
||
| 338 | */ |
||
| 339 | 10 | private function compileStandardRedirects(PermalinkDocument &$pageView) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Write redirects for expanded redirects. |
||
| 360 | * |
||
| 361 | * @param RepeaterPageView $pageView |
||
| 362 | * |
||
| 363 | * @since 0.1.1 |
||
| 364 | */ |
||
| 365 | 3 | private function compileExpandedRedirects(RepeaterPageView &$pageView) |
|
| 391 | |||
| 392 | /// |
||
| 393 | // Twig Functionality |
||
| 394 | /// |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
| 398 | * |
||
| 399 | * @param TemplateInterface $template |
||
| 400 | * @param RepeaterPageView $pageView |
||
| 401 | * @param ExpandedValue $expandedValue |
||
| 402 | * |
||
| 403 | * @since 0.1.1 |
||
| 404 | * |
||
| 405 | * @return string |
||
| 406 | */ |
||
| 407 | 3 | private function buildRepeaterPageViewHTML(TemplateInterface &$template, RepeaterPageView &$pageView, ExpandedValue &$expandedValue) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Get the compiled HTML for a specific ContentItem. |
||
| 434 | * |
||
| 435 | * @since 0.1.1 |
||
| 436 | * |
||
| 437 | * @return string |
||
| 438 | */ |
||
| 439 | View Code Duplication | private function buildDynamicPageViewHTML(TemplateInterface &$template, TemplateReadyDocument &$twigItem) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Get the compiled HTML for a static PageView. |
||
| 461 | * |
||
| 462 | * @since 0.1.1 |
||
| 463 | * |
||
| 464 | * @throws TemplateErrorInterface |
||
| 465 | * |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | 10 | View Code Duplication | private function buildStaticPageViewHTML(StaticPageView &$pageView) |
| 488 | |||
| 489 | /** |
||
| 490 | * Create a Twig template that just needs an array to render. |
||
| 491 | * |
||
| 492 | * @since 0.1.1 |
||
| 493 | * |
||
| 494 | * @throws TemplateErrorInterface |
||
| 495 | * |
||
| 496 | * @return TemplateInterface |
||
| 497 | */ |
||
| 498 | 12 | private function createTwigTemplate(BasePageView &$pageView) |
|
| 524 | } |
||
| 525 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: