| Conditions | 21 |
| Paths | > 20000 |
| Total Lines | 122 |
| Code Lines | 83 |
| 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 |
||
| 140 | public function submit() { |
||
| 141 | global $wgPasswordPolicy; |
||
| 142 | |||
| 143 | $retVal = true; |
||
| 144 | $this->parent->setVarsFromRequest( [ 'wgSitename', '_NamespaceType', |
||
| 145 | '_AdminName', '_AdminPassword', '_AdminPasswordConfirm', '_AdminEmail', |
||
| 146 | '_Subscribe', '_SkipOptional', 'wgMetaNamespace', 'wgPingback' ] ); |
||
| 147 | |||
| 148 | // Validate site name |
||
| 149 | if ( strval( $this->getVar( 'wgSitename' ) ) === '' ) { |
||
| 150 | $this->parent->showError( 'config-site-name-blank' ); |
||
| 151 | $retVal = false; |
||
| 152 | } |
||
| 153 | |||
| 154 | // Fetch namespace |
||
| 155 | $nsType = $this->getVar( '_NamespaceType' ); |
||
| 156 | if ( $nsType == 'site-name' ) { |
||
| 157 | $name = $this->getVar( 'wgSitename' ); |
||
| 158 | // Sanitize for namespace |
||
| 159 | // This algorithm should match the JS one in WebInstallerOutput.php |
||
| 160 | $name = preg_replace( '/[\[\]\{\}|#<>%+? ]/', '_', $name ); |
||
| 161 | $name = str_replace( '&', '&', $name ); |
||
| 162 | $name = preg_replace( '/__+/', '_', $name ); |
||
| 163 | $name = ucfirst( trim( $name, '_' ) ); |
||
| 164 | } elseif ( $nsType == 'generic' ) { |
||
| 165 | $name = wfMessage( 'config-ns-generic' )->text(); |
||
| 166 | } else { // other |
||
| 167 | $name = $this->getVar( 'wgMetaNamespace' ); |
||
| 168 | } |
||
| 169 | |||
| 170 | // Validate namespace |
||
| 171 | if ( strpos( $name, ':' ) !== false ) { |
||
| 172 | $good = false; |
||
| 173 | } else { |
||
| 174 | // Title-style validation |
||
| 175 | $title = Title::newFromText( $name ); |
||
| 176 | if ( !$title ) { |
||
| 177 | $good = $nsType == 'site-name'; |
||
| 178 | } else { |
||
| 179 | $name = $title->getDBkey(); |
||
| 180 | $good = true; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | if ( !$good ) { |
||
| 184 | $this->parent->showError( 'config-ns-invalid', $name ); |
||
| 185 | $retVal = false; |
||
| 186 | } |
||
| 187 | |||
| 188 | // Make sure it won't conflict with any existing namespaces |
||
| 189 | global $wgContLang; |
||
| 190 | $nsIndex = $wgContLang->getNsIndex( $name ); |
||
| 191 | if ( $nsIndex !== false && $nsIndex !== NS_PROJECT ) { |
||
| 192 | $this->parent->showError( 'config-ns-conflict', $name ); |
||
| 193 | $retVal = false; |
||
| 194 | } |
||
| 195 | |||
| 196 | $this->setVar( 'wgMetaNamespace', $name ); |
||
| 197 | |||
| 198 | // Validate username for creation |
||
| 199 | $name = $this->getVar( '_AdminName' ); |
||
| 200 | if ( strval( $name ) === '' ) { |
||
| 201 | $this->parent->showError( 'config-admin-name-blank' ); |
||
| 202 | $cname = $name; |
||
| 203 | $retVal = false; |
||
| 204 | } else { |
||
| 205 | $cname = User::getCanonicalName( $name, 'creatable' ); |
||
| 206 | if ( $cname === false ) { |
||
| 207 | $this->parent->showError( 'config-admin-name-invalid', $name ); |
||
| 208 | $retVal = false; |
||
| 209 | } else { |
||
| 210 | $this->setVar( '_AdminName', $cname ); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | |||
| 214 | // Validate password |
||
| 215 | $msg = false; |
||
| 216 | $pwd = $this->getVar( '_AdminPassword' ); |
||
| 217 | $user = User::newFromName( $cname ); |
||
| 218 | if ( $user ) { |
||
| 219 | $upp = new UserPasswordPolicy( |
||
| 220 | $wgPasswordPolicy['policies'], |
||
| 221 | $wgPasswordPolicy['checks'] |
||
| 222 | ); |
||
| 223 | $status = $upp->checkUserPasswordForGroups( |
||
| 224 | $user, |
||
| 225 | $pwd, |
||
| 226 | [ 'bureaucrat', 'sysop' ] // per Installer::createSysop() |
||
| 227 | ); |
||
| 228 | $valid = $status->isGood() ? true : $status->getMessage(); |
||
| 229 | } else { |
||
| 230 | $valid = 'config-admin-name-invalid'; |
||
| 231 | } |
||
| 232 | if ( strval( $pwd ) === '' ) { |
||
| 233 | // Provide a more specific and helpful message if password field is left blank |
||
| 234 | $msg = 'config-admin-password-blank'; |
||
| 235 | } elseif ( $pwd !== $this->getVar( '_AdminPasswordConfirm' ) ) { |
||
| 236 | $msg = 'config-admin-password-mismatch'; |
||
| 237 | } elseif ( $valid !== true ) { |
||
| 238 | $msg = $valid; |
||
| 239 | } |
||
| 240 | if ( $msg !== false ) { |
||
| 241 | call_user_func( [ $this->parent, 'showError' ], $msg ); |
||
| 242 | $this->setVar( '_AdminPassword', '' ); |
||
| 243 | $this->setVar( '_AdminPasswordConfirm', '' ); |
||
| 244 | $retVal = false; |
||
| 245 | } |
||
| 246 | |||
| 247 | // Validate e-mail if provided |
||
| 248 | $email = $this->getVar( '_AdminEmail' ); |
||
| 249 | if ( $email && !Sanitizer::validateEmail( $email ) ) { |
||
|
|
|||
| 250 | $this->parent->showError( 'config-admin-error-bademail' ); |
||
| 251 | $retVal = false; |
||
| 252 | } |
||
| 253 | // If they asked to subscribe to mediawiki-announce but didn't give |
||
| 254 | // an e-mail, show an error. Bug 29332 |
||
| 255 | if ( !$email && $this->getVar( '_Subscribe' ) ) { |
||
| 256 | $this->parent->showError( 'config-subscribe-noemail' ); |
||
| 257 | $retVal = false; |
||
| 258 | } |
||
| 259 | |||
| 260 | return $retVal; |
||
| 261 | } |
||
| 262 | |||
| 264 |
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.