Passed
Pull Request — main (#22)
by Andrey
28:59 queued 16:23
created

Digit::shortKey()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
nc 3
nop 2
dl 0
loc 18
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
namespace Helldar\Support\Helpers;
4
5
final class Digit
6
{
7
    /**
8
     * Calculating the factorial of a number.
9
     *
10
     * @param  int  $n
11
     *
12
     * @return int
13
     */
14
    public function factorial(int $n = 0): int
15
    {
16
        if ($n === 0) {
17
            return 1;
18
        }
19
20
        return $n * $this->factorial($n - 1);
21
    }
22
23
    /**
24
     * Converts a number into a short version.
25
     * eg: 1000 >> 1K.
26
     *
27
     * @param  float  $number
28
     * @param  int  $precision
29
     *
30
     * @return string
31
     */
32
    public function toShort(float $number, int $precision = 1): string
33
    {
34
        $length = strlen((string) ((int) $number));
35
        $length = ceil($length / 3) * 3 + 1;
36
37
        $suffix = $this->suffix($length);
0 ignored issues
show
Bug introduced by
$length of type double is incompatible with the type integer expected by parameter $length of Helldar\Support\Helpers\Digit::suffix(). ( Ignorable by Annotation )

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

37
        $suffix = $this->suffix(/** @scrutinizer ignore-type */ $length);
Loading history...
38
        $value  = $this->rounded($number, $length, $precision);
0 ignored issues
show
Bug introduced by
$length of type double is incompatible with the type integer expected by parameter $length of Helldar\Support\Helpers\Digit::rounded(). ( Ignorable by Annotation )

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

38
        $value  = $this->rounded($number, /** @scrutinizer ignore-type */ $length, $precision);
Loading history...
39
40
        return $value . $suffix;
41
    }
42
43
    /**
44
     * Create short unique identifier from number.
45
     * Actually using in short URL.
46
     *
47
     * @param  int  $number
48
     * @param  string  $chars
49
     *
50
     * @return string
51
     */
52
    public function shortKey(int $number, string $chars = 'abcdefghijklmnopqrstuvwxyz'): string
53
    {
54
        $length = strlen($chars);
55
        $mod    = $number % $length;
56
57
        if ($number - $mod === 0) {
58
            return substr($chars, $number, 1);
59
        }
60
61
        $result = '';
62
63
        while ($mod > 0 || $number > 0) {
64
            $result = substr($chars, $mod, 1) . $result;
65
            $number = ($number - $mod) / $length;
66
            $mod    = $number % $length;
67
        }
68
69
        return $result;
70
    }
71
72
    /**
73
     * Format a number with grouped with divider.
74
     *
75
     * @param  float  $number
76
     * @param  int  $length
77
     * @param  int  $precision
78
     *
79
     * @return float
80
     */
81
    public function rounded(float $number, int $length = 4, int $precision = 1): float
82
    {
83
        $divided = (float) bcpow(10, $length - 4, 2);
84
85
        return round($number / $divided, $precision);
86
    }
87
88
    protected function suffix(int $length = 0): string
89
    {
90
        $available = [
91
            4  => '',
92
            7  => 'K',
93
            10 => 'M',
94
            13 => 'B',
95
            16 => 'T+',
96
        ];
97
98
        return $available[$length] ?? end($available);
99
    }
100
}
101