GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

UnixProcesses::parse()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 51
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 2
eloc 36
c 2
b 1
f 1
nc 1
nop 1
dl 0
loc 51
rs 9.344
ccs 31
cts 31
cp 1
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Server\Status\Server\Processes;
5
6
use Innmind\Server\Status\{
7
    Server\Processes,
8
    Server\Process,
9
    Server\Process\Pid,
10
    Server\Process\User,
11
    Server\Process\Command,
12
    Server\Process\Memory,
13
    Server\Cpu\Percentage,
14
    Exception\InformationNotAccessible,
15
};
16
use Innmind\TimeContinuum\Clock;
17
use Innmind\Immutable\{
18
    Str,
19
    Sequence,
20
    Map,
21
};
22
use Symfony\Component\Process\Process as SfProcess;
23
24
final class UnixProcesses implements Processes
25
{
26
    private Clock $clock;
27
28
    public function __construct(Clock $clock)
29
    {
30 51
        $this->clock = $clock;
31
    }
32 51
33 51
    /**
34
     * {@inheritdoc}
35
     */
36
    public function all(): Map
37
    {
38 3
        return $this->parse(
39
            $this->run('ps aux'),
40 3
        );
41 3
    }
42
43
    public function get(Pid $pid): Process
44
    {
45 6
        try {
46
            $processes = $this->parse(
47
                $this->run(\sprintf('ps ux -p %s', $pid->toString())),
48 6
            );
49 6
        } catch (InformationNotAccessible $e) {
50
            $processes = $this->parse(
51
                $this->run(\sprintf('ps ux -q %s', $pid->toString())),
52
            );
53
        }
54
55
        if (!$processes->contains($pid->toInt())) {
56
            throw new InformationNotAccessible;
57 6
        }
58 3
59
        return $processes->get($pid->toInt());
60
    }
61 3
62
    private function run(string $command): Str
63
    {
64 9
        $process = SfProcess::fromShellCommandline($command);
65
        $process->run();
66 9
67 9
        if (!$process->isSuccessful()) {
68
            throw new InformationNotAccessible;
69 9
        }
70
71
        return Str::of($process->getOutput());
72
    }
73 9
74
    /**
75
     * @return Map<int, Process>
76
     */
77
    private function parse(Str $output): Map
78
    {
79 9
        $lines = $output
80
            ->trim()
81
            ->split("\n");
82 9
        $columns = $lines
83 9
            ->first()
84
            ->pregSplit('~ +~')
85 9
            ->reduce(
86 9
                Sequence::strings(),
87 9
                static function(Sequence $columns, Str $column): Sequence {
88 9
                    return ($columns)($column->toString());
89
                },
90 9
            );
91 9
92
        /** @var Sequence<Sequence<Str>> */
93
        $partsByLine = $lines
94
            ->drop(1)
95 9
            ->reduce(
96 9
                Sequence::of(Sequence::class),
97 9
                static function(Sequence $lines, Str $line) use ($columns): Sequence {
98
                    return ($lines)(
99 9
                        $line->pregSplit('~ +~', $columns->size()),
100 9
                    );
101
                },
102 9
            );
103
        $processes = $partsByLine->mapTo(
104
            Process::class,
105 9
            function(Sequence $parts) use ($columns): Process {
106 9
                return new Process(
107 9
                    new Pid((int) $parts->get($columns->indexOf('PID'))->toString()),
108 9
                    new User($parts->get($columns->indexOf('USER'))->toString()),
109 9
                    new Percentage((float) $parts->get($columns->indexOf('%CPU'))->toString()),
110 9
                    new Memory((float) $parts->get($columns->indexOf('%MEM'))->toString()),
111 9
                    $this->clock->at(
112 9
                        $parts->get(
113
                            $columns->indexOf(PHP_OS === 'Linux' ? 'START' : 'STARTED'),
114
                        )->toString(),
115 9
                    ),
116
                    new Command($parts->get($columns->indexOf('COMMAND'))->toString()),
117 9
                );
118 9
            },
119 9
        );
120
121 9
        /** @var Map<int, Process> */
122 9
        return $processes->reduce(
123 6
            Map::of('int', Process::class),
124
            static function(Map $processes, Process $process): Map {
125 9
                return ($processes)(
126
                    $process->pid()->toInt(),
127
                    $process,
128
                );
129
            },
130
        );
131
    }
132
}
133