Completed
Push — master ( 6c2fe3...ff207a )
by Gabriel
29s
created

Token::generate()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
ccs 0
cts 18
cp 0
rs 6.1403
cc 8
eloc 13
nc 6
nop 3
crap 72
1
<?php
2
3
namespace Omnimail\Utils;
4
5
class Token
6
{
7
    /**
8
     * @var array
9
     */
10
    private static $characters = [
11
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'o', 'j', 'k', 'l',
12
        'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
13
        'E', 'F', 'G', 'H', 'O', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
14
        'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
15
    ];
16
17
    /**
18
     * Generate a random string
19
     *
20
     * @param int $length
21
     * @param mixed $type
22
     * @param bool $caseSensitive
23
     * @return string
24
     */
25
    public static function generate($length = 128, $type = 'alnum', $caseSensitive = true)
26
    {
27
        $characters = self::$characters;
28
29
        // Provided characters
30
        if (is_array($type)) {
31
            $characters = $type;
32
        } // Hexa decimal
33
        elseif ($type === 'hexa') {
34
            $characters = array_merge(array_slice(self::$characters, 52), array_slice(self::$characters, 26, 6));
35
        } // Case insensitive alpha numeric
36
        elseif ($type === 'alnum' && $caseSensitive === false) {
37
            $characters = array_slice(self::$characters, 26);
38
        } // Alphabet only
39
        elseif ($type === 'alpha') {
40
            $characters = array_slice(self::$characters, 0, 26);
41
        } // Numbers only
42
        elseif ($type === 'digit' || $type === 'numeric') {
43
            $characters = array_slice(self::$characters, 52);
44
        }
45
46
        return self::make($characters, $length);
0 ignored issues
show
Documentation introduced by
$characters is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
47
    }
48
49
    /**
50
     * Make the random string
51
     *
52
     * @param string $characters
53
     * @param int $length
54
     * @return string
55
     */
56
    private static function make($characters, $length)
57
    {
58
        $token = '';
59
60
        do {
61
            $token .=
62
                $characters[(int)floor(hexdec(bin2hex(openssl_random_pseudo_bytes(1, $strong))) % count($characters))];
63
        } while (strlen($token) < $length);
64
65
        return $token;
66
    }
67
}
68