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 |
||
25 | class PageManager extends TrackingManager |
||
26 | { |
||
27 | /** |
||
28 | * The relative (to the stakx project) file path to the redirect template |
||
29 | * |
||
30 | * @var string|bool |
||
31 | */ |
||
32 | private $redirectTemplate; |
||
33 | |||
34 | /** |
||
35 | * @var PageView[] |
||
36 | */ |
||
37 | private $twigExtendsDeps; |
||
38 | |||
39 | /** |
||
40 | * @var ContentItem[][] |
||
41 | */ |
||
42 | private $collections; |
||
43 | |||
44 | /** |
||
45 | * @var Folder |
||
46 | */ |
||
47 | private $targetDir; |
||
48 | |||
49 | /** |
||
50 | * @var PageView[] |
||
51 | */ |
||
52 | private $flatPages; |
||
53 | |||
54 | private $siteMenu; |
||
55 | |||
56 | private $twigOpts; |
||
57 | |||
58 | /** |
||
59 | * @var \Twig_Environment |
||
60 | */ |
||
61 | private $twig; |
||
62 | |||
63 | /** |
||
64 | * PageManager constructor |
||
65 | */ |
||
66 | 1 | public function __construct() |
|
72 | |||
73 | 1 | public function setCollections (&$collections) |
|
79 | |||
80 | public function setRedirectTemplate ($filePath) |
||
84 | |||
85 | /** |
||
86 | * @param Folder $folder The relative target directory as specified from the configuration file |
||
87 | */ |
||
88 | 1 | public function setTargetFolder (&$folder) |
|
92 | |||
93 | 1 | public function configureTwig ($configuration, $options) |
|
100 | |||
101 | 1 | public function createTwigManager () |
|
111 | |||
112 | public function getFlatPages () |
||
116 | |||
117 | /** |
||
118 | * An array representing the website's menu structure with children and grandchildren made from static PageViews |
||
119 | * |
||
120 | * @return array |
||
121 | */ |
||
122 | public function getSiteMenu () |
||
143 | |||
144 | /** |
||
145 | * Go through all of the PageView directories and create a respective PageView for each and classify them as a |
||
146 | * dynamic or static PageView. |
||
147 | * |
||
148 | * @param $pageViewFolders |
||
149 | */ |
||
150 | 1 | public function parsePageViews ($pageViewFolders) |
|
176 | |||
177 | /** |
||
178 | * Compile dynamic and static PageViews |
||
179 | */ |
||
180 | 1 | public function compileAll () |
|
187 | |||
188 | public function compileSome ($filter = array()) |
||
199 | |||
200 | /** |
||
201 | * @param ContentItem $contentItem |
||
202 | */ |
||
203 | public function compileContentItem (&$contentItem) |
||
224 | |||
225 | /** |
||
226 | * Add a new ContentItem to the respective parent PageView of the ContentItem |
||
227 | * |
||
228 | * @param ContentItem $contentItem |
||
229 | */ |
||
230 | public function updatePageView ($contentItem) |
||
243 | |||
244 | /** |
||
245 | * Update an existing Twig variable that's injected globally |
||
246 | * |
||
247 | * @param string $variable |
||
248 | * @param string $value |
||
249 | */ |
||
250 | public function updateTwigVariable ($variable, $value) |
||
254 | |||
255 | /** |
||
256 | * {@inheritdoc} |
||
257 | */ |
||
258 | 1 | public function isTracked($filePath) |
|
262 | |||
263 | /** |
||
264 | * {@inheritdoc} |
||
265 | */ |
||
266 | public function refreshItem($filePath) |
||
282 | |||
283 | /** |
||
284 | * {@inheritdoc} |
||
285 | */ |
||
286 | 1 | protected function handleTrackableItem($filePath, $options = array()) |
|
287 | { |
||
288 | 1 | $pageView = PageView::create($filePath); |
|
289 | 1 | $namespace = $pageView->getType(); |
|
290 | |||
291 | 1 | if ($namespace == PageView::DYNAMIC_TYPE) |
|
292 | 1 | { |
|
293 | $frontMatter = $pageView->getFrontMatter(false); |
||
294 | $collection = $frontMatter['collection']; |
||
295 | |||
296 | foreach ($this->collections[$collection] as &$item) |
||
297 | { |
||
298 | $item->evaluateFrontMatter($frontMatter); |
||
299 | $pageView->addContentItem($item); |
||
300 | } |
||
301 | } |
||
302 | |||
303 | 1 | $this->addObjectToTracker($pageView, $pageView->getRelativeFilePath(), $namespace); |
|
304 | 1 | $this->saveTrackerOptions($pageView->getRelativeFilePath(), array( |
|
305 | 'viewType' => $namespace |
||
306 | 1 | )); |
|
307 | |||
308 | 1 | if ($namespace == PageView::STATIC_TYPE) |
|
309 | 1 | { |
|
310 | 1 | $this->addToSiteMenu($pageView); |
|
311 | |||
312 | 1 | if (!empty($pageView->title)) { |
|
313 | 1 | $this->flatPages[$pageView->title] = $pageView->createJail(); |
|
314 | 1 | } |
|
315 | 1 | } |
|
316 | 1 | } |
|
317 | |||
318 | /** |
||
319 | * Compile a given PageView |
||
320 | * |
||
321 | * @param string $filePath The file path to the PageView to compile |
||
322 | * @param bool $refresh When set to true, the PageView will reread its contents |
||
323 | * |
||
324 | * @throws \Exception |
||
325 | */ |
||
326 | 1 | private function compileFromFilePath ($filePath, $refresh = false) |
|
338 | |||
339 | /** |
||
340 | * @param DynamicPageView|PageView|RepeaterPageView $pageView |
||
341 | * @param bool $refresh |
||
342 | */ |
||
343 | 1 | private function compilePageView ($pageView, $refresh = false) |
|
344 | { |
||
345 | if ($refresh) |
||
346 | 1 | { |
|
347 | $pageView->refreshFileContent(); |
||
348 | } |
||
349 | |||
350 | 1 | switch ($pageView->getType()) |
|
351 | { |
||
352 | 1 | case PageView::REPEATER_TYPE: |
|
353 | 1 | $this->compileRepeaterPageView($pageView); |
|
354 | 1 | $this->compileExpandedRedirects($pageView); |
|
355 | 1 | break; |
|
356 | |||
357 | 1 | case PageView::DYNAMIC_TYPE: |
|
358 | $this->compileDynamicPageView($pageView); |
||
359 | $this->compileNormalRedirects($pageView); |
||
360 | break; |
||
361 | |||
362 | 1 | case PageView::STATIC_TYPE: |
|
363 | 1 | $this->compileStaticPageView($pageView); |
|
364 | 1 | $this->compileNormalRedirects($pageView); |
|
365 | 1 | break; |
|
366 | 1 | } |
|
367 | 1 | } |
|
368 | |||
369 | /** |
||
370 | * @param RepeaterPageView $pageView |
||
371 | */ |
||
372 | 1 | private function compileRepeaterPageView (&$pageView) |
|
393 | |||
394 | /** |
||
395 | * @param PageView $pageView |
||
396 | */ |
||
397 | private function compileDynamicPageView (&$pageView) |
||
415 | |||
416 | /** |
||
417 | * @param PageView $pageView |
||
418 | */ |
||
419 | 1 | private function compileStaticPageView (&$pageView) |
|
420 | { |
||
421 | 1 | $this->twig->addGlobal('__currentTemplate', $pageView->getFilePath()); |
|
422 | |||
423 | 1 | $template = $this->createTemplate($pageView); |
|
424 | 1 | $output = $template->render(array( |
|
425 | 1 | 'this' => $pageView->createJail() |
|
426 | 1 | )); |
|
427 | |||
428 | 1 | $this->output->notice("Writing file: {file}", array('file' => $pageView->getTargetFile())); |
|
429 | 1 | $this->targetDir->writeFile($pageView->getTargetFile(), $output); |
|
430 | 1 | } |
|
431 | |||
432 | /** |
||
433 | * @param DynamicPageView|PageView $pageView |
||
434 | */ |
||
435 | 1 | private function compileNormalRedirects (&$pageView) |
|
436 | { |
||
437 | 1 | foreach ($pageView->getRedirects() as $redirect) |
|
438 | { |
||
439 | $redirectPageView = PageView::createRedirect( |
||
440 | $redirect, |
||
441 | $pageView->getPermalink(), |
||
442 | $this->redirectTemplate |
||
443 | ); |
||
444 | |||
445 | $this->compilePageView($redirectPageView); |
||
446 | 1 | } |
|
447 | 1 | } |
|
448 | |||
449 | /** |
||
450 | * @param RepeaterPageView $pageView |
||
451 | */ |
||
452 | 1 | private function compileExpandedRedirects (&$pageView) |
|
475 | |||
476 | /** |
||
477 | * Add a static PageView to the menu array. Dynamic PageViews are not added to the menu |
||
478 | * |
||
479 | * @param PageView $pageView |
||
480 | */ |
||
481 | 1 | private function addToSiteMenu (&$pageView) |
|
482 | { |
||
483 | 1 | $frontMatter = $pageView->getFrontMatter(); |
|
484 | |||
485 | 1 | if (isset($frontMatter['menu']) && !$frontMatter['menu']) |
|
486 | 1 | { |
|
487 | return; |
||
488 | } |
||
489 | |||
490 | 1 | $url = trim($pageView->getPermalink(), '/'); |
|
491 | |||
492 | 1 | if (empty($url)) |
|
493 | 1 | { |
|
494 | 1 | return; |
|
495 | 1 | } |
|
496 | |||
497 | 1 | $root = &$this->siteMenu; |
|
498 | 1 | $dirs = explode('/', $url); |
|
499 | |||
500 | 1 | while (count($dirs) > 0) |
|
501 | { |
||
502 | 1 | $name = array_shift($dirs); |
|
503 | 1 | $name = (!empty($name)) ? $name : '.'; |
|
504 | |||
505 | 1 | if (!is_null($name) && count($dirs) == 0) |
|
506 | 1 | { |
|
507 | 1 | if (isset($root[$name]) && is_array($root[$name])) |
|
508 | 1 | { |
|
509 | $children = &$pageView->getChildren(); |
||
510 | $children = $root[$name]['children']; |
||
511 | } |
||
512 | |||
513 | 1 | $root[$name] = &$pageView; |
|
514 | 1 | } |
|
515 | else |
||
516 | { |
||
517 | $root[$name]['children'] = array(); |
||
518 | $root = &$root[$name]['children']; |
||
519 | } |
||
520 | 1 | } |
|
521 | 1 | } |
|
522 | |||
523 | /** |
||
524 | * @param PageView $pageView |
||
525 | * |
||
526 | * @return Twig_Template |
||
527 | * @throws Twig_Error_Syntax |
||
528 | */ |
||
529 | 1 | private function createTemplate (&$pageView) |
|
547 | |||
548 | /** |
||
549 | * Find the parent Twig templates of the given template and keep a list of it |
||
550 | * |
||
551 | * @param Twig_Template $template The template created from the PageView's content |
||
552 | * @param PageView $pageView The PageView that has this content. Used to keep a reference of PageViews |
||
553 | */ |
||
554 | 1 | private function trackParentTwigTemplate ($template, &$pageView) |
|
569 | } |