| Conditions | 17 |
| Paths | 300 |
| Total Lines | 76 |
| Code Lines | 47 |
| 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 its section? |
||
| 35 | if ($page->getVariable('published') !== true || $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->getVariable('language') ?? $this->config->getLanguageDefault()][] = $page; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | // adds section to pages collection |
||
| 46 | if (count($sections) > 0) { |
||
| 47 | $menuWeight = 100; |
||
| 48 | |||
| 49 | foreach ($sections as $section => $languages) { |
||
| 50 | foreach ($languages as $lang => $pagesAsArray) { |
||
| 51 | $pageId = $path = Page::slugify($section); |
||
| 52 | if ($lang != $this->config->getLanguageDefault()) { |
||
| 53 | $pageId = sprintf('%s.%s', Page::slugify($section), $lang); |
||
| 54 | } |
||
| 55 | $page = (new Page($pageId))->setVariable('title', ucfirst($section)); |
||
| 56 | if ($this->builder->getPages()->has($pageId)) { |
||
| 57 | $page = clone $this->builder->getPages()->get($pageId); |
||
| 58 | } |
||
| 59 | $pages = new PagesCollection($section, $pagesAsArray); |
||
| 60 | // cascade |
||
| 61 | if ($page->hasVariable('cascade')) { |
||
|
|
|||
| 62 | $cascade = $page->getVariable('cascade'); |
||
| 63 | $pages->map(function (Page $page) use ($cascade) { |
||
| 64 | foreach ($cascade as $key => $value) { |
||
| 65 | if (!$page->hasVariable($key)) { |
||
| 66 | $page->setVariable($key, $value); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | }); |
||
| 70 | } |
||
| 71 | // sorts |
||
| 72 | $pages = $pages->sortByDate(); |
||
| 73 | if ($page->getVariable('sortby')) { |
||
| 74 | $sortMethod = sprintf('sortBy%s', ucfirst($page->getVariable('sortby'))); |
||
| 75 | if (!method_exists($pages, $sortMethod)) { |
||
| 76 | throw new Exception(sprintf( |
||
| 77 | 'In "%s" section "%s" is not a valid value for "sortby" variable.', |
||
| 78 | $page->getId(), |
||
| 79 | $page->getVariable('sortby') |
||
| 80 | )); |
||
| 81 | } |
||
| 82 | $pages = $pages->$sortMethod(); |
||
| 83 | } |
||
| 84 | // adds navigation links |
||
| 85 | $this->addNavigationLinks($pages, $page->getVariable('sortby'), $page->getVariable('circular')); |
||
| 86 | // creates page for each section |
||
| 87 | $page->setPath($path) |
||
| 88 | ->setType(Type::SECTION) |
||
| 89 | ->setVariable('pages', $pages) |
||
| 90 | ->setVariable('date', $pages->first()->getVariable('date')); |
||
| 91 | if ($lang != $this->config->getLanguageDefault()) { |
||
| 92 | $page->setVariable('language', $lang); |
||
| 93 | } |
||
| 94 | // default menu |
||
| 95 | if (!$page->getVariable('menu')) { |
||
| 96 | $page->setVariable('menu', [ |
||
| 97 | 'main' => ['weight' => $menuWeight], |
||
| 98 | ]); |
||
| 99 | } |
||
| 100 | $this->generatedPages->add($page); |
||
| 101 | $menuWeight += 10; |
||
| 102 | } |
||
| 184 |