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