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( |
|
| 75 | TemplateBridgeInterface $templateBridge, |
||
| 76 | Configuration $configuration, |
||
| 77 | CollectionManager $collectionManager, |
||
| 78 | DataManager $dataManager, |
||
| 79 | MenuManager $menuManager, |
||
| 80 | PageManager $pageManager, |
||
| 81 | EventDispatcherInterface $eventDispatcher, |
||
| 82 | LoggerInterface $logger |
||
| 83 | ) { |
||
| 84 | 12 | $this->templateBridge = $templateBridge; |
|
| 85 | 12 | $this->theme = ''; |
|
| 86 | 12 | $this->pageManager = $pageManager; |
|
| 87 | 12 | $this->eventDispatcher = $eventDispatcher; |
|
| 88 | 12 | $this->logger = $logger; |
|
| 89 | 12 | $this->configuration = $configuration; |
|
| 90 | |||
| 91 | 12 | $this->pageViewsFlattened = &$pageManager->getPageViewsFlattened(); |
|
| 92 | 12 | $this->redirectTemplate = $this->configuration->getRedirectTemplate(); |
|
| 93 | |||
| 94 | // Global variables maintained by stakx |
||
| 95 | 12 | $this->templateBridge->setGlobalVariable('site', $configuration->getConfiguration()); |
|
| 96 | 12 | $this->templateBridge->setGlobalVariable('data', $dataManager->getJailedDataItems()); |
|
| 97 | 12 | $this->templateBridge->setGlobalVariable('collections', $collectionManager->getJailedCollections()); |
|
| 98 | 12 | $this->templateBridge->setGlobalVariable('menu', $menuManager->getSiteMenu()); |
|
| 99 | 12 | $this->templateBridge->setGlobalVariable('pages', $pageManager->getJailedStaticPageViews()); |
|
| 100 | 12 | $this->templateBridge->setGlobalVariable('repeaters', $pageManager->getJailedRepeaterPageViews()); |
|
| 101 | 12 | } |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @param Folder $folder |
||
| 105 | */ |
||
| 106 | 12 | public function setTargetFolder(Folder $folder) |
|
| 107 | { |
||
| 108 | 12 | $this->folder = $folder; |
|
| 109 | 12 | } |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @param string $themeName |
||
| 113 | */ |
||
| 114 | public function setThemeName($themeName) |
||
| 115 | { |
||
| 116 | $this->theme = $themeName; |
||
| 117 | } |
||
| 118 | |||
| 119 | /// |
||
| 120 | // Twig parent templates |
||
| 121 | /// |
||
| 122 | |||
| 123 | public function getTemplateMappings() |
||
| 124 | { |
||
| 125 | return $this->templateMapping; |
||
| 126 | } |
||
| 127 | |||
| 128 | /// |
||
| 129 | // Rendering HTML Functionality |
||
| 130 | /// |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get the HTML for a Static PageView. |
||
| 134 | * |
||
| 135 | * This function just **renders** the HTML but does not write it to the filesystem. Use `compilePageView()` for that |
||
| 136 | * instead. |
||
| 137 | * |
||
| 138 | * @param StaticPageView $pageView |
||
| 139 | * |
||
| 140 | * @throws TemplateErrorInterface |
||
| 141 | * |
||
| 142 | * @return string the HTML for a Static PageView |
||
| 143 | */ |
||
| 144 | 10 | public function renderStaticPageView(StaticPageView $pageView) |
|
| 145 | { |
||
| 146 | 10 | $pageView->compile(); |
|
| 147 | |||
| 148 | 10 | return $this->buildStaticPageViewHTML($pageView); |
|
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Get the HTML for a Dynamic PageView and ContentItem. |
||
| 153 | * |
||
| 154 | * This function just **renders** the HTML but does not write it to the filesystem. Use `compileDynamicPageView()` |
||
| 155 | * for that instead. |
||
| 156 | * |
||
| 157 | * @param DynamicPageView $pageView |
||
| 158 | * @param TemplateReadyDocument $contentItem |
||
| 159 | * |
||
| 160 | * @throws TemplateErrorInterface |
||
| 161 | * |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | public function renderDynamicPageView(DynamicPageView $pageView, TemplateReadyDocument $contentItem) |
||
| 165 | { |
||
| 166 | $template = $this->createTwigTemplate($pageView); |
||
| 167 | |||
| 168 | return $this->buildDynamicPageViewHTML($template, $contentItem); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get the HTML for a Repeater PageView. |
||
| 173 | * |
||
| 174 | * @param RepeaterPageView $pageView |
||
| 175 | * @param ExpandedValue $expandedValue |
||
| 176 | * |
||
| 177 | * @throws TemplateErrorInterface |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function renderRepeaterPageView(RepeaterPageView $pageView, ExpandedValue $expandedValue) |
||
| 182 | { |
||
| 183 | $template = $this->createTwigTemplate($pageView); |
||
| 184 | |||
| 185 | return $this->buildRepeaterPageViewHTML($template, $pageView, $expandedValue); |
||
|
|
|||
| 186 | } |
||
| 187 | |||
| 188 | /// |
||
| 189 | // IO Functionality |
||
| 190 | /// |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Compile all of the PageViews registered with the compiler. |
||
| 194 | * |
||
| 195 | * @since 0.1.0 |
||
| 196 | */ |
||
| 197 | 12 | public function compileAll() |
|
| 198 | { |
||
| 199 | 12 | foreach ($this->pageViewsFlattened as &$pageView) |
|
| 200 | { |
||
| 201 | 12 | $this->compilePageView($pageView); |
|
| 202 | } |
||
| 203 | 12 | } |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Compile an individual PageView item. |
||
| 207 | * |
||
| 208 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
| 209 | * respective target file. |
||
| 210 | * |
||
| 211 | * @param DynamicPageView|RepeaterPageView|StaticPageView $pageView The PageView that needs to be compiled |
||
| 212 | * |
||
| 213 | * @since 0.1.1 |
||
| 214 | */ |
||
| 215 | 12 | public function compilePageView(BasePageView &$pageView) |
|
| 216 | { |
||
| 217 | 12 | Service::setOption('currentTemplate', $pageView->getAbsoluteFilePath()); |
|
| 218 | 12 | $this->logger->debug('Compiling {type} PageView: {pageview}', [ |
|
| 219 | 12 | 'pageview' => $pageView->getRelativeFilePath(), |
|
| 220 | 12 | 'type' => $pageView->getType(), |
|
| 221 | ]); |
||
| 222 | |||
| 223 | try |
||
| 224 | { |
||
| 225 | 12 | switch ($pageView->getType()) |
|
| 226 | { |
||
| 227 | case BasePageView::STATIC_TYPE: |
||
| 228 | 10 | $this->compileStaticPageView($pageView); |
|
| 229 | 10 | $this->compileStandardRedirects($pageView); |
|
| 230 | 10 | break; |
|
| 231 | |||
| 232 | case BasePageView::DYNAMIC_TYPE: |
||
| 233 | $this->compileDynamicPageView($pageView); |
||
| 234 | $this->compileStandardRedirects($pageView); |
||
| 235 | break; |
||
| 236 | |||
| 237 | case BasePageView::REPEATER_TYPE: |
||
| 238 | 3 | $this->compileRepeaterPageView($pageView); |
|
| 239 | 3 | $this->compileExpandedRedirects($pageView); |
|
| 240 | 12 | break; |
|
| 241 | } |
||
| 242 | } |
||
| 243 | catch (TemplateErrorInterface $e) |
||
| 244 | { |
||
| 245 | throw new FileAwareException( |
||
| 246 | $e->getMessage(), |
||
| 247 | $e->getCode(), |
||
| 248 | $e, |
||
| 249 | $pageView->getRelativeFilePath(), |
||
| 250 | $e->getTemplateLine() + $pageView->getLineOffset() |
||
| 251 | ); |
||
| 252 | } |
||
| 253 | 12 | } |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Write the compiled output for a static PageView. |
||
| 257 | * |
||
| 258 | * @since 0.1.1 |
||
| 259 | * |
||
| 260 | * @throws TemplateErrorInterface |
||
| 261 | */ |
||
| 262 | 10 | private function compileStaticPageView(StaticPageView &$pageView) |
|
| 263 | { |
||
| 264 | 10 | $this->writeToFilesystem( |
|
| 265 | 10 | $pageView->getTargetFile(), |
|
| 266 | 10 | $this->renderStaticPageView($pageView), |
|
| 267 | 10 | BasePageView::STATIC_TYPE |
|
| 268 | ); |
||
| 269 | 10 | } |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Write the compiled output for a dynamic PageView. |
||
| 273 | * |
||
| 274 | * @param DynamicPageView $pageView |
||
| 275 | * |
||
| 276 | * @since 0.1.1 |
||
| 277 | * |
||
| 278 | * @throws TemplateErrorInterface |
||
| 279 | */ |
||
| 280 | private function compileDynamicPageView(DynamicPageView &$pageView) |
||
| 281 | { |
||
| 282 | $contentItems = $pageView->getCollectableItems(); |
||
| 283 | $template = $this->createTwigTemplate($pageView); |
||
| 284 | |||
| 285 | foreach ($contentItems as &$contentItem) |
||
| 286 | { |
||
| 287 | if ($contentItem->isDraft() && !Service::hasRunTimeFlag(RuntimeStatus::USING_DRAFTS)) |
||
| 288 | { |
||
| 289 | $this->logger->debug('{file}: marked as a draft', [ |
||
| 290 | 'file' => $contentItem->getRelativeFilePath(), |
||
| 291 | ]); |
||
| 292 | |||
| 293 | continue; |
||
| 294 | } |
||
| 295 | |||
| 296 | $this->writeToFilesystem( |
||
| 297 | $contentItem->getTargetFile(), |
||
| 298 | $this->buildDynamicPageViewHTML($template, $contentItem), |
||
| 299 | BasePageView::DYNAMIC_TYPE |
||
| 300 | ); |
||
| 301 | |||
| 302 | $this->compileStandardRedirects($contentItem); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Write the compiled output for a repeater PageView. |
||
| 308 | * |
||
| 309 | * @param RepeaterPageView $pageView |
||
| 310 | * |
||
| 311 | * @since 0.1.1 |
||
| 312 | * |
||
| 313 | * @throws TemplateErrorInterface |
||
| 314 | */ |
||
| 315 | 3 | private function compileRepeaterPageView(RepeaterPageView &$pageView) |
|
| 316 | { |
||
| 317 | 3 | $template = $this->createTwigTemplate($pageView); |
|
| 318 | 3 | $permalinks = $pageView->getRepeaterPermalinks(); |
|
| 319 | |||
| 320 | 3 | foreach ($permalinks as $permalink) |
|
| 321 | { |
||
| 322 | 3 | $this->writeToFilesystem( |
|
| 323 | 3 | $pageView->getTargetFile($permalink->getEvaluated()), |
|
| 324 | 3 | $this->buildRepeaterPageViewHTML($template, $pageView, $permalink), |
|
| 325 | 3 | BasePageView::REPEATER_TYPE |
|
| 326 | ); |
||
| 327 | } |
||
| 328 | 3 | } |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Write the given $output to the $targetFile as a $fileType PageView. |
||
| 332 | * |
||
| 333 | * @param string $targetFile |
||
| 334 | * @param string $output |
||
| 335 | * @param string $fileType |
||
| 336 | */ |
||
| 337 | 12 | private function writeToFilesystem($targetFile, $output, $fileType) |
|
| 345 | |||
| 346 | /// |
||
| 347 | // Redirect handling |
||
| 348 | /// |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Write redirects for standard redirects. |
||
| 352 | * |
||
| 353 | * @throws TemplateErrorInterface |
||
| 354 | * |
||
| 355 | * @since 0.1.1 |
||
| 356 | */ |
||
| 357 | 10 | private function compileStandardRedirects(PermalinkDocument &$pageView) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Write redirects for expanded redirects. |
||
| 378 | * |
||
| 379 | * @param RepeaterPageView $pageView |
||
| 380 | * |
||
| 381 | * @since 0.1.1 |
||
| 382 | */ |
||
| 383 | 3 | private function compileExpandedRedirects(RepeaterPageView &$pageView) |
|
| 409 | |||
| 410 | /// |
||
| 411 | // Twig Functionality |
||
| 412 | /// |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
| 416 | * |
||
| 417 | * @param TemplateInterface $template |
||
| 418 | * @param RepeaterPageView $pageView |
||
| 419 | * @param ExpandedValue $expandedValue |
||
| 420 | * |
||
| 421 | * @since 0.1.1 |
||
| 422 | * |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | 3 | private function buildRepeaterPageViewHTML(TemplateInterface &$template, RepeaterPageView &$pageView, ExpandedValue &$expandedValue) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Get the compiled HTML for a specific ContentItem. |
||
| 452 | * |
||
| 453 | * @param CollectableItem|TemplateReadyDocument $twigItem |
||
| 454 | * |
||
| 455 | * @since 0.1.1 |
||
| 456 | * |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | View Code Duplication | private function buildDynamicPageViewHTML(TemplateInterface &$template, &$twigItem) |
|
| 460 | { |
||
| 461 | $defaultContext = [ |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Get the compiled HTML for a static PageView. |
||
| 481 | * |
||
| 482 | * @since 0.1.1 |
||
| 483 | * |
||
| 484 | * @throws TemplateErrorInterface |
||
| 485 | * |
||
| 486 | * @return string |
||
| 487 | */ |
||
| 488 | 10 | View Code Duplication | private function buildStaticPageViewHTML(StaticPageView &$pageView) |
| 508 | |||
| 509 | /** |
||
| 510 | * Create a Twig template that just needs an array to render. |
||
| 511 | * |
||
| 512 | * @since 0.1.1 |
||
| 513 | * |
||
| 514 | * @throws TemplateErrorInterface |
||
| 515 | * |
||
| 516 | * @return TemplateInterface |
||
| 517 | */ |
||
| 518 | 12 | private function createTwigTemplate(BasePageView &$pageView) |
|
| 544 | } |
||
| 545 |
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.