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:
Complex classes like Compiler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Compiler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 43 | class Compiler |
||
| 44 | { |
||
| 45 | /** @var string|false */ |
||
| 46 | private $redirectTemplate; |
||
| 47 | |||
| 48 | /** @var BasePageView[][] */ |
||
| 49 | private $importDependencies; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Any time a PageView extends another template, that relationship is stored in this array. This is necessary so |
||
| 53 | * when watching a website, we can rebuild the necessary PageViews when these base templates change. |
||
| 54 | * |
||
| 55 | * ``` |
||
| 56 | * array['_layouts/base.html.twig'] = &PageView; |
||
| 57 | * ``` |
||
| 58 | * |
||
| 59 | * @var TemplateInterface[] |
||
| 60 | */ |
||
| 61 | private $templateDependencies; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * All of the PageViews handled by this Compiler instance indexed by their file paths relative to the site root. |
||
| 65 | * |
||
| 66 | * ``` |
||
| 67 | * array['_pages/index.html.twig'] = &PageView; |
||
| 68 | * ``` |
||
| 69 | * |
||
| 70 | * @var BasePageView[] |
||
| 71 | */ |
||
| 72 | private $pageViewsFlattened; |
||
| 73 | |||
| 74 | /** @var string[] */ |
||
| 75 | private $templateMapping; |
||
| 76 | |||
| 77 | /** @var Folder */ |
||
| 78 | private $folder; |
||
| 79 | |||
| 80 | /** @var string */ |
||
| 81 | private $theme; |
||
| 82 | |||
| 83 | private $templateBridge; |
||
| 84 | private $pageManager; |
||
| 85 | private $eventDispatcher; |
||
| 86 | private $configuration; |
||
| 87 | |||
| 88 | 12 | public function __construct(TemplateBridgeInterface $templateBridge, Configuration $configuration, PageManager $pageManager, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger) |
|
| 89 | { |
||
| 90 | 12 | $this->templateBridge = $templateBridge; |
|
| 91 | 12 | $this->theme = ''; |
|
| 92 | 12 | $this->pageManager = $pageManager; |
|
| 93 | 12 | $this->eventDispatcher = $eventDispatcher; |
|
| 94 | 12 | $this->logger = $logger; |
|
|
|
|||
| 95 | 12 | $this->configuration = $configuration; |
|
| 96 | |||
| 97 | 12 | $this->pageViewsFlattened = &$pageManager->getPageViewsFlattened(); |
|
| 98 | 12 | $this->redirectTemplate = $this->configuration->getRedirectTemplate(); |
|
| 99 | 12 | } |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param Folder $folder |
||
| 103 | */ |
||
| 104 | 12 | public function setTargetFolder(Folder $folder) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @param string $themeName |
||
| 111 | */ |
||
| 112 | public function setThemeName($themeName) |
||
| 116 | |||
| 117 | /// |
||
| 118 | // Twig parent templates |
||
| 119 | /// |
||
| 120 | |||
| 121 | public function isImportDependency($filePath) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Check whether a given file path is used as a parent template by a PageView. |
||
| 128 | * |
||
| 129 | * @param string $filePath |
||
| 130 | * |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | public function isParentTemplate($filePath) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Rebuild all of the PageViews that used a given template as a parent. |
||
| 140 | * |
||
| 141 | * @param string $filePath The file path to the parent Twig template |
||
| 142 | */ |
||
| 143 | public function refreshParent($filePath) |
||
| 150 | |||
| 151 | public function getTemplateMappings() |
||
| 155 | |||
| 156 | /// |
||
| 157 | // IO Functionality |
||
| 158 | /// |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Compile all of the PageViews registered with the compiler. |
||
| 162 | * |
||
| 163 | * @since 0.1.0 |
||
| 164 | */ |
||
| 165 | 12 | public function compileAll() |
|
| 172 | |||
| 173 | public function compileImportDependencies($filePath) |
||
| 180 | |||
| 181 | public function compileSome(array $filter = []) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Compile an individual PageView item. |
||
| 198 | * |
||
| 199 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
| 200 | * respective target file. |
||
| 201 | * |
||
| 202 | * @param DynamicPageView|RepeaterPageView|StaticPageView $pageView The PageView that needs to be compiled |
||
| 203 | * |
||
| 204 | * @since 0.1.1 |
||
| 205 | */ |
||
| 206 | 12 | public function compilePageView(BasePageView &$pageView) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Write the compiled output for a static PageView. |
||
| 248 | * |
||
| 249 | * @since 0.1.1 |
||
| 250 | * |
||
| 251 | * @throws TemplateErrorInterface |
||
| 252 | */ |
||
| 253 | 10 | private function compileStaticPageView(StaticPageView &$pageView) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Write the compiled output for a dynamic PageView. |
||
| 266 | * |
||
| 267 | * @param DynamicPageView $pageView |
||
| 268 | * |
||
| 269 | * @since 0.1.1 |
||
| 270 | * |
||
| 271 | * @throws TemplateErrorInterface |
||
| 272 | */ |
||
| 273 | private function compileDynamicPageView(DynamicPageView &$pageView) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Write the compiled output for a repeater PageView. |
||
| 301 | * |
||
| 302 | * @param RepeaterPageView $pageView |
||
| 303 | * |
||
| 304 | * @since 0.1.1 |
||
| 305 | * |
||
| 306 | * @throws TemplateErrorInterface |
||
| 307 | */ |
||
| 308 | 3 | private function compileRepeaterPageView(RepeaterPageView &$pageView) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Write the given $output to the $targetFile as a $fileType PageView. |
||
| 329 | * |
||
| 330 | * @param string $targetFile |
||
| 331 | * @param string $output |
||
| 332 | * @param string $fileType |
||
| 333 | */ |
||
| 334 | 12 | private function writeToFilesystem($targetFile, $output, $fileType) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * @deprecated |
||
| 345 | * |
||
| 346 | * @todo This function needs to be rewritten or removed. Something |
||
| 347 | * |
||
| 348 | * @param ContentItem $contentItem |
||
| 349 | */ |
||
| 350 | public function compileContentItem(ContentItem &$contentItem) |
||
| 364 | |||
| 365 | /// |
||
| 366 | // Redirect handling |
||
| 367 | /// |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Write redirects for standard redirects. |
||
| 371 | * |
||
| 372 | * @throws TemplateErrorInterface |
||
| 373 | * |
||
| 374 | * @since 0.1.1 |
||
| 375 | */ |
||
| 376 | 10 | private function compileStandardRedirects(PermalinkDocument &$pageView) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Write redirects for expanded redirects. |
||
| 397 | * |
||
| 398 | * @param RepeaterPageView $pageView |
||
| 399 | * |
||
| 400 | * @since 0.1.1 |
||
| 401 | */ |
||
| 402 | 3 | private function compileExpandedRedirects(RepeaterPageView &$pageView) |
|
| 428 | |||
| 429 | /// |
||
| 430 | // Twig Functionality |
||
| 431 | /// |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
| 435 | * |
||
| 436 | * @param TemplateInterface $template |
||
| 437 | * @param RepeaterPageView $pageView |
||
| 438 | * @param ExpandedValue $expandedValue |
||
| 439 | * |
||
| 440 | * @since 0.1.1 |
||
| 441 | * |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | 3 | private function renderRepeaterPageView(TemplateInterface &$template, RepeaterPageView &$pageView, ExpandedValue &$expandedValue) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * Get the compiled HTML for a specific ContentItem. |
||
| 471 | * |
||
| 472 | * @since 0.1.1 |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | View Code Duplication | private function renderDynamicPageView(TemplateInterface &$template, TemplateReadyDocument &$twigItem) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Get the compiled HTML for a static PageView. |
||
| 498 | * |
||
| 499 | * @since 0.1.1 |
||
| 500 | * |
||
| 501 | * @throws TemplateErrorInterface |
||
| 502 | * |
||
| 503 | * @return string |
||
| 504 | */ |
||
| 505 | 10 | View Code Duplication | private function renderStaticPageView(StaticPageView &$pageView) |
| 525 | |||
| 526 | /** |
||
| 527 | * Create a Twig template that just needs an array to render. |
||
| 528 | * |
||
| 529 | * @since 0.1.1 |
||
| 530 | * |
||
| 531 | * @throws TemplateErrorInterface |
||
| 532 | * |
||
| 533 | * @return TemplateInterface |
||
| 534 | */ |
||
| 535 | 12 | private function createTwigTemplate(BasePageView &$pageView) |
|
| 561 | } |
||
| 562 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: