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

MemoryContributor   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 22.45 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 56.25%

Importance

Changes 0
Metric Value
dl 11
loc 49
c 0
b 0
f 0
wmc 6
lcom 1
cbo 0
ccs 9
cts 16
cp 0.5625
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 7 1
A getMemoryFree() 0 4 1
A getMemoryAllocated() 0 4 1
A getMemoryUsage() 0 4 1
A formatBytes() 11 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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