Test Failed
Push — master ( 068d94...d12c1b )
by Alec
07:11
created

MemoryUsageReport::buildOn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Accessories\MemoryUsage;
4
5
use AlecRabbit\Accessories\MemoryUsage;
6
use AlecRabbit\Reports\Contracts\ReportableInterface;
7
use AlecRabbit\Reports\Contracts\ReportInterface;
8
use AlecRabbit\Reports\Core\AbstractReport;
9
10
class MemoryUsageReport extends AbstractReport implements MemoryUsageReportInterface
11
{
12
    /** @var int */
13
    protected $usage;
14
15
    /** @var int */
16
    protected $peakUsage;
17
18
    /** @var int */
19
    protected $usageReal;
20
21
    /** @var int */
22
    protected $peakUsageReal;
23
24
    /**
25
     * MemoryUsageReport constructor.
26
     * @param int $usage
27
     * @param int $peakUsage
28 5
     * @param int $usageReal
29
     * @param int $peakUsageReal
30 5
     */
31 5
    public function __construct(
32 5
        int $usage = null,
33 5
        int $peakUsage = null,
34 5
        int $usageReal = null,
35
        int $peakUsageReal = null
36 3
    ) {
37
        $this->usage = $usage ?? memory_get_usage();
38 3
        $this->peakUsage = $peakUsage ?? memory_get_peak_usage();
39
        $this->usageReal = $usageReal ?? memory_get_usage(true);
40
        $this->peakUsageReal = $peakUsageReal ?? memory_get_peak_usage(true);
41
    }
42
43
    public function __toString(): string
44 5
    {
45
        return MemoryUsage::getFormatter()->format($this);
46 5
    }
47
48
    public function buildOn(ReportableInterface $reportable): ReportInterface
49
    {
50
        if ($reportable instanceof MemoryUsage) {
51
            return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type AlecRabbit\Accessories\M...Usage\MemoryUsageReport which is incompatible with the type-hinted return AlecRabbit\Reports\Contracts\ReportInterface.
Loading history...
52 5
        }
53
        throw new \InvalidArgumentException(
54 5
            MemoryUsage::class . ' expected, ' . get_class($reportable) . ' given.'
55
        );
56
    }
57
58
    /**
59
     * {@inheritdoc}
60 5
     */
61
    public function getUsage(): int
62 5
    {
63
        return $this->usage;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68 5
     */
69
    public function getPeakUsage(): int
70 5
    {
71
        return $this->peakUsage;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76 1
     */
77
    public function getUsageReal(): int
78
    {
79 1
        return $this->usageReal;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 1
    public function getPeakUsageReal(): int
86
    {
87
        return $this->peakUsageReal;
88 1
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getUsageString(?string $unit = null, ?int $decimals = null): string
94 1
    {
95
        return
96
            MemoryUsage::getFormatter()->getUsageString($this, $unit, $decimals);
97 1
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getPeakUsageString(?string $unit = null, ?int $decimals = null): string
103 1
    {
104
        return
105
            MemoryUsage::getFormatter()->getPeakUsageString($this, $unit, $decimals);
106 1
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function getUsageRealString(?string $unit = null, ?int $decimals = null): string
112
    {
113
        return
114
            MemoryUsage::getFormatter()->getUsageRealString($this, $unit, $decimals);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function getPeakUsageRealString(?string $unit = null, ?int $decimals = null): string
121
    {
122
        return
123
            MemoryUsage::getFormatter()->getPeakUsageRealString($this, $unit, $decimals);
124
    }
125
}
126