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 | // IO Functionality |
||
125 | /// |
||
126 | |||
127 | /** |
||
128 | * Compile all of the PageViews registered with the compiler. |
||
129 | * |
||
130 | * @since 0.1.0 |
||
131 | */ |
||
132 | 12 | public function compileAll() |
|
139 | |||
140 | /** |
||
141 | * Compile an individual PageView item. |
||
142 | * |
||
143 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
144 | * respective target file. |
||
145 | * |
||
146 | * @param DynamicPageView|RepeaterPageView|StaticPageView $pageView The PageView that needs to be compiled |
||
147 | * |
||
148 | * @since 0.1.1 |
||
149 | */ |
||
150 | 12 | public function compilePageView(BasePageView &$pageView) |
|
189 | |||
190 | /** |
||
191 | * Write the compiled output for a static PageView. |
||
192 | * |
||
193 | * @since 0.1.1 |
||
194 | * |
||
195 | * @throws TemplateErrorInterface |
||
196 | */ |
||
197 | 10 | private function compileStaticPageView(StaticPageView &$pageView) |
|
207 | |||
208 | /** |
||
209 | * Write the compiled output for a dynamic PageView. |
||
210 | * |
||
211 | * @param DynamicPageView $pageView |
||
212 | * |
||
213 | * @since 0.1.1 |
||
214 | * |
||
215 | * @throws TemplateErrorInterface |
||
216 | */ |
||
217 | private function compileDynamicPageView(DynamicPageView &$pageView) |
||
242 | |||
243 | /** |
||
244 | * Write the compiled output for a repeater PageView. |
||
245 | * |
||
246 | * @param RepeaterPageView $pageView |
||
247 | * |
||
248 | * @since 0.1.1 |
||
249 | * |
||
250 | * @throws TemplateErrorInterface |
||
251 | */ |
||
252 | 3 | private function compileRepeaterPageView(RepeaterPageView &$pageView) |
|
270 | |||
271 | /** |
||
272 | * Write the given $output to the $targetFile as a $fileType PageView. |
||
273 | * |
||
274 | * @param string $targetFile |
||
275 | * @param string $output |
||
276 | * @param string $fileType |
||
277 | */ |
||
278 | 12 | private function writeToFilesystem($targetFile, $output, $fileType) |
|
286 | |||
287 | /// |
||
288 | // Redirect handling |
||
289 | /// |
||
290 | |||
291 | /** |
||
292 | * Write redirects for standard redirects. |
||
293 | * |
||
294 | * @throws TemplateErrorInterface |
||
295 | * |
||
296 | * @since 0.1.1 |
||
297 | */ |
||
298 | 10 | private function compileStandardRedirects(PermalinkDocument &$pageView) |
|
316 | |||
317 | /** |
||
318 | * Write redirects for expanded redirects. |
||
319 | * |
||
320 | * @param RepeaterPageView $pageView |
||
321 | * |
||
322 | * @since 0.1.1 |
||
323 | */ |
||
324 | 3 | private function compileExpandedRedirects(RepeaterPageView &$pageView) |
|
350 | |||
351 | /// |
||
352 | // Twig Functionality |
||
353 | /// |
||
354 | |||
355 | /** |
||
356 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
357 | * |
||
358 | * @param TemplateInterface $template |
||
359 | * @param RepeaterPageView $pageView |
||
360 | * @param ExpandedValue $expandedValue |
||
361 | * |
||
362 | * @since 0.1.1 |
||
363 | * |
||
364 | * @return string |
||
365 | */ |
||
366 | 3 | private function renderRepeaterPageView(TemplateInterface &$template, RepeaterPageView &$pageView, ExpandedValue &$expandedValue) |
|
390 | |||
391 | /** |
||
392 | * Get the compiled HTML for a specific ContentItem. |
||
393 | * |
||
394 | * @since 0.1.1 |
||
395 | * |
||
396 | * @return string |
||
397 | */ |
||
398 | View Code Duplication | private function renderDynamicPageView(TemplateInterface &$template, TemplateReadyDocument &$twigItem) |
|
417 | |||
418 | /** |
||
419 | * Get the compiled HTML for a static PageView. |
||
420 | * |
||
421 | * @since 0.1.1 |
||
422 | * |
||
423 | * @throws TemplateErrorInterface |
||
424 | * |
||
425 | * @return string |
||
426 | */ |
||
427 | 10 | View Code Duplication | private function renderStaticPageView(StaticPageView &$pageView) |
447 | |||
448 | /** |
||
449 | * Create a Twig template that just needs an array to render. |
||
450 | * |
||
451 | * @since 0.1.1 |
||
452 | * |
||
453 | * @throws TemplateErrorInterface |
||
454 | * |
||
455 | * @return TemplateInterface |
||
456 | */ |
||
457 | 12 | private function createTwigTemplate(BasePageView &$pageView) |
|
483 | } |
||
484 |
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: