| Conditions | 28 |
| Paths | > 20000 |
| Total Lines | 130 |
| Code Lines | 79 |
| Lines | 16 |
| Ratio | 12.31 % |
| 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 |
||
| 121 | protected function getCurrentUserInfo() { |
||
| 122 | $user = $this->getUser(); |
||
| 123 | $vals = []; |
||
| 124 | $vals['id'] = intval( $user->getId() ); |
||
| 125 | $vals['name'] = $user->getName(); |
||
| 126 | |||
| 127 | if ( $user->isAnon() ) { |
||
| 128 | $vals['anon'] = true; |
||
| 129 | } |
||
| 130 | |||
| 131 | if ( isset( $this->prop['blockinfo'] ) && $user->isBlocked() ) { |
||
| 132 | $vals = array_merge( $vals, self::getBlockInfo( $user->getBlock() ) ); |
||
| 133 | } |
||
| 134 | |||
| 135 | if ( isset( $this->prop['hasmsg'] ) ) { |
||
| 136 | $vals['messages'] = $user->getNewtalk(); |
||
| 137 | } |
||
| 138 | |||
| 139 | View Code Duplication | if ( isset( $this->prop['groups'] ) ) { |
|
| 140 | $vals['groups'] = $user->getEffectiveGroups(); |
||
| 141 | ApiResult::setArrayType( $vals['groups'], 'array' ); // even if empty |
||
| 142 | ApiResult::setIndexedTagName( $vals['groups'], 'g' ); // even if empty |
||
| 143 | } |
||
| 144 | |||
| 145 | View Code Duplication | if ( isset( $this->prop['implicitgroups'] ) ) { |
|
| 146 | $vals['implicitgroups'] = $user->getAutomaticGroups(); |
||
| 147 | ApiResult::setArrayType( $vals['implicitgroups'], 'array' ); // even if empty |
||
| 148 | ApiResult::setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty |
||
| 149 | } |
||
| 150 | |||
| 151 | View Code Duplication | if ( isset( $this->prop['rights'] ) ) { |
|
| 152 | // User::getRights() may return duplicate values, strip them |
||
| 153 | $vals['rights'] = array_values( array_unique( $user->getRights() ) ); |
||
| 154 | ApiResult::setArrayType( $vals['rights'], 'array' ); // even if empty |
||
| 155 | ApiResult::setIndexedTagName( $vals['rights'], 'r' ); // even if empty |
||
| 156 | } |
||
| 157 | |||
| 158 | if ( isset( $this->prop['changeablegroups'] ) ) { |
||
| 159 | $vals['changeablegroups'] = $user->changeableGroups(); |
||
| 160 | ApiResult::setIndexedTagName( $vals['changeablegroups']['add'], 'g' ); |
||
| 161 | ApiResult::setIndexedTagName( $vals['changeablegroups']['remove'], 'g' ); |
||
| 162 | ApiResult::setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' ); |
||
| 163 | ApiResult::setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' ); |
||
| 164 | } |
||
| 165 | |||
| 166 | if ( isset( $this->prop['options'] ) ) { |
||
| 167 | $vals['options'] = $user->getOptions(); |
||
| 168 | $vals['options'][ApiResult::META_BC_BOOLS] = array_keys( $vals['options'] ); |
||
| 169 | } |
||
| 170 | |||
| 171 | if ( isset( $this->prop['preferencestoken'] ) ) { |
||
| 172 | $p = $this->getModulePrefix(); |
||
| 173 | $this->setWarning( |
||
| 174 | "{$p}prop=preferencestoken has been deprecated. Please use action=query&meta=tokens instead." |
||
| 175 | ); |
||
| 176 | } |
||
| 177 | if ( isset( $this->prop['preferencestoken'] ) && |
||
| 178 | !$this->lacksSameOriginSecurity() && |
||
| 179 | $user->isAllowed( 'editmyoptions' ) |
||
| 180 | ) { |
||
| 181 | $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() ); |
||
| 182 | } |
||
| 183 | |||
| 184 | if ( isset( $this->prop['editcount'] ) ) { |
||
| 185 | // use intval to prevent null if a non-logged-in user calls |
||
| 186 | // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount |
||
| 187 | $vals['editcount'] = intval( $user->getEditCount() ); |
||
| 188 | } |
||
| 189 | |||
| 190 | if ( isset( $this->prop['ratelimits'] ) ) { |
||
| 191 | $vals['ratelimits'] = $this->getRateLimits(); |
||
| 192 | } |
||
| 193 | |||
| 194 | if ( isset( $this->prop['realname'] ) && |
||
| 195 | !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) ) |
||
| 196 | ) { |
||
| 197 | $vals['realname'] = $user->getRealName(); |
||
| 198 | } |
||
| 199 | |||
| 200 | if ( $user->isAllowed( 'viewmyprivateinfo' ) ) { |
||
| 201 | if ( isset( $this->prop['email'] ) ) { |
||
| 202 | $vals['email'] = $user->getEmail(); |
||
| 203 | $auth = $user->getEmailAuthenticationTimestamp(); |
||
| 204 | if ( !is_null( $auth ) ) { |
||
| 205 | $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth ); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | if ( isset( $this->prop['registrationdate'] ) ) { |
||
| 211 | $regDate = $user->getRegistration(); |
||
| 212 | if ( $regDate !== false ) { |
||
| 213 | $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate ); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | if ( isset( $this->prop['acceptlang'] ) ) { |
||
| 218 | $langs = $this->getRequest()->getAcceptLang(); |
||
| 219 | $acceptLang = []; |
||
| 220 | foreach ( $langs as $lang => $val ) { |
||
| 221 | $r = [ 'q' => $val ]; |
||
| 222 | ApiResult::setContentValue( $r, 'code', $lang ); |
||
| 223 | $acceptLang[] = $r; |
||
| 224 | } |
||
| 225 | ApiResult::setIndexedTagName( $acceptLang, 'lang' ); |
||
| 226 | $vals['acceptlang'] = $acceptLang; |
||
| 227 | } |
||
| 228 | |||
| 229 | if ( isset( $this->prop['unreadcount'] ) ) { |
||
| 230 | $store = MediaWikiServices::getInstance()->getWatchedItemStore(); |
||
| 231 | $unreadNotifications = $store->countUnreadNotifications( |
||
| 232 | $user, |
||
| 233 | self::WL_UNREAD_LIMIT |
||
| 234 | ); |
||
| 235 | |||
| 236 | if ( $unreadNotifications === true ) { |
||
| 237 | $vals['unreadcount'] = self::WL_UNREAD_LIMIT . '+'; |
||
| 238 | } else { |
||
| 239 | $vals['unreadcount'] = $unreadNotifications; |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | if ( isset( $this->prop['centralids'] ) ) { |
||
| 244 | $vals += self::getCentralUserInfo( |
||
| 245 | $this->getConfig(), $this->getUser(), $this->params['attachedwiki'] |
||
| 246 | ); |
||
| 247 | } |
||
| 248 | |||
| 249 | return $vals; |
||
| 250 | } |
||
| 251 | |||
| 338 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: