| Conditions | 12 |
| Paths | 10 |
| Total Lines | 78 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 | // DEBUG |
||
| 156 | echo $page->getId()."\n"; |
||
| 157 | var_dump($page->getVariable('menu')); |
||
|
|
|||
| 158 | |||
| 159 | /* |
||
| 160 | * String case |
||
| 161 | * ie: |
||
| 162 | * menu: main |
||
| 163 | */ |
||
| 164 | if (is_string($page->getVariable('menu'))) { |
||
| 165 | $item = (new Entry($page->getId())) |
||
| 166 | ->setName($page->getVariable('title')) |
||
| 167 | ->setUrl($page->getUrl()); |
||
| 168 | if (!$this->menus->has($page->getVariable('menu'))) { |
||
| 169 | $this->menus->add(new Menu($page->getVariable('menu'))); |
||
| 170 | } |
||
| 171 | /** @var \Cecil\Collection\Menu\Menu $menu */ |
||
| 172 | $menu = $this->menus->get($page->getVariable('menu')); |
||
| 173 | $menu->add($item); |
||
| 174 | call_user_func_array($this->builder->getMessageCb(), [ |
||
| 175 | 'MENU_PROGRESS', |
||
| 176 | sprintf('%s > %s', $page->getVariable('menu'), $page->getId()), |
||
| 177 | $count, |
||
| 178 | $total, |
||
| 179 | ]); |
||
| 180 | } else { |
||
| 181 | /* |
||
| 182 | * Array case |
||
| 183 | * ie 1: |
||
| 184 | * menu: [main, navigation] |
||
| 185 | * ie 2: |
||
| 186 | * menu: |
||
| 187 | * main: |
||
| 188 | * weight: 999 |
||
| 189 | */ |
||
| 190 | if (is_array($page->getVariable('menu'))) { |
||
| 191 | foreach ($page->getVariable('menu') as $menuName => $property) { |
||
| 192 | $item = (new Entry($page->getId())) |
||
| 193 | ->setName($page->getVariable('title')) |
||
| 194 | ->setUrl($page->getId()); |
||
| 195 | if (is_array($property) |
||
| 196 | && array_key_exists('weight', $property) |
||
| 197 | && $property['weight'] !== null) { |
||
| 198 | $weight = $property['weight']; |
||
| 199 | $item->setWeight($property['weight']); |
||
| 200 | } |
||
| 201 | if (!$this->menus->has($menuName)) { |
||
| 202 | $this->menus->add(new Menu($menuName)); |
||
| 203 | } |
||
| 204 | /** @var \Cecil\Collection\Menu\Menu $menu */ |
||
| 205 | $menu = $this->menus->get($menuName); |
||
| 206 | $menu->add($item); |
||
| 207 | call_user_func_array($this->builder->getMessageCb(), [ |
||
| 208 | 'MENU_PROGRESS', |
||
| 209 | sprintf('%s > %s (%s)', $menuName, $page->getId(), $weight), |
||
| 210 | $count, |
||
| 211 | $total, |
||
| 212 | ]); |
||
| 219 |