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() |
|
| 97 | |||
| 98 | public function compileSome($filter = array()) |
||
| 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) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Write the compiled output for a static PageView. |
||
| 148 | * |
||
| 149 | * @param PageView $pageView |
||
| 150 | * |
||
| 151 | * @since 0.1.1 |
||
| 152 | */ |
||
| 153 | 10 | private function compileStaticPageView(&$pageView) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Write the compiled output for a dynamic PageView. |
||
| 164 | * |
||
| 165 | * @param DynamicPageView $pageView |
||
| 166 | * |
||
| 167 | * @since 0.1.1 |
||
| 168 | */ |
||
| 169 | 1 | private function compileDynamicPageViews(&$pageView) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Write the compiled output for a repeater PageView. |
||
| 195 | * |
||
| 196 | * @param RepeaterPageView $pageView |
||
| 197 | * |
||
| 198 | * @since 0.1.1 |
||
| 199 | */ |
||
| 200 | 2 | View Code Duplication | private function compileRepeaterPageViews(&$pageView) |
| 217 | |||
| 218 | /** |
||
| 219 | * @deprecated |
||
| 220 | * |
||
| 221 | * @todo This function needs to be rewritten or removed. Something |
||
| 222 | * |
||
| 223 | * @param ContentItem $contentItem |
||
| 224 | */ |
||
| 225 | View Code Duplication | public function compileContentItem(&$contentItem) |
|
| 238 | |||
| 239 | /// |
||
| 240 | // Redirect handling |
||
| 241 | /// |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Write redirects for standard redirects. |
||
| 245 | * |
||
| 246 | * @param PageView $pageView |
||
| 247 | * |
||
| 248 | * @since 0.1.1 |
||
| 249 | */ |
||
| 250 | 11 | private function compileStandardRedirects(&$pageView) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Write redirects for expanded redirects. |
||
| 268 | * |
||
| 269 | * @param RepeaterPageView $pageView |
||
| 270 | * |
||
| 271 | * @since 0.1.1 |
||
| 272 | */ |
||
| 273 | 2 | private function compileExpandedRedirects(&$pageView) |
|
| 295 | |||
| 296 | /// |
||
| 297 | // Twig Functionality |
||
| 298 | /// |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
| 302 | * |
||
| 303 | * @param Twig_Template $template |
||
| 304 | * @param PageView $pageView |
||
| 305 | * @param ExpandedValue $expandedValue |
||
| 306 | * |
||
| 307 | * @since 0.1.1 |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | 2 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Get the compiled HTML for a specific ContentItem. |
||
| 328 | * |
||
| 329 | * @param Twig_Template $template |
||
| 330 | * @param PageView $pageView |
||
| 331 | * @param ContentItem $contentItem |
||
| 332 | * |
||
| 333 | * @since 0.1.1 |
||
| 334 | * |
||
| 335 | * @return string |
||
| 336 | */ |
||
| 337 | 1 | private function renderDynamicPageView(&$template, &$pageView, &$contentItem) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Get the compiled HTML for a static PageView. |
||
| 349 | * |
||
| 350 | * @param PageView $pageView |
||
| 351 | * |
||
| 352 | * @since 0.1.1 |
||
| 353 | * |
||
| 354 | * @throws \Exception |
||
| 355 | * @throws \Throwable |
||
| 356 | * @throws Twig_Error_Syntax |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | 10 | private function renderStaticPageView(&$pageView) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Create a Twig template that just needs an array to render. |
||
| 373 | * |
||
| 374 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
| 375 | * |
||
| 376 | * @since 0.1.1 |
||
| 377 | * |
||
| 378 | * @throws \Exception |
||
| 379 | * @throws \Throwable |
||
| 380 | * @throws Twig_Error_Syntax |
||
| 381 | * |
||
| 382 | * @return Twig_Template |
||
| 383 | */ |
||
| 384 | 13 | private function createTwigTemplate(&$pageView) |
|
| 402 | } |
||
| 403 |
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.