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) |
|
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) |
|
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) |
|
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.