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