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 |
||
31 | class Compiler extends BaseManager |
||
32 | { |
||
33 | /** @var string|false */ |
||
34 | private $redirectTemplate; |
||
35 | |||
36 | /** @var PageView[] */ |
||
37 | private $pageViewsFlattened; |
||
38 | |||
39 | /** @var PageView[][] */ |
||
40 | private $pageViews; |
||
41 | |||
42 | /** @var Folder */ |
||
43 | private $folder; |
||
44 | |||
45 | /** @var Twig_Environment */ |
||
46 | private $twig; |
||
47 | |||
48 | 13 | public function __construct() |
|
54 | |||
55 | /** |
||
56 | * @param string|false $template |
||
57 | */ |
||
58 | public function setRedirectTemplate($template) |
||
59 | { |
||
60 | $this->redirectTemplate = $template; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param Folder $folder |
||
65 | */ |
||
66 | 13 | public function setTargetFolder(Folder $folder) |
|
70 | |||
71 | /** |
||
72 | * @param PageView[][] $pageViews |
||
73 | * @param PageView[] $pageViewsFlattened |
||
74 | */ |
||
75 | 13 | public function setPageViews(array &$pageViews, array &$pageViewsFlattened) |
|
80 | |||
81 | /// |
||
82 | // IO Functionality |
||
83 | /// |
||
84 | |||
85 | /** |
||
86 | * Compile all of the PageViews registered with the compiler. |
||
87 | * |
||
88 | * @since 0.1.0 |
||
89 | */ |
||
90 | 13 | public function compileAll() |
|
91 | { |
||
92 | 13 | foreach ($this->pageViewsFlattened as &$pageView) |
|
93 | { |
||
94 | 13 | $this->compilePageView($pageView); |
|
95 | } |
||
96 | 13 | } |
|
97 | |||
98 | public function compileSome($filter = array()) |
||
99 | { |
||
100 | /** @var PageView $pageView */ |
||
101 | foreach ($this->pageViewsFlattened as &$pageView) |
||
102 | { |
||
103 | if ($pageView->hasTwigDependency($filter['namespace'], $filter['dependency'])) |
||
104 | { |
||
105 | $this->compilePageView($pageView); |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Compile an individual PageView item. |
||
112 | * |
||
113 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
114 | * respective target file. |
||
115 | * |
||
116 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
||
117 | * |
||
118 | * @since 0.1.1 |
||
119 | */ |
||
120 | 13 | private function compilePageView(&$pageView) |
|
121 | { |
||
122 | 13 | switch ($pageView->getType()) |
|
123 | { |
||
124 | 13 | case PageView::STATIC_TYPE: |
|
125 | 10 | $this->compileStaticPageView($pageView); |
|
126 | 10 | $this->compileStandardRedirects($pageView); |
|
127 | 10 | break; |
|
128 | |||
129 | 3 | case PageView::DYNAMIC_TYPE: |
|
130 | 1 | $this->compileDynamicPageViews($pageView); |
|
1 ignored issue
–
show
|
|||
131 | 1 | $this->compileStandardRedirects($pageView); |
|
132 | 1 | break; |
|
133 | |||
134 | 2 | case PageView::REPEATER_TYPE: |
|
135 | 2 | $this->compileRepeaterPageViews($pageView); |
|
1 ignored issue
–
show
|
|||
136 | 2 | $this->compileExpandedRedirects($pageView); |
|
137 | 2 | break; |
|
138 | } |
||
139 | 13 | } |
|
140 | |||
141 | /** |
||
142 | * Write the compiled output for a static PageView. |
||
143 | * |
||
144 | * @param PageView $pageView |
||
145 | * |
||
146 | * @since 0.1.1 |
||
147 | */ |
||
148 | 10 | private function compileStaticPageView(&$pageView) |
|
156 | |||
157 | /** |
||
158 | * Write the compiled output for a dynamic PageView. |
||
159 | * |
||
160 | * @param DynamicPageView $pageView |
||
161 | * |
||
162 | * @since 0.1.1 |
||
163 | */ |
||
164 | 1 | View Code Duplication | private function compileDynamicPageViews(&$pageView) |
178 | |||
179 | /** |
||
180 | * Write the compiled output for a repeater PageView. |
||
181 | * |
||
182 | * @param RepeaterPageView $pageView |
||
183 | * |
||
184 | * @since 0.1.1 |
||
185 | */ |
||
186 | 2 | View Code Duplication | private function compileRepeaterPageViews(&$pageView) |
203 | |||
204 | /** |
||
205 | * @deprecated |
||
206 | * |
||
207 | * @todo This function needs to be rewritten or removed. Something |
||
208 | * |
||
209 | * @param ContentItem $contentItem |
||
210 | */ |
||
211 | View Code Duplication | public function compileContentItem(&$contentItem) |
|
224 | |||
225 | /// |
||
226 | // Redirect handling |
||
227 | /// |
||
228 | |||
229 | /** |
||
230 | * Write redirects for standard redirects. |
||
231 | * |
||
232 | * @param PageView $pageView |
||
233 | * |
||
234 | * @since 0.1.1 |
||
235 | */ |
||
236 | 11 | private function compileStandardRedirects(&$pageView) |
|
251 | |||
252 | /** |
||
253 | * Write redirects for expanded redirects. |
||
254 | * |
||
255 | * @param RepeaterPageView $pageView |
||
256 | * |
||
257 | * @since 0.1.1 |
||
258 | */ |
||
259 | 2 | private function compileExpandedRedirects(&$pageView) |
|
281 | |||
282 | /// |
||
283 | // Twig Functionality |
||
284 | /// |
||
285 | |||
286 | /** |
||
287 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
288 | * |
||
289 | * @param Twig_Template $template |
||
290 | * @param PageView $pageView |
||
291 | * @param ExpandedValue $expandedValue |
||
292 | * |
||
293 | * @since 0.1.1 |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | 2 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
|
311 | |||
312 | /** |
||
313 | * Get the compiled HTML for a specific ContentItem. |
||
314 | * |
||
315 | * @param Twig_Template $template |
||
316 | * @param PageView $pageView |
||
317 | * @param ContentItem $contentItem |
||
318 | * |
||
319 | * @since 0.1.1 |
||
320 | * |
||
321 | * @return string |
||
322 | */ |
||
323 | 1 | private function renderDynamicPageView(&$template, &$pageView, &$contentItem) |
|
332 | |||
333 | /** |
||
334 | * Get the compiled HTML for a static PageView. |
||
335 | * |
||
336 | * @param PageView $pageView |
||
337 | * |
||
338 | * @since 0.1.1 |
||
339 | * |
||
340 | * @throws \Exception |
||
341 | * @throws \Throwable |
||
342 | * @throws Twig_Error_Syntax |
||
343 | * |
||
344 | * @return string |
||
345 | */ |
||
346 | 10 | private function renderStaticPageView(&$pageView) |
|
356 | |||
357 | /** |
||
358 | * Create a Twig template that just needs an array to render. |
||
359 | * |
||
360 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
361 | * |
||
362 | * @since 0.1.1 |
||
363 | * |
||
364 | * @throws \Exception |
||
365 | * @throws \Throwable |
||
366 | * @throws Twig_Error_Syntax |
||
367 | * |
||
368 | * @return Twig_Template |
||
369 | */ |
||
370 | 13 | private function createTwigTemplate(&$pageView) |
|
388 | } |
||
389 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.