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