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