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 | 1 | /** |
|
67 | 1 | * @var \Twig_Environment |
|
68 | 1 | */ |
|
69 | private $twig; |
||
70 | 1 | ||
71 | 1 | /** |
|
72 | * PageManager constructor |
||
73 | 1 | */ |
|
74 | public function __construct() |
||
84 | |||
85 | /** |
||
86 | * Give this manager the collections we'll be using for dynamic PageViews |
||
87 | * |
||
88 | 1 | * @param ContentItem[][] $collections |
|
89 | */ |
||
90 | 1 | public function setCollections (&$collections) |
|
94 | |||
95 | 1 | /** |
|
96 | 1 | * Set the template used for redirects |
|
97 | * |
||
98 | 1 | * @param false|string $filePath The path to the redirect template |
|
99 | 1 | */ |
|
100 | public function setRedirectTemplate ($filePath) |
||
104 | 1 | ||
105 | 1 | /** |
|
106 | 1 | * The location where the compiled website will be written to |
|
107 | 1 | * |
|
108 | * @param Folder $folder The relative target directory as specified from the configuration file |
||
109 | 1 | */ |
|
110 | 1 | public function setTargetFolder (&$folder) |
|
114 | |||
115 | public function configureTwig ($configuration, $options) |
||
122 | |||
123 | 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 | public function getSiteMenu () |
||
150 | 1 | ||
151 | /** |
||
152 | 1 | * 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 | public function parsePageViews ($pageViewFolders) |
||
183 | |||
184 | 1 | /** |
|
185 | 1 | * Compile dynamic and static PageViews |
|
186 | 1 | */ |
|
187 | 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 | 1 | ||
290 | /** |
||
291 | 1 | * {@inheritdoc} |
|
292 | 1 | */ |
|
293 | protected function handleTrackableItem($filePath, $options = array()) |
||
321 | |||
322 | /** |
||
323 | * Create a Twig environment |
||
324 | */ |
||
325 | private function createTwigManager () |
||
335 | |||
336 | 1 | /** |
|
337 | 1 | * 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 | 1 | */ |
|
344 | private function compileFromFilePath ($filePath, $refresh = false) |
||
370 | |||
371 | /** |
||
372 | 1 | * @param DynamicPageView|PageView|RepeaterPageView $pageView |
|
373 | * @param bool $refresh |
||
374 | 1 | */ |
|
375 | 1 | private function compilePageView ($pageView, $refresh = false) |
|
400 | |||
401 | /** |
||
402 | * @param RepeaterPageView $pageView |
||
403 | */ |
||
404 | private function compileRepeaterPageView (&$pageView) |
||
425 | 1 | ||
426 | 1 | /** |
|
427 | * @param PageView $pageView |
||
428 | 1 | */ |
|
429 | 1 | private function compileDynamicPageView (&$pageView) |
|
447 | 1 | ||
448 | /** |
||
449 | * @param PageView $pageView |
||
450 | */ |
||
451 | private function compileStaticPageView (&$pageView) |
||
463 | |||
464 | /** |
||
465 | * @param DynamicPageView|PageView $pageView |
||
466 | */ |
||
467 | private function compileNormalRedirects (&$pageView) |
||
480 | |||
481 | 1 | /** |
|
482 | * @param RepeaterPageView $pageView |
||
483 | 1 | */ |
|
484 | private function compileExpandedRedirects (&$pageView) |
||
507 | 1 | ||
508 | 1 | /** |
|
509 | * Add a static PageView to the menu array. Dynamic PageViews are not added to the menu |
||
510 | * |
||
511 | * @param PageView $pageView |
||
512 | */ |
||
513 | 1 | private function addToSiteMenu (&$pageView) |
|
558 | |||
559 | /** |
||
560 | * @param PageView $pageView |
||
561 | * |
||
562 | * @return Twig_Template |
||
563 | * @throws Twig_Error_Syntax |
||
564 | */ |
||
565 | private function createTemplate (&$pageView) |
||
583 | |||
584 | /** |
||
585 | * Find the parent Twig templates of the given template and keep a list of it |
||
586 | * |
||
587 | * @param Twig_Template $template The template created from the PageView's content |
||
588 | * @param PageView $pageView The PageView that has this content. Used to keep a reference of PageViews |
||
589 | */ |
||
590 | private function trackParentTwigTemplate ($template, &$pageView) |
||
605 | } |