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 | 14 | public function __construct() |
|
55 | |||
56 | /** |
||
57 | * @param string|false $template |
||
58 | */ |
||
59 | public function setRedirectTemplate($template) |
||
60 | { |
||
61 | $this->redirectTemplate = $template; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param Folder $folder |
||
66 | */ |
||
67 | 14 | public function setTargetFolder(Folder $folder) |
|
71 | |||
72 | /** |
||
73 | * @param PageView[][] $pageViews |
||
74 | * @param PageView[] $pageViewsFlattened |
||
75 | */ |
||
76 | 14 | 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 | 14 | public function compileAll() |
|
92 | { |
||
93 | 14 | foreach ($this->pageViewsFlattened as &$pageView) |
|
94 | { |
||
95 | 14 | $this->compilePageView($pageView); |
|
96 | } |
||
97 | 14 | } |
|
98 | |||
99 | public function compileSome($filter = array()) |
||
100 | { |
||
101 | /** @var PageView $pageView */ |
||
102 | foreach ($this->pageViewsFlattened as &$pageView) |
||
103 | { |
||
104 | if ($pageView->hasTwigDependency($filter['namespace'], $filter['dependency'])) |
||
105 | { |
||
106 | $this->compilePageView($pageView); |
||
107 | } |
||
108 | } |
||
109 | } |
||
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 | 14 | private function compilePageView(&$pageView) |
|
122 | { |
||
123 | 14 | $this->output->debug('Compiling {type} PageView: {pageview}', array( |
|
124 | 14 | 'pageview' => $pageView->getRelativeFilePath(), |
|
125 | 14 | 'type' => $pageView->getType() |
|
126 | )); |
||
127 | |||
128 | 14 | switch ($pageView->getType()) |
|
129 | { |
||
130 | 14 | case PageView::STATIC_TYPE: |
|
131 | 10 | $this->compileStaticPageView($pageView); |
|
132 | 10 | $this->compileStandardRedirects($pageView); |
|
133 | 10 | break; |
|
134 | |||
135 | 4 | case PageView::DYNAMIC_TYPE: |
|
136 | 2 | $this->compileDynamicPageViews($pageView); |
|
1 ignored issue
–
show
|
|||
137 | 2 | $this->compileStandardRedirects($pageView); |
|
138 | 2 | break; |
|
139 | |||
140 | 2 | case PageView::REPEATER_TYPE: |
|
141 | 2 | $this->compileRepeaterPageViews($pageView); |
|
1 ignored issue
–
show
|
|||
142 | 2 | $this->compileExpandedRedirects($pageView); |
|
143 | 2 | break; |
|
144 | } |
||
145 | 14 | } |
|
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) |
|
171 | { |
||
172 | 2 | $contentItems = $pageView->getContentItems(); |
|
173 | 2 | $template = $this->createTwigTemplate($pageView); |
|
174 | |||
175 | 2 | foreach ($contentItems as &$contentItem) |
|
176 | { |
||
177 | 2 | if ($contentItem->isDraft() && !Service::getParameter(BuildableCommand::USE_DRAFTS)) |
|
178 | { |
||
179 | 1 | $this->output->debug('{file}: marked as a draft', array( |
|
180 | 1 | 'file' => $contentItem->getRelativeFilePath() |
|
181 | )); |
||
182 | |||
183 | 1 | continue; |
|
184 | } |
||
185 | |||
186 | 2 | $targetFile = $contentItem->getTargetFile(); |
|
187 | 2 | $output = $this->renderDynamicPageView($template, $pageView, $contentItem); |
|
188 | |||
189 | 2 | $this->output->notice('Writing file: {file}', array('file' => $targetFile)); |
|
190 | 2 | $this->folder->writeFile($targetFile, $output); |
|
191 | } |
||
192 | 2 | } |
|
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) |
202 | { |
||
203 | 2 | $pageView->rewindPermalink(); |
|
204 | |||
205 | 2 | $template = $this->createTwigTemplate($pageView); |
|
206 | 2 | $permalinks = $pageView->getRepeaterPermalinks(); |
|
207 | |||
208 | 2 | foreach ($permalinks as $permalink) |
|
209 | { |
||
210 | 2 | $pageView->bumpPermalink(); |
|
211 | 2 | $targetFile = $pageView->getTargetFile(); |
|
212 | 2 | $output = $this->renderRepeaterPageView($template, $pageView, $permalink); |
|
213 | |||
214 | 2 | $this->output->notice('Writing repeater file: {file}', array('file' => $targetFile)); |
|
215 | 2 | $this->folder->writeFile($targetFile, $output); |
|
216 | } |
||
217 | 2 | } |
|
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 | 12 | 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 | 2 | 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 | 14 | 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.