Passed
Pull Request — main (#105)
by Andrey
12:50
created

Digit::convertToString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
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 2
    public function factorial(int $n = 0): int
15
    {
16 2
        if ($n === 0) {
17 2
            return 1;
18
        }
19
20 2
        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 2
    public function toShort(float $number, int $precision = 1): string
33
    {
34 2
        $length = strlen((string) ((int) $number));
35 2
        $length = ceil($length / 3) * 3 + 1;
36
37 2
        $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 2
        $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 2
        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 2
    public function shortKey(int $number, string $chars = 'abcdefghijklmnopqrstuvwxyz'): string
53
    {
54 2
        $length = strlen($chars);
55 2
        $mod    = $number % $length;
56
57 2
        if ($number - $mod === 0) {
58 2
            return substr($chars, $number, 1);
59
        }
60
61 2
        $result = '';
62
63 2
        while ($mod > 0 || $number > 0) {
64 2
            $result = substr($chars, $mod, 1) . $result;
65 2
            $number = ($number - $mod) / $length;
66 2
            $mod    = $number % $length;
67
        }
68
69 2
        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 4
    public function rounded(float $number, int $length = 4, int $precision = 1): float
82
    {
83 4
        $divided = (float) bcpow(10, $length - 4, 2);
84
85 4
        return round($number / $divided, $precision);
86
    }
87
88 2
    /**
89
     * Converts a numeric value to a string.
90
     *
91 2
     * @param  float|int  $value
92
     *
93
     * @return string
94
     */
95
    public function convertToString(float $value): string
96
    {
97
        return $value;
98 2
    }
99
100
    protected function suffix(int $length = 0): string
101
    {
102
        $available = [
103
            4  => '',
104
            7  => 'K',
105
            10 => 'M',
106
            13 => 'B',
107
            16 => 'T+',
108
        ];
109
110
        return $available[$length] ?? end($available);
111
    }
112
}
113