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 |
||
| 39 | class Compiler extends BaseManager |
||
| 40 | { |
||
| 41 | /** @var string|false */ |
||
| 42 | private $redirectTemplate; |
||
| 43 | |||
| 44 | /** @var BasePageView[][] */ |
||
| 45 | private $importDependencies; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Any time a PageView extends another template, that relationship is stored in this array. This is necessary so |
||
| 49 | * when watching a website, we can rebuild the necessary PageViews when these base templates change. |
||
| 50 | * |
||
| 51 | * ``` |
||
| 52 | * array['_layouts/base.html.twig'] = &PageView; |
||
| 53 | * ``` |
||
| 54 | * |
||
| 55 | * @var TemplateInterface[] |
||
| 56 | */ |
||
| 57 | private $templateDependencies; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * All of the PageViews handled by this Compiler instance indexed by their file paths relative to the site root. |
||
| 61 | * |
||
| 62 | * ``` |
||
| 63 | * array['_pages/index.html.twig'] = &PageView; |
||
| 64 | * ``` |
||
| 65 | * |
||
| 66 | * @var BasePageView[] |
||
| 67 | */ |
||
| 68 | private $pageViewsFlattened; |
||
| 69 | |||
| 70 | /** @var string[] */ |
||
| 71 | private $templateMapping; |
||
| 72 | |||
| 73 | /** @var BasePageView[][] */ |
||
| 74 | private $pageViews; |
||
| 75 | |||
| 76 | /** @var Folder */ |
||
| 77 | private $folder; |
||
| 78 | |||
| 79 | /** @var string */ |
||
| 80 | private $theme; |
||
| 81 | |||
| 82 | /** @var TemplateBridgeInterface */ |
||
| 83 | private $templateBridge; |
||
| 84 | |||
| 85 | 14 | public function __construct(TemplateBridgeInterface $templateBridge) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @param string|false $template |
||
| 95 | */ |
||
| 96 | public function setRedirectTemplate($template) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param Folder $folder |
||
| 103 | */ |
||
| 104 | 14 | public function setTargetFolder(Folder $folder) |
|
| 108 | |||
| 109 | 12 | public function setPageManager(PageManager $manager) |
|
| 110 | { |
||
| 111 | 12 | $this->pageViews = &$manager->getPageViews(); |
|
| 112 | 12 | $this->pageViewsFlattened = &$manager->getPageViewsFlattened(); |
|
| 113 | 12 | } |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @deprecated Use setPageManager() |
||
| 117 | * |
||
| 118 | * @param BasePageView[][] $pageViews |
||
| 119 | * @param BasePageView[] $pageViewsFlattened |
||
| 120 | */ |
||
| 121 | public function setPageViews(array &$pageViews, array &$pageViewsFlattened) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param string $themeName |
||
| 129 | */ |
||
| 130 | public function setThemeName($themeName) |
||
| 134 | |||
| 135 | /// |
||
| 136 | // Twig parent templates |
||
| 137 | /// |
||
| 138 | |||
| 139 | public function isImportDependency($filePath) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Check whether a given file path is used as a parent template by a PageView |
||
| 146 | * |
||
| 147 | * @param string $filePath |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | public function isParentTemplate($filePath) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Rebuild all of the PageViews that used a given template as a parent |
||
| 158 | * |
||
| 159 | * @param string $filePath The file path to the parent Twig template |
||
| 160 | */ |
||
| 161 | public function refreshParent($filePath) |
||
| 162 | { |
||
| 163 | foreach ($this->templateDependencies[$filePath] as &$parentTemplate) |
||
|
|
|||
| 164 | { |
||
| 165 | $this->compilePageView($parentTemplate); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | |||
| 169 | public function getTemplateMappings() |
||
| 173 | |||
| 174 | /// |
||
| 175 | // IO Functionality |
||
| 176 | /// |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Compile all of the PageViews registered with the compiler. |
||
| 180 | * |
||
| 181 | * @since 0.1.0 |
||
| 182 | */ |
||
| 183 | 14 | public function compileAll() |
|
| 190 | |||
| 191 | public function compileImportDependencies($filePath) |
||
| 198 | |||
| 199 | public function compileSome(array $filter = array()) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Compile an individual PageView from a given path. |
||
| 216 | * |
||
| 217 | * @param string $filePath |
||
| 218 | * |
||
| 219 | * @throws FileNotFoundException When the given file path isn't tracked by the Compiler. |
||
| 220 | */ |
||
| 221 | public function compilePageViewFromPath($filePath) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Compile an individual PageView item. |
||
| 233 | * |
||
| 234 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
| 235 | * respective target file. |
||
| 236 | * |
||
| 237 | * @param DynamicPageView|RepeaterPageView|StaticPageView $pageView The PageView that needs to be compiled |
||
| 238 | * |
||
| 239 | * @since 0.1.1 |
||
| 240 | */ |
||
| 241 | 14 | public function compilePageView(BasePageView &$pageView) |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Write the compiled output for a static PageView. |
||
| 283 | * |
||
| 284 | * @since 0.1.1 |
||
| 285 | * |
||
| 286 | * @throws TemplateErrorInterface |
||
| 287 | */ |
||
| 288 | 10 | private function compileStaticPageView(StaticPageView &$pageView) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Write the compiled output for a dynamic PageView. |
||
| 301 | * |
||
| 302 | * @param DynamicPageView $pageView |
||
| 303 | * |
||
| 304 | * @since 0.1.1 |
||
| 305 | * |
||
| 306 | * @throws TemplateErrorInterface |
||
| 307 | */ |
||
| 308 | 2 | private function compileDynamicPageViews(DynamicPageView &$pageView) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Write the compiled output for a repeater PageView. |
||
| 336 | * |
||
| 337 | * @param RepeaterPageView $pageView |
||
| 338 | |||
| 339 | * @since 0.1.1 |
||
| 340 | * |
||
| 341 | * @throws TemplateErrorInterface |
||
| 342 | */ |
||
| 343 | 2 | private function compileRepeaterPageViews(RepeaterPageView &$pageView) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * @deprecated |
||
| 363 | * |
||
| 364 | * @todo This function needs to be rewritten or removed. Something |
||
| 365 | * |
||
| 366 | * @param ContentItem $contentItem |
||
| 367 | */ |
||
| 368 | public function compileContentItem(ContentItem &$contentItem) |
||
| 382 | |||
| 383 | /// |
||
| 384 | // Redirect handling |
||
| 385 | /// |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Write redirects for standard redirects. |
||
| 389 | * |
||
| 390 | * @throws TemplateErrorInterface |
||
| 391 | * |
||
| 392 | * @since 0.1.1 |
||
| 393 | */ |
||
| 394 | 12 | private function compileStandardRedirects(PermalinkDocument &$pageView) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Write redirects for expanded redirects. |
||
| 415 | * |
||
| 416 | * @param RepeaterPageView $pageView |
||
| 417 | * |
||
| 418 | * @since 0.1.1 |
||
| 419 | */ |
||
| 420 | 2 | private function compileExpandedRedirects(RepeaterPageView &$pageView) |
|
| 446 | |||
| 447 | /// |
||
| 448 | // Twig Functionality |
||
| 449 | /// |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
| 453 | * |
||
| 454 | * @param TemplateInterface $template |
||
| 455 | * @param RepeaterPageView $pageView |
||
| 456 | * @param ExpandedValue $expandedValue |
||
| 457 | * |
||
| 458 | * @since 0.1.1 |
||
| 459 | * |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | 2 | private function renderRepeaterPageView(TemplateInterface &$template, RepeaterPageView &$pageView, ExpandedValue &$expandedValue) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Get the compiled HTML for a specific ContentItem. |
||
| 477 | * |
||
| 478 | * @since 0.1.1 |
||
| 479 | * |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | 2 | private function renderDynamicPageView(TemplateInterface &$template, TemplateReadyDocument &$twigItem) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Get the compiled HTML for a static PageView. |
||
| 492 | * |
||
| 493 | * @since 0.1.1 |
||
| 494 | * |
||
| 495 | * @throws TemplateErrorInterface |
||
| 496 | * |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | 10 | private function renderStaticPageView(StaticPageView &$pageView) |
|
| 507 | |||
| 508 | /** |
||
| 509 | * Create a Twig template that just needs an array to render. |
||
| 510 | * |
||
| 511 | * @since 0.1.1 |
||
| 512 | * |
||
| 513 | * @throws TemplateErrorInterface |
||
| 514 | * |
||
| 515 | * @return TemplateInterface |
||
| 516 | */ |
||
| 517 | 14 | private function createTwigTemplate(BasePageView &$pageView) |
|
| 563 | } |
||
| 564 |