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 | public function __construct() |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param string|false $template |
||
| 53 | */ |
||
| 54 | public function setRedirectTemplate ($template) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param Folder $folder |
||
| 61 | */ |
||
| 62 | public function setTargetFolder (Folder $folder) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param PageView[] $pageViews |
||
| 69 | */ |
||
| 70 | 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 | 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 | private function compilePageView (&$pageView) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Write the compiled output for a static PageView |
||
| 121 | * |
||
| 122 | * @param PageView $pageView |
||
| 123 | * @since 0.1.1 |
||
| 124 | */ |
||
| 125 | private function compileStaticPageView (&$pageView) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Write the compiled output for a dynamic PageView |
||
| 136 | * |
||
| 137 | * @param DynamicPageView $pageView |
||
| 138 | * @since 0.1.1 |
||
| 139 | */ |
||
| 140 | View Code Duplication | private function compileDynamicPageViews (&$pageView) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Write the compiled output for a repeater PageView |
||
| 157 | * |
||
| 158 | * @param RepeaterPageView $pageView |
||
| 159 | * @since 0.1.1 |
||
| 160 | */ |
||
| 161 | View Code Duplication | private function compileRepeaterPageViews (&$pageView) |
|
| 178 | |||
| 179 | /// |
||
| 180 | // Twig Functionality |
||
| 181 | /// |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get the compiled HTML for a specific iteration of a repeater PageView |
||
| 185 | * |
||
| 186 | * @param Twig_Template $template |
||
| 187 | * @param PageView $pageView |
||
| 188 | * @param ExpandedValue $expandedValue |
||
| 189 | * |
||
| 190 | * @since 0.1.1 |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | private function renderRepeaterPageView (&$template, &$pageView, &$expandedValue) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get the compiled HTML for a specific ContentItem |
||
| 210 | * |
||
| 211 | * @param Twig_Template $template |
||
| 212 | * @param PageView $pageView |
||
| 213 | * @param ContentItem $contentItem |
||
| 214 | * |
||
| 215 | * @since 0.1.1 |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | private function renderDynamicPageView (&$template, &$pageView, &$contentItem) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get the compiled HTML for a static PageView |
||
| 230 | * |
||
| 231 | * @param PageView $pageView |
||
| 232 | * |
||
| 233 | * @since 0.1.1 |
||
| 234 | * @return string |
||
| 235 | * |
||
| 236 | * @throws \Exception |
||
| 237 | * @throws \Throwable |
||
| 238 | * @throws Twig_Error_Syntax |
||
| 239 | */ |
||
| 240 | private function renderStaticPageView (&$pageView) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Create a Twig template that just needs an array to render |
||
| 253 | * |
||
| 254 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
| 255 | * @since 0.1.1 |
||
| 256 | * |
||
| 257 | * @return Twig_Template |
||
| 258 | * |
||
| 259 | * @throws \Exception |
||
| 260 | * @throws \Throwable |
||
| 261 | * @throws Twig_Error_Syntax |
||
| 262 | */ |
||
| 263 | private function createTwigTemplate (&$pageView) |
||
| 281 | } |
||
| 282 |
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.