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