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 |
||
40 | class Compiler |
||
41 | { |
||
42 | /** @var string|false */ |
||
43 | private $redirectTemplate; |
||
44 | |||
45 | /** |
||
46 | * All of the PageViews handled by this Compiler instance indexed by their file paths relative to the site root. |
||
47 | * |
||
48 | * ``` |
||
49 | * array['_pages/index.html.twig'] = &PageView; |
||
50 | * ``` |
||
51 | * |
||
52 | * @var BasePageView[] |
||
53 | */ |
||
54 | private $pageViewsFlattened; |
||
55 | |||
56 | /** @var string[] */ |
||
57 | private $templateMapping; |
||
58 | |||
59 | /** @var Folder */ |
||
60 | private $folder; |
||
61 | |||
62 | /** @var string */ |
||
63 | private $theme; |
||
64 | |||
65 | private $templateBridge; |
||
66 | private $pageManager; |
||
67 | private $eventDispatcher; |
||
68 | private $configuration; |
||
69 | |||
70 | 12 | public function __construct( |
|
97 | |||
98 | /** |
||
99 | * @param Folder $folder |
||
100 | */ |
||
101 | 12 | public function setTargetFolder(Folder $folder) |
|
105 | |||
106 | /** |
||
107 | * @param string $themeName |
||
108 | */ |
||
109 | public function setThemeName($themeName) |
||
113 | |||
114 | /// |
||
115 | // Twig parent templates |
||
116 | /// |
||
117 | |||
118 | public function getTemplateMappings() |
||
122 | |||
123 | /// |
||
124 | // Rendering HTML Functionality |
||
125 | /// |
||
126 | |||
127 | /** |
||
128 | * Get the HTML for a Static PageView. |
||
129 | * |
||
130 | * This function just **renders** the HTML but does not write it to the filesystem. Use `compilePageView()` for that |
||
131 | * instead. |
||
132 | * |
||
133 | * @param StaticPageView $pageView |
||
134 | * |
||
135 | * @throws TemplateErrorInterface |
||
136 | * |
||
137 | * @return string The HTML for a Static PageView. |
||
138 | */ |
||
139 | 10 | public function renderStaticPageView(StaticPageView $pageView) |
|
145 | |||
146 | /** |
||
147 | * Get the HTML for a Dynamic PageView and ContentItem. |
||
148 | * |
||
149 | * This function just **renders** the HTML but does not write it to the filesystem. Use `compileDynamicPageView()` |
||
150 | * for that instead. |
||
151 | * |
||
152 | * @param DynamicPageView $pageView |
||
153 | * @param TemplateReadyDocument $contentItem |
||
154 | * |
||
155 | * @throws TemplateErrorInterface |
||
156 | * |
||
157 | * @return string |
||
158 | */ |
||
159 | public function renderDynamicPageView(DynamicPageView $pageView, TemplateReadyDocument $contentItem) |
||
165 | |||
166 | /** |
||
167 | * Get the HTML for a Repeater PageView. |
||
168 | * |
||
169 | * @param RepeaterPageView $pageView |
||
170 | * @param ExpandedValue $expandedValue |
||
171 | * |
||
172 | * @throws TemplateErrorInterface |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | public function renderRepeaterPageView(RepeaterPageView $pageView, ExpandedValue $expandedValue) |
||
182 | |||
183 | /// |
||
184 | // IO Functionality |
||
185 | /// |
||
186 | |||
187 | /** |
||
188 | * Compile all of the PageViews registered with the compiler. |
||
189 | * |
||
190 | * @since 0.1.0 |
||
191 | */ |
||
192 | 12 | public function compileAll() |
|
199 | |||
200 | /** |
||
201 | * Compile an individual PageView item. |
||
202 | * |
||
203 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
204 | * respective target file. |
||
205 | * |
||
206 | * @param DynamicPageView|RepeaterPageView|StaticPageView $pageView The PageView that needs to be compiled |
||
207 | * |
||
208 | * @since 0.1.1 |
||
209 | */ |
||
210 | 12 | public function compilePageView(BasePageView &$pageView) |
|
249 | |||
250 | /** |
||
251 | * Write the compiled output for a static PageView. |
||
252 | * |
||
253 | * @since 0.1.1 |
||
254 | * |
||
255 | * @throws TemplateErrorInterface |
||
256 | */ |
||
257 | 10 | private function compileStaticPageView(StaticPageView &$pageView) |
|
265 | |||
266 | /** |
||
267 | * Write the compiled output for a dynamic PageView. |
||
268 | * |
||
269 | * @param DynamicPageView $pageView |
||
270 | * |
||
271 | * @since 0.1.1 |
||
272 | * |
||
273 | * @throws TemplateErrorInterface |
||
274 | */ |
||
275 | private function compileDynamicPageView(DynamicPageView &$pageView) |
||
300 | |||
301 | /** |
||
302 | * Write the compiled output for a repeater PageView. |
||
303 | * |
||
304 | * @param RepeaterPageView $pageView |
||
305 | * |
||
306 | * @since 0.1.1 |
||
307 | * |
||
308 | * @throws TemplateErrorInterface |
||
309 | */ |
||
310 | 3 | private function compileRepeaterPageView(RepeaterPageView &$pageView) |
|
328 | |||
329 | /** |
||
330 | * Write the given $output to the $targetFile as a $fileType PageView. |
||
331 | * |
||
332 | * @param string $targetFile |
||
333 | * @param string $output |
||
334 | * @param string $fileType |
||
335 | */ |
||
336 | 12 | private function writeToFilesystem($targetFile, $output, $fileType) |
|
344 | |||
345 | /// |
||
346 | // Redirect handling |
||
347 | /// |
||
348 | |||
349 | /** |
||
350 | * Write redirects for standard redirects. |
||
351 | * |
||
352 | * @throws TemplateErrorInterface |
||
353 | * |
||
354 | * @since 0.1.1 |
||
355 | */ |
||
356 | 10 | private function compileStandardRedirects(PermalinkDocument &$pageView) |
|
374 | |||
375 | /** |
||
376 | * Write redirects for expanded redirects. |
||
377 | * |
||
378 | * @param RepeaterPageView $pageView |
||
379 | * |
||
380 | * @since 0.1.1 |
||
381 | */ |
||
382 | 3 | private function compileExpandedRedirects(RepeaterPageView &$pageView) |
|
408 | |||
409 | /// |
||
410 | // Twig Functionality |
||
411 | /// |
||
412 | |||
413 | /** |
||
414 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
415 | * |
||
416 | * @param TemplateInterface $template |
||
417 | * @param RepeaterPageView $pageView |
||
418 | * @param ExpandedValue $expandedValue |
||
419 | * |
||
420 | * @since 0.1.1 |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | 3 | private function buildRepeaterPageViewHTML(TemplateInterface &$template, RepeaterPageView &$pageView, ExpandedValue &$expandedValue) |
|
448 | |||
449 | /** |
||
450 | * Get the compiled HTML for a specific ContentItem. |
||
451 | * |
||
452 | * @since 0.1.1 |
||
453 | * |
||
454 | * @return string |
||
455 | */ |
||
456 | View Code Duplication | private function buildDynamicPageViewHTML(TemplateInterface &$template, TemplateReadyDocument &$twigItem) |
|
475 | |||
476 | /** |
||
477 | * Get the compiled HTML for a static PageView. |
||
478 | * |
||
479 | * @since 0.1.1 |
||
480 | * |
||
481 | * @throws TemplateErrorInterface |
||
482 | * |
||
483 | * @return string |
||
484 | */ |
||
485 | 10 | View Code Duplication | private function buildStaticPageViewHTML(StaticPageView &$pageView) |
505 | |||
506 | /** |
||
507 | * Create a Twig template that just needs an array to render. |
||
508 | * |
||
509 | * @since 0.1.1 |
||
510 | * |
||
511 | * @throws TemplateErrorInterface |
||
512 | * |
||
513 | * @return TemplateInterface |
||
514 | */ |
||
515 | 12 | private function createTwigTemplate(BasePageView &$pageView) |
|
541 | } |
||
542 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: