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 $pageViews; |
||
38 | |||
39 | /** @var Folder */ |
||
40 | private $folder; |
||
41 | |||
42 | /** @var Twig_Environment */ |
||
43 | private $twig; |
||
44 | |||
45 | 5 | public function __construct() |
|
51 | |||
52 | /** |
||
53 | * @param string|false $template |
||
54 | */ |
||
55 | public function setRedirectTemplate($template) |
||
59 | |||
60 | /** |
||
61 | * @param Folder $folder |
||
62 | */ |
||
63 | 5 | public function setTargetFolder(Folder $folder) |
|
67 | |||
68 | /** |
||
69 | * @param PageView[] $pageViews |
||
70 | */ |
||
71 | 5 | public function setPageViews(array $pageViews) |
|
75 | |||
76 | /// |
||
77 | // IO Functionality |
||
78 | /// |
||
79 | |||
80 | /** |
||
81 | * Compile all of the PageViews registered with the compiler. |
||
82 | * |
||
83 | * @since 0.1.0 |
||
84 | */ |
||
85 | 5 | public function compileAll() |
|
92 | |||
93 | /** |
||
94 | * Compile an individual PageView item. |
||
95 | * |
||
96 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
97 | * respective target file. |
||
98 | * |
||
99 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
||
100 | * |
||
101 | * @since 0.1.1 |
||
102 | */ |
||
103 | 5 | private function compilePageView(&$pageView) |
|
123 | |||
124 | /** |
||
125 | * Write the compiled output for a static PageView. |
||
126 | * |
||
127 | * @param PageView $pageView |
||
128 | * |
||
129 | * @since 0.1.1 |
||
130 | */ |
||
131 | 5 | private function compileStaticPageView(&$pageView) |
|
139 | |||
140 | /** |
||
141 | * Write the compiled output for a dynamic PageView. |
||
142 | * |
||
143 | * @param DynamicPageView $pageView |
||
144 | * |
||
145 | * @since 0.1.1 |
||
146 | */ |
||
147 | View Code Duplication | private function compileDynamicPageViews(&$pageView) |
|
161 | |||
162 | /** |
||
163 | * Write the compiled output for a repeater PageView. |
||
164 | * |
||
165 | * @param RepeaterPageView $pageView |
||
166 | * |
||
167 | * @since 0.1.1 |
||
168 | */ |
||
169 | View Code Duplication | private function compileRepeaterPageViews(&$pageView) |
|
186 | |||
187 | /// |
||
188 | // Redirect handling |
||
189 | /// |
||
190 | |||
191 | /** |
||
192 | * Write redirects for standard redirects. |
||
193 | * |
||
194 | * @param PageView $pageView |
||
195 | * |
||
196 | * @since 0.1.1 |
||
197 | */ |
||
198 | 5 | private function compileStandardRedirects(&$pageView) |
|
213 | |||
214 | /** |
||
215 | * Write redirects for expanded redirects. |
||
216 | * |
||
217 | * @param RepeaterPageView $pageView |
||
218 | * |
||
219 | * @since 0.1.1 |
||
220 | */ |
||
221 | private function compileExpandedRedirects(&$pageView) |
||
243 | |||
244 | /// |
||
245 | // Twig Functionality |
||
246 | /// |
||
247 | |||
248 | /** |
||
249 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
250 | * |
||
251 | * @param Twig_Template $template |
||
252 | * @param PageView $pageView |
||
253 | * @param ExpandedValue $expandedValue |
||
254 | * |
||
255 | * @since 0.1.1 |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
||
273 | |||
274 | /** |
||
275 | * Get the compiled HTML for a specific ContentItem. |
||
276 | * |
||
277 | * @param Twig_Template $template |
||
278 | * @param PageView $pageView |
||
279 | * @param ContentItem $contentItem |
||
280 | * |
||
281 | * @since 0.1.1 |
||
282 | * |
||
283 | * @return string |
||
284 | */ |
||
285 | private function renderDynamicPageView(&$template, &$pageView, &$contentItem) |
||
294 | |||
295 | /** |
||
296 | * Get the compiled HTML for a static PageView. |
||
297 | * |
||
298 | * @param PageView $pageView |
||
299 | * |
||
300 | * @since 0.1.1 |
||
301 | * |
||
302 | * @throws \Exception |
||
303 | * @throws \Throwable |
||
304 | * @throws Twig_Error_Syntax |
||
305 | * |
||
306 | * @return string |
||
307 | */ |
||
308 | 5 | private function renderStaticPageView(&$pageView) |
|
318 | |||
319 | /** |
||
320 | * Create a Twig template that just needs an array to render. |
||
321 | * |
||
322 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
323 | * |
||
324 | * @since 0.1.1 |
||
325 | * |
||
326 | * @throws \Exception |
||
327 | * @throws \Throwable |
||
328 | * @throws Twig_Error_Syntax |
||
329 | * |
||
330 | * @return Twig_Template |
||
331 | */ |
||
332 | 5 | private function createTwigTemplate(&$pageView) |
|
350 | } |
||
351 |
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.