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 |
||
| 30 | class Compiler extends BaseManager |
||
| 31 | { |
||
| 32 | /** @var string|false */ |
||
| 33 | private $redirectTemplate; |
||
| 34 | |||
| 35 | /** @var PageView[] */ |
||
| 36 | private $pageViews; |
||
| 37 | |||
| 38 | /** @var Folder */ |
||
| 39 | private $folder; |
||
| 40 | |||
| 41 | /** @var Twig_Environment */ |
||
| 42 | private $twig; |
||
| 43 | |||
| 44 | 5 | public function __construct() |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @param string|false $template |
||
| 53 | */ |
||
| 54 | public function setRedirectTemplate ($template) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param Folder $folder |
||
| 61 | */ |
||
| 62 | 5 | public function setTargetFolder (Folder $folder) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @param PageView[] $pageViews |
||
| 69 | */ |
||
| 70 | 5 | public function setPageViews (array $pageViews) |
|
| 74 | |||
| 75 | /// |
||
| 76 | // IO Functionality |
||
| 77 | /// |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Compile all of the PageViews registered with the compiler |
||
| 81 | * |
||
| 82 | * @since 0.1.0 |
||
| 83 | */ |
||
| 84 | 5 | public function compileAll () |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Compile an individual PageView item |
||
| 94 | * |
||
| 95 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
| 96 | * respective target file. |
||
| 97 | * |
||
| 98 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
||
| 99 | * @since 0.1.1 |
||
| 100 | */ |
||
| 101 | 5 | private function compilePageView (&$pageView) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Write the compiled output for a static PageView |
||
| 124 | * |
||
| 125 | * @param PageView $pageView |
||
| 126 | * @since 0.1.1 |
||
| 127 | */ |
||
| 128 | 5 | private function compileStaticPageView (&$pageView) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Write the compiled output for a dynamic PageView |
||
| 139 | * |
||
| 140 | * @param DynamicPageView $pageView |
||
| 141 | * @since 0.1.1 |
||
| 142 | */ |
||
| 143 | View Code Duplication | private function compileDynamicPageViews (&$pageView) |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Write the compiled output for a repeater PageView |
||
| 160 | * |
||
| 161 | * @param RepeaterPageView $pageView |
||
| 162 | * @since 0.1.1 |
||
| 163 | */ |
||
| 164 | View Code Duplication | private function compileRepeaterPageViews (&$pageView) |
|
| 181 | |||
| 182 | /// |
||
| 183 | // Redirect handling |
||
| 184 | /// |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Write redirects for standard redirects |
||
| 188 | * |
||
| 189 | * @param PageView $pageView |
||
| 190 | * @since 0.1.1 |
||
| 191 | */ |
||
| 192 | private function compileStandardRedirects (&$pageView) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Write redirects for expanded redirects |
||
| 210 | * |
||
| 211 | * @param RepeaterPageView $pageView |
||
| 212 | * @since 0.1.1 |
||
| 213 | */ |
||
| 214 | private function compileExpandedRedirects (&$pageView) |
||
| 236 | |||
| 237 | /// |
||
| 238 | // Twig Functionality |
||
| 239 | /// |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get the compiled HTML for a specific iteration of a repeater PageView |
||
| 243 | * |
||
| 244 | * @param Twig_Template $template |
||
| 245 | * @param PageView $pageView |
||
| 246 | * @param ExpandedValue $expandedValue |
||
| 247 | * |
||
| 248 | * @since 0.1.1 |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | private function renderRepeaterPageView (&$template, &$pageView, &$expandedValue) |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Get the compiled HTML for a specific ContentItem |
||
| 268 | * |
||
| 269 | * @param Twig_Template $template |
||
| 270 | * @param PageView $pageView |
||
| 271 | * @param ContentItem $contentItem |
||
| 272 | * |
||
| 273 | * @since 0.1.1 |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | private function renderDynamicPageView (&$template, &$pageView, &$contentItem) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get the compiled HTML for a static PageView |
||
| 288 | * |
||
| 289 | * @param PageView $pageView |
||
| 290 | * |
||
| 291 | * @since 0.1.1 |
||
| 292 | * @return string |
||
| 293 | * |
||
| 294 | * @throws \Exception |
||
| 295 | * @throws \Throwable |
||
| 296 | * @throws Twig_Error_Syntax |
||
| 297 | */ |
||
| 298 | 5 | private function renderStaticPageView (&$pageView) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Create a Twig template that just needs an array to render |
||
| 311 | * |
||
| 312 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
| 313 | * @since 0.1.1 |
||
| 314 | * |
||
| 315 | * @return Twig_Template |
||
| 316 | * |
||
| 317 | * @throws \Exception |
||
| 318 | * @throws \Throwable |
||
| 319 | * @throws Twig_Error_Syntax |
||
| 320 | */ |
||
| 321 | 5 | private function createTwigTemplate (&$pageView) |
|
| 339 | } |
||
| 340 |
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.