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 |
||
| 43 | class Compiler |
||
| 44 | { |
||
| 45 | /** @var string|false */ |
||
| 46 | private $redirectTemplate; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * All of the PageViews handled by this Compiler instance indexed by their file paths relative to the site root. |
||
| 50 | * |
||
| 51 | * ``` |
||
| 52 | * array['_pages/index.html.twig'] = &PageView; |
||
| 53 | * ``` |
||
| 54 | * |
||
| 55 | * @var BasePageView[] |
||
| 56 | */ |
||
| 57 | private $pageViewsFlattened; |
||
| 58 | |||
| 59 | /** @var string[] */ |
||
| 60 | private $templateMapping; |
||
| 61 | |||
| 62 | /** @var Folder */ |
||
| 63 | private $folder; |
||
| 64 | |||
| 65 | /** @var string */ |
||
| 66 | private $theme; |
||
| 67 | |||
| 68 | private $templateBridge; |
||
| 69 | private $pageManager; |
||
| 70 | private $eventDispatcher; |
||
| 71 | private $configuration; |
||
| 72 | private $logger; |
||
| 73 | |||
| 74 | 12 | public function __construct( |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @param Folder $folder |
||
| 105 | */ |
||
| 106 | 12 | public function setTargetFolder(Folder $folder) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @param string $themeName |
||
| 113 | */ |
||
| 114 | public function setThemeName($themeName) |
||
| 118 | |||
| 119 | /// |
||
| 120 | // Twig parent templates |
||
| 121 | /// |
||
| 122 | |||
| 123 | public function getTemplateBridge() |
||
| 127 | |||
| 128 | public function getTemplateMappings() |
||
| 132 | |||
| 133 | /// |
||
| 134 | // Rendering HTML Functionality |
||
| 135 | /// |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get the HTML for a Static PageView. |
||
| 139 | * |
||
| 140 | * This function just **renders** the HTML but does not write it to the filesystem. Use `compilePageView()` for that |
||
| 141 | * instead. |
||
| 142 | * |
||
| 143 | * @param StaticPageView $pageView |
||
| 144 | * |
||
| 145 | * @throws TemplateErrorInterface |
||
| 146 | * |
||
| 147 | * @return string the HTML for a Static PageView |
||
| 148 | */ |
||
| 149 | 10 | public function renderStaticPageView(StaticPageView $pageView) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Get the HTML for a Dynamic PageView and ContentItem. |
||
| 158 | * |
||
| 159 | * This function just **renders** the HTML but does not write it to the filesystem. Use `compileDynamicPageView()` |
||
| 160 | * for that instead. |
||
| 161 | * |
||
| 162 | * @param DynamicPageView $pageView |
||
| 163 | * @param TemplateReadyDocument $contentItem |
||
| 164 | * |
||
| 165 | * @throws TemplateErrorInterface |
||
| 166 | * |
||
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public function renderDynamicPageView(DynamicPageView $pageView, TemplateReadyDocument $contentItem) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Get the HTML for a Repeater PageView. |
||
| 178 | * |
||
| 179 | * @param RepeaterPageView $pageView |
||
| 180 | * @param ExpandedValue $expandedValue |
||
| 181 | * |
||
| 182 | * @throws TemplateErrorInterface |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function renderRepeaterPageView(RepeaterPageView $pageView, ExpandedValue $expandedValue) |
||
| 192 | |||
| 193 | /// |
||
| 194 | // IO Functionality |
||
| 195 | /// |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Compile all of the PageViews registered with the compiler. |
||
| 199 | * |
||
| 200 | * @since 0.1.0 |
||
| 201 | */ |
||
| 202 | 12 | public function compileAll() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Compile an individual PageView item. |
||
| 212 | * |
||
| 213 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
| 214 | * respective target file. |
||
| 215 | * |
||
| 216 | * @param DynamicPageView|RepeaterPageView|StaticPageView $pageView The PageView that needs to be compiled |
||
| 217 | * |
||
| 218 | * @since 0.1.1 |
||
| 219 | */ |
||
| 220 | 12 | public function compilePageView(BasePageView &$pageView) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Write the compiled output for a static PageView. |
||
| 262 | * |
||
| 263 | * @since 0.1.1 |
||
| 264 | * |
||
| 265 | * @throws TemplateErrorInterface |
||
| 266 | */ |
||
| 267 | 10 | private function compileStaticPageView(StaticPageView &$pageView) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Write the compiled output for a dynamic PageView. |
||
| 278 | * |
||
| 279 | * @param DynamicPageView $pageView |
||
| 280 | * |
||
| 281 | * @since 0.1.1 |
||
| 282 | * |
||
| 283 | * @throws TemplateErrorInterface |
||
| 284 | */ |
||
| 285 | private function compileDynamicPageView(DynamicPageView &$pageView) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Write the compiled output for a repeater PageView. |
||
| 313 | * |
||
| 314 | * @param RepeaterPageView $pageView |
||
| 315 | * |
||
| 316 | * @since 0.1.1 |
||
| 317 | * |
||
| 318 | * @throws TemplateErrorInterface |
||
| 319 | */ |
||
| 320 | 3 | private function compileRepeaterPageView(RepeaterPageView &$pageView) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Write the given $output to the $targetFile as a $fileType PageView. |
||
| 337 | * |
||
| 338 | * @param string $targetFile |
||
| 339 | * @param string $output |
||
| 340 | * @param string $fileType |
||
| 341 | */ |
||
| 342 | 12 | private function writeToFilesystem($targetFile, $output, $fileType) |
|
| 350 | |||
| 351 | /// |
||
| 352 | // Redirect handling |
||
| 353 | /// |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Write redirects for standard redirects. |
||
| 357 | * |
||
| 358 | * @throws TemplateErrorInterface |
||
| 359 | * |
||
| 360 | * @since 0.1.1 |
||
| 361 | */ |
||
| 362 | 10 | private function compileStandardRedirects(PermalinkDocument &$pageView) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Write redirects for expanded redirects. |
||
| 383 | * |
||
| 384 | * @param RepeaterPageView $pageView |
||
| 385 | * |
||
| 386 | * @since 0.1.1 |
||
| 387 | */ |
||
| 388 | 3 | private function compileExpandedRedirects(RepeaterPageView &$pageView) |
|
| 414 | |||
| 415 | /// |
||
| 416 | // Twig Functionality |
||
| 417 | /// |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
| 421 | * |
||
| 422 | * @param TemplateInterface $template |
||
| 423 | * @param RepeaterPageView $pageView |
||
| 424 | * @param ExpandedValue $expandedValue |
||
| 425 | * |
||
| 426 | * @since 0.1.1 |
||
| 427 | * |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | 3 | private function buildRepeaterPageViewHTML(TemplateInterface &$template, RepeaterPageView &$pageView, ExpandedValue &$expandedValue) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Get the compiled HTML for a specific ContentItem. |
||
| 457 | * |
||
| 458 | * @param CollectableItem|TemplateReadyDocument $twigItem |
||
| 459 | * |
||
| 460 | * @since 0.1.1 |
||
| 461 | * |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | View Code Duplication | private function buildDynamicPageViewHTML(TemplateInterface &$template, &$twigItem) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Get the compiled HTML for a static PageView. |
||
| 486 | * |
||
| 487 | * @since 0.1.1 |
||
| 488 | * |
||
| 489 | * @throws TemplateErrorInterface |
||
| 490 | * |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | 10 | View Code Duplication | private function buildStaticPageViewHTML(StaticPageView &$pageView) |
| 513 | |||
| 514 | /** |
||
| 515 | * Create a Twig template that just needs an array to render. |
||
| 516 | * |
||
| 517 | * @since 0.1.1 |
||
| 518 | * |
||
| 519 | * @throws TemplateErrorInterface |
||
| 520 | * |
||
| 521 | * @return TemplateInterface |
||
| 522 | */ |
||
| 523 | 12 | private function createTwigTemplate(BasePageView &$pageView) |
|
| 549 | } |
||
| 550 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.