| Conditions | 11 |
| Paths | 14 |
| Total Lines | 83 |
| Code Lines | 47 |
| 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 |
||
| 136 | protected function collectPages(): void |
||
| 137 | { |
||
| 138 | $filteredPages = $this->builder->getPages()->filter(function (Page $page) { |
||
| 139 | return $page->hasVariable('menu') |
||
| 140 | && $page->getVariable('published') |
||
| 141 | && in_array($page->getVariable('language') ?? $this->config->getLanguageDefault(), array_column($this->config->getLanguages(), 'code')); |
||
| 142 | }); |
||
| 143 | |||
| 144 | $total = count($filteredPages); |
||
| 145 | $count = 0; |
||
| 146 | /** @var \Cecil\Collection\Page\Page $page */ |
||
| 147 | foreach ($filteredPages as $page) { |
||
| 148 | $count++; |
||
| 149 | $language = $page->getVariable('language') ?? $this->config->getLanguageDefault(); |
||
| 150 | /** |
||
| 151 | * Array case. |
||
| 152 | * |
||
| 153 | * ie 1: |
||
| 154 | * menu: [main, navigation] |
||
| 155 | * ie 2: |
||
| 156 | * menu: |
||
| 157 | * main: |
||
| 158 | * weight: 999 |
||
| 159 | */ |
||
| 160 | if (is_array($page->getVariable('menu'))) { |
||
| 161 | foreach ($page->getVariable('menu') as $key => $value) { |
||
| 162 | $menuName = $key; |
||
| 163 | $property = $value; |
||
| 164 | $weight = null; |
||
| 165 | if (is_int($key)) { |
||
| 166 | $menuName = $value; |
||
| 167 | $property = null; |
||
| 168 | } |
||
| 169 | if (!is_string($menuName)) { |
||
| 170 | $this->builder->getLogger()->error( |
||
| 171 | sprintf( |
||
| 172 | 'Menu\'s name of page "%s" must be a string, not "%s"', |
||
| 173 | $page->getId(), |
||
| 174 | PrintLogger::format($menuName) |
||
| 175 | ), |
||
| 176 | ['progress' => [$count, $total]] |
||
| 177 | ); |
||
| 178 | continue; |
||
| 179 | } |
||
| 180 | $item = (new Entry($page->getIdWithoutLang())) |
||
| 181 | ->setName($page->getVariable('title')) |
||
| 182 | ->setUrl((new PageRenderer($this->config))->getUrl($page)); |
||
| 183 | if (isset($property['weight'])) { |
||
| 184 | $weight = $property['weight']; |
||
| 185 | $item->setWeight($property['weight']); |
||
| 186 | } |
||
| 187 | // add Menu if not exists |
||
| 188 | if (!$this->menus[$language]->has($menuName)) { |
||
| 189 | $this->menus[$language]->add(new Menu($menuName)); |
||
| 190 | } |
||
| 191 | /** @var \Cecil\Collection\Menu\Menu $menu */ |
||
| 192 | $menu = $this->menus[$language]->get($menuName); |
||
| 193 | $menu->add($item); |
||
| 194 | |||
| 195 | $message = sprintf('Page menu entry "%s > %s" created (weight: %s)', $menuName, $page->getId(), $weight ?? 'N/A'); |
||
| 196 | $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]); |
||
| 197 | } |
||
| 198 | continue; |
||
| 199 | } |
||
| 200 | /** |
||
| 201 | * String case. |
||
| 202 | * |
||
| 203 | * ie: |
||
| 204 | * menu: main |
||
| 205 | */ |
||
| 206 | $item = (new Entry($page->getIdWithoutLang())) |
||
| 207 | ->setName($page->getVariable('title')) |
||
| 208 | ->setUrl((new PageRenderer($this->config))->getUrl($page)); |
||
| 209 | // add Menu if not exists |
||
| 210 | if (!$this->menus[$language]->has($page->getVariable('menu'))) { |
||
| 211 | $this->menus[$language]->add(new Menu($page->getVariable('menu'))); |
||
| 212 | } |
||
| 213 | /** @var \Cecil\Collection\Menu\Menu $menu */ |
||
| 214 | $menu = $this->menus[$language]->get($page->getVariable('menu')); |
||
| 215 | $menu->add($item); |
||
| 216 | |||
| 217 | $message = sprintf('Page menu entry "%s > %s" created', $page->getVariable('menu'), $page->getId()); |
||
| 218 | $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]); |
||
| 219 | } |
||
| 222 |