Token   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 10
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 62
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C generate() 0 23 8
A make() 0 10 2
1
<?php
2
3
namespace Sinergi\Users\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 .= $characters[(int)floor(hexdec(bin2hex(openssl_random_pseudo_bytes(1, $strong))) % count($characters))];
62
        } while (strlen($token) < $length);
63
64
        return $token;
65
    }
66
}
67