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 |
||
44 | class Compiler |
||
45 | { |
||
46 | /** @var string|false */ |
||
47 | private $redirectTemplate; |
||
48 | |||
49 | /** |
||
50 | * All of the PageViews handled by this Compiler instance indexed by their file paths relative to the site root. |
||
51 | * |
||
52 | * ``` |
||
53 | * array['_pages/index.html.twig'] = &PageView; |
||
54 | * ``` |
||
55 | * |
||
56 | * @var BasePageView[] |
||
57 | */ |
||
58 | private $pageViewsFlattened; |
||
59 | |||
60 | /** @var string[] */ |
||
61 | private $templateMapping; |
||
62 | |||
63 | /** @var WritableFolder */ |
||
64 | private $folder; |
||
65 | |||
66 | /** @var string */ |
||
67 | private $theme; |
||
68 | |||
69 | private $templateBridge; |
||
70 | private $pageManager; |
||
71 | private $eventDispatcher; |
||
72 | private $configuration; |
||
73 | private $logger; |
||
74 | |||
75 | 12 | public function __construct( |
|
105 | |||
106 | /** |
||
107 | * @param WritableFolder $folder |
||
108 | */ |
||
109 | 12 | public function setTargetFolder(WritableFolder $folder) |
|
113 | |||
114 | /** |
||
115 | * @param string $themeName |
||
116 | */ |
||
117 | public function setThemeName($themeName) |
||
121 | |||
122 | /// |
||
123 | // Twig parent templates |
||
124 | /// |
||
125 | |||
126 | public function getTemplateBridge() |
||
130 | |||
131 | public function getTemplateMappings() |
||
135 | |||
136 | /// |
||
137 | // Rendering HTML Functionality |
||
138 | /// |
||
139 | |||
140 | /** |
||
141 | * Get the HTML for a Static PageView. |
||
142 | * |
||
143 | * This function just **renders** the HTML but does not write it to the filesystem. Use `compilePageView()` for that |
||
144 | * instead. |
||
145 | * |
||
146 | * @param StaticPageView $pageView |
||
147 | * |
||
148 | * @throws TemplateErrorInterface |
||
149 | * |
||
150 | * @return string the HTML for a Static PageView |
||
151 | */ |
||
152 | 10 | public function renderStaticPageView(StaticPageView $pageView) |
|
158 | |||
159 | /** |
||
160 | * Get the HTML for a Dynamic PageView and ContentItem. |
||
161 | * |
||
162 | * This function just **renders** the HTML but does not write it to the filesystem. Use `compileDynamicPageView()` |
||
163 | * for that instead. |
||
164 | * |
||
165 | * @param DynamicPageView $pageView |
||
166 | * @param TemplateReadyDocument $contentItem |
||
167 | * |
||
168 | * @throws TemplateErrorInterface |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | public function renderDynamicPageView(DynamicPageView $pageView, TemplateReadyDocument $contentItem) |
||
178 | |||
179 | /** |
||
180 | * Get the HTML for a Repeater PageView. |
||
181 | * |
||
182 | * @param RepeaterPageView $pageView |
||
183 | * @param ExpandedValue $expandedValue |
||
184 | * |
||
185 | * @throws TemplateErrorInterface |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function renderRepeaterPageView(RepeaterPageView $pageView, ExpandedValue $expandedValue) |
||
195 | |||
196 | /// |
||
197 | // IO Functionality |
||
198 | /// |
||
199 | |||
200 | /** |
||
201 | * Compile all of the PageViews registered with the compiler. |
||
202 | * |
||
203 | * @since 0.1.0 |
||
204 | */ |
||
205 | 12 | public function compileAll() |
|
212 | |||
213 | /** |
||
214 | * Compile an individual PageView item. |
||
215 | * |
||
216 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
217 | * respective target file. |
||
218 | * |
||
219 | * @param DynamicPageView|RepeaterPageView|StaticPageView $pageView The PageView that needs to be compiled |
||
220 | * |
||
221 | * @since 0.1.1 |
||
222 | */ |
||
223 | 12 | public function compilePageView(BasePageView &$pageView) |
|
224 | { |
||
225 | 12 | Service::setOption('currentTemplate', $pageView->getAbsoluteFilePath()); |
|
226 | 12 | $this->logger->debug('Compiling {type} PageView: {pageview}', [ |
|
227 | 12 | 'pageview' => $pageView->getRelativeFilePath(), |
|
228 | 12 | 'type' => $pageView->getType(), |
|
229 | ]); |
||
230 | |||
231 | try |
||
232 | { |
||
233 | 12 | switch ($pageView->getType()) |
|
234 | { |
||
235 | 12 | case BasePageView::STATIC_TYPE: |
|
236 | 10 | $this->compileStaticPageView($pageView); |
|
237 | 10 | $this->compileStandardRedirects($pageView); |
|
238 | 10 | break; |
|
239 | |||
240 | 3 | case BasePageView::DYNAMIC_TYPE: |
|
241 | $this->compileDynamicPageView($pageView); |
||
242 | $this->compileStandardRedirects($pageView); |
||
243 | break; |
||
244 | |||
245 | 3 | case BasePageView::REPEATER_TYPE: |
|
246 | 3 | $this->compileRepeaterPageView($pageView); |
|
247 | 3 | $this->compileExpandedRedirects($pageView); |
|
248 | 12 | break; |
|
249 | } |
||
250 | } |
||
251 | catch (TemplateErrorInterface $e) |
||
252 | { |
||
253 | throw new FileAwareException( |
||
254 | $e->getMessage(), |
||
255 | $e->getCode(), |
||
256 | $e, |
||
257 | $pageView->getRelativeFilePath(), |
||
258 | $e->getTemplateLine() + $pageView->getLineOffset() |
||
259 | ); |
||
260 | } |
||
261 | 12 | } |
|
262 | |||
263 | /** |
||
264 | * Write the compiled output for a static PageView. |
||
265 | * |
||
266 | * @since 0.1.1 |
||
267 | * |
||
268 | * @throws TemplateErrorInterface |
||
269 | */ |
||
270 | 10 | private function compileStaticPageView(StaticPageView &$pageView) |
|
278 | |||
279 | /** |
||
280 | * Write the compiled output for a dynamic PageView. |
||
281 | * |
||
282 | * @param DynamicPageView $pageView |
||
283 | * |
||
284 | * @since 0.1.1 |
||
285 | * |
||
286 | * @throws TemplateErrorInterface |
||
287 | */ |
||
288 | private function compileDynamicPageView(DynamicPageView &$pageView) |
||
313 | |||
314 | /** |
||
315 | * Write the compiled output for a repeater PageView. |
||
316 | * |
||
317 | * @param RepeaterPageView $pageView |
||
318 | * |
||
319 | * @since 0.1.1 |
||
320 | * |
||
321 | * @throws TemplateErrorInterface |
||
322 | */ |
||
323 | 3 | private function compileRepeaterPageView(RepeaterPageView &$pageView) |
|
337 | |||
338 | /** |
||
339 | * Write the given $output to the $targetFile as a $fileType PageView. |
||
340 | * |
||
341 | * @param string $targetFile |
||
342 | * @param string $output |
||
343 | * @param string $fileType |
||
344 | */ |
||
345 | 12 | private function writeToFilesystem($targetFile, $output, $fileType) |
|
353 | |||
354 | /// |
||
355 | // Redirect handling |
||
356 | /// |
||
357 | |||
358 | /** |
||
359 | * Write redirects for standard redirects. |
||
360 | * |
||
361 | * @throws TemplateErrorInterface |
||
362 | * |
||
363 | * @since 0.1.1 |
||
364 | */ |
||
365 | 10 | private function compileStandardRedirects(PermalinkDocument &$pageView) |
|
366 | { |
||
367 | 10 | $redirects = $pageView->getRedirects(); |
|
368 | |||
369 | 10 | foreach ($redirects as $redirect) |
|
370 | { |
||
371 | 1 | $redirectPageView = BasePageView::createRedirect( |
|
372 | 1 | $redirect, |
|
373 | 1 | $pageView->getPermalink(), |
|
374 | 1 | $this->redirectTemplate |
|
375 | ); |
||
376 | 1 | $redirectPageView->evaluateFrontMatter([], [ |
|
377 | 1 | 'site' => $this->configuration->getConfiguration(), |
|
378 | ]); |
||
379 | |||
380 | 1 | $redirectEvent = new RedirectPreOutput( |
|
381 | 1 | $redirect, |
|
382 | 1 | $pageView->getPermalink(), |
|
383 | 1 | $pageView, |
|
384 | 1 | $redirectPageView |
|
385 | ); |
||
386 | 1 | $this->eventDispatcher->dispatch(RedirectPreOutput::NAME, $redirectEvent); |
|
387 | |||
388 | 1 | $this->compileStaticPageView($redirectPageView); |
|
389 | } |
||
390 | 10 | } |
|
391 | |||
392 | /** |
||
393 | * Write redirects for expanded redirects. |
||
394 | * |
||
395 | * @param RepeaterPageView $pageView |
||
396 | * |
||
397 | * @since 0.1.1 |
||
398 | */ |
||
399 | 3 | private function compileExpandedRedirects(RepeaterPageView &$pageView) |
|
400 | { |
||
401 | 3 | $permalinks = $pageView->getRepeaterPermalinks(); |
|
402 | |||
403 | /** @var ExpandedValue[] $repeaterRedirect */ |
||
404 | 3 | foreach ($pageView->getRepeaterRedirects() as $repeaterRedirect) |
|
405 | { |
||
406 | /** |
||
407 | * @var int |
||
408 | * @var ExpandedValue $redirect |
||
409 | */ |
||
410 | 1 | foreach ($repeaterRedirect as $index => $redirect) |
|
411 | { |
||
412 | 1 | $redirectPageView = BasePageView::createRedirect( |
|
413 | 1 | $redirect->getEvaluated(), |
|
414 | 1 | $permalinks[$index]->getEvaluated(), |
|
415 | 1 | $this->redirectTemplate |
|
416 | ); |
||
417 | 1 | $redirectPageView->evaluateFrontMatter([], [ |
|
418 | 1 | 'site' => $this->configuration->getConfiguration(), |
|
419 | ]); |
||
420 | |||
421 | 1 | $redirectEvent = new RedirectPreOutput( |
|
422 | 1 | $redirect->getEvaluated(), |
|
423 | 1 | $permalinks[$index]->getEvaluated(), |
|
424 | 1 | $pageView, |
|
425 | 1 | $redirectPageView |
|
426 | ); |
||
427 | 1 | $this->eventDispatcher->dispatch(RedirectPreOutput::NAME, $redirectEvent); |
|
428 | |||
429 | 1 | $this->compilePageView($redirectPageView); |
|
430 | } |
||
431 | } |
||
432 | 3 | } |
|
433 | |||
434 | /// |
||
435 | // Twig Functionality |
||
436 | /// |
||
437 | |||
438 | /** |
||
439 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
440 | * |
||
441 | * @param TemplateInterface $template |
||
442 | * @param RepeaterPageView $pageView |
||
443 | * @param ExpandedValue $expandedValue |
||
444 | * |
||
445 | * @since 0.1.1 |
||
446 | * |
||
447 | * @return string |
||
448 | */ |
||
449 | 3 | private function buildRepeaterPageViewHTML(TemplateInterface &$template, RepeaterPageView &$pageView, ExpandedValue &$expandedValue) |
|
473 | |||
474 | /** |
||
475 | * Get the compiled HTML for a specific ContentItem. |
||
476 | * |
||
477 | * @param CollectableItem|TemplateReadyDocument $twigItem |
||
478 | * |
||
479 | * @since 0.1.1 |
||
480 | * |
||
481 | * @return string |
||
482 | */ |
||
483 | View Code Duplication | private function buildDynamicPageViewHTML(TemplateInterface &$template, &$twigItem) |
|
502 | |||
503 | /** |
||
504 | * Get the compiled HTML for a static PageView. |
||
505 | * |
||
506 | * @since 0.1.1 |
||
507 | * |
||
508 | * @throws TemplateErrorInterface |
||
509 | * |
||
510 | * @return string |
||
511 | */ |
||
512 | 10 | View Code Duplication | private function buildStaticPageViewHTML(StaticPageView &$pageView) |
532 | |||
533 | /** |
||
534 | * Create a Twig template that just needs an array to render. |
||
535 | * |
||
536 | * @since 0.1.1 |
||
537 | * |
||
538 | * @throws TemplateErrorInterface |
||
539 | * |
||
540 | * @return TemplateInterface |
||
541 | */ |
||
542 | 12 | private function createTwigTemplate(BasePageView &$pageView) |
|
568 | } |
||
569 |
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.