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 |
||
32 | class Compiler extends BaseManager |
||
33 | { |
||
34 | /** @var string|false */ |
||
35 | private $redirectTemplate; |
||
36 | |||
37 | /** @var PageView[] */ |
||
38 | private $pageViewsFlattened; |
||
39 | |||
40 | /** @var PageView[][] */ |
||
41 | private $pageViews; |
||
42 | |||
43 | /** @var Folder */ |
||
44 | private $folder; |
||
45 | |||
46 | /** @var Twig_Environment */ |
||
47 | private $twig; |
||
48 | |||
49 | 13 | public function __construct() |
|
55 | |||
56 | /** |
||
57 | * @param string|false $template |
||
58 | */ |
||
59 | public function setRedirectTemplate($template) |
||
63 | |||
64 | /** |
||
65 | * @param Folder $folder |
||
66 | */ |
||
67 | 13 | public function setTargetFolder(Folder $folder) |
|
71 | |||
72 | /** |
||
73 | * @param PageView[][] $pageViews |
||
74 | * @param PageView[] $pageViewsFlattened |
||
75 | */ |
||
76 | 13 | public function setPageViews(array &$pageViews, array &$pageViewsFlattened) |
|
81 | |||
82 | /// |
||
83 | // IO Functionality |
||
84 | /// |
||
85 | |||
86 | /** |
||
87 | * Compile all of the PageViews registered with the compiler. |
||
88 | * |
||
89 | * @since 0.1.0 |
||
90 | */ |
||
91 | 13 | public function compileAll() |
|
98 | |||
99 | public function compileSome($filter = array()) |
||
110 | |||
111 | /** |
||
112 | * Compile an individual PageView item. |
||
113 | * |
||
114 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
115 | * respective target file. |
||
116 | * |
||
117 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
||
118 | * |
||
119 | * @since 0.1.1 |
||
120 | */ |
||
121 | 13 | private function compilePageView(&$pageView) |
|
146 | |||
147 | /** |
||
148 | * Write the compiled output for a static PageView. |
||
149 | * |
||
150 | * @param PageView $pageView |
||
151 | * |
||
152 | * @since 0.1.1 |
||
153 | */ |
||
154 | 10 | private function compileStaticPageView(&$pageView) |
|
162 | |||
163 | /** |
||
164 | * Write the compiled output for a dynamic PageView. |
||
165 | * |
||
166 | * @param DynamicPageView $pageView |
||
167 | * |
||
168 | * @since 0.1.1 |
||
169 | */ |
||
170 | 2 | private function compileDynamicPageViews(&$pageView) |
|
193 | |||
194 | /** |
||
195 | * Write the compiled output for a repeater PageView. |
||
196 | * |
||
197 | * @param RepeaterPageView $pageView |
||
198 | * |
||
199 | * @since 0.1.1 |
||
200 | */ |
||
201 | 2 | View Code Duplication | private function compileRepeaterPageViews(&$pageView) |
218 | |||
219 | /** |
||
220 | * @deprecated |
||
221 | * |
||
222 | * @todo This function needs to be rewritten or removed. Something |
||
223 | * |
||
224 | * @param ContentItem $contentItem |
||
225 | */ |
||
226 | View Code Duplication | public function compileContentItem(&$contentItem) |
|
239 | |||
240 | /// |
||
241 | // Redirect handling |
||
242 | /// |
||
243 | |||
244 | /** |
||
245 | * Write redirects for standard redirects. |
||
246 | * |
||
247 | * @param PageView $pageView |
||
248 | * |
||
249 | * @since 0.1.1 |
||
250 | */ |
||
251 | 11 | private function compileStandardRedirects(&$pageView) |
|
266 | |||
267 | /** |
||
268 | * Write redirects for expanded redirects. |
||
269 | * |
||
270 | * @param RepeaterPageView $pageView |
||
271 | * |
||
272 | * @since 0.1.1 |
||
273 | */ |
||
274 | 2 | private function compileExpandedRedirects(&$pageView) |
|
296 | |||
297 | /// |
||
298 | // Twig Functionality |
||
299 | /// |
||
300 | |||
301 | /** |
||
302 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
303 | * |
||
304 | * @param Twig_Template $template |
||
305 | * @param PageView $pageView |
||
306 | * @param ExpandedValue $expandedValue |
||
307 | * |
||
308 | * @since 0.1.1 |
||
309 | * |
||
310 | * @return string |
||
311 | */ |
||
312 | 2 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
|
326 | |||
327 | /** |
||
328 | * Get the compiled HTML for a specific ContentItem. |
||
329 | * |
||
330 | * @param Twig_Template $template |
||
331 | * @param PageView $pageView |
||
332 | * @param ContentItem $contentItem |
||
333 | * |
||
334 | * @since 0.1.1 |
||
335 | * |
||
336 | * @return string |
||
337 | */ |
||
338 | 1 | private function renderDynamicPageView(&$template, &$pageView, &$contentItem) |
|
347 | |||
348 | /** |
||
349 | * Get the compiled HTML for a static PageView. |
||
350 | * |
||
351 | * @param PageView $pageView |
||
352 | * |
||
353 | * @since 0.1.1 |
||
354 | * |
||
355 | * @throws \Exception |
||
356 | * @throws \Throwable |
||
357 | * @throws Twig_Error_Syntax |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | 10 | private function renderStaticPageView(&$pageView) |
|
371 | |||
372 | /** |
||
373 | * Create a Twig template that just needs an array to render. |
||
374 | * |
||
375 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
376 | * |
||
377 | * @since 0.1.1 |
||
378 | * |
||
379 | * @throws \Exception |
||
380 | * @throws \Throwable |
||
381 | * @throws Twig_Error_Syntax |
||
382 | * |
||
383 | * @return Twig_Template |
||
384 | */ |
||
385 | 13 | private function createTwigTemplate(&$pageView) |
|
403 | } |
||
404 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.