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