Conditions | 2 |
Paths | 2 |
Total Lines | 10 |
Code Lines | 4 |
Lines | 0 |
Ratio | 0 % |
Tests | 5 |
CRAP Score | 2 |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
30 | 5 | public static function string(int $length = 32): string |
|
31 | { |
||
32 | 5 | if ($length < 1) { |
|
33 | 1 | throw new \InvalidArgumentException('First parameter ($length) must be greater than 0.'); |
|
34 | } |
||
35 | |||
36 | // Optimization: we can generate a quarter fewer bits to completely cover the desired length in base64 |
||
37 | /** @psalm-suppress ArgumentTypeCoercion */ |
||
38 | 4 | $bytes = random_bytes((int) ceil($length * 0.75)); |
|
39 | 4 | return substr(StringHelper::base64UrlEncode($bytes), 0, $length); |
|
40 | } |
||
42 |