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