Conditions | 6 |
Paths | 3 |
Total Lines | 57 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Tests | 22 |
CRAP Score | 6.0208 |
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 |
||
7629 | public static function str_snakeize(string $str, string $encoding = 'UTF-8'): string |
||
7630 | { |
||
7631 | 22 | if ($str === '') { |
|
7632 | return ''; |
||
7633 | } |
||
7634 | |||
7635 | 22 | $str = \str_replace( |
|
7636 | 22 | '-', |
|
7637 | 22 | '_', |
|
7638 | 22 | self::normalize_whitespace($str) |
|
7639 | ); |
||
7640 | |||
7641 | 22 | if ($encoding !== 'UTF-8' && $encoding !== 'CP850') { |
|
7642 | 19 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
7643 | } |
||
7644 | |||
7645 | 22 | $str = (string) \preg_replace_callback( |
|
7646 | 22 | '/([\\p{N}|\\p{Lu}])/u', |
|
7647 | /** |
||
7648 | * @param string[] $matches |
||
7649 | * |
||
7650 | * @psalm-pure |
||
7651 | * |
||
7652 | * @return string |
||
7653 | */ |
||
7654 | static function (array $matches) use ($encoding): string { |
||
7655 | 9 | $match = $matches[1]; |
|
7656 | 9 | $match_int = (int) $match; |
|
7657 | |||
7658 | 9 | if ((string) $match_int === $match) { |
|
7659 | 4 | return '_' . $match . '_'; |
|
7660 | } |
||
7661 | |||
7662 | 5 | if ($encoding === 'UTF-8') { |
|
7663 | 5 | return '_' . \mb_strtolower($match); |
|
7664 | } |
||
7665 | |||
7666 | return '_' . self::strtolower($match, $encoding); |
||
7667 | 22 | }, |
|
7668 | 22 | $str |
|
7669 | ); |
||
7670 | |||
7671 | 22 | $str = (string) \preg_replace( |
|
7672 | [ |
||
7673 | 22 | '/\\s+/u', // convert spaces to "_" |
|
7674 | '/^\\s+|\\s+$/u', // trim leading & trailing spaces |
||
7675 | '/_+/', // remove double "_" |
||
7676 | ], |
||
7677 | [ |
||
7678 | 22 | '_', |
|
7679 | '', |
||
7680 | '_', |
||
7681 | ], |
||
7682 | 22 | $str |
|
7683 | ); |
||
7684 | |||
7685 | 22 | return \trim(\trim($str, '_')); // trim leading & trailing "_" + whitespace |
|
7686 | } |
||
13723 |