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 |
||
| 32 | class PageManager extends TrackingManager |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * The relative (to the stakx project) file path to the redirect template |
||
| 36 | * |
||
| 37 | * @var string|bool |
||
| 38 | */ |
||
| 39 | private $redirectTemplate; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var PageView[] |
||
| 43 | */ |
||
| 44 | private $twigExtendsDeps; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var ContentItem[][] |
||
| 48 | */ |
||
| 49 | private $collections; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var Folder |
||
| 53 | */ |
||
| 54 | private $targetDir; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var PageView[] |
||
| 58 | */ |
||
| 59 | private $flatPages; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var PageView[] |
||
| 63 | */ |
||
| 64 | private $siteMenu; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | private $twigOpts; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var \Twig_Environment |
||
| 73 | */ |
||
| 74 | private $twig; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * PageManager constructor |
||
| 78 | */ |
||
| 79 | 3 | public function __construct() |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Give this manager the collections we'll be using for dynamic PageViews |
||
| 92 | * |
||
| 93 | * @param ContentItem[][] $collections |
||
| 94 | */ |
||
| 95 | 3 | public function setCollections (&$collections) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Set the template used for redirects |
||
| 102 | * |
||
| 103 | * @param false|string $filePath The path to the redirect template |
||
| 104 | */ |
||
| 105 | public function setRedirectTemplate ($filePath) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * The location where the compiled website will be written to |
||
| 112 | * |
||
| 113 | * @param Folder $folder The relative target directory as specified from the configuration file |
||
| 114 | */ |
||
| 115 | 3 | public function setTargetFolder (&$folder) |
|
| 119 | |||
| 120 | 3 | public function configureTwig ($configuration, $options) |
|
| 127 | |||
| 128 | 1 | public function getFlatPages () |
|
| 132 | |||
| 133 | /** |
||
| 134 | * An array representing the website's menu structure with children and grandchildren made from static PageViews |
||
| 135 | * |
||
| 136 | * @return JailObject[] |
||
| 137 | */ |
||
| 138 | 1 | public function getSiteMenu () |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Go through all of the PageView directories and create a respective PageView for each and classify them as a |
||
| 158 | * dynamic or static PageView. |
||
| 159 | * |
||
| 160 | * @param $pageViewFolders |
||
| 161 | */ |
||
| 162 | 3 | public function parsePageViews ($pageViewFolders) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Compile dynamic and static PageViews |
||
| 189 | */ |
||
| 190 | 3 | public function compileAll () |
|
| 197 | |||
| 198 | public function compileSome ($filter = array()) |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param ContentItem $contentItem |
||
| 212 | */ |
||
| 213 | public function compileContentItem (&$contentItem) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Add a new ContentItem to the respective parent PageView of the ContentItem |
||
| 237 | * |
||
| 238 | * @param ContentItem $contentItem |
||
| 239 | */ |
||
| 240 | public function updatePageView ($contentItem) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Update an existing Twig variable that's injected globally |
||
| 256 | * |
||
| 257 | * @param string $variable |
||
| 258 | * @param string $value |
||
| 259 | */ |
||
| 260 | public function updateTwigVariable ($variable, $value) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | 3 | public function isTracked($filePath) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | public function refreshItem($filePath) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * {@inheritdoc} |
||
| 295 | */ |
||
| 296 | 3 | protected function handleTrackableItem($filePath, $options = array()) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * @param DynamicPageView $pageView |
||
| 323 | */ |
||
| 324 | 3 | private function handleTrackableDynamicPageView ($pageView) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @param PageView $pageView |
||
| 343 | */ |
||
| 344 | 3 | private function handleTrackableStaticPageView ($pageView) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Create a Twig environment |
||
| 354 | */ |
||
| 355 | 3 | private function createTwigManager () |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Compile a given PageView |
||
| 368 | * |
||
| 369 | * @param string $filePath The file path to the PageView to compile |
||
| 370 | * |
||
| 371 | * @throws \Exception |
||
| 372 | */ |
||
| 373 | 3 | private function compileFromFilePath ($filePath) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * @param DynamicPageView|RepeaterPageView|PageView $pageView |
||
| 396 | */ |
||
| 397 | 3 | private function compilePageView ($pageView) |
|
| 417 | |||
| 418 | /** |
||
| 419 | * @param RepeaterPageView $pageView |
||
| 420 | */ |
||
| 421 | 3 | private function compileRepeaterPageView (&$pageView) |
|
| 442 | |||
| 443 | /** |
||
| 444 | * @param PageView $pageView |
||
| 445 | */ |
||
| 446 | 3 | private function compileDynamicPageView (&$pageView) |
|
| 469 | |||
| 470 | /** |
||
| 471 | * @param PageView $pageView |
||
| 472 | */ |
||
| 473 | 3 | private function compileStaticPageView (&$pageView) |
|
| 485 | |||
| 486 | /** |
||
| 487 | * @param DynamicPageView|PageView $pageView |
||
| 488 | */ |
||
| 489 | 3 | private function compileNormalRedirects (&$pageView) |
|
| 502 | |||
| 503 | /** |
||
| 504 | * @param RepeaterPageView $pageView |
||
| 505 | */ |
||
| 506 | 3 | private function compileExpandedRedirects (&$pageView) |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Add a static PageView to the menu array. Dynamic PageViews are not added to the menu |
||
| 532 | * |
||
| 533 | * @param PageView $pageView |
||
| 534 | */ |
||
| 535 | 3 | private function addToSiteMenu (&$pageView) |
|
| 580 | |||
| 581 | /** |
||
| 582 | * @param PageView $pageView |
||
| 583 | * |
||
| 584 | * @return Twig_Template |
||
| 585 | * @throws Twig_Error_Syntax |
||
| 586 | */ |
||
| 587 | 3 | private function createTemplate (&$pageView) |
|
| 605 | |||
| 606 | /** |
||
| 607 | * Find the parent Twig templates of the given template and keep a list of it |
||
| 608 | * |
||
| 609 | * @param Twig_Template $template The template created from the PageView's content |
||
| 610 | * @param PageView $pageView The PageView that has this content. Used to keep a reference of PageViews |
||
| 611 | */ |
||
| 612 | 3 | private function trackParentTwigTemplate ($template, &$pageView) |
|
| 627 | } |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.