Passed
Push — main ( 56b906...87c1e5 )
by William
10:41
created

generateRandomString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 18
rs 10
1
<?php
2
3
4
function generateRandomString($length = 10)
5
{
6
    // Check if the length is valid
7
    if ($length <= 0) {
8
        return false;
9
    }
10
11
    // Generate random bytes and convert to a string
12
    $randomBytes = random_bytes($length);
13
    $randomString = base64_encode($randomBytes);
14
15
    // Remove any non-alphanumeric characters
16
    $randomString = preg_replace('/[^a-zA-Z0-9]/', '', $randomString);
17
18
    // Truncate or pad the string to the desired length
19
    $randomString = substr($randomString, 0, $length);
20
21
    return $randomString;
22
}