| Conditions | 44 |
| Paths | 600 |
| Total Lines | 196 |
| Code Lines | 129 |
| Lines | 40 |
| Ratio | 20.41 % |
| 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 |
||
| 99 | public function execute() { |
||
| 100 | $params = $this->extractRequestParams(); |
||
| 101 | |||
| 102 | View Code Duplication | if ( !is_null( $params['prop'] ) ) { |
|
| 103 | $this->prop = array_flip( $params['prop'] ); |
||
| 104 | } else { |
||
| 105 | $this->prop = []; |
||
| 106 | } |
||
| 107 | |||
| 108 | $users = (array)$params['users']; |
||
| 109 | $goodNames = $done = []; |
||
| 110 | $result = $this->getResult(); |
||
| 111 | // Canonicalize user names |
||
| 112 | foreach ( $users as $u ) { |
||
| 113 | $n = User::getCanonicalName( $u ); |
||
| 114 | if ( $n === false || $n === '' ) { |
||
| 115 | $vals = [ 'name' => $u, 'invalid' => true ]; |
||
| 116 | $fit = $result->addValue( [ 'query', $this->getModuleName() ], |
||
| 117 | null, $vals ); |
||
| 118 | if ( !$fit ) { |
||
| 119 | $this->setContinueEnumParameter( 'users', |
||
| 120 | implode( '|', array_diff( $users, $done ) ) ); |
||
| 121 | $goodNames = []; |
||
| 122 | break; |
||
| 123 | } |
||
| 124 | $done[] = $u; |
||
| 125 | } else { |
||
| 126 | $goodNames[] = $n; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | $result = $this->getResult(); |
||
| 131 | |||
| 132 | if ( count( $goodNames ) ) { |
||
| 133 | $this->addTables( 'user' ); |
||
| 134 | $this->addFields( User::selectFields() ); |
||
| 135 | $this->addWhereFld( 'user_name', $goodNames ); |
||
| 136 | |||
| 137 | $this->showHiddenUsersAddBlockInfo( isset( $this->prop['blockinfo'] ) ); |
||
| 138 | |||
| 139 | $data = []; |
||
| 140 | $res = $this->select( __METHOD__ ); |
||
| 141 | $this->resetQueryParams(); |
||
| 142 | |||
| 143 | // get user groups if needed |
||
| 144 | if ( isset( $this->prop['groups'] ) || isset( $this->prop['rights'] ) ) { |
||
| 145 | $userGroups = []; |
||
| 146 | |||
| 147 | $this->addTables( 'user' ); |
||
| 148 | $this->addWhereFld( 'user_name', $goodNames ); |
||
| 149 | $this->addTables( 'user_groups' ); |
||
| 150 | $this->addJoinConds( [ 'user_groups' => [ 'INNER JOIN', 'ug_user=user_id' ] ] ); |
||
| 151 | $this->addFields( [ 'user_name', 'ug_group' ] ); |
||
| 152 | $userGroupsRes = $this->select( __METHOD__ ); |
||
| 153 | |||
| 154 | foreach ( $userGroupsRes as $row ) { |
||
| 155 | $userGroups[$row->user_name][] = $row->ug_group; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | foreach ( $res as $row ) { |
||
| 160 | // create user object and pass along $userGroups if set |
||
| 161 | // that reduces the number of database queries needed in User dramatically |
||
| 162 | if ( !isset( $userGroups ) ) { |
||
| 163 | $user = User::newFromRow( $row ); |
||
| 164 | } else { |
||
| 165 | if ( !isset( $userGroups[$row->user_name] ) || !is_array( $userGroups[$row->user_name] ) ) { |
||
| 166 | $userGroups[$row->user_name] = []; |
||
| 167 | } |
||
| 168 | $user = User::newFromRow( $row, [ 'user_groups' => $userGroups[$row->user_name] ] ); |
||
| 169 | } |
||
| 170 | $name = $user->getName(); |
||
| 171 | |||
| 172 | $data[$name]['userid'] = $user->getId(); |
||
| 173 | $data[$name]['name'] = $name; |
||
| 174 | |||
| 175 | if ( isset( $this->prop['editcount'] ) ) { |
||
| 176 | $data[$name]['editcount'] = $user->getEditCount(); |
||
| 177 | } |
||
| 178 | |||
| 179 | if ( isset( $this->prop['registration'] ) ) { |
||
| 180 | $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() ); |
||
| 181 | } |
||
| 182 | |||
| 183 | if ( isset( $this->prop['groups'] ) ) { |
||
| 184 | $data[$name]['groups'] = $user->getEffectiveGroups(); |
||
| 185 | } |
||
| 186 | |||
| 187 | if ( isset( $this->prop['implicitgroups'] ) ) { |
||
| 188 | $data[$name]['implicitgroups'] = $user->getAutomaticGroups(); |
||
| 189 | } |
||
| 190 | |||
| 191 | if ( isset( $this->prop['rights'] ) ) { |
||
| 192 | $data[$name]['rights'] = $user->getRights(); |
||
| 193 | } |
||
| 194 | if ( $row->ipb_deleted ) { |
||
| 195 | $data[$name]['hidden'] = true; |
||
| 196 | } |
||
| 197 | if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->ipb_by_text ) ) { |
||
| 198 | $data[$name]['blockid'] = (int)$row->ipb_id; |
||
| 199 | $data[$name]['blockedby'] = $row->ipb_by_text; |
||
| 200 | $data[$name]['blockedbyid'] = (int)$row->ipb_by; |
||
| 201 | $data[$name]['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ); |
||
| 202 | $data[$name]['blockreason'] = $row->ipb_reason; |
||
| 203 | $data[$name]['blockexpiry'] = $row->ipb_expiry; |
||
| 204 | } |
||
| 205 | |||
| 206 | if ( isset( $this->prop['emailable'] ) ) { |
||
| 207 | $data[$name]['emailable'] = $user->canReceiveEmail(); |
||
| 208 | } |
||
| 209 | |||
| 210 | if ( isset( $this->prop['gender'] ) ) { |
||
| 211 | $gender = $user->getOption( 'gender' ); |
||
| 212 | if ( strval( $gender ) === '' ) { |
||
| 213 | $gender = 'unknown'; |
||
| 214 | } |
||
| 215 | $data[$name]['gender'] = $gender; |
||
| 216 | } |
||
| 217 | |||
| 218 | if ( isset( $this->prop['centralids'] ) ) { |
||
| 219 | $data[$name] += ApiQueryUserInfo::getCentralUserInfo( |
||
| 220 | $this->getConfig(), $user, $params['attachedwiki'] |
||
| 221 | ); |
||
| 222 | } |
||
| 223 | |||
| 224 | View Code Duplication | if ( !is_null( $params['token'] ) ) { |
|
| 225 | $tokenFunctions = $this->getTokenFunctions(); |
||
| 226 | foreach ( $params['token'] as $t ) { |
||
| 227 | $val = call_user_func( $tokenFunctions[$t], $user ); |
||
| 228 | if ( $val === false ) { |
||
| 229 | $this->setWarning( "Action '$t' is not allowed for the current user" ); |
||
| 230 | } else { |
||
| 231 | $data[$name][$t . 'token'] = $val; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | $context = $this->getContext(); |
||
| 239 | // Second pass: add result data to $retval |
||
| 240 | foreach ( $goodNames as $u ) { |
||
| 241 | if ( !isset( $data[$u] ) ) { |
||
| 242 | $data[$u] = [ 'name' => $u ]; |
||
| 243 | $urPage = new UserrightsPage; |
||
| 244 | $urPage->setContext( $context ); |
||
| 245 | $iwUser = $urPage->fetchUser( $u ); |
||
| 246 | |||
| 247 | if ( $iwUser instanceof UserRightsProxy ) { |
||
| 248 | $data[$u]['interwiki'] = true; |
||
| 249 | |||
| 250 | View Code Duplication | if ( !is_null( $params['token'] ) ) { |
|
| 251 | $tokenFunctions = $this->getTokenFunctions(); |
||
| 252 | |||
| 253 | foreach ( $params['token'] as $t ) { |
||
| 254 | $val = call_user_func( $tokenFunctions[$t], $iwUser ); |
||
| 255 | if ( $val === false ) { |
||
| 256 | $this->setWarning( "Action '$t' is not allowed for the current user" ); |
||
| 257 | } else { |
||
| 258 | $data[$u][$t . 'token'] = $val; |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } else { |
||
| 263 | $data[$u]['missing'] = true; |
||
| 264 | if ( isset( $this->prop['cancreate'] ) && !$this->getConfig()->get( 'DisableAuthManager' ) ) { |
||
| 265 | $data[$u]['cancreate'] = MediaWiki\Auth\AuthManager::singleton()->canCreateAccount( $u ) |
||
| 266 | ->isGood(); |
||
| 267 | } |
||
| 268 | } |
||
| 269 | } else { |
||
| 270 | View Code Duplication | if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) { |
|
| 271 | ApiResult::setArrayType( $data[$u]['groups'], 'array' ); |
||
| 272 | ApiResult::setIndexedTagName( $data[$u]['groups'], 'g' ); |
||
| 273 | } |
||
| 274 | View Code Duplication | if ( isset( $this->prop['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) { |
|
| 275 | ApiResult::setArrayType( $data[$u]['implicitgroups'], 'array' ); |
||
| 276 | ApiResult::setIndexedTagName( $data[$u]['implicitgroups'], 'g' ); |
||
| 277 | } |
||
| 278 | View Code Duplication | if ( isset( $this->prop['rights'] ) && isset( $data[$u]['rights'] ) ) { |
|
| 279 | ApiResult::setArrayType( $data[$u]['rights'], 'array' ); |
||
| 280 | ApiResult::setIndexedTagName( $data[$u]['rights'], 'r' ); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | $fit = $result->addValue( [ 'query', $this->getModuleName() ], |
||
| 285 | null, $data[$u] ); |
||
| 286 | if ( !$fit ) { |
||
| 287 | $this->setContinueEnumParameter( 'users', |
||
| 288 | implode( '|', array_diff( $users, $done ) ) ); |
||
| 289 | break; |
||
| 290 | } |
||
| 291 | $done[] = $u; |
||
| 292 | } |
||
| 293 | $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'user' ); |
||
| 294 | } |
||
| 295 | |||
| 353 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.