Conditions | 2 |
Paths | 2 |
Total Lines | 21 |
Code Lines | 13 |
Lines | 21 |
Ratio | 100 % |
Tests | 0 |
CRAP Score | 6 |
Changes | 0 |
1 | <?php |
||
16 | public function __invoke(): Cpu |
||
17 | { |
||
18 | $process = new Process('top -bn1 | grep \'%Cpu\''); |
||
19 | $process->run(); |
||
20 | |||
21 | if (!$process->isSuccessful()) { |
||
22 | throw new CpuUsageNotAccessible; |
||
23 | } |
||
24 | |||
25 | $percentages = (new Str($process->getOutput())) |
||
26 | ->trim() |
||
27 | ->capture( |
||
28 | '~^%Cpu(s): (?P<user>\d+\.?\d*) us, (?P<sys>\d+\.?\d*) sy, (\d+\.?\d*) ni, (?P<idle>\d+\.?\d*) id~' |
||
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.