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 |
||
| 21 | class PageManager extends BaseManager |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var PageView[] |
||
| 25 | */ |
||
| 26 | private $dynamicPageViews; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var PageView[] |
||
| 30 | */ |
||
| 31 | private $staticPageViews; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var ContentItem[][] |
||
| 35 | */ |
||
| 36 | private $collections; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var Folder |
||
| 40 | */ |
||
| 41 | private $targetDir; |
||
| 42 | |||
| 43 | private $siteMenu; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var \Twig_Environment |
||
| 47 | */ |
||
| 48 | private $twig; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * PageManager constructor |
||
| 52 | */ |
||
| 53 | public function __construct() |
||
| 60 | |||
| 61 | public function configureTwig ($configuration, $options) |
||
| 68 | |||
| 69 | /** |
||
| 70 | * An array representing the website's menu structure with children and grandchildren made from static PageViews |
||
| 71 | * |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | public function getSiteMenu () |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Go through all of the PageView directories and create a respective PageView for each and classify them as a |
||
| 81 | * dynamic or static PageView. |
||
| 82 | * |
||
| 83 | * @param $pageViewFolders |
||
| 84 | */ |
||
| 85 | public function parsePageViews ($pageViewFolders) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Go through all of the dynamic PageViews and prepare the necessary information for each one. |
||
| 130 | * |
||
| 131 | * For example, permalinks are dynamic generated based on FrontMatter so this function sets the permalink for each |
||
| 132 | * ContentItem in a collection. This is called before dynamic PageViews are compiled in order to allow access to |
||
| 133 | * this information to Twig by the time it is compiled. |
||
| 134 | * |
||
| 135 | * @param ContentItem[] $collections |
||
| 136 | */ |
||
| 137 | public function prepareDynamicPageViews ($collections) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Compile dynamic and static PageViews |
||
| 159 | * |
||
| 160 | * @param Folder $targetDir The relative target directory as specified from the configuration file |
||
| 161 | */ |
||
| 162 | public function compileAll (&$targetDir) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Compile a single PageView into the appropriate output path |
||
| 172 | * |
||
| 173 | * @param string $filePath |
||
| 174 | */ |
||
| 175 | public function compileSingle ($filePath) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param ContentItem $contentItem |
||
| 201 | */ |
||
| 202 | public function compileContentItem (&$contentItem) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Check whether or not a given file path is Page View |
||
| 220 | * |
||
| 221 | * @param string $filePath |
||
| 222 | * |
||
| 223 | * @return bool True if the file path given is to a Page View |
||
| 224 | */ |
||
| 225 | public function isPageView ($filePath) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Update an existing Twig variable that's injected globally |
||
| 232 | * |
||
| 233 | * @param string $variable |
||
| 234 | * @param string $value |
||
| 235 | */ |
||
| 236 | public function updateTwigVariable ($variable, $value) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * A dynamic PageView is one that is built from a collection and each collection item deserves its own page. This |
||
| 243 | * function goes through all of the dynamic PageViews and compiles each page |
||
| 244 | */ |
||
| 245 | private function compileDynamicPageViews () |
||
| 252 | |||
| 253 | /** |
||
| 254 | * A static PageView is built from a single Twig file and is not automatically rendered based on a collection's |
||
| 255 | * content. This function goes through all of the static PageViews and compiles them. |
||
| 256 | * |
||
| 257 | * @throws \Exception |
||
| 258 | */ |
||
| 259 | private function compileStaticPageViews () |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param PageView $pageView |
||
| 269 | */ |
||
| 270 | private function compileDynamicPageView ($pageView) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * @param PageView $pageView |
||
| 290 | */ |
||
| 291 | private function compileStaticPageView ($pageView) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Add a static PageView to the menu array. Dynamic PageViews are not added to the menu |
||
| 305 | * |
||
| 306 | * @param array $frontMatter |
||
| 307 | */ |
||
| 308 | private function addToSiteMenu ($frontMatter) |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param PageView $pageView |
||
| 342 | * |
||
| 343 | * @return Twig_Template |
||
| 344 | * @throws Twig_Error_Syntax |
||
| 345 | */ |
||
| 346 | private function createTemplate ($pageView) |
||
| 359 | } |
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.