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