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 | public function setPageManager(PageManager $manager) |
||
| 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) |
||
| 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) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Write the compiled output for a dynamic PageView. |
||
| 299 | * |
||
| 300 | * @param DynamicPageView $pageView |
||
| 301 | * |
||
| 302 | * @since 0.1.1 |
||
| 303 | * |
||
| 304 | * @throws TemplateErrorInterface |
||
| 305 | */ |
||
| 306 | 2 | private function compileDynamicPageViews(DynamicPageView &$pageView) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Write the compiled output for a repeater PageView. |
||
| 334 | * |
||
| 335 | * @param RepeaterPageView $pageView |
||
| 336 | |||
| 337 | * @since 0.1.1 |
||
| 338 | * |
||
| 339 | * @throws TemplateErrorInterface |
||
| 340 | */ |
||
| 341 | 2 | private function compileRepeaterPageViews(RepeaterPageView &$pageView) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @deprecated |
||
| 361 | * |
||
| 362 | * @todo This function needs to be rewritten or removed. Something |
||
| 363 | * |
||
| 364 | * @param ContentItem $contentItem |
||
| 365 | */ |
||
| 366 | public function compileContentItem(ContentItem &$contentItem) |
||
| 380 | |||
| 381 | /// |
||
| 382 | // Redirect handling |
||
| 383 | /// |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Write redirects for standard redirects. |
||
| 387 | * |
||
| 388 | * @throws TemplateErrorInterface |
||
| 389 | * |
||
| 390 | * @since 0.1.1 |
||
| 391 | */ |
||
| 392 | 12 | private function compileStandardRedirects(PermalinkDocument &$pageView) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Write redirects for expanded redirects. |
||
| 410 | * |
||
| 411 | * @param RepeaterPageView $pageView |
||
| 412 | * |
||
| 413 | * @since 0.1.1 |
||
| 414 | */ |
||
| 415 | 2 | private function compileExpandedRedirects(RepeaterPageView &$pageView) |
|
| 437 | |||
| 438 | /// |
||
| 439 | // Twig Functionality |
||
| 440 | /// |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
| 444 | * |
||
| 445 | * @param TemplateInterface $template |
||
| 446 | * @param RepeaterPageView $pageView |
||
| 447 | * @param ExpandedValue $expandedValue |
||
| 448 | * |
||
| 449 | * @since 0.1.1 |
||
| 450 | * |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | 2 | private function renderRepeaterPageView(TemplateInterface &$template, RepeaterPageView &$pageView, ExpandedValue &$expandedValue) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Get the compiled HTML for a specific ContentItem. |
||
| 468 | * |
||
| 469 | * @since 0.1.1 |
||
| 470 | * |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | 2 | private function renderDynamicPageView(TemplateInterface &$template, TemplateReadyDocument &$twigItem) |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Get the compiled HTML for a static PageView. |
||
| 483 | * |
||
| 484 | * @since 0.1.1 |
||
| 485 | * |
||
| 486 | * @throws TemplateErrorInterface |
||
| 487 | * |
||
| 488 | * @return string |
||
| 489 | */ |
||
| 490 | 10 | private function renderStaticPageView(StaticPageView &$pageView) |
|
| 498 | |||
| 499 | /** |
||
| 500 | * Create a Twig template that just needs an array to render. |
||
| 501 | * |
||
| 502 | * @since 0.1.1 |
||
| 503 | * |
||
| 504 | * @throws TemplateErrorInterface |
||
| 505 | * |
||
| 506 | * @return TemplateInterface |
||
| 507 | */ |
||
| 508 | 14 | private function createTwigTemplate(BasePageView &$pageView) |
|
| 554 | } |
||
| 555 |