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) |
||
| 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) |
|
| 121 | { |
||
| 122 | 13 | switch ($pageView->getType()) |
|
| 123 | 1 | { |
|
| 124 | 13 | case PageView::STATIC_TYPE: |
|
| 125 | 10 | $this->compileStaticPageView($pageView); |
|
| 126 | 10 | $this->compileStandardRedirects($pageView); |
|
| 127 | 10 | break; |
|
| 128 | |||
| 129 | 3 | case PageView::DYNAMIC_TYPE: |
|
| 130 | 1 | $this->compileDynamicPageViews($pageView); |
|
| 131 | 1 | $this->compileStandardRedirects($pageView); |
|
| 132 | 1 | break; |
|
| 133 | |||
| 134 | 2 | case PageView::REPEATER_TYPE: |
|
| 135 | 2 | $this->compileRepeaterPageViews($pageView); |
|
| 136 | 2 | $this->compileExpandedRedirects($pageView); |
|
| 137 | 2 | break; |
|
| 138 | 13 | } |
|
| 139 | 13 | } |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Write the compiled output for a static PageView. |
||
| 143 | * |
||
| 144 | * @param PageView $pageView |
||
| 145 | * |
||
| 146 | * @since 0.1.1 |
||
| 147 | */ |
||
| 148 | 10 | private function compileStaticPageView(&$pageView) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Write the compiled output for a dynamic PageView. |
||
| 159 | * |
||
| 160 | * @param DynamicPageView $pageView |
||
| 161 | * |
||
| 162 | * @since 0.1.1 |
||
| 163 | */ |
||
| 164 | 1 | View Code Duplication | private function compileDynamicPageViews(&$pageView) |
| 165 | { |
||
| 166 | 1 | $contentItems = $pageView->getContentItems(); |
|
| 167 | 1 | $template = $this->createTwigTemplate($pageView); |
|
| 168 | |||
| 169 | 1 | foreach ($contentItems as &$contentItem) |
|
| 170 | { |
||
| 171 | 1 | $targetFile = $contentItem->getTargetFile(); |
|
| 172 | 1 | $output = $this->renderDynamicPageView($template, $pageView, $contentItem); |
|
| 173 | |||
| 174 | 1 | $this->output->notice('Writing file: {file}', array('file' => $targetFile)); |
|
| 175 | 1 | $this->folder->writeFile($targetFile, $output); |
|
| 176 | 1 | } |
|
| 177 | 1 | } |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Write the compiled output for a repeater PageView. |
||
| 181 | * |
||
| 182 | * @param RepeaterPageView $pageView |
||
| 183 | * |
||
| 184 | * @since 0.1.1 |
||
| 185 | */ |
||
| 186 | 2 | View Code Duplication | private function compileRepeaterPageViews(&$pageView) |
| 187 | { |
||
| 188 | 2 | $pageView->rewindPermalink(); |
|
| 189 | |||
| 190 | 2 | $template = $this->createTwigTemplate($pageView); |
|
| 191 | 2 | $permalinks = $pageView->getRepeaterPermalinks(); |
|
| 192 | |||
| 193 | 2 | foreach ($permalinks as $permalink) |
|
| 194 | { |
||
| 195 | 2 | $pageView->bumpPermalink(); |
|
| 196 | 2 | $targetFile = $pageView->getTargetFile(); |
|
| 197 | 2 | $output = $this->renderRepeaterPageView($template, $pageView, $permalink); |
|
| 198 | |||
| 199 | 2 | $this->output->notice('Writing repeater file: {file}', array('file' => $targetFile)); |
|
| 200 | 2 | $this->folder->writeFile($targetFile, $output); |
|
| 201 | 2 | } |
|
| 202 | 2 | } |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @deprecated |
||
| 206 | * @todo This function needs to be rewritten or removed. Something |
||
| 207 | * |
||
| 208 | * @param ContentItem $contentItem |
||
| 209 | */ |
||
| 210 | View Code Duplication | public function compileContentItem(&$contentItem) |
|
| 223 | |||
| 224 | /// |
||
| 225 | // Redirect handling |
||
| 226 | /// |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Write redirects for standard redirects. |
||
| 230 | * |
||
| 231 | * @param PageView $pageView |
||
| 232 | * |
||
| 233 | * @since 0.1.1 |
||
| 234 | */ |
||
| 235 | 11 | private function compileStandardRedirects(&$pageView) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Write redirects for expanded redirects. |
||
| 253 | * |
||
| 254 | * @param RepeaterPageView $pageView |
||
| 255 | * |
||
| 256 | * @since 0.1.1 |
||
| 257 | */ |
||
| 258 | 2 | private function compileExpandedRedirects(&$pageView) |
|
| 259 | { |
||
| 260 | 2 | $permalinks = $pageView->getRepeaterPermalinks(); |
|
| 261 | |||
| 262 | /** @var ExpandedValue[] $repeaterRedirect */ |
||
| 263 | 2 | foreach ($pageView->getRepeaterRedirects() as $repeaterRedirect) |
|
| 264 | { |
||
| 265 | /** |
||
| 266 | * @var int $index |
||
| 267 | * @var ExpandedValue $redirect |
||
| 268 | */ |
||
| 269 | foreach ($repeaterRedirect as $index => $redirect) |
||
| 270 | { |
||
| 271 | $redirectPageView = PageView::createRedirect( |
||
| 272 | $redirect->getEvaluated(), |
||
| 273 | $permalinks[$index]->getEvaluated(), |
||
| 274 | $this->redirectTemplate |
||
| 275 | ); |
||
| 276 | $this->compilePageView($redirectPageView); |
||
| 277 | } |
||
| 278 | 2 | } |
|
| 279 | 2 | } |
|
| 280 | |||
| 281 | /// |
||
| 282 | // Twig Functionality |
||
| 283 | /// |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
| 287 | * |
||
| 288 | * @param Twig_Template $template |
||
| 289 | * @param PageView $pageView |
||
| 290 | * @param ExpandedValue $expandedValue |
||
| 291 | * |
||
| 292 | * @since 0.1.1 |
||
| 293 | * |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | 2 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
|
| 297 | { |
||
| 298 | 2 | $this->twig->addGlobal('__currentTemplate', $pageView->getFilePath()); |
|
| 299 | |||
| 300 | 2 | $pageView->setFrontMatter(array( |
|
| 301 | 2 | 'permalink' => $expandedValue->getEvaluated(), |
|
| 302 | 2 | 'iterators' => $expandedValue->getIterators(), |
|
| 303 | 2 | )); |
|
| 304 | |||
| 305 | return $template |
||
| 306 | 2 | ->render(array( |
|
| 307 | 2 | 'this' => $pageView->createJail(), |
|
| 308 | 2 | )); |
|
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Get the compiled HTML for a specific ContentItem. |
||
| 313 | * |
||
| 314 | * @param Twig_Template $template |
||
| 315 | * @param PageView $pageView |
||
| 316 | * @param ContentItem $contentItem |
||
| 317 | * |
||
| 318 | * @since 0.1.1 |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | 1 | private function renderDynamicPageView(&$template, &$pageView, &$contentItem) |
|
| 323 | { |
||
| 324 | 1 | $this->twig->addGlobal('__currentTemplate', $pageView->getFilePath()); |
|
| 325 | |||
| 326 | return $template |
||
| 327 | 1 | ->render(array( |
|
| 328 | 1 | 'this' => $contentItem->createJail(), |
|
| 329 | 1 | )); |
|
| 330 | } |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Get the compiled HTML for a static PageView. |
||
| 334 | * |
||
| 335 | * @param PageView $pageView |
||
| 336 | * |
||
| 337 | * @since 0.1.1 |
||
| 338 | * |
||
| 339 | * @throws \Exception |
||
| 340 | * @throws \Throwable |
||
| 341 | * @throws Twig_Error_Syntax |
||
| 342 | * |
||
| 343 | * @return string |
||
| 344 | */ |
||
| 345 | 10 | private function renderStaticPageView(&$pageView) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Create a Twig template that just needs an array to render. |
||
| 358 | * |
||
| 359 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
| 360 | * |
||
| 361 | * @since 0.1.1 |
||
| 362 | * |
||
| 363 | * @throws \Exception |
||
| 364 | * @throws \Throwable |
||
| 365 | * @throws Twig_Error_Syntax |
||
| 366 | * |
||
| 367 | * @return Twig_Template |
||
| 368 | */ |
||
| 369 | 13 | private function createTwigTemplate(&$pageView) |
|
| 387 | } |
||
| 388 |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.