Conditions | 10 |
Paths | 48 |
Total Lines | 58 |
Code Lines | 36 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
26 | public function generate(): void |
||
27 | { |
||
28 | $sections = []; |
||
29 | |||
30 | // identifying sections |
||
31 | /** @var Page $page */ |
||
32 | foreach ($this->builder->getPages() as $page) { |
||
33 | if ($page->getSection()) { |
||
34 | // excludes page from section |
||
35 | if ($page->getVariable('exclude')) { |
||
36 | $alteredPage = clone $page; |
||
37 | $alteredPage->setSection(''); |
||
38 | $this->builder->getPages()->replace($page->getId(), $alteredPage); |
||
39 | continue; |
||
40 | } |
||
41 | $sections[$page->getSection()][] = $page; |
||
42 | } |
||
43 | } |
||
44 | |||
45 | // adds section to pages collection |
||
46 | if (count($sections) > 0) { |
||
47 | $menuWeight = 100; |
||
48 | foreach ($sections as $section => $pagesAsArray) { |
||
49 | $pageId = $path = Page::slugify($section); |
||
50 | $page = (new Page($pageId))->setVariable('title', ucfirst($section)); |
||
51 | if ($this->builder->getPages()->has($pageId)) { |
||
52 | $page = clone $this->builder->getPages()->get($pageId); |
||
53 | } |
||
54 | $pages = new PagesCollection($section, $pagesAsArray); |
||
55 | // sorts |
||
56 | $pages = $pages->sortByDate(); |
||
57 | /** @var \Cecil\Collection\Page\Page $page */ |
||
58 | if ($page->getVariable('sortby')) { |
||
59 | $sortMethod = sprintf('sortBy%s', ucfirst($page->getVariable('sortby'))); |
||
60 | if (!method_exists($pages, $sortMethod)) { |
||
61 | throw new Exception(sprintf( |
||
62 | 'In page "%s" the value "%s" is not valid for "sortby" variable.', |
||
63 | $page->getId(), |
||
64 | $page->getVariable('sortby') |
||
65 | )); |
||
66 | } |
||
67 | $pages = $pages->$sortMethod(); |
||
68 | } |
||
69 | // adds navigation links |
||
70 | $this->addNavigationLinks($pages, $page->getVariable('sortby')); |
||
71 | // creates page for each section |
||
72 | $page->setPath($path) |
||
73 | ->setType(Type::SECTION) |
||
74 | ->setVariable('pages', $pages) |
||
75 | ->setVariable('date', $pages->first()->getVariable('date')); |
||
76 | // default menu |
||
77 | if (!$page->getVariable('menu')) { |
||
78 | $page->setVariable('menu', [ |
||
79 | 'main' => ['weight' => $menuWeight], |
||
80 | ]); |
||
81 | } |
||
82 | $this->generatedPages->add($page); |
||
83 | $menuWeight += 10; |
||
84 | } |
||
145 |