MemoryUsageReportFormatter::assertUnits()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Accessories\MemoryUsage;
4
5
use AlecRabbit\Accessories\MemoryUsage\Contracts\MemoryUsageConstants;
6
use AlecRabbit\Accessories\Pretty;
7
use AlecRabbit\Formatters\Core\AbstractFormatter;
8
use AlecRabbit\Reports\Core\Formattable;
9
use function AlecRabbit\Helpers\bounds;
10
use function AlecRabbit\str_wrap;
11
use const AlecRabbit\Helpers\Strings\Constants\BYTES_UNITS;
12
13
class MemoryUsageReportFormatter extends AbstractFormatter implements MemoryUsageConstants
14
{
15
    /** @var null|array */
16
    protected static $unitsArray;
17
18
    /** @var string */
19
    protected $units = 'MB';
20
21
    /** @var int */
22
    protected $decimals = 2;
23
24
    /** {@inheritDoc} */
25 11
    public function __construct(?int $options = null)
26
    {
27 11
        parent::__construct($options);
28 11
        $this->options = $options ?? static::OPTION_SHOW_REAL_USAGE;
29 11
    }
30
31
    /**
32
     * @param string $units
33
     */
34 2
    public function setUnits(string $units): void
35
    {
36 2
        $this->units = $this->refineUnits($units);
37 1
    }
38
39
    /**
40
     * @param null|string $units
41
     * @return string
42
     */
43 4
    protected function refineUnits(?string $units): string
44
    {
45 4
        $units = $units ?? $this->units;
46 4
        $this->assertUnits($units);
47 3
        return $units;
48
    }
49
50
    /**
51
     * @param string $units
52
     */
53 4
    protected function assertUnits(string $units): void
54
    {
55 4
        if (false === array_key_exists(strtoupper($units), BYTES_UNITS)) {
56 1
            throw new \RuntimeException(
57 1
                'Unsupported units: "' . $units . '"'
58
            );
59
        }
60 3
    }
61
62
    /**
63
     * @param int $decimals
64
     */
65 1
    public function setDecimals(int $decimals): void
66
    {
67 1
        $this->decimals = $this->refineDecimals($decimals);
68 1
    }
69
70
    /**
71
     * @param null|int $decimals
72
     * @return int
73
     */
74 3
    protected function refineDecimals(?int $decimals): int
75
    {
76 3
        return (int)bounds($decimals ?? $this->decimals, 0, self::MAX_DECIMALS);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 5
    public function format(Formattable $report): string
83
    {
84 5
        if ($report instanceof MemoryUsageReport) {
85 4
            $diffStr = $this->getDiffStr($report);
86
            return
87 4
                sprintf(
88 4
                    self::STRING_FORMAT,
89 4
                    Pretty::bytes($report->getUsage(), $this->units, $this->decimals),
90 4
                    Pretty::bytes($report->getUsageReal(), $this->units, $this->decimals),
91 4
                    Pretty::bytes($report->getPeakUsage(), $this->units, $this->decimals),
92 4
                    Pretty::bytes($report->getPeakUsageReal(), $this->units, $this->decimals)
93 4
                ) . $diffStr;
94
        }
95 1
        return $this->errorMessage($report, MemoryUsageReport::class);
96
    }
97
98
    /**
99
     * @param MemoryUsageReport $report
100
     * @return string
101
     */
102 4
    protected function getDiffStr(MemoryUsageReport $report): string
103
    {
104 4
        if ((null === $usageDiff = $report->getUsageDiff()) || (0 === $usageDiff)) {
105 4
            return '';
106
        }
107
        return
108 1
            ' ' . sprintf(
109 1
                self::DIFF_FORMAT,
110 1
                $this->prepareBytesString($usageDiff, ' '),
111 1
                $this->prepareBytesString($report->getUsageRealDiff(), '(', ')'),
0 ignored issues
show
Bug introduced by
It seems like $report->getUsageRealDiff() can also be of type null; however, parameter $value of AlecRabbit\Accessories\M...r::prepareBytesString() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

111
                $this->prepareBytesString(/** @scrutinizer ignore-type */ $report->getUsageRealDiff(), '(', ')'),
Loading history...
112 1
                $this->prepareBytesString($report->getPeakUsageDiff(), ' '),
113 1
                $this->prepareBytesString($report->getPeakUsageRealDiff(), '(', ')')
114
            );
115
    }
116
117 1
    protected function prepareBytesString(int $value, string $open = '', string $close = ''): string
118
    {
119 1
        return 0 === $value ? '' : str_wrap(Pretty::bytes($value), $open, $close);
120
    }
121
122
    /**
123
     * @param int $value
124
     * @param null|string $units
125
     * @param null|int $decimals
126
     * @return string
127
     */
128 2
    public function getString(int $value, ?string $units = null, ?int $decimals = null): string
129
    {
130
        return
131 2
            Pretty::bytes($value, $this->refineUnits($units), $this->refineDecimals($decimals));
132
    }
133
}
134