Pretty   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 169
ccs 47
cts 47
cp 1
rs 10
c 0
b 0
f 0
wmc 16

14 Methods

Rating   Name   Duplication   Size   Complexity  
A bytes() 0 4 1
A __construct() 0 2 1
A refineDecimals() 0 8 1
A setThousandsSeparator() 0 3 1
A setDecimalPoint() 0 3 1
A percent() 0 15 1
A useconds() 0 4 1
A nanoseconds() 0 4 1
A resetMaxDecimals() 0 3 1
A time() 0 12 3
A setMaxDecimals() 0 3 1
A seconds() 0 4 1
A resetThousandsSeparator() 0 3 1
A resetDecimalPoint() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit;
6
7
use function AlecRabbit\Helpers\bounds;
8
use const AlecRabbit\Helpers\Constants\DEFAULT_PRECISION;
9
10
class Pretty
11
{
12
    public const DEFAULT_DECIMALS = 2;
13
    public const MAX_DECIMALS = 6;
14
    public const DECIMAL_POINT = '.';
15
    public const THOUSANDS_SEPARATOR = '';
16
    public const DEFAULT_PRECISION = DEFAULT_PRECISION;
17
18
    /** @var null|string */
19
    protected static $decimalPoint;
20
    /** @var null|string */
21
    protected static $thousandsSeparator;
22
    /** @var null|int */
23
    protected static $maxDecimals;
24
25
    /**
26
     * Static class. Private Constructor.
27
     */
28
    // @codeCoverageIgnoreStart
29
    private function __construct()
30
    {
31
    }
32
    // @codeCoverageIgnoreEnd
33
34
    /**
35
     * @param int $number
36
     * @param null|string $unit
37
     * @param int|null $decimals
38
     * @return string
39
     */
40 11
    public static function bytes(int $number, ?string $unit = null, ?int $decimals = null): string
41
    {
42
        return
43 11
            format_bytes($number, $unit, static::refineDecimals($decimals));
44
    }
45
46
    /**
47
     * @param null|int $decimals
48
     * @return int
49
     */
50 64
    public static function refineDecimals(?int $decimals): int
51
    {
52
53
        return
54 64
            (int)bounds(
55 64
                $decimals ?? static::DEFAULT_DECIMALS,
56 64
                0,
57 64
                static::$maxDecimals ?? static::MAX_DECIMALS
58
            );
59
    }
60
61
    /**
62
     * @param float $seconds
63
     * @param null|int $units
64
     * @param null|int $decimals
65
     * @return string
66
     */
67 21
    public static function seconds(float $seconds, ?int $units = null, ?int $decimals = null): string
68
    {
69
        return
70 21
            static::time($seconds, $units, $decimals);
71
    }
72
73
    /**
74
     * @param float $seconds
75
     * @param int|null $units
76
     * @param int|null $decimals
77
     * @return string
78
     */
79 77
    public static function time(float $seconds, ?int $units = null, ?int $decimals = null): string
80
    {
81 77
        if (null === $units && null === $decimals) {
82 52
            return format_time_auto($seconds);
83
        }
84
        return
85 25
            format_time(
86 25
                $seconds,
87 25
                $units,
88 25
                static::refineDecimals($decimals),
89 25
                static::$decimalPoint ?? static::DECIMAL_POINT,
90 25
                static::$thousandsSeparator ?? static::THOUSANDS_SEPARATOR
91
            );
92
    }
93
94
    /**
95
     * @param float $useconds
96
     * @param null|int $units
97
     * @param null|int $decimals
98
     * @return string
99
     */
100 20
    public static function useconds(float $useconds, ?int $units = null, ?int $decimals = null): string
101
    {
102
        return
103 20
            static::time($useconds / 1000000, $units, $decimals);
104
    }
105
106
    /**
107
     * @param float $nanoseconds
108
     * @param null|int $units
109
     * @param null|int $decimals
110
     * @return string
111
     */
112 15
    public static function nanoseconds(float $nanoseconds, ?int $units = null, ?int $decimals = null): string
113
    {
114
        return
115 15
            static::time($nanoseconds / 1000000000, $units, $decimals);
116
    }
117
118
    /**
119
     * @param float $fraction
120
     * @param null|int $decimals
121
     * @param null|string $prefix
122
     * @param string $suffix
123
     * @return string
124
     */
125 28
    public static function percent(
126
        float $fraction,
127
        ?int $decimals = null,
128
        ?string $prefix = null,
129
        string $suffix = '%'
130
    ): string {
131
        return
132 28
            ($prefix ?? '') .
133 28
            number_format(
134 28
                $fraction * 100,
135 28
                static::refineDecimals($decimals),
136 28
                static::$decimalPoint ?? static::DECIMAL_POINT,
137 28
                static::$thousandsSeparator ?? static::THOUSANDS_SEPARATOR
138
            ) .
139 28
            $suffix;
140
    }
141
142
    /**
143
     * @param string $decimalPoint
144
     */
145 14
    public static function setDecimalPoint(string $decimalPoint): void
146
    {
147 14
        self::$decimalPoint = $decimalPoint;
148 14
    }
149
150
    /**
151
     * @param string $thousandsSeparator
152
     */
153 14
    public static function setThousandsSeparator(string $thousandsSeparator): void
154
    {
155 14
        self::$thousandsSeparator = $thousandsSeparator;
156 14
    }
157
158
    /**
159
     * @param int $maxDecimals
160
     */
161 7
    public static function setMaxDecimals(int $maxDecimals): void
162
    {
163 7
        self::$maxDecimals = abs($maxDecimals);
164 7
    }
165
166 21
    public static function resetDecimalPoint(): void
167
    {
168 21
        self::$decimalPoint = null;
169 21
    }
170
171 21
    public static function resetThousandsSeparator(): void
172
    {
173 21
        self::$thousandsSeparator = null;
174 21
    }
175
176 7
    public static function resetMaxDecimals(): void
177
    {
178 7
        self::$maxDecimals = null;
179 7
    }
180
}
181