| Conditions | 26 |
| Paths | 163 |
| Total Lines | 127 |
| Code Lines | 86 |
| Lines | 32 |
| Ratio | 25.2 % |
| 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 |
||
| 190 | private function init() { |
||
| 191 | if ($this->init) { |
||
| 192 | return; |
||
| 193 | } |
||
| 194 | $this->init = true; |
||
| 195 | |||
| 196 | $l = $this->l10nFac->get('lib'); |
||
| 197 | View Code Duplication | if ($this->config->getSystemValue('knowledgebaseenabled', true)) { |
|
| 198 | $this->add([ |
||
| 199 | 'type' => 'settings', |
||
| 200 | 'id' => 'help', |
||
| 201 | 'order' => 5, |
||
| 202 | 'href' => $this->urlGenerator->linkToRoute('settings_help'), |
||
| 203 | 'name' => $l->t('Help'), |
||
| 204 | 'icon' => $this->urlGenerator->imagePath('settings', 'help.svg'), |
||
| 205 | ]); |
||
| 206 | } |
||
| 207 | |||
| 208 | if ($this->userSession->isLoggedIn()) { |
||
| 209 | View Code Duplication | if ($this->isAdmin()) { |
|
| 210 | // App management |
||
| 211 | $this->add([ |
||
| 212 | 'type' => 'settings', |
||
| 213 | 'id' => 'core_apps', |
||
| 214 | 'order' => 3, |
||
| 215 | 'href' => $this->urlGenerator->linkToRoute('settings.AppSettings.viewApps'), |
||
| 216 | 'icon' => $this->urlGenerator->imagePath('settings', 'apps.svg'), |
||
| 217 | 'name' => $l->t('Apps'), |
||
| 218 | ]); |
||
| 219 | } |
||
| 220 | |||
| 221 | // Personal and (if applicable) admin settings |
||
| 222 | $this->add([ |
||
| 223 | 'type' => 'settings', |
||
| 224 | 'id' => 'settings', |
||
| 225 | 'order' => 1, |
||
| 226 | 'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'), |
||
| 227 | 'name' => $l->t('Settings'), |
||
| 228 | 'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'), |
||
| 229 | ]); |
||
| 230 | |||
| 231 | $logoutUrl = \OC_User::getLogoutUrl($this->urlGenerator); |
||
| 232 | if($logoutUrl !== '') { |
||
| 233 | // Logout |
||
| 234 | $this->add([ |
||
| 235 | 'type' => 'settings', |
||
| 236 | 'id' => 'logout', |
||
| 237 | 'order' => 99999, |
||
| 238 | 'href' => $logoutUrl, |
||
| 239 | 'name' => $l->t('Log out'), |
||
| 240 | 'icon' => $this->urlGenerator->imagePath('core', 'actions/logout.svg'), |
||
| 241 | ]); |
||
| 242 | } |
||
| 243 | |||
| 244 | View Code Duplication | if ($this->isSubadmin()) { |
|
| 245 | // User management |
||
| 246 | $this->add([ |
||
| 247 | 'type' => 'settings', |
||
| 248 | 'id' => 'core_users', |
||
| 249 | 'order' => 4, |
||
| 250 | 'href' => $this->urlGenerator->linkToRoute('settings_users'), |
||
| 251 | 'name' => $l->t('Users'), |
||
| 252 | 'icon' => $this->urlGenerator->imagePath('settings', 'users.svg'), |
||
| 253 | ]); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | if ($this->appManager === 'null') { |
||
| 258 | return; |
||
| 259 | } |
||
| 260 | |||
| 261 | if ($this->userSession->isLoggedIn()) { |
||
| 262 | $apps = $this->appManager->getEnabledAppsForUser($this->userSession->getUser()); |
||
|
|
|||
| 263 | } else { |
||
| 264 | $apps = $this->appManager->getInstalledApps(); |
||
| 265 | } |
||
| 266 | |||
| 267 | foreach ($apps as $app) { |
||
| 268 | if (!$this->userSession->isLoggedIn() && !$this->appManager->isEnabledForUser($app, $this->userSession->getUser())) { |
||
| 269 | continue; |
||
| 270 | } |
||
| 271 | |||
| 272 | // load plugins and collections from info.xml |
||
| 273 | $info = $this->appManager->getAppInfo($app); |
||
| 274 | if (empty($info['navigations'])) { |
||
| 275 | continue; |
||
| 276 | } |
||
| 277 | foreach ($info['navigations'] as $nav) { |
||
| 278 | if (!isset($nav['name'])) { |
||
| 279 | continue; |
||
| 280 | } |
||
| 281 | if (!isset($nav['route'])) { |
||
| 282 | continue; |
||
| 283 | } |
||
| 284 | $role = isset($nav['@attributes']['role']) ? $nav['@attributes']['role'] : 'all'; |
||
| 285 | if ($role === 'admin' && !$this->isAdmin()) { |
||
| 286 | continue; |
||
| 287 | } |
||
| 288 | $l = $this->l10nFac->get($app); |
||
| 289 | $id = isset($nav['id']) ? $nav['id'] : $app; |
||
| 290 | $order = isset($nav['order']) ? $nav['order'] : 100; |
||
| 291 | $type = isset($nav['type']) ? $nav['type'] : 'link'; |
||
| 292 | $route = $this->urlGenerator->linkToRoute($nav['route']); |
||
| 293 | $icon = isset($nav['icon']) ? $nav['icon'] : 'app.svg'; |
||
| 294 | foreach ([$icon, "$app.svg"] as $i) { |
||
| 295 | try { |
||
| 296 | $icon = $this->urlGenerator->imagePath($app, $i); |
||
| 297 | break; |
||
| 298 | } catch (\RuntimeException $ex) { |
||
| 299 | // no icon? - ignore it then |
||
| 300 | } |
||
| 301 | } |
||
| 302 | if ($icon === null) { |
||
| 303 | $icon = $this->urlGenerator->imagePath('core', 'default-app-icon'); |
||
| 304 | } |
||
| 305 | |||
| 306 | $this->add([ |
||
| 307 | 'id' => $id, |
||
| 308 | 'order' => $order, |
||
| 309 | 'href' => $route, |
||
| 310 | 'icon' => $icon, |
||
| 311 | 'type' => $type, |
||
| 312 | 'name' => $l->t($nav['name']), |
||
| 313 | ]); |
||
| 314 | } |
||
| 315 | } |
||
| 316 | } |
||
| 317 | |||
| 334 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: