Passed
Push — master ( d12c1b...36b4d9 )
by Alec
02:02
created

MemoryUsage   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 61
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setFormatter() 0 3 1
A getPeak() 0 4 1
A get() 0 4 1
A getFormatter() 0 6 2
A reportStatic() 0 4 1
A createEmptyReport() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Accessories;
4
5
use AlecRabbit\Accessories\MemoryUsage\MemoryUsageReport;
6
use AlecRabbit\Accessories\MemoryUsage\MemoryUsageReportFormatter;
7
use AlecRabbit\Reports\Contracts\ReportInterface;
8
use AlecRabbit\Reports\Core\Reportable;
9
10
class MemoryUsage extends Reportable
11
{
12
    /** @var null|MemoryUsageReportFormatter */
13
    protected static $formatter;
14
15
    /**
16
     * @param bool $real
17
     * @param null|string $unit
18
     * @param int|null $decimals
19
     * @return string
20
     */
21 2
    public static function get(bool $real = false, ?string $unit = null, ?int $decimals = null): string
22
    {
23
        return
24 2
            Pretty::bytes(memory_get_usage($real), $unit, $decimals);
25
    }
26
27
    /**
28
     * @param bool $real
29
     * @param null|string $unit
30
     * @param int|null $decimals
31
     * @return string
32
     */
33 2
    public static function getPeak(bool $real = false, ?string $unit = null, ?int $decimals = null): string
34
    {
35
        return
36 2
            Pretty::bytes(memory_get_peak_usage($real), $unit, $decimals);
37
    }
38
39
    /**
40
     * @return MemoryUsageReport
41
     */
42 5
    public static function reportStatic(): MemoryUsageReport
43
    {
44
        return
45 5
            new MemoryUsageReport();
46
    }
47
48
    /**
49
     * @return MemoryUsageReportFormatter
50
     */
51 9
    public static function getFormatter(): MemoryUsageReportFormatter
52
    {
53 9
        if (null === static::$formatter) {
54 1
            static::$formatter = new MemoryUsageReportFormatter();
55
        }
56 9
        return static::$formatter;
57
    }
58
59
    /**
60
     * @param null|MemoryUsageReportFormatter $formatter
61
     */
62 2
    public static function setFormatter(?MemoryUsageReportFormatter $formatter): void
63
    {
64 2
        self::$formatter = $formatter;
65 2
    }
66
67 1
    protected function createEmptyReport(): ReportInterface
68
    {
69
        return
70 1
            static::reportStatic();
71
    }
72
}
73