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) |
||
| 106 | { |
||
| 107 | $this->redirectTemplate = $filePath; |
||
| 108 | } |
||
| 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 () |
|
| 139 | { |
||
| 140 | 1 | $jailedMenu = array(); |
|
| 141 | |||
| 142 | 1 | foreach ($this->siteMenu as $key => $value) |
|
| 143 | { |
||
| 144 | // If it's an array, it means the parent is hidden from the site menu therefore its children should be too |
||
| 145 | 1 | if (is_array($this->siteMenu[$key])) |
|
| 146 | { |
||
| 147 | continue; |
||
| 148 | } |
||
| 149 | |||
| 150 | 1 | $jailedMenu[$key] = $value->createJail(); |
|
| 151 | } |
||
| 152 | |||
| 153 | 1 | return $jailedMenu; |
|
| 154 | } |
||
| 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) |
|
| 163 | { |
||
| 164 | 3 | if (empty($pageViewFolders)) { return; } |
|
| 165 | |||
| 166 | /** |
||
| 167 | * The name of the folder where PageViews are located |
||
| 168 | * |
||
| 169 | * @var $pageViewFolder string |
||
| 170 | */ |
||
| 171 | 3 | foreach ($pageViewFolders as $pageViewFolderName) |
|
| 172 | { |
||
| 173 | 3 | $pageViewFolder = $this->fs->absolutePath($pageViewFolderName); |
|
| 174 | |||
| 175 | 3 | if (!$this->fs->exists($pageViewFolder)) |
|
| 176 | { |
||
| 177 | continue; |
||
| 178 | } |
||
| 179 | |||
| 180 | 3 | $this->scanTrackableItems($pageViewFolder, array( |
|
| 181 | 'fileExplorer' => FileExplorer::INCLUDE_ONLY_FILES |
||
| 182 | 3 | ), array('/.html$/', '/.twig$/')); |
|
| 183 | 3 | $this->saveFolderDefinition($pageViewFolderName); |
|
| 184 | } |
||
| 185 | 3 | } |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Compile dynamic and static PageViews |
||
| 189 | */ |
||
| 190 | 3 | public function compileAll () |
|
| 191 | { |
||
| 192 | 3 | foreach (array_keys($this->trackedItemsFlattened) as $filePath) |
|
| 193 | { |
||
| 194 | 3 | $this->compileFromFilePath($filePath); |
|
| 195 | } |
||
| 196 | 3 | } |
|
| 197 | |||
| 198 | public function compileSome ($filter = array()) |
||
| 199 | { |
||
| 200 | /** @var PageView $pageView */ |
||
| 201 | foreach ($this->trackedItemsFlattened as $pageView) |
||
| 202 | { |
||
| 203 | if ($pageView->hasTwigDependency($filter['namespace'], $filter['dependency'])) |
||
| 204 | { |
||
| 205 | $this->compilePageView($pageView); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param ContentItem $contentItem |
||
| 212 | */ |
||
| 213 | public function compileContentItem (&$contentItem) |
||
| 214 | { |
||
| 215 | $pageView = $contentItem->getPageView(); |
||
| 216 | |||
| 217 | // This ContentItem doesn't have an individual PageView dedicated to displaying this item |
||
| 218 | if (is_null($pageView)) |
||
| 219 | { |
||
| 220 | return; |
||
| 221 | } |
||
| 222 | |||
| 223 | $template = $this->createTemplate($pageView); |
||
| 224 | $contentItem->evaluateFrontMatter( |
||
| 225 | $pageView->getFrontMatter(false) |
||
| 226 | ); |
||
| 227 | |||
| 228 | $output = $template->render(array( |
||
| 229 | 'this' => $contentItem |
||
| 230 | )); |
||
| 231 | |||
| 232 | $this->targetDir->writeFile($contentItem->getTargetFile(), $output); |
||
| 233 | } |
||
| 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) |
||
| 241 | { |
||
| 242 | /** @var DynamicPageView $pageView */ |
||
| 243 | foreach ($this->trackedItems['dynamic'] as &$pageView) |
||
| 244 | { |
||
| 245 | $fm = $pageView->getFrontMatter(false); |
||
| 246 | |||
| 247 | if ($fm['collection'] == $contentItem->getCollection()) |
||
| 248 | { |
||
| 249 | $pageView->addContentItem($contentItem); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 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) |
||
| 261 | { |
||
| 262 | $this->twig->addGlobal($variable, $value); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | 3 | public function isTracked($filePath) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | */ |
||
| 276 | public function refreshItem($filePath) |
||
| 277 | { |
||
| 278 | if (parent::isTracked($filePath)) |
||
|
|
|||
| 279 | { |
||
| 280 | $this->compileFromFilePath($filePath); |
||
| 281 | |||
| 282 | return; |
||
| 283 | } |
||
| 284 | |||
| 285 | $this->createTwigManager(); |
||
| 286 | |||
| 287 | foreach ($this->twigExtendsDeps[$filePath] as $pageView) |
||
| 288 | { |
||
| 289 | $this->compilePageView($pageView); |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * {@inheritdoc} |
||
| 295 | */ |
||
| 296 | 3 | protected function handleTrackableItem($filePath, $options = array()) |
|
| 297 | { |
||
| 298 | 3 | $pageView = PageView::create($filePath); |
|
| 299 | 3 | $namespace = $pageView->getType(); |
|
| 300 | |||
| 301 | switch ($namespace) |
||
| 302 | { |
||
| 303 | 3 | case PageView::DYNAMIC_TYPE: |
|
| 304 | 3 | $this->handleTrackableDynamicPageView($pageView); |
|
| 305 | 3 | break; |
|
| 306 | |||
| 307 | 3 | case PageView::STATIC_TYPE: |
|
| 308 | 3 | $this->handleTrackableStaticPageView($pageView); |
|
| 309 | 3 | break; |
|
| 310 | |||
| 311 | default: |
||
| 312 | 3 | break; |
|
| 313 | } |
||
| 314 | |||
| 315 | 3 | $this->addObjectToTracker($pageView, $pageView->getRelativeFilePath(), $namespace); |
|
| 316 | 3 | $this->saveTrackerOptions($pageView->getRelativeFilePath(), array( |
|
| 317 | 3 | 'viewType' => $namespace |
|
| 318 | )); |
||
| 319 | 3 | } |
|
| 320 | |||
| 321 | /** |
||
| 322 | * @param DynamicPageView $pageView |
||
| 323 | */ |
||
| 324 | 3 | private function handleTrackableDynamicPageView ($pageView) |
|
| 325 | { |
||
| 326 | 3 | $frontMatter = $pageView->getFrontMatter(false); |
|
| 327 | 3 | $collection = $frontMatter['collection']; |
|
| 328 | |||
| 329 | 3 | if (!isset($this->collections[$collection])) |
|
| 330 | { |
||
| 331 | throw new \RuntimeException("The '$collection' collection is not defined"); |
||
| 332 | } |
||
| 333 | |||
| 334 | 3 | foreach ($this->collections[$collection] as &$item) |
|
| 335 | { |
||
| 336 | 3 | $item->evaluateFrontMatter($frontMatter); |
|
| 337 | 3 | $pageView->addContentItem($item); |
|
| 338 | } |
||
| 339 | 3 | } |
|
| 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 () |
|
| 356 | { |
||
| 357 | 3 | $twig = new TwigManager(); |
|
| 358 | 3 | $twig->configureTwig( |
|
| 359 | 3 | $this->twigOpts['configuration'], |
|
| 360 | 3 | $this->twigOpts['options'] |
|
| 361 | ); |
||
| 362 | |||
| 363 | 3 | $this->twig = TwigManager::getInstance(); |
|
| 364 | 3 | } |
|
| 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) |
|
| 374 | { |
||
| 375 | 3 | if (!$this->isTracked($filePath)) |
|
| 376 | { |
||
| 377 | throw new TrackedItemNotFoundException('PageView not found'); |
||
| 378 | } |
||
| 379 | |||
| 380 | /** @var DynamicPageView|PageView|RepeaterPageView $pageView */ |
||
| 381 | 3 | $pageView = &$this->trackedItemsFlattened[$filePath]; |
|
| 382 | |||
| 383 | try |
||
| 384 | { |
||
| 385 | 3 | $pageView->refreshFileContent(); |
|
| 386 | 3 | $this->compilePageView($pageView); |
|
| 387 | } |
||
| 388 | catch (\Exception $e) |
||
| 389 | { |
||
| 390 | throw FileAwareException::castException($e, $filePath); |
||
| 391 | } |
||
| 392 | 3 | } |
|
| 393 | |||
| 394 | /** |
||
| 395 | * @param DynamicPageView|RepeaterPageView|PageView $pageView |
||
| 396 | */ |
||
| 397 | 3 | private function compilePageView ($pageView) |
|
| 398 | { |
||
| 399 | 3 | switch ($pageView->getType()) |
|
| 400 | { |
||
| 401 | 3 | case PageView::REPEATER_TYPE: |
|
| 402 | 3 | $this->compileRepeaterPageView($pageView); |
|
| 403 | 3 | $this->compileExpandedRedirects($pageView); |
|
| 404 | 3 | break; |
|
| 405 | |||
| 406 | 3 | case PageView::DYNAMIC_TYPE: |
|
| 407 | 3 | $this->compileDynamicPageView($pageView); |
|
| 408 | 3 | $this->compileNormalRedirects($pageView); |
|
| 409 | 3 | break; |
|
| 410 | |||
| 411 | 3 | case PageView::STATIC_TYPE: |
|
| 412 | 3 | $this->compileStaticPageView($pageView); |
|
| 413 | 3 | $this->compileNormalRedirects($pageView); |
|
| 414 | 3 | break; |
|
| 415 | } |
||
| 416 | 3 | } |
|
| 417 | |||
| 418 | /** |
||
| 419 | * @param RepeaterPageView $pageView |
||
| 420 | */ |
||
| 421 | 3 | private function compileRepeaterPageView (&$pageView) |
|
| 422 | { |
||
| 423 | 3 | $template = $this->createTemplate($pageView); |
|
| 424 | 3 | $pageView->rewindPermalink(); |
|
| 425 | |||
| 426 | 3 | foreach ($pageView->getRepeaterPermalinks() as $permalink) |
|
| 427 | { |
||
| 428 | 3 | $pageView->bumpPermalink(); |
|
| 429 | 3 | $pageView->setFrontMatter(array( |
|
| 430 | 3 | 'permalink' => $permalink->getEvaluated(), |
|
| 431 | 3 | 'iterators' => $permalink->getIterators() |
|
| 432 | )); |
||
| 433 | |||
| 434 | 3 | $output = $template->render(array( |
|
| 435 | 3 | 'this' => $pageView->createJail() |
|
| 436 | )); |
||
| 437 | |||
| 438 | 3 | $this->output->notice("Writing repeater file: {file}", array('file' => $pageView->getTargetFile())); |
|
| 439 | 3 | $this->targetDir->writeFile($pageView->getTargetFile(), $output); |
|
| 440 | } |
||
| 441 | 3 | } |
|
| 442 | |||
| 443 | /** |
||
| 444 | * @param PageView $pageView |
||
| 445 | */ |
||
| 446 | 3 | private function compileDynamicPageView (&$pageView) |
|
| 447 | { |
||
| 448 | 3 | $template = $this->createTemplate($pageView); |
|
| 449 | |||
| 450 | 3 | $pageViewFrontMatter = $pageView->getFrontMatter(false); |
|
| 451 | 3 | $collection = $pageViewFrontMatter['collection']; |
|
| 452 | |||
| 453 | 3 | if (!isset($this->collections[$collection])) |
|
| 454 | { |
||
| 455 | throw new \RuntimeException("The '$collection' collection is not defined"); |
||
| 456 | } |
||
| 457 | |||
| 458 | /** @var ContentItem $contentItem */ |
||
| 459 | 3 | foreach ($this->collections[$collection] as &$contentItem) |
|
| 460 | { |
||
| 461 | 3 | $output = $template->render(array( |
|
| 462 | 3 | 'this' => $contentItem->createJail() |
|
| 463 | )); |
||
| 464 | |||
| 465 | 3 | $this->output->notice("Writing file: {file}", array('file' => $contentItem->getTargetFile())); |
|
| 466 | 3 | $this->targetDir->writeFile($contentItem->getTargetFile(), $output); |
|
| 467 | } |
||
| 468 | 3 | } |
|
| 469 | |||
| 470 | /** |
||
| 471 | * @param PageView $pageView |
||
| 472 | */ |
||
| 473 | 3 | private function compileStaticPageView (&$pageView) |
|
| 474 | { |
||
| 475 | 3 | $this->twig->addGlobal('__currentTemplate', $pageView->getFilePath()); |
|
| 476 | |||
| 477 | 3 | $template = $this->createTemplate($pageView); |
|
| 478 | 3 | $output = $template->render(array( |
|
| 479 | 3 | 'this' => $pageView->createJail() |
|
| 480 | )); |
||
| 481 | |||
| 482 | 3 | $this->output->notice("Writing file: {file}", array('file' => $pageView->getTargetFile())); |
|
| 483 | 3 | $this->targetDir->writeFile($pageView->getTargetFile(), $output); |
|
| 484 | 3 | } |
|
| 485 | |||
| 486 | /** |
||
| 487 | * @param DynamicPageView|PageView $pageView |
||
| 488 | */ |
||
| 489 | 3 | private function compileNormalRedirects (&$pageView) |
|
| 490 | { |
||
| 491 | 3 | foreach ($pageView->getRedirects() as $redirect) |
|
| 492 | { |
||
| 493 | $redirectPageView = PageView::createRedirect( |
||
| 494 | $redirect, |
||
| 495 | $pageView->getPermalink(), |
||
| 496 | $this->redirectTemplate |
||
| 497 | ); |
||
| 498 | |||
| 499 | $this->compilePageView($redirectPageView); |
||
| 500 | } |
||
| 501 | 3 | } |
|
| 502 | |||
| 503 | /** |
||
| 504 | * @param RepeaterPageView $pageView |
||
| 505 | */ |
||
| 506 | 3 | private function compileExpandedRedirects (&$pageView) |
|
| 507 | { |
||
| 508 | 3 | $permalinks = $pageView->getRepeaterPermalinks(); |
|
| 509 | |||
| 510 | /** @var ExpandedValue[] $repeaterRedirect */ |
||
| 511 | 3 | foreach ($pageView->getRepeaterRedirects() as $repeaterRedirect) |
|
| 512 | { |
||
| 513 | /** |
||
| 514 | * @var int $index |
||
| 515 | * @var ExpandedValue $redirect |
||
| 516 | */ |
||
| 517 | foreach ($repeaterRedirect as $index => $redirect) |
||
| 518 | { |
||
| 519 | $redirectPageView = PageView::createRedirect( |
||
| 520 | $redirect->getEvaluated(), |
||
| 521 | $permalinks[$index]->getEvaluated(), |
||
| 522 | $this->redirectTemplate |
||
| 523 | ); |
||
| 524 | |||
| 525 | $this->compilePageView($redirectPageView); |
||
| 526 | } |
||
| 527 | } |
||
| 528 | 3 | } |
|
| 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) |
|
| 536 | { |
||
| 537 | 3 | $frontMatter = $pageView->getFrontMatter(); |
|
| 538 | |||
| 539 | 3 | if (isset($frontMatter['menu']) && !$frontMatter['menu']) |
|
| 540 | { |
||
| 541 | return; |
||
| 542 | } |
||
| 543 | |||
| 544 | 3 | $url = trim($pageView->getPermalink(), '/'); |
|
| 545 | |||
| 546 | 3 | if (empty($url)) |
|
| 547 | { |
||
| 548 | return; |
||
| 549 | } |
||
| 550 | |||
| 551 | 3 | $root = &$this->siteMenu; |
|
| 552 | 3 | $dirs = explode('/', $url); |
|
| 553 | |||
| 554 | 3 | while (count($dirs) > 0) |
|
| 555 | { |
||
| 556 | 3 | $name = array_shift($dirs); |
|
| 557 | 3 | $name = (!empty($name)) ? $name : '.'; |
|
| 558 | |||
| 559 | 3 | if (!is_null($name) && count($dirs) == 0) |
|
| 560 | { |
||
| 561 | 3 | if (isset($root[$name]) && is_array($root[$name])) |
|
| 562 | { |
||
| 563 | 3 | $children = &$pageView->getChildren(); |
|
| 564 | 3 | $children = $root[$name]['children']; |
|
| 565 | } |
||
| 566 | |||
| 567 | 3 | $root[$name] = &$pageView; |
|
| 568 | } |
||
| 569 | else |
||
| 570 | { |
||
| 571 | 3 | if (!isset($root[$name]['children'])) |
|
| 572 | { |
||
| 573 | 3 | $root[$name]['children'] = array(); |
|
| 574 | } |
||
| 575 | |||
| 576 | 3 | $root = &$root[$name]['children']; |
|
| 577 | } |
||
| 578 | } |
||
| 579 | 3 | } |
|
| 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) |
|
| 613 | { |
||
| 614 | 3 | if (!$this->tracking) { return; } |
|
| 615 | |||
| 616 | /** @var Twig_Template $parent */ |
||
| 617 | $parent = $template->getParent(array()); |
||
| 618 | |||
| 619 | while ($parent !== false) |
||
| 620 | { |
||
| 621 | $filePath = $this->fs->getRelativePath($parent->getSourceContext()->getPath()); |
||
| 622 | |||
| 623 | $this->twigExtendsDeps[$filePath][(string)$pageView->getFilePath()] = &$pageView; |
||
| 624 | $parent = $parent->getParent(array()); |
||
| 625 | } |
||
| 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.