Keys   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 0
dl 0
loc 81
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getPool() 0 28 7
A secureCrypt() 0 19 3
A getHashedToken() 0 10 2
1
<?php
2
3
namespace Infinitypaul\MultiStep\Controller;
4
5
class Keys
6
{
7
    /**
8
     * Get the pool to use based on the type of prefix hash.
9
     * @param  string $type
10
     * @return string
11
     */
12
    private static function getPool($type = 'alnum')
13
    {
14
        switch ($type) {
15
            case 'alnum':
16
                $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
17
                break;
18
            case 'alpha':
19
                $pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
20
                break;
21
            case 'hexdec':
22
                $pool = '0123456789abcdef';
23
                break;
24
            case 'numeric':
25
                $pool = '0123456789';
26
                break;
27
            case 'nozero':
28
                $pool = '123456789';
29
                break;
30
            case 'distinct':
31
                $pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
32
                break;
33
            default:
34
                $pool = (string) $type;
35
                break;
36
        }
37
38
        return $pool;
39
    }
40
41
    /**
42
     * Generate a random secure crypt figure.
43
     * @param  int $min
44
     * @param  int $max
45
     * @return int
46
     */
47
    private static function secureCrypt($min, $max)
48
    {
49
        $range = $max - $min;
50
51
        if ($range < 0) {
52
            return $min; // not so random...
53
        }
54
55
        $log = log($range, 2);
56
        $bytes = (int) ($log / 8) + 1; // length in bytes
57
        $bits = (int) $log + 1; // length in bits
58
        $filter = (int) (1 << $bits) - 1; // set all lower bits to 1
59
        do {
60
            $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
61
            $rnd = $rnd & $filter; // discard irrelevant bits
62
        } while ($rnd >= $range);
63
64
        return $min + $rnd;
65
    }
66
67
    /**
68
     * Finally, generate a hashed token.
69
     *
70
     * @param string $key
71
     * @param int $length
72
     *
73
     * @return string
74
     */
75
    public static function getHashedToken($key = '', $length = 25)
76
    {
77
        $token = '';
78
        $max = strlen(static::getPool());
0 ignored issues
show
Bug introduced by
Since getPool() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getPool() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
79
        for ($i = 0; $i < $length; $i++) {
80
            $token .= static::getPool()[static::secureCrypt(0, $max)];
0 ignored issues
show
Bug introduced by
Since getPool() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of getPool() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
Bug introduced by
Since secureCrypt() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of secureCrypt() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
81
        }
82
83
        return $key.$token;
84
    }
85
}
86