Conditions | 19 |
Paths | 780 |
Total Lines | 79 |
Code Lines | 46 |
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 | $subPages = new PagesCollection("section-$pageId", $pagesAsArray); |
||
71 | // cascade variables |
||
72 | if ($page->hasVariable('cascade')) { |
||
73 | $cascade = $page->getVariable('cascade'); |
||
74 | if (\is_array($cascade)) { |
||
75 | $subPages->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 | $pages = Section::sortSubPages($this->config, $page, $subPages); |
||
86 | // adds navigation links (excludes taxonomy pages) |
||
87 | $sortBy = $page->getVariable('sortby')['variable'] ?? $page->getVariable('sortby') ?? $this->config->get('pages.sortby')['variable'] ?? $this->config->get('pages.sortby') ?? 'date'; |
||
88 | if (!\in_array($page->getId(), array_keys((array) $this->config->get('taxonomies')))) { |
||
89 | $this->addNavigationLinks($pages, $sortBy, $page->getVariable('circular') ?? false); |
||
90 | } |
||
91 | // creates page for each section |
||
92 | $page->setType(Type::SECTION->value) |
||
93 | ->setSection($path) |
||
94 | ->setPages($pages) |
||
95 | ->setVariable('language', $language) |
||
96 | ->setVariable('date', $pages->first()->getVariable('date')) |
||
97 | ->setVariable('langref', $path); |
||
98 | // human readable title |
||
99 | if ($page->getVariable('title') == 'index') { |
||
100 | $page->setVariable('title', $section); |
||
101 | } |
||
102 | // default menu |
||
103 | if (!$page->getVariable('menu')) { |
||
104 | $page->setVariable('menu', ['main' => ['weight' => $menuWeight]]); |
||
105 | } |
||
106 | |||
107 | try { |
||
108 | $this->generatedPages->add($page); |
||
109 | } catch (\DomainException) { |
||
110 | $this->generatedPages->replace($page->getId(), $page); |
||
111 | } |
||
112 | } |
||
113 | $menuWeight += 10; |
||
114 | } |
||
180 |