| Conditions | 13 |
| Paths | 4 |
| Total Lines | 99 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 30 | public function process() |
||
| 31 | { |
||
| 32 | // create the 'menus' collection with a default 'main' menu |
||
| 33 | $main = new Menu('main'); |
||
| 34 | $this->menus = new MenusCollection('menus'); |
||
| 35 | $this->menus->add($main); |
||
| 36 | |||
| 37 | // Collect 'menu' entries from pages |
||
| 38 | $this->collectPages(); |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Removing/adding/replacing menus entries from config |
||
| 42 | * ie: |
||
| 43 | * menus: |
||
| 44 | * main: |
||
| 45 | * - id: example |
||
| 46 | * name: "Example" |
||
| 47 | * url: https://example.com |
||
| 48 | * weight: 999 |
||
| 49 | * - id: about |
||
| 50 | * enabled: false. |
||
| 51 | */ |
||
| 52 | if ($menusConfig = $this->builder->getConfig()->get('menus')) { |
||
| 53 | call_user_func_array($this->builder->getMessageCb(), ['MENU', 'Creating menus (config)']); |
||
| 54 | $totalConfig = array_sum(array_map('count', $menusConfig)); |
||
| 55 | $countConfig = 0; |
||
| 56 | |||
| 57 | foreach ($menusConfig as $menuConfig => $entry) { |
||
| 58 | if (!$this->menus->has($menuConfig)) { |
||
| 59 | $this->menus->add(new Menu($menuConfig)); |
||
| 60 | } |
||
| 61 | /** @var \Cecil\Collection\Menu\Menu $menu */ |
||
| 62 | $menu = $this->menus->get($menuConfig); |
||
| 63 | foreach ($entry as $key => $property) { |
||
| 64 | $countConfig++; |
||
| 65 | $enabled = true; |
||
| 66 | $updated = false; |
||
| 67 | |||
| 68 | // ID is required |
||
| 69 | if (!array_key_exists('id', $property)) { |
||
| 70 | throw new Exception(sprintf('"id" is required for menu entry "%s"', $key)); |
||
| 71 | } |
||
| 72 | // enabled? |
||
| 73 | if (array_key_exists('enabled', $property) && false === $property['enabled']) { |
||
| 74 | $enabled = false; |
||
| 75 | if (!$menu->has($property['id'])) { |
||
| 76 | call_user_func_array($this->builder->getMessageCb(), [ |
||
| 77 | 'MENU_PROGRESS', |
||
| 78 | sprintf('%s > %s (disabled)', (string) $menu, $property['id']), |
||
| 79 | $countConfig, |
||
| 80 | $totalConfig, |
||
| 81 | ]); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | // is entry already exist? |
||
| 85 | if ($menu->has($property['id'])) { |
||
| 86 | // remove a disabled entry |
||
| 87 | if (!$enabled) { |
||
| 88 | call_user_func_array($this->builder->getMessageCb(), [ |
||
| 89 | 'MENU_PROGRESS', |
||
| 90 | sprintf('%s > %s (removed)', $menu, $property['id']), |
||
| 91 | $countConfig, |
||
| 92 | $totalConfig, |
||
| 93 | ]); |
||
| 94 | $menu->remove($property['id']); |
||
| 95 | continue; |
||
| 96 | } |
||
| 97 | // merge properties |
||
| 98 | $updated = true; |
||
| 99 | $current = $menu->get($property['id'])->toArray(); |
||
| 100 | $property = array_merge($current, $property); |
||
| 101 | call_user_func_array($this->builder->getMessageCb(), [ |
||
| 102 | 'MENU_PROGRESS', |
||
| 103 | sprintf('%s > %s (updated)', $menu, $property['id']), |
||
| 104 | $countConfig, |
||
| 105 | $totalConfig, |
||
| 106 | ]); |
||
| 107 | } |
||
| 108 | // add/replace entry |
||
| 109 | if ($enabled) { |
||
| 110 | $item = (new Entry($property['id'])) |
||
| 111 | ->setName($property['name'] ?? ucfirst($property['id'])) |
||
| 112 | ->setUrl($property['url'] ?? '/404') |
||
| 113 | ->setWeight($property['weight'] ?? 0); |
||
| 114 | $menu->add($item); |
||
| 115 | if (!$updated) { |
||
| 116 | call_user_func_array($this->builder->getMessageCb(), [ |
||
| 117 | 'MENU_PROGRESS', |
||
| 118 | sprintf('%s > %s', $menu, $property['id']), |
||
| 119 | $countConfig, |
||
| 120 | $totalConfig, |
||
| 121 | ]); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | $this->builder->setMenus($this->menus); |
||
| 129 | } |
||
| 219 |