Hashes::generate()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.074

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 2
dl 0
loc 19
rs 9.8666
c 0
b 0
f 0
ccs 10
cts 12
cp 0.8333
crap 4.074
1
<?php declare(strict_types = 1);
2
3
namespace Apicart\Utils\Hashes;
4
5
use InvalidArgumentException;
6
7
final class Hashes
8
{
9
10
	/**
11
	 * @throws InvalidArgumentException
12
	 */
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
	}
33
34
}
35