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 |
||
| 38 | class Compiler extends BaseManager |
||
| 39 | { |
||
| 40 | /** @var string|false */ |
||
| 41 | private $redirectTemplate; |
||
| 42 | |||
| 43 | /** @var PageView[][] */ |
||
| 44 | private $importDependencies; |
||
| 45 | |||
| 46 | /** @var Twig_Template[] */ |
||
| 47 | private $templateDependencies; |
||
| 48 | |||
| 49 | /** @var PageView[] */ |
||
| 50 | private $pageViewsFlattened; |
||
| 51 | |||
| 52 | /** @var string[] */ |
||
| 53 | private $templateMapping; |
||
| 54 | |||
| 55 | /** @var PageView[][] */ |
||
| 56 | private $pageViews; |
||
| 57 | |||
| 58 | /** @var Folder */ |
||
| 59 | private $folder; |
||
| 60 | |||
| 61 | /** @var string */ |
||
| 62 | private $theme; |
||
| 63 | |||
| 64 | /** @var Twig_Environment */ |
||
| 65 | private $twig; |
||
| 66 | |||
| 67 | 14 | public function __construct() |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @param string|false $template |
||
| 77 | */ |
||
| 78 | public function setRedirectTemplate($template) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param Folder $folder |
||
| 85 | */ |
||
| 86 | 14 | public function setTargetFolder(Folder $folder) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @param PageView[][] $pageViews |
||
| 93 | * @param PageView[] $pageViewsFlattened |
||
| 94 | */ |
||
| 95 | 14 | public function setPageViews(array &$pageViews, array &$pageViewsFlattened) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param string $themeName |
||
| 103 | */ |
||
| 104 | public function setThemeName($themeName) |
||
| 108 | |||
| 109 | /// |
||
| 110 | // Twig parent templates |
||
| 111 | /// |
||
| 112 | |||
| 113 | public function isImportDependency($filePath) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Check whether a given file path is used as a parent template by a PageView |
||
| 120 | * |
||
| 121 | * @param string $filePath |
||
| 122 | * |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | public function isParentTemplate($filePath) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Rebuild all of the PageViews that used a given template as a parent |
||
| 132 | * |
||
| 133 | * @param string $filePath The file path to the parent Twig template |
||
| 134 | */ |
||
| 135 | public function refreshParent($filePath) |
||
| 142 | |||
| 143 | public function getTemplateMappings() |
||
| 147 | |||
| 148 | /// |
||
| 149 | // IO Functionality |
||
| 150 | /// |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Compile all of the PageViews registered with the compiler. |
||
| 154 | * |
||
| 155 | * @since 0.1.0 |
||
| 156 | */ |
||
| 157 | 14 | public function compileAll() |
|
| 164 | |||
| 165 | public function compileImportDependencies($filePath) |
||
| 172 | |||
| 173 | public function compileSome(array $filter = array()) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Compile an individual PageView item. |
||
| 190 | * |
||
| 191 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
| 192 | * respective target file. |
||
| 193 | * |
||
| 194 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
||
| 195 | * |
||
| 196 | * @since 0.1.1 |
||
| 197 | */ |
||
| 198 | 14 | public function compilePageView(PageView &$pageView) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Write the compiled output for a static PageView. |
||
| 240 | * |
||
| 241 | * @param PageView $pageView |
||
| 242 | * |
||
| 243 | * @since 0.1.1 |
||
| 244 | */ |
||
| 245 | 10 | private function compileStaticPageView(PageView &$pageView) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Write the compiled output for a dynamic PageView. |
||
| 256 | * |
||
| 257 | * @param DynamicPageView $pageView |
||
| 258 | * |
||
| 259 | * @since 0.1.1 |
||
| 260 | */ |
||
| 261 | 2 | private function compileDynamicPageViews(DynamicPageView &$pageView) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Write the compiled output for a repeater PageView. |
||
| 289 | * |
||
| 290 | * @param RepeaterPageView $pageView |
||
| 291 | * |
||
| 292 | * @since 0.1.1 |
||
| 293 | */ |
||
| 294 | 2 | private function compileRepeaterPageViews(RepeaterPageView &$pageView) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @deprecated |
||
| 314 | * |
||
| 315 | * @todo This function needs to be rewritten or removed. Something |
||
| 316 | * |
||
| 317 | * @param ContentItem $contentItem |
||
| 318 | */ |
||
| 319 | public function compileContentItem(ContentItem &$contentItem) |
||
| 333 | |||
| 334 | /// |
||
| 335 | // Redirect handling |
||
| 336 | /// |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Write redirects for standard redirects. |
||
| 340 | * |
||
| 341 | * @param PermalinkDocument $pageView |
||
| 342 | * |
||
| 343 | * @since 0.1.1 |
||
| 344 | */ |
||
| 345 | 12 | private function compileStandardRedirects(PermalinkDocument &$pageView) |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Write redirects for expanded redirects. |
||
| 363 | * |
||
| 364 | * @param RepeaterPageView $pageView |
||
| 365 | * |
||
| 366 | * @since 0.1.1 |
||
| 367 | */ |
||
| 368 | 2 | private function compileExpandedRedirects(RepeaterPageView &$pageView) |
|
| 390 | |||
| 391 | /// |
||
| 392 | // Twig Functionality |
||
| 393 | /// |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
| 397 | * |
||
| 398 | * @param Twig_Template $template |
||
| 399 | * @param PageView $pageView |
||
| 400 | * @param ExpandedValue $expandedValue |
||
| 401 | * |
||
| 402 | * @since 0.1.1 |
||
| 403 | * |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | 2 | private function renderRepeaterPageView(Twig_Template &$template, RepeaterPageView &$pageView, ExpandedValue &$expandedValue) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Get the compiled HTML for a specific ContentItem. |
||
| 421 | * |
||
| 422 | * @param Twig_Template $template |
||
| 423 | * @param TwigDocument $twigItem |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | * @since 0.1.1 |
||
| 427 | * |
||
| 428 | */ |
||
| 429 | 2 | private function renderDynamicPageView(Twig_Template &$template, TwigDocument &$twigItem) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Get the compiled HTML for a static PageView. |
||
| 439 | * |
||
| 440 | * @param PageView $pageView |
||
| 441 | * |
||
| 442 | * @since 0.1.1 |
||
| 443 | * |
||
| 444 | * @throws \Exception |
||
| 445 | * @throws \Throwable |
||
| 446 | * @throws Twig_Error_Syntax |
||
| 447 | * |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | 10 | private function renderStaticPageView(PageView &$pageView) |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Create a Twig template that just needs an array to render. |
||
| 461 | * |
||
| 462 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
| 463 | * |
||
| 464 | * @since 0.1.1 |
||
| 465 | * |
||
| 466 | * @throws \Exception |
||
| 467 | * @throws \Throwable |
||
| 468 | * @throws Twig_Error_Syntax |
||
| 469 | * |
||
| 470 | * @return Twig_Template |
||
| 471 | */ |
||
| 472 | 14 | private function createTwigTemplate(PageView &$pageView) |
|
| 517 | } |
||
| 518 |