Conditions | 6 |
Paths | 3 |
Total Lines | 57 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Tests | 23 |
CRAP Score | 6.0184 |
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 |
||
7600 | 22 | public static function str_snakeize(string $str, string $encoding = 'UTF-8'): string |
|
7601 | { |
||
7602 | 22 | if ($str === '') { |
|
7603 | return ''; |
||
7604 | } |
||
7605 | |||
7606 | 22 | $str = \str_replace( |
|
7607 | 22 | '-', |
|
7608 | 22 | '_', |
|
7609 | 22 | self::normalize_whitespace($str) |
|
7610 | ); |
||
7611 | |||
7612 | 22 | if ($encoding !== 'UTF-8' && $encoding !== 'CP850') { |
|
7613 | 19 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
|
7614 | } |
||
7615 | |||
7616 | 22 | $str = (string) \preg_replace_callback( |
|
7617 | 22 | '/([\\p{N}|\\p{Lu}])/u', |
|
7618 | /** |
||
7619 | * @param string[] $matches |
||
7620 | * |
||
7621 | * @psalm-pure |
||
7622 | * |
||
7623 | * @return string |
||
7624 | */ |
||
7625 | static function (array $matches) use ($encoding): string { |
||
7626 | 9 | $match = $matches[1]; |
|
7627 | 9 | $match_int = (int) $match; |
|
7628 | |||
7629 | 9 | if ((string) $match_int === $match) { |
|
7630 | 4 | return '_' . $match . '_'; |
|
7631 | } |
||
7632 | |||
7633 | 5 | if ($encoding === 'UTF-8') { |
|
7634 | 5 | return '_' . \mb_strtolower($match); |
|
7635 | } |
||
7636 | |||
7637 | return '_' . self::strtolower($match, $encoding); |
||
7638 | 22 | }, |
|
7639 | 22 | $str |
|
7640 | ); |
||
7641 | |||
7642 | 22 | $str = (string) \preg_replace( |
|
7643 | [ |
||
7644 | 22 | '/\\s+/u', // convert spaces to "_" |
|
7645 | '/^\\s+|\\s+$/u', // trim leading & trailing spaces |
||
7646 | '/_+/', // remove double "_" |
||
7647 | ], |
||
7648 | [ |
||
7649 | 22 | '_', |
|
7650 | '', |
||
7651 | '_', |
||
7652 | ], |
||
7653 | 22 | $str |
|
7654 | ); |
||
7655 | |||
7656 | 22 | return \trim(\trim($str, '_')); // trim leading & trailing "_" + whitespace |
|
7657 | } |
||
13694 |