| Conditions | 40 |
| Paths | 1157 |
| Total Lines | 198 |
| Code Lines | 97 |
| Lines | 23 |
| Ratio | 11.62 % |
| 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 |
||
| 257 | public function splitTitleString( $text, $defaultNamespace = NS_MAIN ) { |
||
| 258 | $dbkey = str_replace( ' ', '_', $text ); |
||
| 259 | |||
| 260 | # Initialisation |
||
| 261 | $parts = [ |
||
| 262 | 'interwiki' => '', |
||
| 263 | 'local_interwiki' => false, |
||
| 264 | 'fragment' => '', |
||
| 265 | 'namespace' => $defaultNamespace, |
||
| 266 | 'dbkey' => $dbkey, |
||
| 267 | 'user_case_dbkey' => $dbkey, |
||
| 268 | ]; |
||
| 269 | |||
| 270 | # Strip Unicode bidi override characters. |
||
| 271 | # Sometimes they slip into cut-n-pasted page titles, where the |
||
| 272 | # override chars get included in list displays. |
||
| 273 | $dbkey = preg_replace( '/\xE2\x80[\x8E\x8F\xAA-\xAE]/S', '', $dbkey ); |
||
| 274 | |||
| 275 | # Clean up whitespace |
||
| 276 | # Note: use of the /u option on preg_replace here will cause |
||
| 277 | # input with invalid UTF-8 sequences to be nullified out in PHP 5.2.x, |
||
| 278 | # conveniently disabling them. |
||
| 279 | $dbkey = preg_replace( |
||
| 280 | '/[ _\xA0\x{1680}\x{180E}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}]+/u', |
||
| 281 | '_', |
||
| 282 | $dbkey |
||
| 283 | ); |
||
| 284 | $dbkey = trim( $dbkey, '_' ); |
||
| 285 | |||
| 286 | if ( strpos( $dbkey, UtfNormal\Constants::UTF8_REPLACEMENT ) !== false ) { |
||
| 287 | # Contained illegal UTF-8 sequences or forbidden Unicode chars. |
||
| 288 | throw new MalformedTitleException( 'title-invalid-utf8', $text ); |
||
| 289 | } |
||
| 290 | |||
| 291 | $parts['dbkey'] = $dbkey; |
||
| 292 | |||
| 293 | # Initial colon indicates main namespace rather than specified default |
||
| 294 | # but should not create invalid {ns,title} pairs such as {0,Project:Foo} |
||
| 295 | View Code Duplication | if ( $dbkey !== '' && ':' == $dbkey[0] ) { |
|
| 296 | $parts['namespace'] = NS_MAIN; |
||
| 297 | $dbkey = substr( $dbkey, 1 ); # remove the colon but continue processing |
||
| 298 | $dbkey = trim( $dbkey, '_' ); # remove any subsequent whitespace |
||
| 299 | } |
||
| 300 | |||
| 301 | if ( $dbkey == '' ) { |
||
| 302 | throw new MalformedTitleException( 'title-invalid-empty', $text ); |
||
| 303 | } |
||
| 304 | |||
| 305 | # Namespace or interwiki prefix |
||
| 306 | $prefixRegexp = "/^(.+?)_*:_*(.*)$/S"; |
||
| 307 | do { |
||
| 308 | $m = []; |
||
| 309 | if ( preg_match( $prefixRegexp, $dbkey, $m ) ) { |
||
| 310 | $p = $m[1]; |
||
| 311 | $ns = $this->language->getNsIndex( $p ); |
||
| 312 | if ( $ns !== false ) { |
||
| 313 | # Ordinary namespace |
||
| 314 | $dbkey = $m[2]; |
||
| 315 | $parts['namespace'] = $ns; |
||
| 316 | # For Talk:X pages, check if X has a "namespace" prefix |
||
| 317 | if ( $ns == NS_TALK && preg_match( $prefixRegexp, $dbkey, $x ) ) { |
||
| 318 | if ( $this->language->getNsIndex( $x[1] ) ) { |
||
| 319 | # Disallow Talk:File:x type titles... |
||
| 320 | throw new MalformedTitleException( 'title-invalid-talk-namespace', $text ); |
||
| 321 | } elseif ( Interwiki::isValidInterwiki( $x[1] ) ) { |
||
| 322 | // TODO: get rid of global state! |
||
| 323 | # Disallow Talk:Interwiki:x type titles... |
||
| 324 | throw new MalformedTitleException( 'title-invalid-talk-namespace', $text ); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | } elseif ( Interwiki::isValidInterwiki( $p ) ) { |
||
| 328 | # Interwiki link |
||
| 329 | $dbkey = $m[2]; |
||
| 330 | $parts['interwiki'] = $this->language->lc( $p ); |
||
| 331 | |||
| 332 | # Redundant interwiki prefix to the local wiki |
||
| 333 | foreach ( $this->localInterwikis as $localIW ) { |
||
| 334 | if ( 0 == strcasecmp( $parts['interwiki'], $localIW ) ) { |
||
| 335 | if ( $dbkey == '' ) { |
||
| 336 | # Empty self-links should point to the Main Page, to ensure |
||
| 337 | # compatibility with cross-wiki transclusions and the like. |
||
| 338 | $mainPage = Title::newMainPage(); |
||
| 339 | return [ |
||
| 340 | 'interwiki' => $mainPage->getInterwiki(), |
||
| 341 | 'local_interwiki' => true, |
||
| 342 | 'fragment' => $mainPage->getFragment(), |
||
| 343 | 'namespace' => $mainPage->getNamespace(), |
||
| 344 | 'dbkey' => $mainPage->getDBkey(), |
||
| 345 | 'user_case_dbkey' => $mainPage->getUserCaseDBKey() |
||
| 346 | ]; |
||
| 347 | } |
||
| 348 | $parts['interwiki'] = ''; |
||
| 349 | # local interwikis should behave like initial-colon links |
||
| 350 | $parts['local_interwiki'] = true; |
||
| 351 | |||
| 352 | # Do another namespace split... |
||
| 353 | continue 2; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | # If there's an initial colon after the interwiki, that also |
||
| 358 | # resets the default namespace |
||
| 359 | View Code Duplication | if ( $dbkey !== '' && $dbkey[0] == ':' ) { |
|
| 360 | $parts['namespace'] = NS_MAIN; |
||
| 361 | $dbkey = substr( $dbkey, 1 ); |
||
| 362 | } |
||
| 363 | } |
||
| 364 | # If there's no recognized interwiki or namespace, |
||
| 365 | # then let the colon expression be part of the title. |
||
| 366 | } |
||
| 367 | break; |
||
| 368 | } while ( true ); |
||
| 369 | |||
| 370 | $fragment = strstr( $dbkey, '#' ); |
||
| 371 | if ( false !== $fragment ) { |
||
| 372 | $parts['fragment'] = str_replace( '_', ' ', substr( $fragment, 1 ) ); |
||
| 373 | $dbkey = substr( $dbkey, 0, strlen( $dbkey ) - strlen( $fragment ) ); |
||
| 374 | # remove whitespace again: prevents "Foo_bar_#" |
||
| 375 | # becoming "Foo_bar_" |
||
| 376 | $dbkey = preg_replace( '/_*$/', '', $dbkey ); |
||
| 377 | } |
||
| 378 | |||
| 379 | # Reject illegal characters. |
||
| 380 | $rxTc = self::getTitleInvalidRegex(); |
||
| 381 | $matches = []; |
||
| 382 | if ( preg_match( $rxTc, $dbkey, $matches ) ) { |
||
| 383 | throw new MalformedTitleException( 'title-invalid-characters', $text, [ $matches[0] ] ); |
||
| 384 | } |
||
| 385 | |||
| 386 | # Pages with "/./" or "/../" appearing in the URLs will often be un- |
||
| 387 | # reachable due to the way web browsers deal with 'relative' URLs. |
||
| 388 | # Also, they conflict with subpage syntax. Forbid them explicitly. |
||
| 389 | View Code Duplication | if ( |
|
| 390 | strpos( $dbkey, '.' ) !== false && |
||
| 391 | ( |
||
| 392 | $dbkey === '.' || $dbkey === '..' || |
||
| 393 | strpos( $dbkey, './' ) === 0 || |
||
| 394 | strpos( $dbkey, '../' ) === 0 || |
||
| 395 | strpos( $dbkey, '/./' ) !== false || |
||
| 396 | strpos( $dbkey, '/../' ) !== false || |
||
| 397 | substr( $dbkey, -2 ) == '/.' || |
||
| 398 | substr( $dbkey, -3 ) == '/..' |
||
| 399 | ) |
||
| 400 | ) { |
||
| 401 | throw new MalformedTitleException( 'title-invalid-relative', $text ); |
||
| 402 | } |
||
| 403 | |||
| 404 | # Magic tilde sequences? Nu-uh! |
||
| 405 | if ( strpos( $dbkey, '~~~' ) !== false ) { |
||
| 406 | throw new MalformedTitleException( 'title-invalid-magic-tilde', $text ); |
||
| 407 | } |
||
| 408 | |||
| 409 | # Limit the size of titles to 255 bytes. This is typically the size of the |
||
| 410 | # underlying database field. We make an exception for special pages, which |
||
| 411 | # don't need to be stored in the database, and may edge over 255 bytes due |
||
| 412 | # to subpage syntax for long titles, e.g. [[Special:Block/Long name]] |
||
| 413 | $maxLength = ( $parts['namespace'] != NS_SPECIAL ) ? 255 : 512; |
||
| 414 | if ( strlen( $dbkey ) > $maxLength ) { |
||
| 415 | throw new MalformedTitleException( 'title-invalid-too-long', $text, |
||
| 416 | [ Message::numParam( $maxLength ) ] ); |
||
| 417 | } |
||
| 418 | |||
| 419 | # Normally, all wiki links are forced to have an initial capital letter so [[foo]] |
||
| 420 | # and [[Foo]] point to the same place. Don't force it for interwikis, since the |
||
| 421 | # other site might be case-sensitive. |
||
| 422 | $parts['user_case_dbkey'] = $dbkey; |
||
| 423 | if ( $parts['interwiki'] === '' ) { |
||
| 424 | $dbkey = Title::capitalize( $dbkey, $parts['namespace'] ); |
||
| 425 | } |
||
| 426 | |||
| 427 | # Can't make a link to a namespace alone... "empty" local links can only be |
||
| 428 | # self-links with a fragment identifier. |
||
| 429 | if ( $dbkey == '' && $parts['interwiki'] === '' ) { |
||
| 430 | if ( $parts['namespace'] != NS_MAIN ) { |
||
| 431 | throw new MalformedTitleException( 'title-invalid-empty', $text ); |
||
| 432 | } |
||
| 433 | } |
||
| 434 | |||
| 435 | // Allow IPv6 usernames to start with '::' by canonicalizing IPv6 titles. |
||
| 436 | // IP names are not allowed for accounts, and can only be referring to |
||
| 437 | // edits from the IP. Given '::' abbreviations and caps/lowercaps, |
||
| 438 | // there are numerous ways to present the same IP. Having sp:contribs scan |
||
| 439 | // them all is silly and having some show the edits and others not is |
||
| 440 | // inconsistent. Same for talk/userpages. Keep them normalized instead. |
||
| 441 | if ( $parts['namespace'] == NS_USER || $parts['namespace'] == NS_USER_TALK ) { |
||
| 442 | $dbkey = IP::sanitizeIP( $dbkey ); |
||
| 443 | } |
||
| 444 | |||
| 445 | // Any remaining initial :s are illegal. |
||
| 446 | if ( $dbkey !== '' && ':' == $dbkey[0] ) { |
||
| 447 | throw new MalformedTitleException( 'title-invalid-leading-colon', $text ); |
||
| 448 | } |
||
| 449 | |||
| 450 | # Fill fields |
||
| 451 | $parts['dbkey'] = $dbkey; |
||
| 452 | |||
| 453 | return $parts; |
||
| 454 | } |
||
| 455 | |||
| 485 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.