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 | |||
69 | 12 | public function __construct( |
|
96 | |||
97 | /** |
||
98 | * @param Folder $folder |
||
99 | */ |
||
100 | 12 | public function setTargetFolder(Folder $folder) |
|
104 | |||
105 | /** |
||
106 | * @param string $themeName |
||
107 | */ |
||
108 | public function setThemeName($themeName) |
||
112 | |||
113 | /// |
||
114 | // Twig parent templates |
||
115 | /// |
||
116 | |||
117 | public function getTemplateMappings() |
||
121 | |||
122 | /// |
||
123 | // Rendering HTML Functionality |
||
124 | /// |
||
125 | |||
126 | /** |
||
127 | * Get the HTML for a Static PageView. |
||
128 | * |
||
129 | * This function just **renders** the HTML but does not write it to the filesystem. Use `compilePageView()` for that |
||
130 | * instead. |
||
131 | * |
||
132 | * @param StaticPageView $pageView |
||
133 | * |
||
134 | * @throws TemplateErrorInterface |
||
135 | * |
||
136 | * @return string the HTML for a Static PageView |
||
137 | */ |
||
138 | 10 | public function renderStaticPageView(StaticPageView $pageView) |
|
144 | |||
145 | /** |
||
146 | * Get the HTML for a Dynamic PageView and ContentItem. |
||
147 | * |
||
148 | * This function just **renders** the HTML but does not write it to the filesystem. Use `compileDynamicPageView()` |
||
149 | * for that instead. |
||
150 | * |
||
151 | * @param DynamicPageView $pageView |
||
152 | * @param TemplateReadyDocument $contentItem |
||
153 | * |
||
154 | * @throws TemplateErrorInterface |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function renderDynamicPageView(DynamicPageView $pageView, TemplateReadyDocument $contentItem) |
||
164 | |||
165 | /** |
||
166 | * Get the HTML for a Repeater PageView. |
||
167 | * |
||
168 | * @param RepeaterPageView $pageView |
||
169 | * @param ExpandedValue $expandedValue |
||
170 | * |
||
171 | * @throws TemplateErrorInterface |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | public function renderRepeaterPageView(RepeaterPageView $pageView, ExpandedValue $expandedValue) |
||
181 | |||
182 | /// |
||
183 | // IO Functionality |
||
184 | /// |
||
185 | |||
186 | /** |
||
187 | * Compile all of the PageViews registered with the compiler. |
||
188 | * |
||
189 | * @since 0.1.0 |
||
190 | */ |
||
191 | 12 | public function compileAll() |
|
198 | |||
199 | /** |
||
200 | * Compile an individual PageView item. |
||
201 | * |
||
202 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
203 | * respective target file. |
||
204 | * |
||
205 | * @param DynamicPageView|RepeaterPageView|StaticPageView $pageView The PageView that needs to be compiled |
||
206 | * |
||
207 | * @since 0.1.1 |
||
208 | */ |
||
209 | 12 | public function compilePageView(BasePageView &$pageView) |
|
248 | |||
249 | /** |
||
250 | * Write the compiled output for a static PageView. |
||
251 | * |
||
252 | * @since 0.1.1 |
||
253 | * |
||
254 | * @throws TemplateErrorInterface |
||
255 | */ |
||
256 | 10 | private function compileStaticPageView(StaticPageView &$pageView) |
|
264 | |||
265 | /** |
||
266 | * Write the compiled output for a dynamic PageView. |
||
267 | * |
||
268 | * @param DynamicPageView $pageView |
||
269 | * |
||
270 | * @since 0.1.1 |
||
271 | * |
||
272 | * @throws TemplateErrorInterface |
||
273 | */ |
||
274 | private function compileDynamicPageView(DynamicPageView &$pageView) |
||
299 | |||
300 | /** |
||
301 | * Write the compiled output for a repeater PageView. |
||
302 | * |
||
303 | * @param RepeaterPageView $pageView |
||
304 | * |
||
305 | * @since 0.1.1 |
||
306 | * |
||
307 | * @throws TemplateErrorInterface |
||
308 | */ |
||
309 | 3 | private function compileRepeaterPageView(RepeaterPageView &$pageView) |
|
327 | |||
328 | /** |
||
329 | * Write the given $output to the $targetFile as a $fileType PageView. |
||
330 | * |
||
331 | * @param string $targetFile |
||
332 | * @param string $output |
||
333 | * @param string $fileType |
||
334 | */ |
||
335 | 12 | private function writeToFilesystem($targetFile, $output, $fileType) |
|
343 | |||
344 | /// |
||
345 | // Redirect handling |
||
346 | /// |
||
347 | |||
348 | /** |
||
349 | * Write redirects for standard redirects. |
||
350 | * |
||
351 | * @throws TemplateErrorInterface |
||
352 | * |
||
353 | * @since 0.1.1 |
||
354 | */ |
||
355 | 10 | private function compileStandardRedirects(PermalinkDocument &$pageView) |
|
373 | |||
374 | /** |
||
375 | * Write redirects for expanded redirects. |
||
376 | * |
||
377 | * @param RepeaterPageView $pageView |
||
378 | * |
||
379 | * @since 0.1.1 |
||
380 | */ |
||
381 | 3 | private function compileExpandedRedirects(RepeaterPageView &$pageView) |
|
407 | |||
408 | /// |
||
409 | // Twig Functionality |
||
410 | /// |
||
411 | |||
412 | /** |
||
413 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
414 | * |
||
415 | * @param TemplateInterface $template |
||
416 | * @param RepeaterPageView $pageView |
||
417 | * @param ExpandedValue $expandedValue |
||
418 | * |
||
419 | * @since 0.1.1 |
||
420 | * |
||
421 | * @return string |
||
422 | */ |
||
423 | 3 | private function buildRepeaterPageViewHTML(TemplateInterface &$template, RepeaterPageView &$pageView, ExpandedValue &$expandedValue) |
|
447 | |||
448 | /** |
||
449 | * Get the compiled HTML for a specific ContentItem. |
||
450 | * |
||
451 | * @since 0.1.1 |
||
452 | * |
||
453 | * @return string |
||
454 | */ |
||
455 | View Code Duplication | private function buildDynamicPageViewHTML(TemplateInterface &$template, TemplateReadyDocument &$twigItem) |
|
474 | |||
475 | /** |
||
476 | * Get the compiled HTML for a static PageView. |
||
477 | * |
||
478 | * @since 0.1.1 |
||
479 | * |
||
480 | * @throws TemplateErrorInterface |
||
481 | * |
||
482 | * @return string |
||
483 | */ |
||
484 | 10 | View Code Duplication | private function buildStaticPageViewHTML(StaticPageView &$pageView) |
504 | |||
505 | /** |
||
506 | * Create a Twig template that just needs an array to render. |
||
507 | * |
||
508 | * @since 0.1.1 |
||
509 | * |
||
510 | * @throws TemplateErrorInterface |
||
511 | * |
||
512 | * @return TemplateInterface |
||
513 | */ |
||
514 | 12 | private function createTwigTemplate(BasePageView &$pageView) |
|
540 | } |
||
541 |
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: