Conditions | 7 |
Paths | 5 |
Total Lines | 170 |
Code Lines | 78 |
Lines | 0 |
Ratio | 0 % |
Tests | 51 |
CRAP Score | 7.0084 |
Changes | 3 | ||
Bugs | 0 | Features | 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 |
||
8461 | 35 | public static function str_titleize_for_humans( |
|
8462 | string $str, |
||
8463 | array $ignore = [], |
||
8464 | string $encoding = 'UTF-8' |
||
8465 | ): string { |
||
8466 | 35 | if ($str === '') { |
|
8467 | return ''; |
||
8468 | } |
||
8469 | |||
8470 | 35 | $small_words = [ |
|
8471 | '(?<!q&)a', |
||
8472 | 'an', |
||
8473 | 'and', |
||
8474 | 'as', |
||
8475 | 'at(?!&t)', |
||
8476 | 'but', |
||
8477 | 'by', |
||
8478 | 'en', |
||
8479 | 'for', |
||
8480 | 'if', |
||
8481 | 'in', |
||
8482 | 'of', |
||
8483 | 'on', |
||
8484 | 'or', |
||
8485 | 'the', |
||
8486 | 'to', |
||
8487 | 'v[.]?', |
||
8488 | 'via', |
||
8489 | 'vs[.]?', |
||
8490 | ]; |
||
8491 | |||
8492 | 35 | if ($ignore !== []) { |
|
8493 | 1 | $small_words = \array_merge($small_words, $ignore); |
|
8494 | } |
||
8495 | |||
8496 | 35 | $small_words_rx = \implode('|', $small_words); |
|
8497 | 35 | $apostrophe_rx = '(?x: [\'’] [[:lower:]]* )?'; |
|
8498 | |||
8499 | 35 | $str = \trim($str); |
|
8500 | |||
8501 | 35 | if (!self::has_lowercase($str)) { |
|
8502 | 2 | $str = self::strtolower($str, $encoding); |
|
8503 | } |
||
8504 | |||
8505 | // the main substitutions |
||
8506 | 35 | $str = (string) \preg_replace_callback( |
|
8507 | '~\\b (_*) (?: # 1. Leading underscore and |
||
8508 | ( (?<=[ ][/\\\\]) [[:alpha:]]+ [-_[:alpha:]/\\\\]+ | # 2. file path or |
||
8509 | 35 | [-_[:alpha:]]+ [@.:] [-_[:alpha:]@.:/]+ ' . $apostrophe_rx . ' ) # URL, domain, or email |
|
8510 | | |
||
8511 | 35 | ( (?i: ' . $small_words_rx . ' ) ' . $apostrophe_rx . ' ) # 3. or small word (case-insensitive) |
|
8512 | | |
||
8513 | 35 | ( [[:alpha:]] [[:lower:]\'’()\[\]{}]* ' . $apostrophe_rx . ' ) # 4. or word w/o internal caps |
|
8514 | | |
||
8515 | 35 | ( [[:alpha:]] [[:alpha:]\'’()\[\]{}]* ' . $apostrophe_rx . ' ) # 5. or some other word |
|
8516 | ) (_*) \\b # 6. With trailing underscore |
||
8517 | ~ux', |
||
8518 | /** |
||
8519 | * @param string[] $matches |
||
8520 | * |
||
8521 | * @psalm-pure |
||
8522 | * |
||
8523 | * @return string |
||
8524 | */ |
||
8525 | 35 | static function (array $matches) use ($encoding): string { |
|
8526 | // preserve leading underscore |
||
8527 | 35 | $str = $matches[1]; |
|
8528 | 35 | if ($matches[2]) { |
|
8529 | // preserve URLs, domains, emails and file paths |
||
8530 | 5 | $str .= $matches[2]; |
|
8531 | 35 | } elseif ($matches[3]) { |
|
8532 | // lower-case small words |
||
8533 | 25 | $str .= self::strtolower($matches[3], $encoding); |
|
8534 | 35 | } elseif ($matches[4]) { |
|
8535 | // capitalize word w/o internal caps |
||
8536 | 34 | $str .= static::ucfirst($matches[4], $encoding); |
|
8537 | } else { |
||
8538 | // preserve other kinds of word (iPhone) |
||
8539 | 7 | $str .= $matches[5]; |
|
8540 | } |
||
8541 | // preserve trailing underscore |
||
8542 | 35 | $str .= $matches[6]; |
|
8543 | |||
8544 | 35 | return $str; |
|
8545 | 35 | }, |
|
8546 | 35 | $str |
|
8547 | ); |
||
8548 | |||
8549 | // Exceptions for small words: capitalize at start of title... |
||
8550 | 35 | $str = (string) \preg_replace_callback( |
|
8551 | '~( \\A [[:punct:]]* # start of title... |
||
8552 | | [:.;?!][ ]+ # or of subsentence... |
||
8553 | | [ ][\'"“‘(\[][ ]* ) # or of inserted subphrase... |
||
8554 | 35 | ( ' . $small_words_rx . ' ) \\b # ...followed by small word |
|
8555 | ~uxi', |
||
8556 | /** |
||
8557 | * @param string[] $matches |
||
8558 | * |
||
8559 | * @psalm-pure |
||
8560 | * |
||
8561 | * @return string |
||
8562 | */ |
||
8563 | 35 | static function (array $matches) use ($encoding): string { |
|
8564 | 11 | return $matches[1] . static::ucfirst($matches[2], $encoding); |
|
8565 | 35 | }, |
|
8566 | 35 | $str |
|
8567 | ); |
||
8568 | |||
8569 | // ...and end of title |
||
8570 | 35 | $str = (string) \preg_replace_callback( |
|
8571 | 35 | '~\\b ( ' . $small_words_rx . ' ) # small word... |
|
8572 | (?= [[:punct:]]* \Z # ...at the end of the title... |
||
8573 | | [\'"’”)\]] [ ] ) # ...or of an inserted subphrase? |
||
8574 | ~uxi', |
||
8575 | /** |
||
8576 | * @param string[] $matches |
||
8577 | * |
||
8578 | * @psalm-pure |
||
8579 | * |
||
8580 | * @return string |
||
8581 | */ |
||
8582 | 35 | static function (array $matches) use ($encoding): string { |
|
8583 | 3 | return static::ucfirst($matches[1], $encoding); |
|
8584 | 35 | }, |
|
8585 | 35 | $str |
|
8586 | ); |
||
8587 | |||
8588 | // Exceptions for small words in hyphenated compound words. |
||
8589 | // e.g. "in-flight" -> In-Flight |
||
8590 | 35 | $str = (string) \preg_replace_callback( |
|
8591 | '~\\b |
||
8592 | (?<! -) # Negative lookbehind for a hyphen; we do not want to match man-in-the-middle but do want (in-flight) |
||
8593 | 35 | ( ' . $small_words_rx . ' ) |
|
8594 | (?= -[[:alpha:]]+) # lookahead for "-someword" |
||
8595 | ~uxi', |
||
8596 | /** |
||
8597 | * @param string[] $matches |
||
8598 | * |
||
8599 | * @psalm-pure |
||
8600 | * |
||
8601 | * @return string |
||
8602 | */ |
||
8603 | 35 | static function (array $matches) use ($encoding): string { |
|
8604 | return static::ucfirst($matches[1], $encoding); |
||
8605 | 35 | }, |
|
8606 | 35 | $str |
|
8607 | ); |
||
8608 | |||
8609 | // e.g. "Stand-in" -> "Stand-In" (Stand is already capped at this point) |
||
8610 | 35 | $str = (string) \preg_replace_callback( |
|
8611 | '~\\b |
||
8612 | (?<!…) # Negative lookbehind for a hyphen; we do not want to match man-in-the-middle but do want (stand-in) |
||
8613 | ( [[:alpha:]]+- ) # $1 = first word and hyphen, should already be properly capped |
||
8614 | 35 | ( ' . $small_words_rx . ' ) # ...followed by small word |
|
8615 | (?! - ) # Negative lookahead for another - |
||
8616 | ~uxi', |
||
8617 | /** |
||
8618 | * @param string[] $matches |
||
8619 | * |
||
8620 | * @psalm-pure |
||
8621 | * |
||
8622 | * @return string |
||
8623 | */ |
||
8624 | 35 | static function (array $matches) use ($encoding): string { |
|
8625 | return $matches[1] . static::ucfirst($matches[2], $encoding); |
||
8626 | 35 | }, |
|
8627 | 35 | $str |
|
8628 | ); |
||
8629 | |||
8630 | 35 | return $str; |
|
8631 | } |
||
13666 |