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 ( 9ca8cc...2705d1 )
by Baptiste
03:06
created

UnixProcesses::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 11
loc 11
c 0
b 0
f 0
ccs 5
cts 6
cp 0.8333
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
crap 2.0185
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 9
    public function __construct(TimeContinuumInterface $clock)
31
    {
32 9
        $this->clock = $clock;
33 9
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function all(): MapInterface
39
    {
40
        return $this->parse(
41
            $this->run('ps aux')
42
        );
43
    }
44
45 1
    public function get(Pid $pid): Process
46
    {
47 1
        $flag = PHP_OS === 'Linux' ? 'q' : 'p';
48
49
        return $this
50 1
            ->parse($this->run(sprintf('ps ux -%s %s', $flag, $pid)))
51
            ->get($pid->toInt());
1 ignored issue
show
Documentation introduced by
$pid->toInt() is of type integer, but the function expects a object<Innmind\Immutable\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
    }
53
54 1 View Code Duplication
    private function run(string $command): Str
1 ignored issue
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...
55
    {
56 1
        $process = new SfProcess($command);
57 1
        $process->run();
58
59 1
        if (!$process->isSuccessful()) {
60 1
            throw new InformationNotAccessible;
61
        }
62
63
        return new Str($process->getOutput());
64
    }
65
66
    /**
67
     * @return MapInterface<int, Process>
1 ignored issue
show
Documentation introduced by
The doc-type MapInterface<int, could not be parsed: Expected "|" or "end of type", but got "<" at position 12. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
68
     */
69
    private function parse(Str $output): MapInterface
70
    {
71
        $lines = $output
72
            ->trim()
73
            ->split("\n");
74
        $columns = $lines
75
            ->first()
76
            ->pregSplit('~ +~')
77
            ->reduce(
78
                new Sequence,
79
                static function(Sequence $columns, Str $column): Sequence {
80
                    return $columns->add((string) $column);
81
                }
82
            );
83
84
        return $lines
85
            ->drop(1)
86
            ->reduce(
87
                new Sequence,
88
                static function(Sequence $lines, Str $line) use ($columns): Sequence {
89
                    return $lines->add(
90
                        $line->pregSplit('~ +~', $columns->size())
91
                    );
92
                }
93
            )
94
            ->map(function(StreamInterface $parts) use ($columns): Process {
95
                return new Process(
96
                    new Pid((int) (string) $parts->get($columns->indexOf('PID'))),
97
                    new User((string) $parts->get($columns->indexOf('USER'))),
98
                    new Percentage((float) (string) $parts->get($columns->indexOf('%CPU'))),
99
                    new Memory((float) (string) $parts->get($columns->indexOf('%MEM'))),
100
                    $this->clock->at((string) $parts->get($columns->indexOf('STARTED'))),
101
                    new Command((string) $parts->get($columns->indexOf('COMMAND')))
102
                );
103
            })
104
            ->reduce(
105
                new Map('int', Process::class),
106
                static function(Map $processes, Process $process): Map {
107
                    return $processes->put(
108
                        $process->pid()->toInt(),
1 ignored issue
show
Documentation introduced by
$process->pid()->toInt() is of type integer, but the function expects a object<Innmind\Immutable\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
109
                        $process
1 ignored issue
show
Documentation introduced by
$process is of type object<Innmind\Server\Status\Server\Process>, but the function expects a object<Innmind\Immutable\S>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
110
                    );
111
                }
112
            );
113
    }
114
}
115