Entropic::genPassword()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace ExoUNX\Entropic;
4
5
use Exception;
6
7
/**
8
 * Class Entropic
9
 * @package ExoUNX\Entropic
10
 */
11
class Entropic
12
{
13
    /**
14
     * Printable ASCII characters
15
     */
16
17
    private const ASCII_SYMBOL = "!\\\"#$%&'()*+,-./:;<=>?@[\]^_`{|}~";
18
19
    private const ASCII_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
20
21
    private const ASCII_LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
22
23
    private const ASCII_NUMERICAL = "0123456789";
24
25
    private const DEFAULT_CHARSET = self::ASCII_SYMBOL.self::ASCII_LOWERCASE.self::ASCII_UPPERCASE.self::ASCII_NUMERICAL;
26
27
    /**
28
     * @param $length
29
     *
30
     * @return string
31
     * @throws Exception
32
     */
33
    public function genPassword(int $length): string
34
    {
35
        if (empty($this->getOps())) {
36
            $password = $this->generateRandomString($length);
37
        } else {
38
            $password = $this->generateRandomString($this->getOps());
39
        }
40
41
        return $password;
42
    }
43
44
    /**
45
     * @return int|null
46
     * Working on CLI
47
     */
48
    private function getOps(): ?int
49
    {
50
        if (getopt("c")) {
51
            return getopt("c")['c'];
52
        } else {
53
            return null;
54
        }
55
    }
56
57
    /**
58
     * @param  int  $length
59
     *
60
     * @return string
61
     * @throws Exception
62
     */
63
    private function generateRandomString(int $length): string
64
    {
65
        $str = null;
66
67
        $default_charset = $this->defaultCharset();
0 ignored issues
show
Coding Style introduced by
$default_charset does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
68
69
        $size = count($default_charset) - 1;
0 ignored issues
show
Coding Style introduced by
$default_charset does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
70
71
        for ($len = $length, $i = 0; $i < $len; $i++) {
72
            $k       = random_int(0, $size);
73
            $str[$i] = $default_charset[$k];
0 ignored issues
show
Coding Style introduced by
$default_charset does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
74
        }
75
76
        return implode('', $str);
77
    }
78
79
    private function defaultCharset(): array
80
    {
81
        return str_split(self::DEFAULT_CHARSET);
82
    }
83
}