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) |
||
68 | { |
||
69 | $this->redirectTemplate = $template; |
||
70 | } |
||
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 | /** |
||
100 | * Check whether a given file path is used as a parent template by a PageView |
||
101 | * |
||
102 | * @param string $filePath |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | public function isParentTemplate($filePath) |
||
107 | { |
||
108 | return isset($this->templateDependencies[$filePath]); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Rebuild all of the PageViews that used a given template as a parent |
||
113 | * |
||
114 | * @param string $filePath The file path to the parent Twig template |
||
115 | */ |
||
116 | public function refreshParent($filePath) |
||
117 | { |
||
118 | foreach ($this->templateDependencies[$filePath] as &$parentTemplate) |
||
|
|||
119 | { |
||
120 | $this->compilePageView($parentTemplate); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | /// |
||
125 | // IO Functionality |
||
126 | /// |
||
127 | |||
128 | /** |
||
129 | * Compile all of the PageViews registered with the compiler. |
||
130 | * |
||
131 | * @since 0.1.0 |
||
132 | */ |
||
133 | 14 | public function compileAll() |
|
140 | |||
141 | public function compileSome($filter = array()) |
||
142 | { |
||
152 | |||
153 | /** |
||
154 | * Compile an individual PageView item. |
||
155 | * |
||
156 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
157 | * respective target file. |
||
158 | * |
||
159 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
||
160 | * |
||
161 | * @since 0.1.1 |
||
162 | */ |
||
163 | 14 | public function compilePageView(&$pageView) |
|
188 | |||
189 | /** |
||
190 | * Write the compiled output for a static PageView. |
||
191 | * |
||
192 | * @param PageView $pageView |
||
193 | * |
||
194 | * @since 0.1.1 |
||
195 | */ |
||
196 | 10 | private function compileStaticPageView(&$pageView) |
|
204 | |||
205 | /** |
||
206 | * Write the compiled output for a dynamic PageView. |
||
207 | * |
||
208 | * @param DynamicPageView $pageView |
||
209 | * |
||
210 | * @since 0.1.1 |
||
211 | */ |
||
212 | 2 | private function compileDynamicPageViews(&$pageView) |
|
235 | |||
236 | /** |
||
237 | * Write the compiled output for a repeater PageView. |
||
238 | * |
||
239 | * @param RepeaterPageView $pageView |
||
240 | * |
||
241 | * @since 0.1.1 |
||
242 | */ |
||
243 | 2 | View Code Duplication | private function compileRepeaterPageViews(&$pageView) |
260 | |||
261 | /** |
||
262 | * @deprecated |
||
263 | * |
||
264 | * @todo This function needs to be rewritten or removed. Something |
||
265 | * |
||
266 | * @param ContentItem $contentItem |
||
267 | */ |
||
268 | View Code Duplication | public function compileContentItem(&$contentItem) |
|
281 | |||
282 | /// |
||
283 | // Redirect handling |
||
284 | /// |
||
285 | |||
286 | /** |
||
287 | * Write redirects for standard redirects. |
||
288 | * |
||
289 | * @param PageView $pageView |
||
290 | * |
||
291 | * @since 0.1.1 |
||
292 | */ |
||
293 | 12 | private function compileStandardRedirects(&$pageView) |
|
308 | |||
309 | /** |
||
310 | * Write redirects for expanded redirects. |
||
311 | * |
||
312 | * @param RepeaterPageView $pageView |
||
313 | * |
||
314 | * @since 0.1.1 |
||
315 | */ |
||
316 | 2 | private function compileExpandedRedirects(&$pageView) |
|
338 | |||
339 | /// |
||
340 | // Twig Functionality |
||
341 | /// |
||
342 | |||
343 | /** |
||
344 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
345 | * |
||
346 | * @param Twig_Template $template |
||
347 | * @param PageView $pageView |
||
348 | * @param ExpandedValue $expandedValue |
||
349 | * |
||
350 | * @since 0.1.1 |
||
351 | * |
||
352 | * @return string |
||
353 | */ |
||
354 | 2 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
|
368 | |||
369 | /** |
||
370 | * Get the compiled HTML for a specific ContentItem. |
||
371 | * |
||
372 | * @param Twig_Template $template |
||
373 | * @param PageView $pageView |
||
374 | * @param ContentItem $contentItem |
||
375 | * |
||
376 | * @since 0.1.1 |
||
377 | * |
||
378 | * @return string |
||
379 | */ |
||
380 | 2 | private function renderDynamicPageView(&$template, &$pageView, &$contentItem) |
|
389 | |||
390 | /** |
||
391 | * Get the compiled HTML for a static PageView. |
||
392 | * |
||
393 | * @param PageView $pageView |
||
394 | * |
||
395 | * @since 0.1.1 |
||
396 | * |
||
397 | * @throws \Exception |
||
398 | * @throws \Throwable |
||
399 | * @throws Twig_Error_Syntax |
||
400 | * |
||
401 | * @return string |
||
402 | */ |
||
403 | 10 | private function renderStaticPageView(&$pageView) |
|
413 | |||
414 | /** |
||
415 | * Create a Twig template that just needs an array to render. |
||
416 | * |
||
417 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
418 | * |
||
419 | * @since 0.1.1 |
||
420 | * |
||
421 | * @throws \Exception |
||
422 | * @throws \Throwable |
||
423 | * @throws Twig_Error_Syntax |
||
424 | * |
||
425 | * @return Twig_Template |
||
426 | */ |
||
427 | 14 | private function createTwigTemplate(&$pageView) |
|
460 | } |
||
461 |