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.
Completed
Push — develop ( e41626...d433ef )
by Baptiste
02:32
created

UnixProcesses   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Test Coverage

Coverage 92.16%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 9
eloc 52
c 3
b 1
f 1
dl 0
loc 98
ccs 47
cts 51
cp 0.9216
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 45 2
A get() 0 17 3
A run() 0 10 2
A __construct() 0 3 1
A all() 0 4 1
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\TimeContinuumInterface;
17
use Innmind\Immutable\{
18
    MapInterface,
19
    Str,
20
    StreamInterface,
21
    Sequence,
22
    Map
23
};
24
use Symfony\Component\Process\Process as SfProcess;
25
26
final class UnixProcesses implements Processes
27
{
28
    private $clock;
29
30 51
    public function __construct(TimeContinuumInterface $clock)
31
    {
32 51
        $this->clock = $clock;
33 51
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 3
    public function all(): MapInterface
39
    {
40 3
        return $this->parse(
41 3
            $this->run('ps aux')
42
        );
43
    }
44
45 6
    public function get(Pid $pid): Process
46
    {
47
        try {
48 6
            $processes = $this->parse(
49 6
                $this->run(sprintf('ps ux -p %s', $pid))
50
            );
51
        } catch (InformationNotAccessible $e) {
52
            $processes = $this->parse(
53
                $this->run(sprintf('ps ux -q %s', $pid))
54
            );
55
        }
56
57 6
        if (!$processes->contains($pid->toInt())) {
58 3
            throw new InformationNotAccessible;
59
        }
60
61 3
        return $processes->get($pid->toInt());
62
    }
63
64 9
    private function run(string $command): Str
65
    {
66 9
        $process = new SfProcess($command);
0 ignored issues
show
Bug introduced by
$command of type string is incompatible with the type array expected by parameter $command of Symfony\Component\Process\Process::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
        $process = new SfProcess(/** @scrutinizer ignore-type */ $command);
Loading history...
67 9
        $process->run();
68
69 9
        if (!$process->isSuccessful()) {
70
            throw new InformationNotAccessible;
71
        }
72
73 9
        return new Str($process->getOutput());
74
    }
75
76
    /**
77
     * @return MapInterface<int, Process>
78
     */
79 9
    private function parse(Str $output): MapInterface
80
    {
81
        $lines = $output
82 9
            ->trim()
83 9
            ->split("\n");
84
        $columns = $lines
85 9
            ->first()
86 9
            ->pregSplit('~ +~')
87 9
            ->reduce(
88 9
                new Sequence,
89
                static function(Sequence $columns, Str $column): Sequence {
90 9
                    return $columns->add((string) $column);
91 9
                }
92
            );
93
94
        return $lines
95 9
            ->drop(1)
96 9
            ->reduce(
97 9
                new Sequence,
98
                static function(Sequence $lines, Str $line) use ($columns): Sequence {
99 9
                    return $lines->add(
100 9
                        $line->pregSplit('~ +~', $columns->size())
101
                    );
102 9
                }
103
            )
104
            ->map(function(StreamInterface $parts) use ($columns): Process {
105 9
                return new Process(
106 9
                    new Pid((int) (string) $parts->get($columns->indexOf('PID'))),
107 9
                    new User((string) $parts->get($columns->indexOf('USER'))),
108 9
                    new Percentage((float) (string) $parts->get($columns->indexOf('%CPU'))),
109 9
                    new Memory((float) (string) $parts->get($columns->indexOf('%MEM'))),
110 9
                    $this->clock->at(
111 9
                        (string) $parts->get(
112 9
                            $columns->indexOf(PHP_OS === 'Linux' ? 'START' : 'STARTED')
113
                        )
114
                    ),
115 9
                    new Command((string) $parts->get($columns->indexOf('COMMAND')))
116
                );
117 9
            })
118 9
            ->reduce(
119 9
                new Map('int', Process::class),
120
                static function(Map $processes, Process $process): Map {
121 9
                    return $processes->put(
122 9
                        $process->pid()->toInt(),
123 6
                        $process
124
                    );
125 9
                }
126
            );
127
    }
128
}
129