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 |
||
39 | class Compiler |
||
40 | { |
||
41 | /** @var string|false */ |
||
42 | private $redirectTemplate; |
||
43 | |||
44 | /** |
||
45 | * All of the PageViews handled by this Compiler instance indexed by their file paths relative to the site root. |
||
46 | * |
||
47 | * ``` |
||
48 | * array['_pages/index.html.twig'] = &PageView; |
||
49 | * ``` |
||
50 | * |
||
51 | * @var BasePageView[] |
||
52 | */ |
||
53 | private $pageViewsFlattened; |
||
54 | |||
55 | /** @var string[] */ |
||
56 | private $templateMapping; |
||
57 | |||
58 | /** @var Folder */ |
||
59 | private $folder; |
||
60 | |||
61 | /** @var string */ |
||
62 | private $theme; |
||
63 | |||
64 | private $templateBridge; |
||
65 | private $pageManager; |
||
66 | private $eventDispatcher; |
||
67 | private $configuration; |
||
68 | private $logger; |
||
69 | |||
70 | 12 | public function __construct( |
|
98 | |||
99 | /** |
||
100 | * @param Folder $folder |
||
101 | */ |
||
102 | 12 | public function setTargetFolder(Folder $folder) |
|
106 | |||
107 | /** |
||
108 | * @param string $themeName |
||
109 | */ |
||
110 | public function setThemeName($themeName) |
||
114 | |||
115 | /// |
||
116 | // Twig parent templates |
||
117 | /// |
||
118 | |||
119 | public function getTemplateMappings() |
||
123 | |||
124 | /// |
||
125 | // Rendering HTML Functionality |
||
126 | /// |
||
127 | |||
128 | /** |
||
129 | * Get the HTML for a Static PageView. |
||
130 | * |
||
131 | * This function just **renders** the HTML but does not write it to the filesystem. Use `compilePageView()` for that |
||
132 | * instead. |
||
133 | * |
||
134 | * @param StaticPageView $pageView |
||
135 | * |
||
136 | * @throws TemplateErrorInterface |
||
137 | * |
||
138 | * @return string the HTML for a Static PageView |
||
139 | */ |
||
140 | 10 | public function renderStaticPageView(StaticPageView $pageView) |
|
146 | |||
147 | /** |
||
148 | * Get the HTML for a Dynamic PageView and ContentItem. |
||
149 | * |
||
150 | * This function just **renders** the HTML but does not write it to the filesystem. Use `compileDynamicPageView()` |
||
151 | * for that instead. |
||
152 | * |
||
153 | * @param DynamicPageView $pageView |
||
154 | * @param TemplateReadyDocument $contentItem |
||
155 | * |
||
156 | * @throws TemplateErrorInterface |
||
157 | * |
||
158 | * @return string |
||
159 | */ |
||
160 | public function renderDynamicPageView(DynamicPageView $pageView, TemplateReadyDocument $contentItem) |
||
166 | |||
167 | /** |
||
168 | * Get the HTML for a Repeater PageView. |
||
169 | * |
||
170 | * @param RepeaterPageView $pageView |
||
171 | * @param ExpandedValue $expandedValue |
||
172 | * |
||
173 | * @throws TemplateErrorInterface |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | public function renderRepeaterPageView(RepeaterPageView $pageView, ExpandedValue $expandedValue) |
||
183 | |||
184 | /// |
||
185 | // IO Functionality |
||
186 | /// |
||
187 | |||
188 | /** |
||
189 | * Compile all of the PageViews registered with the compiler. |
||
190 | * |
||
191 | * @since 0.1.0 |
||
192 | */ |
||
193 | 12 | public function compileAll() |
|
200 | |||
201 | /** |
||
202 | * Compile an individual PageView item. |
||
203 | * |
||
204 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
205 | * respective target file. |
||
206 | * |
||
207 | * @param DynamicPageView|RepeaterPageView|StaticPageView $pageView The PageView that needs to be compiled |
||
208 | * |
||
209 | * @since 0.1.1 |
||
210 | */ |
||
211 | 12 | public function compilePageView(BasePageView &$pageView) |
|
212 | { |
||
213 | 12 | Service::setOption('currentTemplate', $pageView->getAbsoluteFilePath()); |
|
214 | 12 | $this->logger->debug('Compiling {type} PageView: {pageview}', [ |
|
215 | 12 | 'pageview' => $pageView->getRelativeFilePath(), |
|
216 | 12 | 'type' => $pageView->getType(), |
|
217 | ]); |
||
218 | |||
219 | try |
||
220 | { |
||
221 | 12 | switch ($pageView->getType()) |
|
222 | { |
||
223 | 12 | case BasePageView::STATIC_TYPE: |
|
224 | 10 | $this->compileStaticPageView($pageView); |
|
225 | 10 | $this->compileStandardRedirects($pageView); |
|
226 | 10 | break; |
|
227 | |||
228 | 3 | case BasePageView::DYNAMIC_TYPE: |
|
229 | $this->compileDynamicPageView($pageView); |
||
230 | $this->compileStandardRedirects($pageView); |
||
231 | break; |
||
232 | |||
233 | 3 | case BasePageView::REPEATER_TYPE: |
|
234 | 3 | $this->compileRepeaterPageView($pageView); |
|
235 | 3 | $this->compileExpandedRedirects($pageView); |
|
236 | 12 | break; |
|
237 | } |
||
238 | } |
||
239 | catch (TemplateErrorInterface $e) |
||
240 | { |
||
241 | throw new FileAwareException( |
||
242 | $e->getMessage(), |
||
243 | $e->getCode(), |
||
244 | $e, |
||
245 | $pageView->getRelativeFilePath(), |
||
246 | $e->getTemplateLine() + $pageView->getLineOffset() |
||
247 | ); |
||
248 | } |
||
249 | 12 | } |
|
250 | |||
251 | /** |
||
252 | * Write the compiled output for a static PageView. |
||
253 | * |
||
254 | * @since 0.1.1 |
||
255 | * |
||
256 | * @throws TemplateErrorInterface |
||
257 | */ |
||
258 | 10 | private function compileStaticPageView(StaticPageView &$pageView) |
|
266 | |||
267 | /** |
||
268 | * Write the compiled output for a dynamic PageView. |
||
269 | * |
||
270 | * @param DynamicPageView $pageView |
||
271 | * |
||
272 | * @since 0.1.1 |
||
273 | * |
||
274 | * @throws TemplateErrorInterface |
||
275 | */ |
||
276 | private function compileDynamicPageView(DynamicPageView &$pageView) |
||
301 | |||
302 | /** |
||
303 | * Write the compiled output for a repeater PageView. |
||
304 | * |
||
305 | * @param RepeaterPageView $pageView |
||
306 | * |
||
307 | * @since 0.1.1 |
||
308 | * |
||
309 | * @throws TemplateErrorInterface |
||
310 | */ |
||
311 | 3 | private function compileRepeaterPageView(RepeaterPageView &$pageView) |
|
325 | |||
326 | /** |
||
327 | * Write the given $output to the $targetFile as a $fileType PageView. |
||
328 | * |
||
329 | * @param string $targetFile |
||
330 | * @param string $output |
||
331 | * @param string $fileType |
||
332 | */ |
||
333 | 12 | private function writeToFilesystem($targetFile, $output, $fileType) |
|
341 | |||
342 | /// |
||
343 | // Redirect handling |
||
344 | /// |
||
345 | |||
346 | /** |
||
347 | * Write redirects for standard redirects. |
||
348 | * |
||
349 | * @throws TemplateErrorInterface |
||
350 | * |
||
351 | * @since 0.1.1 |
||
352 | */ |
||
353 | 10 | private function compileStandardRedirects(PermalinkDocument &$pageView) |
|
371 | |||
372 | /** |
||
373 | * Write redirects for expanded redirects. |
||
374 | * |
||
375 | * @param RepeaterPageView $pageView |
||
376 | * |
||
377 | * @since 0.1.1 |
||
378 | */ |
||
379 | 3 | private function compileExpandedRedirects(RepeaterPageView &$pageView) |
|
405 | |||
406 | /// |
||
407 | // Twig Functionality |
||
408 | /// |
||
409 | |||
410 | /** |
||
411 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
412 | * |
||
413 | * @param TemplateInterface $template |
||
414 | * @param RepeaterPageView $pageView |
||
415 | * @param ExpandedValue $expandedValue |
||
416 | * |
||
417 | * @since 0.1.1 |
||
418 | * |
||
419 | * @return string |
||
420 | */ |
||
421 | 3 | private function buildRepeaterPageViewHTML(TemplateInterface &$template, RepeaterPageView &$pageView, ExpandedValue &$expandedValue) |
|
445 | |||
446 | /** |
||
447 | * Get the compiled HTML for a specific ContentItem. |
||
448 | * |
||
449 | * @since 0.1.1 |
||
450 | * |
||
451 | * @return string |
||
452 | */ |
||
453 | View Code Duplication | private function buildDynamicPageViewHTML(TemplateInterface &$template, TemplateReadyDocument &$twigItem) |
|
472 | |||
473 | /** |
||
474 | * Get the compiled HTML for a static PageView. |
||
475 | * |
||
476 | * @since 0.1.1 |
||
477 | * |
||
478 | * @throws TemplateErrorInterface |
||
479 | * |
||
480 | * @return string |
||
481 | */ |
||
482 | 10 | View Code Duplication | private function buildStaticPageViewHTML(StaticPageView &$pageView) |
502 | |||
503 | /** |
||
504 | * Create a Twig template that just needs an array to render. |
||
505 | * |
||
506 | * @since 0.1.1 |
||
507 | * |
||
508 | * @throws TemplateErrorInterface |
||
509 | * |
||
510 | * @return TemplateInterface |
||
511 | */ |
||
512 | 12 | private function createTwigTemplate(BasePageView &$pageView) |
|
538 | } |
||
539 |
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.