| Conditions | 13 |
| Paths | 44 |
| Total Lines | 94 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 3 | Features | 1 |
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 |
||
| 139 | protected function collectPages() |
||
| 140 | { |
||
| 141 | $count = 0; |
||
| 142 | |||
| 143 | $filteredPages = $this->builder->getPages()->filter(function (Page $page) { |
||
| 144 | return $page->hasVariable('menu') && $page->getVariable('published'); |
||
| 145 | }); |
||
| 146 | |||
| 147 | $total = count($filteredPages); |
||
| 148 | |||
| 149 | if ($total > 0) { |
||
| 150 | $this->builder->getLogger()->debug('Creating pages menus'); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** @var \Cecil\Collection\Page\Page $page */ |
||
| 154 | foreach ($filteredPages as $page) { |
||
| 155 | $count++; |
||
| 156 | $lang = $page->getVariable('language'); |
||
| 157 | /** |
||
| 158 | * Array case. |
||
| 159 | * |
||
| 160 | * ie 1: |
||
| 161 | * menu: |
||
| 162 | * main: |
||
| 163 | * weight: 999 |
||
| 164 | * ie 2: |
||
| 165 | * menu: [main, navigation] |
||
| 166 | */ |
||
| 167 | if (is_array($page->getVariable('menu'))) { |
||
| 168 | foreach ($page->getVariable('menu') as $key => $value) { |
||
| 169 | $menuName = $key; |
||
| 170 | $property = $value; |
||
| 171 | $weight = null; |
||
| 172 | if (is_int($key)) { |
||
| 173 | $menuName = $value; |
||
| 174 | $property = null; |
||
| 175 | } |
||
| 176 | if (!is_string($menuName)) { |
||
| 177 | $this->builder->getLogger()->error( |
||
| 178 | sprintf( |
||
| 179 | 'Menu name of page "%s" must be string, not "%s"', |
||
| 180 | $page->getId(), |
||
| 181 | PrintLogger::format($menuName) |
||
| 182 | ), |
||
| 183 | ['progress' => [$count, $total]] |
||
| 184 | ); |
||
| 185 | continue; |
||
| 186 | } |
||
| 187 | $item = (new Entry($page->getIdWithoutLang())) |
||
| 188 | ->setName($page->getVariable('title')) |
||
| 189 | ->setUrl($page->getId()); |
||
| 190 | if (array_key_exists('weight', (array) $property)) { |
||
| 191 | $weight = $property['weight']; |
||
| 192 | $item->setWeight($property['weight']); |
||
| 193 | } |
||
| 194 | // checks if page's language exists |
||
| 195 | if (!array_key_exists($lang, $this->menus)) { |
||
| 196 | if ($lang === null) { |
||
| 197 | throw new Exception('The default language is not correctly defined in config.'); |
||
| 198 | } |
||
| 199 | |||
| 200 | throw new Exception(sprintf('Language "%s" is not defined in config.', $lang)); |
||
| 201 | } |
||
| 202 | // add Menu if not exists |
||
| 203 | if (!$this->menus[$lang]->has($menuName)) { |
||
| 204 | $this->menus[$lang]->add(new Menu($menuName)); |
||
| 205 | } |
||
| 206 | /** @var \Cecil\Collection\Menu\Menu $menu */ |
||
| 207 | $menu = $this->menus[$lang]->get($menuName); |
||
| 208 | $menu->add($item); |
||
| 209 | |||
| 210 | $message = sprintf('%s > %s (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->getId())) |
||
| 222 | ->setName($page->getVariable('title')) |
||
| 223 | ->setUrl($page->getUrl()); |
||
| 224 | if (!$this->menus[$lang]->has($page->getVariable('menu'))) { |
||
| 225 | $this->menus[$lang]->add(new Menu($page->getVariable('menu'))); |
||
| 226 | } |
||
| 227 | /** @var \Cecil\Collection\Menu\Menu $menu */ |
||
| 228 | $menu = $this->menus[$lang]->get($page->getVariable('menu')); |
||
| 229 | $menu->add($item); |
||
| 230 | |||
| 231 | $message = sprintf('%s > %s', $page->getVariable('menu'), $page->getId()); |
||
| 232 | $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]); |
||
| 233 | } |
||
| 236 |