Conditions | 4 |
Paths | 4 |
Total Lines | 19 |
Code Lines | 12 |
Lines | 0 |
Ratio | 0 % |
Tests | 10 |
CRAP Score | 4.074 |
Changes | 0 |
1 | <?php declare(strict_types = 1); |
||
13 | 1 | public static function generate(int $length = 32, string $charList = '0-9a-z'): string |
|
14 | { |
||
15 | $charList = count_chars(preg_replace_callback('#.-.#', function (array $matches): string { |
||
16 | 1 | return implode('', range($matches[0][0], $matches[0][2])); |
|
17 | 1 | }, $charList), 3); |
|
18 | |||
19 | 1 | $charListLength = strlen($charList); |
|
20 | 1 | if ($length < 1) { |
|
21 | throw new InvalidArgumentException('Length must be greater than zero.'); |
||
22 | 1 | } elseif ($charListLength < 2) { |
|
23 | throw new InvalidArgumentException('Character list must contain at least two chars.'); |
||
24 | } |
||
25 | |||
26 | 1 | $hash = ''; |
|
27 | 1 | for ($i = 0; $i < $length; $i++) { |
|
28 | 1 | $hash .= $charList[random_int(0, $charListLength - 1)]; |
|
29 | } |
||
30 | |||
31 | 1 | return $hash; |
|
32 | } |
||
35 |