Passed
Pull Request — master (#20)
by Hilmi Erdem
10:06
created

NumericGenerator::generateRangeForLength()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 14
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * @copyright 2021 Hilmi Erdem KEREN
5
 * @license MIT
6
 */
7
8
namespace Erdemkeren\Otp\Generators;
9
10
use Throwable;
11
use Erdemkeren\Otp\Contracts\GeneratorContract;
12
13
class NumericGenerator implements GeneratorContract
14
{
15
    public function generate(int $length = null): string
16
    {
17
        $range = $this->generateRangeForLength($length);
0 ignored issues
show
Bug introduced by
It seems like $length can also be of type null; however, parameter $length of Erdemkeren\Otp\Generator...enerateRangeForLength() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

17
        $range = $this->generateRangeForLength(/** @scrutinizer ignore-type */ $length);
Loading history...
18
19
        try {
20
            $int = random_int($range[0], $range[1]);
21
        } catch (Throwable) {
22
            $int = rand($range[0], $range[1]);
23
        }
24
25
        return (string) $int;
26
    }
27
28
    protected function generateRangeForLength(int $length): array
29
    {
30
        $min = 1;
31
        $max = 9;
32
33
        while ($length > 1) {
34
            $min .= 0;
35
            $max .= 9;
36
37
            $length--;
38
        }
39
40
        return [
41
            $min, $max,
42
        ];
43
    }
44
}
45