Conditions | 6 |
Paths | 3 |
Total Lines | 57 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Tests | 18 |
CRAP Score | 6.0359 |
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 |
||
8192 | public static function str_snakeize(string $str, string $encoding = 'UTF-8'): string |
||
8193 | { |
||
8194 | 9 | if ($str === '') { |
|
8195 | return ''; |
||
8196 | } |
||
8197 | |||
8198 | $str = \str_replace( |
||
8199 | '-', |
||
8200 | '_', |
||
8201 | self::normalize_whitespace($str) |
||
8202 | ); |
||
8203 | |||
8204 | if ($encoding !== 'UTF-8' && $encoding !== 'CP850') { |
||
8205 | $encoding = self::normalize_encoding($encoding, 'UTF-8'); |
||
8206 | } |
||
8207 | |||
8208 | $str = (string) \preg_replace_callback( |
||
8209 | '/([\\p{N}|\\p{Lu}])/u', |
||
8210 | 22 | /** |
|
8211 | * @param string[] $matches |
||
8212 | * |
||
8213 | * @psalm-pure |
||
8214 | 22 | * |
|
8215 | 22 | * @return string |
|
8216 | 22 | */ |
|
8217 | 22 | static function (array $matches) use ($encoding): string { |
|
8218 | $match = $matches[1]; |
||
8219 | $match_int = (int) $match; |
||
8220 | 22 | ||
8221 | 19 | if ((string) $match_int === $match) { |
|
8222 | return '_' . $match . '_'; |
||
8223 | } |
||
8224 | 22 | ||
8225 | 22 | if ($encoding === 'UTF-8') { |
|
8226 | return '_' . \mb_strtolower($match); |
||
8227 | } |
||
8228 | |||
8229 | return '_' . self::strtolower($match, $encoding); |
||
8230 | }, |
||
8231 | $str |
||
8232 | ); |
||
8233 | |||
8234 | 9 | $str = (string) \preg_replace( |
|
8235 | 9 | [ |
|
8236 | '/\\s+/u', // convert spaces to "_" |
||
8237 | 9 | '/^\\s+|\\s+$/u', // trim leading & trailing spaces |
|
8238 | 4 | '/_+/', // remove double "_" |
|
8239 | ], |
||
8240 | [ |
||
8241 | 5 | '_', |
|
8242 | 5 | '', |
|
8243 | '_', |
||
8244 | ], |
||
8245 | $str |
||
8246 | 22 | ); |
|
8247 | 22 | ||
8248 | return \trim(\trim($str, '_')); // trim leading & trailing "_" + whitespace |
||
8249 | } |
||
14796 |