Conditions | 16 |
Paths | 97 |
Total Lines | 47 |
Code Lines | 28 |
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 |
||
34 | public function generate(): void |
||
35 | { |
||
36 | foreach ($this->config->getLanguages() as $lang) { |
||
37 | $language = $lang['code']; |
||
38 | $pageId = 'index'; |
||
39 | if ($language != $this->config->getLanguageDefault()) { |
||
40 | $pageId = "$language/$pageId"; |
||
41 | } |
||
42 | // creates a new index page... |
||
43 | $page = (new Page($pageId))->setPath('')->setVariable('title', 'Home'); |
||
44 | // ... clones it if already exists |
||
45 | if ($this->builder->getPages()->has($pageId)) { |
||
46 | $page = clone $this->builder->getPages()->get($pageId); |
||
47 | } |
||
48 | /** @var \Cecil\Collection\Page\Page $page */ |
||
49 | $page->setType(Type::HOMEPAGE->value); |
||
50 | // collects all pages |
||
51 | $pages = $this->builder->getPages()->filter(function (Page $page) use ($language) { |
||
52 | return $page->getType() == Type::PAGE->value |
||
53 | && $page->getVariable('published') === true |
||
54 | && ($page->getVariable('excluded') !== true && $page->getVariable('exclude') !== true) |
||
55 | && $page->isVirtual() === false |
||
56 | && $page->getVariable('language') == $language; |
||
57 | }); |
||
58 | // or collects pages from a specified section |
||
59 | /** @var \Cecil\Collection\Page\Collection $pages */ |
||
60 | if ($page->hasVariable('pagesfrom') && $this->builder->getPages()->has((string) $page->getVariable('pagesfrom'))) { |
||
61 | $pages = $this->builder->getPages()->get((string) $page->getVariable('pagesfrom'))->getPages(); |
||
62 | } |
||
63 | if ($pages instanceof \Cecil\Collection\Page\Collection) { |
||
64 | // sorts pages |
||
65 | $sortBy = $page->getVariable('sortby') ?? $this->config->get('pages.sortby'); |
||
66 | $pages = $pages->sortBy($sortBy); |
||
67 | $page->setPages($pages); |
||
68 | if ($pages->first()) { |
||
69 | $page->setVariable('date', $pages->first()->getVariable('date')); |
||
70 | } |
||
71 | } |
||
72 | // set default "main" menu |
||
73 | if (!$page->getVariable('menu')) { |
||
74 | $page->setVariable('menu', ['main' => ['weight' => 0]]); |
||
75 | } |
||
76 | // add an alias redirection from the root directory if language path prefix is enabled for the default language |
||
77 | if ($language == $this->config->getLanguageDefault() && $this->config->isEnabled('language.prefix')) { |
||
78 | $page->setVariable('alias', '../'); |
||
79 | } |
||
80 | $this->generatedPages->add($page); |
||
81 | } |
||
84 |