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 | 13 | ||
| 49 | public function __construct() |
||
| 50 | 13 | { |
|
| 51 | parent::__construct(); |
||
| 52 | 13 | ||
| 53 | 13 | $this->twig = TwigManager::getInstance(); |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param string|false $template |
||
| 58 | */ |
||
| 59 | public function setRedirectTemplate($template) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param Folder $folder |
||
| 66 | 13 | */ |
|
| 67 | public function setTargetFolder(Folder $folder) |
||
| 68 | 13 | { |
|
| 69 | 13 | $this->folder = $folder; |
|
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param PageView[][] $pageViews |
||
| 74 | * @param PageView[] $pageViewsFlattened |
||
| 75 | 13 | */ |
|
| 76 | public function setPageViews(array &$pageViews, array &$pageViewsFlattened) |
||
| 77 | 13 | { |
|
| 78 | 13 | $this->pageViews = &$pageViews; |
|
| 79 | 13 | $this->pageViewsFlattened = &$pageViewsFlattened; |
|
| 80 | } |
||
| 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 | 13 | */ |
|
| 91 | public function compileAll() |
||
| 92 | 13 | { |
|
| 93 | foreach ($this->pageViewsFlattened as &$pageView) |
||
| 94 | 13 | { |
|
| 95 | $this->compilePageView($pageView); |
||
| 96 | 13 | } |
|
| 97 | } |
||
| 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 | 13 | */ |
|
| 121 | private function compilePageView(&$pageView) |
||
| 146 | |||
| 147 | /** |
||
| 148 | 10 | * Write the compiled output for a static PageView. |
|
| 149 | * |
||
| 150 | 10 | * @param PageView $pageView |
|
| 151 | 10 | * |
|
| 152 | * @since 0.1.1 |
||
| 153 | 10 | */ |
|
| 154 | 10 | private function compileStaticPageView(&$pageView) |
|
| 162 | |||
| 163 | /** |
||
| 164 | 1 | * Write the compiled output for a dynamic PageView. |
|
| 165 | * |
||
| 166 | 1 | * @param DynamicPageView $pageView |
|
| 167 | 1 | * |
|
| 168 | * @since 0.1.1 |
||
| 169 | 1 | */ |
|
| 170 | private function compileDynamicPageViews(&$pageView) |
||
| 193 | 2 | ||
| 194 | /** |
||
| 195 | 2 | * Write the compiled output for a repeater PageView. |
|
| 196 | 2 | * |
|
| 197 | 2 | * @param RepeaterPageView $pageView |
|
| 198 | * |
||
| 199 | 2 | * @since 0.1.1 |
|
| 200 | 2 | */ |
|
| 201 | 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 | 11 | /// |
|
| 241 | // Redirect handling |
||
| 242 | 4 | /// |
|
| 243 | |||
| 244 | 4 | /** |
|
| 245 | 4 | * Write redirects for standard redirects. |
|
| 246 | * |
||
| 247 | * @param PageView $pageView |
||
| 248 | 4 | * |
|
| 249 | * @since 0.1.1 |
||
| 250 | 11 | */ |
|
| 251 | 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 | private function compileExpandedRedirects(&$pageView) |
||
| 296 | |||
| 297 | 2 | /// |
|
| 298 | // Twig Functionality |
||
| 299 | 2 | /// |
|
| 300 | |||
| 301 | 2 | /** |
|
| 302 | 2 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
|
| 303 | 2 | * |
|
| 304 | * @param Twig_Template $template |
||
| 305 | * @param PageView $pageView |
||
| 306 | * @param ExpandedValue $expandedValue |
||
| 307 | 2 | * |
|
| 308 | 2 | * @since 0.1.1 |
|
| 309 | * |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
||
| 326 | |||
| 327 | /** |
||
| 328 | 1 | * Get the compiled HTML for a specific ContentItem. |
|
| 329 | 1 | * |
|
| 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 | private function renderDynamicPageView(&$template, &$pageView, &$contentItem) |
||
| 347 | |||
| 348 | 10 | /** |
|
| 349 | * Get the compiled HTML for a static PageView. |
||
| 350 | * |
||
| 351 | 10 | * @param PageView $pageView |
|
| 352 | 10 | * |
|
| 353 | 10 | * @since 0.1.1 |
|
| 354 | * |
||
| 355 | * @throws \Exception |
||
| 356 | * @throws \Throwable |
||
| 357 | * @throws Twig_Error_Syntax |
||
| 358 | * |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | private function renderStaticPageView(&$pageView) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Create a Twig template that just needs an array to render. |
||
| 374 | 13 | * |
|
| 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 | 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.