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