Completed
Push — master ( ff6903...445499 )
by Nicolas
01:40
created

MemoryContributor::getMemoryFree()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nwidart\Actuator\Health;
6
7
class MemoryContributor implements HealthContributor
8
{
9 1
    public function run(): array
10
    {
11
        return [
12 1
            'free' => $this->getMemoryFree(),
13 1
            'memory' => $this->getMemoryAllocated(),
14
        ];
15
    }
16
17
    /**
18
     * Return the memory free in kbytes
19
     */
20 1
    private function getMemoryFree(): int
21
    {
22 1
        return $this->getMemoryAllocated() - $this->getMemoryUsage();
23
    }
24
25
    /**
26
     * Return the maximum memory allocated to php in kbytes
27
     */
28 1
    private function getMemoryAllocated(): int
29
    {
30 1
        return ((int) ini_get('memory_limit')) * 1024;
31
    }
32
33
    /**
34
     * Return the memory usage in kbytes
35
     */
36 1
    private function getMemoryUsage(): int
37
    {
38 1
        return memory_get_usage(true) / 1024;
39
    }
40
41
    /*
42
    * Source https://stackoverflow.com/questions/2510434/format-bytes-to-kilobytes-megabytes-gigabytes
43
    */
44 View Code Duplication
    private function formatBytes($bytes, $precision = 2): string
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        $units = ['B', 'KB', 'MB', 'GB', 'TB'];
47
48
        $bytes = max($bytes, 0);
49
        $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
50
        $pow = min($pow, count($units) - 1);
51
        $bytes /= (1 << (10 * $pow));
52
53
        return round($bytes, $precision) . ' ' . $units[$pow];
54
    }
55
}
56