Complex classes like PageManager 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 PageManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class PageManager extends TrackingManager |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * The relative (to the stakx project) file path to the redirect template |
||
| 31 | * |
||
| 32 | * @var string|bool |
||
| 33 | */ |
||
| 34 | private $redirectTemplate; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var PageView[] |
||
| 38 | */ |
||
| 39 | private $twigExtendsDeps; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var ContentItem[][] |
||
| 43 | */ |
||
| 44 | private $collections; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Folder |
||
| 48 | */ |
||
| 49 | private $targetDir; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var PageView[] |
||
| 53 | */ |
||
| 54 | private $flatPages; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var PageView[] |
||
| 58 | */ |
||
| 59 | private $siteMenu; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private $twigOpts; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var \Twig_Environment |
||
| 68 | */ |
||
| 69 | private $twig; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * PageManager constructor |
||
| 73 | */ |
||
| 74 | 4 | public function __construct() |
|
| 75 | { |
||
| 76 | 4 | parent::__construct(); |
|
| 77 | |||
| 78 | 4 | $this->redirectTemplate = false; |
|
| 79 | 4 | $this->twigExtendsDeps = array(); |
|
| 80 | 4 | $this->collections = array(); |
|
| 81 | 4 | $this->flatPages = array(); |
|
| 82 | 4 | $this->siteMenu = array(); |
|
| 83 | 4 | } |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Give this manager the collections we'll be using for dynamic PageViews |
||
| 87 | * |
||
| 88 | * @param ContentItem[][] $collections |
||
| 89 | */ |
||
| 90 | 1 | public function setCollections (&$collections) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Set the template used for redirects |
||
| 97 | * |
||
| 98 | * @param false|string $filePath The path to the redirect template |
||
| 99 | */ |
||
| 100 | 1 | public function setRedirectTemplate ($filePath) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * The location where the compiled website will be written to |
||
| 107 | * |
||
| 108 | * @param Folder $folder The relative target directory as specified from the configuration file |
||
| 109 | */ |
||
| 110 | 4 | public function setTargetFolder (&$folder) |
|
| 114 | |||
| 115 | 1 | public function configureTwig ($configuration, $options) |
|
| 122 | |||
| 123 | 1 | public function getFlatPages () |
|
| 127 | |||
| 128 | /** |
||
| 129 | * An array representing the website's menu structure with children and grandchildren made from static PageViews |
||
| 130 | * |
||
| 131 | * @return JailObject[] |
||
| 132 | */ |
||
| 133 | 1 | public function getSiteMenu () |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Go through all of the PageView directories and create a respective PageView for each and classify them as a |
||
| 153 | * dynamic or static PageView. |
||
| 154 | * |
||
| 155 | * @param $pageViewFolders |
||
| 156 | */ |
||
| 157 | 4 | public function parsePageViews ($pageViewFolders) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Compile dynamic and static PageViews |
||
| 186 | */ |
||
| 187 | 1 | public function compileAll () |
|
| 194 | |||
| 195 | public function compileSome ($filter = array()) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param ContentItem $contentItem |
||
| 209 | */ |
||
| 210 | public function compileContentItem (&$contentItem) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Add a new ContentItem to the respective parent PageView of the ContentItem |
||
| 234 | * |
||
| 235 | * @param ContentItem $contentItem |
||
| 236 | */ |
||
| 237 | public function updatePageView ($contentItem) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Update an existing Twig variable that's injected globally |
||
| 253 | * |
||
| 254 | * @param string $variable |
||
| 255 | * @param string $value |
||
| 256 | */ |
||
| 257 | public function updateTwigVariable ($variable, $value) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritdoc} |
||
| 264 | */ |
||
| 265 | public function isTracked($filePath) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * {@inheritdoc} |
||
| 272 | */ |
||
| 273 | public function refreshItem($filePath) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * {@inheritdoc} |
||
| 292 | */ |
||
| 293 | 3 | protected function handleTrackableItem($filePath, $options = array()) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Create a Twig environment |
||
| 324 | */ |
||
| 325 | 1 | private function createTwigManager () |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Compile a given PageView |
||
| 338 | * |
||
| 339 | * @param string $filePath The file path to the PageView to compile |
||
| 340 | * @param bool $refresh When set to true, the PageView will reread its contents |
||
| 341 | * |
||
| 342 | * @throws \Exception |
||
| 343 | */ |
||
| 344 | private function compileFromFilePath ($filePath, $refresh = false) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param DynamicPageView|PageView|RepeaterPageView $pageView |
||
| 366 | * @param bool $refresh |
||
| 367 | */ |
||
| 368 | private function compilePageView ($pageView, $refresh = false) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @param RepeaterPageView $pageView |
||
| 396 | */ |
||
| 397 | private function compileRepeaterPageView (&$pageView) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param PageView $pageView |
||
| 421 | */ |
||
| 422 | private function compileDynamicPageView (&$pageView) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @param PageView $pageView |
||
| 443 | */ |
||
| 444 | private function compileStaticPageView (&$pageView) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param DynamicPageView|PageView $pageView |
||
| 459 | */ |
||
| 460 | private function compileNormalRedirects (&$pageView) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @param RepeaterPageView $pageView |
||
| 476 | */ |
||
| 477 | private function compileExpandedRedirects (&$pageView) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Add a static PageView to the menu array. Dynamic PageViews are not added to the menu |
||
| 503 | * |
||
| 504 | * @param PageView $pageView |
||
| 505 | */ |
||
| 506 | private function addToSiteMenu (&$pageView) |
||
| 551 | |||
| 552 | /** |
||
| 553 | * @param PageView $pageView |
||
| 554 | * |
||
| 555 | * @return Twig_Template |
||
| 556 | * @throws Twig_Error_Syntax |
||
| 557 | */ |
||
| 558 | private function createTemplate (&$pageView) |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Find the parent Twig templates of the given template and keep a list of it |
||
| 579 | * |
||
| 580 | * @param Twig_Template $template The template created from the PageView's content |
||
| 581 | * @param PageView $pageView The PageView that has this content. Used to keep a reference of PageViews |
||
| 582 | */ |
||
| 583 | private function trackParentTwigTemplate ($template, &$pageView) |
||
| 598 | } |