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 |
||
33 | class Compiler extends BaseManager |
||
34 | { |
||
35 | /** @var string|false */ |
||
36 | private $redirectTemplate; |
||
37 | |||
38 | /** @var Twig_Template[] */ |
||
39 | private $templateDependencies; |
||
40 | |||
41 | /** @var PageView[] */ |
||
42 | private $pageViewsFlattened; |
||
43 | |||
44 | /** @var PageView[][] */ |
||
45 | private $pageViews; |
||
46 | |||
47 | /** @var Folder */ |
||
48 | private $folder; |
||
49 | |||
50 | /** @var string */ |
||
51 | private $theme; |
||
52 | |||
53 | /** @var Twig_Environment */ |
||
54 | private $twig; |
||
55 | |||
56 | 14 | public function __construct() |
|
63 | |||
64 | /** |
||
65 | * @param string|false $template |
||
66 | */ |
||
67 | public function setRedirectTemplate($template) |
||
71 | |||
72 | /** |
||
73 | * @param Folder $folder |
||
74 | */ |
||
75 | 14 | public function setTargetFolder(Folder $folder) |
|
79 | |||
80 | /** |
||
81 | * @param PageView[][] $pageViews |
||
82 | * @param PageView[] $pageViewsFlattened |
||
83 | */ |
||
84 | 14 | public function setPageViews(array &$pageViews, array &$pageViewsFlattened) |
|
89 | |||
90 | public function setThemeName($themeName) |
||
94 | |||
95 | /// |
||
96 | // Twig parent templates |
||
97 | /// |
||
98 | |||
99 | public function isParentTemplate($filePath) |
||
103 | |||
104 | public function refreshParent($filePath) |
||
111 | |||
112 | /// |
||
113 | // IO Functionality |
||
114 | /// |
||
115 | |||
116 | /** |
||
117 | * Compile all of the PageViews registered with the compiler. |
||
118 | * |
||
119 | * @since 0.1.0 |
||
120 | */ |
||
121 | 14 | public function compileAll() |
|
128 | |||
129 | public function compileSome($filter = array()) |
||
140 | |||
141 | /** |
||
142 | * Compile an individual PageView item. |
||
143 | * |
||
144 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
145 | * respective target file. |
||
146 | * |
||
147 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
||
148 | * |
||
149 | * @since 0.1.1 |
||
150 | */ |
||
151 | 14 | public function compilePageView(&$pageView) |
|
176 | |||
177 | /** |
||
178 | * Write the compiled output for a static PageView. |
||
179 | * |
||
180 | * @param PageView $pageView |
||
181 | * |
||
182 | * @since 0.1.1 |
||
183 | */ |
||
184 | 10 | private function compileStaticPageView(&$pageView) |
|
192 | |||
193 | /** |
||
194 | * Write the compiled output for a dynamic PageView. |
||
195 | * |
||
196 | * @param DynamicPageView $pageView |
||
197 | * |
||
198 | * @since 0.1.1 |
||
199 | */ |
||
200 | 2 | private function compileDynamicPageViews(&$pageView) |
|
223 | |||
224 | /** |
||
225 | * Write the compiled output for a repeater PageView. |
||
226 | * |
||
227 | * @param RepeaterPageView $pageView |
||
228 | * |
||
229 | * @since 0.1.1 |
||
230 | */ |
||
231 | 2 | View Code Duplication | private function compileRepeaterPageViews(&$pageView) |
248 | |||
249 | /** |
||
250 | * @deprecated |
||
251 | * |
||
252 | * @todo This function needs to be rewritten or removed. Something |
||
253 | * |
||
254 | * @param ContentItem $contentItem |
||
255 | */ |
||
256 | View Code Duplication | public function compileContentItem(&$contentItem) |
|
269 | |||
270 | /// |
||
271 | // Redirect handling |
||
272 | /// |
||
273 | |||
274 | /** |
||
275 | * Write redirects for standard redirects. |
||
276 | * |
||
277 | * @param PageView $pageView |
||
278 | * |
||
279 | * @since 0.1.1 |
||
280 | */ |
||
281 | 12 | private function compileStandardRedirects(&$pageView) |
|
296 | |||
297 | /** |
||
298 | * Write redirects for expanded redirects. |
||
299 | * |
||
300 | * @param RepeaterPageView $pageView |
||
301 | * |
||
302 | * @since 0.1.1 |
||
303 | */ |
||
304 | 2 | private function compileExpandedRedirects(&$pageView) |
|
326 | |||
327 | /// |
||
328 | // Twig Functionality |
||
329 | /// |
||
330 | |||
331 | /** |
||
332 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
333 | * |
||
334 | * @param Twig_Template $template |
||
335 | * @param PageView $pageView |
||
336 | * @param ExpandedValue $expandedValue |
||
337 | * |
||
338 | * @since 0.1.1 |
||
339 | * |
||
340 | * @return string |
||
341 | */ |
||
342 | 2 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
|
356 | |||
357 | /** |
||
358 | * Get the compiled HTML for a specific ContentItem. |
||
359 | * |
||
360 | * @param Twig_Template $template |
||
361 | * @param PageView $pageView |
||
362 | * @param ContentItem $contentItem |
||
363 | * |
||
364 | * @since 0.1.1 |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | 2 | private function renderDynamicPageView(&$template, &$pageView, &$contentItem) |
|
377 | |||
378 | /** |
||
379 | * Get the compiled HTML for a static PageView. |
||
380 | * |
||
381 | * @param PageView $pageView |
||
382 | * |
||
383 | * @since 0.1.1 |
||
384 | * |
||
385 | * @throws \Exception |
||
386 | * @throws \Throwable |
||
387 | * @throws Twig_Error_Syntax |
||
388 | * |
||
389 | * @return string |
||
390 | */ |
||
391 | 10 | private function renderStaticPageView(&$pageView) |
|
401 | |||
402 | /** |
||
403 | * Create a Twig template that just needs an array to render. |
||
404 | * |
||
405 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
406 | * |
||
407 | * @since 0.1.1 |
||
408 | * |
||
409 | * @throws \Exception |
||
410 | * @throws \Throwable |
||
411 | * @throws Twig_Error_Syntax |
||
412 | * |
||
413 | * @return Twig_Template |
||
414 | */ |
||
415 | 14 | private function createTwigTemplate(&$pageView) |
|
448 | } |
||
449 |