| Conditions | 10 |
| Paths | 24 |
| Total Lines | 81 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 2 | 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 |
||
| 134 | protected function collectPages() |
||
| 135 | { |
||
| 136 | $count = 0; |
||
| 137 | |||
| 138 | $filteredPages = $this->builder->getPages() |
||
| 139 | ->filter(function (Page $page) { |
||
| 140 | if ($page->getVariable('menu')) { |
||
| 141 | return true; |
||
| 142 | } |
||
| 143 | }); |
||
| 144 | |||
| 145 | $total = count($filteredPages); |
||
| 146 | |||
| 147 | if ($total > 0) { |
||
| 148 | call_user_func_array($this->builder->getMessageCb(), ['MENU', 'Creating menus (pages)']); |
||
| 149 | } |
||
| 150 | |||
| 151 | /** @var \Cecil\Collection\Page\Page $page */ |
||
| 152 | foreach ($filteredPages as $page) { |
||
| 153 | $count++; |
||
| 154 | /** |
||
| 155 | * Array case. |
||
| 156 | * |
||
| 157 | * ie 1: |
||
| 158 | * menu: |
||
| 159 | * main: |
||
| 160 | * weight: 999 |
||
| 161 | * ie 2: |
||
| 162 | * menu: [main, navigation] |
||
| 163 | */ |
||
| 164 | if (is_array($page->getVariable('menu'))) { |
||
| 165 | foreach ($page->getVariable('menu') as $key => $value) { |
||
| 166 | $menuName = $key; |
||
| 167 | $property = $value; |
||
| 168 | $weight = null; |
||
| 169 | if (is_int($key)) { |
||
| 170 | $menuName = $value; |
||
| 171 | $property = null; |
||
| 172 | } |
||
| 173 | $item = (new Entry($page->getId())) |
||
| 174 | ->setName($page->getVariable('title')) |
||
| 175 | ->setUrl($page->getId()); |
||
| 176 | if (array_key_exists('weight', (array) $property)) { |
||
| 177 | $weight = $property['weight']; |
||
| 178 | $item->setWeight($property['weight']); |
||
| 179 | } |
||
| 180 | if (!$this->menus->has($menuName)) { |
||
| 181 | $this->menus->add(new Menu($menuName)); |
||
| 182 | } |
||
| 183 | /** @var \Cecil\Collection\Menu\Menu $menu */ |
||
| 184 | $menu = $this->menus->get($menuName); |
||
| 185 | $menu->add($item); |
||
| 186 | call_user_func_array($this->builder->getMessageCb(), [ |
||
| 187 | 'MENU_PROGRESS', |
||
| 188 | sprintf('%s > %s (%s)', $menuName, $page->getId(), $weight), |
||
| 189 | $count, |
||
| 190 | $total, |
||
| 191 | ]); |
||
| 192 | } |
||
| 193 | continue; |
||
| 194 | } |
||
| 195 | /** |
||
| 196 | * String case. |
||
| 197 | * |
||
| 198 | * ie: |
||
| 199 | * menu: main |
||
| 200 | */ |
||
| 201 | $item = (new Entry($page->getId())) |
||
| 202 | ->setName($page->getVariable('title')) |
||
| 203 | ->setUrl($page->getUrl()); |
||
| 204 | if (!$this->menus->has($page->getVariable('menu'))) { |
||
| 205 | $this->menus->add(new Menu($page->getVariable('menu'))); |
||
| 206 | } |
||
| 207 | /** @var \Cecil\Collection\Menu\Menu $menu */ |
||
| 208 | $menu = $this->menus->get($page->getVariable('menu')); |
||
| 209 | $menu->add($item); |
||
| 210 | call_user_func_array($this->builder->getMessageCb(), [ |
||
| 211 | 'MENU_PROGRESS', |
||
| 212 | sprintf('%s > %s', $page->getVariable('menu'), $page->getId()), |
||
| 213 | $count, |
||
| 214 | $total, |
||
| 215 | ]); |
||
| 219 |