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 |
||
| 35 | class Compiler extends BaseManager |
||
| 36 | { |
||
| 37 | /** @var string|false */ |
||
| 38 | private $redirectTemplate; |
||
| 39 | |||
| 40 | /** @var Twig_Template[] */ |
||
| 41 | private $templateDependencies; |
||
| 42 | |||
| 43 | /** @var PageView[] */ |
||
| 44 | private $pageViewsFlattened; |
||
| 45 | |||
| 46 | /** @var string[] */ |
||
| 47 | private $templateMapping; |
||
| 48 | |||
| 49 | /** @var PageView[][] */ |
||
| 50 | private $pageViews; |
||
| 51 | |||
| 52 | /** @var Folder */ |
||
| 53 | private $folder; |
||
| 54 | |||
| 55 | /** @var string */ |
||
| 56 | private $theme; |
||
| 57 | |||
| 58 | /** @var Twig_Environment */ |
||
| 59 | private $twig; |
||
| 60 | |||
| 61 | 14 | public function __construct() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * @param string|false $template |
||
| 71 | */ |
||
| 72 | public function setRedirectTemplate($template) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param Folder $folder |
||
| 79 | */ |
||
| 80 | 14 | public function setTargetFolder(Folder $folder) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param PageView[][] $pageViews |
||
| 87 | * @param PageView[] $pageViewsFlattened |
||
| 88 | */ |
||
| 89 | 14 | public function setPageViews(array &$pageViews, array &$pageViewsFlattened) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $themeName |
||
| 97 | */ |
||
| 98 | public function setThemeName($themeName) |
||
| 102 | |||
| 103 | /// |
||
| 104 | // Twig parent templates |
||
| 105 | /// |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Check whether a given file path is used as a parent template by a PageView |
||
| 109 | * |
||
| 110 | * @param string $filePath |
||
| 111 | * |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | 2 | public function isParentTemplate($filePath) |
|
| 115 | { |
||
| 116 | 2 | return isset($this->templateDependencies[$filePath]); |
|
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Rebuild all of the PageViews that used a given template as a parent |
||
| 121 | * |
||
| 122 | * @param string $filePath The file path to the parent Twig template |
||
| 123 | */ |
||
| 124 | 1 | public function refreshParent($filePath) |
|
| 125 | { |
||
| 126 | 1 | foreach ($this->templateDependencies[$filePath] as &$parentTemplate) |
|
|
|
|||
| 127 | { |
||
| 128 | $this->compilePageView($parentTemplate); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | public function getTemplateMappings() |
||
| 136 | |||
| 137 | /// |
||
| 138 | // IO Functionality |
||
| 139 | /// |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Compile all of the PageViews registered with the compiler. |
||
| 143 | * |
||
| 144 | * @since 0.1.0 |
||
| 145 | */ |
||
| 146 | 14 | public function compileAll() |
|
| 153 | |||
| 154 | public function compileSome($filter = array()) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Compile an individual PageView item. |
||
| 171 | * |
||
| 172 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
| 173 | * respective target file. |
||
| 174 | * |
||
| 175 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
||
| 176 | * |
||
| 177 | * @since 0.1.1 |
||
| 178 | */ |
||
| 179 | 14 | public function compilePageView(&$pageView) |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Write the compiled output for a static PageView. |
||
| 221 | * |
||
| 222 | * @param PageView $pageView |
||
| 223 | * |
||
| 224 | * @since 0.1.1 |
||
| 225 | */ |
||
| 226 | 10 | private function compileStaticPageView(&$pageView) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Write the compiled output for a dynamic PageView. |
||
| 237 | * |
||
| 238 | * @param DynamicPageView $pageView |
||
| 239 | * |
||
| 240 | * @since 0.1.1 |
||
| 241 | */ |
||
| 242 | 2 | private function compileDynamicPageViews(&$pageView) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Write the compiled output for a repeater PageView. |
||
| 268 | * |
||
| 269 | * @param RepeaterPageView $pageView |
||
| 270 | * |
||
| 271 | * @since 0.1.1 |
||
| 272 | */ |
||
| 273 | 2 | private function compileRepeaterPageViews(&$pageView) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * @deprecated |
||
| 293 | * |
||
| 294 | * @todo This function needs to be rewritten or removed. Something |
||
| 295 | * |
||
| 296 | * @param ContentItem $contentItem |
||
| 297 | */ |
||
| 298 | public function compileContentItem(&$contentItem) |
||
| 312 | |||
| 313 | /// |
||
| 314 | // Redirect handling |
||
| 315 | /// |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Write redirects for standard redirects. |
||
| 319 | * |
||
| 320 | * @param PageView $pageView |
||
| 321 | * |
||
| 322 | * @since 0.1.1 |
||
| 323 | */ |
||
| 324 | 12 | private function compileStandardRedirects(&$pageView) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Write redirects for expanded redirects. |
||
| 342 | * |
||
| 343 | * @param RepeaterPageView $pageView |
||
| 344 | * |
||
| 345 | * @since 0.1.1 |
||
| 346 | */ |
||
| 347 | 2 | private function compileExpandedRedirects(&$pageView) |
|
| 369 | |||
| 370 | /// |
||
| 371 | // Twig Functionality |
||
| 372 | /// |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
| 376 | * |
||
| 377 | * @param Twig_Template $template |
||
| 378 | * @param PageView $pageView |
||
| 379 | * @param ExpandedValue $expandedValue |
||
| 380 | * |
||
| 381 | * @since 0.1.1 |
||
| 382 | * |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | 2 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Get the compiled HTML for a specific ContentItem. |
||
| 400 | * |
||
| 401 | * @param Twig_Template $template |
||
| 402 | * @param ContentItem $contentItem |
||
| 403 | * |
||
| 404 | * @since 0.1.1 |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | 2 | private function renderDynamicPageView(&$template, &$contentItem) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Get the compiled HTML for a static PageView. |
||
| 418 | * |
||
| 419 | * @param PageView $pageView |
||
| 420 | * |
||
| 421 | * @since 0.1.1 |
||
| 422 | * |
||
| 423 | * @throws \Exception |
||
| 424 | * @throws \Throwable |
||
| 425 | * @throws Twig_Error_Syntax |
||
| 426 | * |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | 10 | private function renderStaticPageView(&$pageView) |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Create a Twig template that just needs an array to render. |
||
| 440 | * |
||
| 441 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
| 442 | * |
||
| 443 | * @since 0.1.1 |
||
| 444 | * |
||
| 445 | * @throws \Exception |
||
| 446 | * @throws \Throwable |
||
| 447 | * @throws Twig_Error_Syntax |
||
| 448 | * |
||
| 449 | * @return Twig_Template |
||
| 450 | */ |
||
| 451 | 14 | private function createTwigTemplate(&$pageView) |
|
| 486 | } |
||
| 487 |