Conditions | 13 |
Paths | 73 |
Total Lines | 49 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
28 | public function generate(): void |
||
29 | { |
||
30 | foreach ($this->config->getLanguages() as $lang) { |
||
31 | $language = $lang['code']; |
||
32 | |||
33 | $pageId = 'index'; |
||
34 | if ($language != $this->config->getLanguageDefault()) { |
||
35 | $pageId = \sprintf('index.%s', $language); |
||
36 | } |
||
37 | // creates a new index page... |
||
38 | $page = (new Page($pageId))->setPath('')->setVariable('title', 'Home'); |
||
39 | // ... clones it if already exists |
||
40 | if ($this->builder->getPages()->has($pageId)) { |
||
41 | $page = clone $this->builder->getPages()->get($pageId); |
||
42 | } |
||
43 | // collects all pages |
||
44 | $subPages = $this->builder->getPages()->filter(function (Page $page) use ($language) { |
||
45 | return $page->getType() == TYPE::PAGE |
||
46 | && $page->isVirtual() === false |
||
47 | && $page->getVariable('exclude') !== true |
||
48 | && $page->getVariable('language') == $language; |
||
49 | }); |
||
50 | |||
51 | /** @var \Cecil\Collection\Page\Page $page */ |
||
52 | if ($page->hasVariable('pagesfrom') && $this->builder->getPages()->has((string) $page->getVariable('pagesfrom'))) { |
||
53 | $subPages = $this->builder->getPages()->get((string) $page->getVariable('pagesfrom'))->getVariable('pages'); |
||
54 | } |
||
55 | // sorts |
||
56 | /** @var \Cecil\Collection\Page\Collection $subPages */ |
||
57 | /** @var \Cecil\Collection\Page\Page $page */ |
||
58 | $pages = $subPages->sortByDate(); |
||
59 | if ($page->hasVariable('sortby')) { |
||
60 | $sortMethod = \sprintf('sortBy%s', ucfirst((string) $page->getVariable('sortby'))); |
||
61 | if (!method_exists($pages, $sortMethod)) { |
||
62 | throw new RuntimeException(\sprintf('In page "%s" "%s" is not a valid value for "sortby" variable.', $page->getId(), $page->getVariable('sortby'))); |
||
63 | } |
||
64 | $pages = $pages->$sortMethod(); |
||
65 | } |
||
66 | /** @var \Cecil\Collection\Page\Page $page */ |
||
67 | $page->setType(Type::HOMEPAGE) |
||
68 | ->setVariable('pages', $pages); |
||
69 | if ($pages->first()) { |
||
70 | $page->setVariable('date', $pages->first()->getVariable('date')); |
||
71 | } |
||
72 | // default menu |
||
73 | if (!$page->getVariable('menu')) { |
||
74 | $page->setVariable('menu', ['main' => ['weight' => 0]]); |
||
75 | } |
||
76 | $this->generatedPages->add($page); |
||
77 | } |
||
80 |