Passed
Push — master ( 3c333b...51dbda )
by Alec
01:37
created

MemoryUsage::getFormatter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Accessories;
4
5
use AlecRabbit\Accessories\MemoryUsage\Contracts\MemoryUsageReportFormatterInterface;
6
use AlecRabbit\Accessories\MemoryUsage\MemoryUsageReport;
7
use AlecRabbit\Accessories\MemoryUsage\MemoryUsageReportFormatter;
8
9
class MemoryUsage
10
{
11
    /** @var null|MemoryUsageReportFormatterInterface */
12
    protected static $formatter;
13
14
    /**
15
     * Static class. Private Constructor.
16
     */
17
    private function __construct() // @codeCoverageIgnoreStart
18
    {
19
    } // @codeCoverageIgnoreEnd
20
21
    /**
22
     * @param bool $real
23
     * @param null|string $unit
24
     * @param int|null $decimals
25
     * @return string
26
     */
27 2
    public static function get(bool $real = false, ?string $unit = null, ?int $decimals = null): string
28
    {
29
        return
30 2
            Pretty::bytes(memory_get_usage($real), $unit, $decimals);
31
    }
32
33
    /**
34
     * @param bool $real
35
     * @param null|string $unit
36
     * @param int|null $decimals
37
     * @return string
38
     */
39 2
    public static function getPeak(bool $real = false, ?string $unit = null, ?int $decimals = null): string
40
    {
41
        return
42 2
            Pretty::bytes(memory_get_peak_usage($real), $unit, $decimals);
43
    }
44
45
    /**
46
     * @return MemoryUsageReport
47
     */
48 4
    public static function report(): MemoryUsageReport
49
    {
50
        return
51 4
            new MemoryUsageReport(
52 4
                memory_get_usage(),
53 4
                memory_get_peak_usage(),
54 4
                memory_get_usage(true),
55 4
                memory_get_peak_usage(true)
56
            );
57
    }
58
59
    /**
60
     * @return MemoryUsageReportFormatterInterface
61
     */
62 7
    public static function getFormatter(): MemoryUsageReportFormatterInterface
63
    {
64 7
        if (null === static::$formatter) {
65 1
            static::$formatter = new MemoryUsageReportFormatter();
66
        }
67 7
        return static::$formatter;
68
    }
69
70
    /**
71
     * @param null|MemoryUsageReportFormatterInterface $formatter
72
     */
73 2
    public static function setFormatter(?MemoryUsageReportFormatterInterface $formatter): void
74
    {
75 2
        self::$formatter = $formatter;
76 2
    }
77
}
78