| Conditions | 19 |
| Paths | 22 |
| Total Lines | 82 |
| Code Lines | 49 |
| 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 |
||
| 82 | public function get($name, array $options = []) |
||
| 83 | { |
||
| 84 | $group = $options['group']; |
||
| 85 | |||
| 86 | $menuItem = $this->menuFactory->createItem( |
||
| 87 | $options['name'], |
||
| 88 | [ |
||
| 89 | 'label' => $group['label'], |
||
| 90 | ] |
||
| 91 | ); |
||
| 92 | |||
| 93 | if (empty($group['on_top'])) { |
||
| 94 | foreach ($group['items'] as $item) { |
||
| 95 | if (isset($item['admin']) && !empty($item['admin'])) { |
||
| 96 | $admin = $this->pool->getInstance($item['admin']); |
||
| 97 | |||
| 98 | // skip menu item if no `list` url is available or user doesn't have the LIST access rights |
||
| 99 | if (!$admin->hasRoute('list') || !$admin->hasAccess('list')) { |
||
| 100 | continue; |
||
| 101 | } |
||
| 102 | |||
| 103 | $label = $admin->getLabel(); |
||
| 104 | $options = $admin->generateMenuUrl('list', [], $item['route_absolute']); |
||
| 105 | $options['extras'] = [ |
||
| 106 | 'label_catalogue' => $admin->getTranslationDomain(), |
||
| 107 | 'admin' => $admin, |
||
| 108 | ]; |
||
| 109 | } else { |
||
| 110 | //NEXT_MAJOR: Remove if statement of null checker. |
||
| 111 | if (null !== $this->checker) { |
||
| 112 | if ((!empty($item['roles']) && !$this->checker->isGranted($item['roles'])) |
||
| 113 | || (!empty($group['roles']) && !$this->checker->isGranted($group['roles'], $item['route'])) |
||
| 114 | ) { |
||
| 115 | continue; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | $label = $item['label']; |
||
| 120 | $options = [ |
||
| 121 | 'route' => $item['route'], |
||
| 122 | 'routeParameters' => $item['route_params'], |
||
| 123 | 'routeAbsolute' => $item['route_absolute'], |
||
| 124 | 'extras' => [ |
||
| 125 | 'label_catalogue' => $group['label_catalogue'], |
||
| 126 | ], |
||
| 127 | ]; |
||
| 128 | } |
||
| 129 | |||
| 130 | $menuItem->addChild($label, $options); |
||
| 131 | } |
||
| 132 | |||
| 133 | if (false === $menuItem->hasChildren()) { |
||
| 134 | $menuItem->setDisplay(false); |
||
| 135 | } elseif (!empty($group['keep_open'])) { |
||
| 136 | $menuItem->setAttribute('class', 'keep-open'); |
||
| 137 | $menuItem->setExtra('keep_open', $group['keep_open']); |
||
| 138 | } |
||
| 139 | } else { |
||
| 140 | foreach ($group['items'] as $item) { |
||
| 141 | if (isset($item['admin']) && !empty($item['admin'])) { |
||
| 142 | $admin = $this->pool->getInstance($item['admin']); |
||
| 143 | |||
| 144 | // Do not display group if no `list` url is available or user doesn't have the LIST access rights |
||
| 145 | if (!$admin->hasRoute('list') || !$admin->hasAccess('list')) { |
||
| 146 | $menuItem->setDisplay(false); |
||
| 147 | |||
| 148 | continue; |
||
| 149 | } |
||
| 150 | |||
| 151 | $options = $admin->generateUrl('list'); |
||
| 152 | $menuItem->setExtra('route', $admin->getBaseRouteName().'_list'); |
||
| 153 | $menuItem->setExtra('on_top', $group['on_top']); |
||
| 154 | $menuItem->setUri($options); |
||
| 155 | } else { |
||
| 156 | $router = $this->pool->getContainer()->get('router'); |
||
| 157 | $menuItem->setUri($router->generate($item['route'])); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | return $menuItem; |
||
| 163 | } |
||
| 164 | |||
| 178 |
If you suppress an error, we recommend checking for the error condition explicitly: