| Conditions | 19 |
| Paths | 780 |
| Total Lines | 80 |
| Code Lines | 47 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 35 | public function generate(): void |
||
| 36 | { |
||
| 37 | $sections = []; |
||
| 38 | |||
| 39 | // identifying sections from all pages |
||
| 40 | /** @var Page $page */ |
||
| 41 | foreach ($this->builder->getPages() ?? [] as $page) { |
||
| 42 | // top level (root) sections |
||
| 43 | if ($page->getSection()) { |
||
| 44 | // do not add "not published" and "not excluded" pages to its section |
||
| 45 | if ( |
||
| 46 | $page->getVariable('published') !== true |
||
| 47 | || ($page->getVariable('excluded') || $page->getVariable('exclude')) |
||
| 48 | ) { |
||
| 49 | continue; |
||
| 50 | } |
||
| 51 | $sections[$page->getSection()][$page->getVariable('language', $this->config->getLanguageDefault())][] = $page; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | |||
| 55 | // adds each section to pages collection |
||
| 56 | if (\count($sections) > 0) { |
||
| 57 | $menuWeight = 100; |
||
| 58 | |||
| 59 | foreach ($sections as $section => $languages) { |
||
| 60 | foreach ($languages as $language => $pagesAsArray) { |
||
| 61 | $pageId = $path = Page::slugify($section); |
||
| 62 | if ($language != $this->config->getLanguageDefault()) { |
||
| 63 | $pageId = "$language/$pageId"; |
||
| 64 | } |
||
| 65 | $page = (new Page($pageId))->setVariable('title', ucfirst($section)) |
||
| 66 | ->setPath($path); |
||
| 67 | if ($this->builder->getPages()->has($pageId)) { |
||
| 68 | $page = clone $this->builder->getPages()->get($pageId); |
||
| 69 | } |
||
| 70 | $pages = new PagesCollection("section-$pageId", $pagesAsArray); |
||
| 71 | // cascade variables |
||
| 72 | if ($page->hasVariable('cascade')) { |
||
| 73 | $cascade = $page->getVariable('cascade'); |
||
| 74 | if (\is_array($cascade)) { |
||
| 75 | $pages->map(function (Page $page) use ($cascade) { |
||
| 76 | foreach ($cascade as $key => $value) { |
||
| 77 | if (!$page->hasVariable($key)) { |
||
| 78 | $page->setVariable($key, $value); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | }); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | // sorts pages |
||
| 85 | $sortBy = $page->getVariable('sortby') ?? $this->config->get('pages.sortby'); |
||
| 86 | $pages = $pages->sortBy($sortBy); |
||
| 87 | // adds navigation links (excludes taxonomy pages) |
||
| 88 | $sortBy = $page->getVariable('sortby')['variable'] ?? $page->getVariable('sortby') ?? $this->config->get('pages.sortby')['variable'] ?? $this->config->get('pages.sortby') ?? 'date'; |
||
| 89 | if (!\in_array($page->getId(), array_keys((array) $this->config->get('taxonomies')))) { |
||
| 90 | $this->addNavigationLinks($pages, $sortBy, $page->getVariable('circular') ?? false); |
||
| 91 | } |
||
| 92 | // creates page for each section |
||
| 93 | $page->setType(Type::SECTION->value) |
||
| 94 | ->setSection($path) |
||
| 95 | ->setPages($pages) |
||
| 96 | ->setVariable('language', $language) |
||
| 97 | ->setVariable('date', $pages->first()->getVariable('date')) |
||
| 98 | ->setVariable('langref', $path); |
||
| 99 | // human readable title |
||
| 100 | if ($page->getVariable('title') == 'index') { |
||
| 101 | $page->setVariable('title', $section); |
||
| 102 | } |
||
| 103 | // default menu |
||
| 104 | if (!$page->getVariable('menu')) { |
||
| 105 | $page->setVariable('menu', ['main' => ['weight' => $menuWeight]]); |
||
| 106 | } |
||
| 107 | |||
| 108 | try { |
||
| 109 | $this->generatedPages->add($page); |
||
| 110 | } catch (\DomainException) { |
||
| 111 | $this->generatedPages->replace($page->getId(), $page); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | $menuWeight += 10; |
||
| 115 | } |
||
| 164 |