Server   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 95
Duplicated Lines 25.26 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 1
dl 24
loc 95
ccs 44
cts 44
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getCpuUsage() 0 10 2
B getProcesses() 0 27 4
A getProcessUsage() 12 12 3
A getProcessCount() 12 12 3
A getLoadAverage() 0 4 1
A getCurrentLoadAverage() 0 4 1

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
namespace Innmind\ProvisionerBundle\Server;
4
5
use Symfony\Component\Process\Process;
6
7
/**
8
 * Helper to retrieve information about the server usage (cpu and load average)
9
 */
10
class Server implements ServerInterface
11
{
12
    /**
13
     * {@inheritdoc}
14
     */
15 3
    public function getCpuUsage()
16
    {
17 3
        $usage = 0;
18
19 3
        foreach ($this->getProcesses() as $process) {
20 3
            $usage += $process['usage'];
21 3
        }
22
23 3
        return $usage;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 12
    public function getProcesses()
30
    {
31 12
        $processes = [];
32 12
        $process = new Process('ps -eo pcpu,command');
33 12
        $process->run();
34 12
        $output = $process->getOutput();
35 12
        $output = explode("\n", $output);
36
37 12
        foreach ($output as $line) {
38 12
            $line = trim($line);
39
40 12
            if (empty($line)) {
41 12
                continue;
42
            }
43
44 12
            list($cpu, $process) = explode(' ', $line, 2);
45
46 12
            if (is_numeric($cpu)) {
47 12
                $processes[] = [
48 12
                    'usage' => (float) $cpu,
49
                    'name' => $process
50 12
                ];
51 12
            }
52 12
        }
53
54 12
        return $processes;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 3 View Code Duplication
    public function getProcessUsage($processName)
0 ignored issues
show
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...
61
    {
62 3
        $usage = 0;
63
64 3
        foreach ($this->getProcesses() as $process) {
65 3
            if (strpos($process['name'], $processName) !== false) {
66 3
                $usage += $process['usage'];
67 3
            }
68 3
        }
69
70 3
        return $usage;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 3 View Code Duplication
    public function getProcessCount($command)
0 ignored issues
show
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...
77
    {
78 3
        $count = 0;
79
80 3
        foreach ($this->getProcesses() as $process) {
81 3
            if (strpos($process['name'], $command) !== false) {
82 3
                $count++;
83 3
            }
84 3
        }
85
86 3
        return $count;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92 6
    public function getLoadAverage()
93
    {
94 6
        return sys_getloadavg();
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 3
    public function getCurrentLoadAverage()
101
    {
102 3
        return $this->getLoadAverage()[0];
103
    }
104
}
105