| Conditions | 12 |
| Paths | 20 |
| Total Lines | 70 |
| Code Lines | 49 |
| Lines | 8 |
| Ratio | 11.43 % |
| 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 |
||
| 62 | public function getPanel() { |
||
| 63 | $tmpl = new Template('settings', 'panels/personal/profile'); |
||
| 64 | // Assign some data |
||
| 65 | $lang = $this->lfactory->findLanguage(); |
||
| 66 | $userLang = $this->config->getUserValue( $this->userSession->getUser(), 'core', 'lang', $lang); |
||
|
|
|||
| 67 | $languageCodes = $this->lfactory->findAvailableLanguages(); |
||
| 68 | // array of common languages |
||
| 69 | $commonLangCodes = [ |
||
| 70 | 'en', 'es', 'fr', 'de', 'de_DE', 'ja', 'ar', 'ru', 'nl', 'it', 'pt_BR', 'pt_PT', 'da', 'fi_FI', 'nb_NO', 'sv', 'tr', 'zh_CN', 'ko' |
||
| 71 | ]; |
||
| 72 | $languageNames = $this->helper->getLanguageCodes(); |
||
| 73 | $languages= []; |
||
| 74 | $commonLanguages = []; |
||
| 75 | foreach($languageCodes as $lang) { |
||
| 76 | $l = $this->lfactory->get('settings', $lang); |
||
| 77 | // TRANSLATORS this is the language name for the language switcher in the personal settings and should be the localized version |
||
| 78 | $potentialName = (string) $l->t('__language_name__'); |
||
| 79 | if($l->getLanguageCode() === $lang && substr($potentialName, 0, 1) !== '_') {//first check if the language name is in the translation file |
||
| 80 | $ln = ['code'=>$lang, 'name'=> $potentialName]; |
||
| 81 | } elseif (isset($languageNames[$lang])) { |
||
| 82 | $ln = ['code'=>$lang, 'name'=>$languageNames[$lang]]; |
||
| 83 | } else {//fallback to language code |
||
| 84 | $ln = ['code'=>$lang, 'name'=>$lang]; |
||
| 85 | } |
||
| 86 | // put appropriate languages into appropriate arrays, to print them sorted |
||
| 87 | // used language -> common languages -> divider -> other languages |
||
| 88 | if ($lang === $userLang) { |
||
| 89 | $userLang = $ln; |
||
| 90 | } elseif (in_array($lang, $commonLangCodes)) { |
||
| 91 | $commonLanguages[array_search($lang, $commonLangCodes)]=$ln; |
||
| 92 | } else { |
||
| 93 | $languages[]=$ln; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | // if user language is not available but set somehow: show the actual code as name |
||
| 97 | if (!is_array($userLang)) { |
||
| 98 | $userLang = [ |
||
| 99 | 'code' => $userLang, |
||
| 100 | 'name' => $userLang, |
||
| 101 | ]; |
||
| 102 | } |
||
| 103 | ksort($commonLanguages); |
||
| 104 | // sort now by displayed language not the iso-code |
||
| 105 | usort( $languages, function ($a, $b) { |
||
| 106 | View Code Duplication | if ($a['code'] === $a['name'] && $b['code'] !== $b['name']) { |
|
| 107 | // If a doesn't have a name, but b does, list b before a |
||
| 108 | return 1; |
||
| 109 | } |
||
| 110 | View Code Duplication | if ($a['code'] !== $a['name'] && $b['code'] === $b['name']) { |
|
| 111 | // If a does have a name, but b doesn't, list a before b |
||
| 112 | return -1; |
||
| 113 | } |
||
| 114 | // Otherwise compare the names |
||
| 115 | return strcmp($a['name'], $b['name']); |
||
| 116 | }); |
||
| 117 | |||
| 118 | $tmpl->assign('email', $this->userSession->getUser()->getEMailAddress()); |
||
| 119 | $tmpl->assign('languages', $languages); |
||
| 120 | $tmpl->assign('commonlanguages', $commonLanguages); |
||
| 121 | $tmpl->assign('activelanguage', $userLang); |
||
| 122 | $tmpl->assign('displayName', $this->userSession->getUser()->getDisplayName()); |
||
| 123 | $tmpl->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true) === true); |
||
| 124 | $tmpl->assign('avatarChangeSupported', $this->userSession->getUser()->canChangeAvatar()); |
||
| 125 | $tmpl->assign('displayNameChangeSupported', $this->userSession->getUser()->canChangeDisplayName()); |
||
| 126 | $tmpl->assign('passwordChangeSupported', $this->userSession->getUser()->canChangePassword()); |
||
| 127 | $groups = $this->groupManager->getUserGroupIds($this->userSession->getUser()); |
||
| 128 | sort($groups); |
||
| 129 | $tmpl->assign('groups', $groups); |
||
| 130 | return $tmpl; |
||
| 131 | } |
||
| 132 | |||
| 138 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: