Conditions | 2 |
Paths | 2 |
Total Lines | 21 |
Code Lines | 13 |
Lines | 21 |
Ratio | 100 % |
Tests | 5 |
CRAP Score | 2.9322 |
Changes | 0 |
1 | <?php |
||
16 | 1 | public function __invoke(): Cpu |
|
17 | { |
||
18 | 1 | $process = new Process('top -l 1 -s 0 | grep \'CPU usage\''); |
|
19 | 1 | $process->run(); |
|
20 | |||
21 | 1 | if (!$process->isSuccessful()) { |
|
22 | 1 | throw new CpuUsageNotAccessible; |
|
23 | } |
||
24 | |||
25 | $percentages = (new Str($process->getOutput())) |
||
26 | ->trim() |
||
27 | ->capture( |
||
28 | '~^CPU usage: (?P<user>\d+\.?\d*)% user, (?P<sys>\d+\.?\d*)% sys, (?P<idle>\d+\.?\d*)% idle$~' |
||
29 | ); |
||
30 | |||
31 | return new Cpu( |
||
32 | new Percentage((float) (string) $percentages->get('user')), |
||
33 | new Percentage((float) (string) $percentages->get('sys')), |
||
34 | new Percentage((float) (string) $percentages->get('idle')) |
||
35 | ); |
||
36 | } |
||
37 | } |
||
38 |
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.