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 ( 246022...160f54 )
by Baptiste
03:38
created

UnixProcesses   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 103
Duplicated Lines 10.68 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 92.45%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 14
dl 11
loc 103
ccs 49
cts 53
cp 0.9245
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A all() 0 6 1
A get() 0 18 3
A run() 11 11 2
A parse() 0 49 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 12
    public function __construct(TimeContinuumInterface $clock)
31
    {
32 12
        $this->clock = $clock;
33 12
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 1
    public function all(): MapInterface
39
    {
40 1
        return $this->parse(
41 1
            $this->run('ps aux')
42
        );
43
    }
44
45 2
    public function get(Pid $pid): Process
46
    {
47
        try {
48 2
            $processes = $this->parse(
49 2
                $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 2
        if (!$processes->contains($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...
58 1
            throw new InformationNotAccessible;
59
        }
60
61 1
        return $processes->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...
62
    }
63
64 3 View Code Duplication
    private function run(string $command): Str
65
    {
66 3
        $process = new SfProcess($command);
67 3
        $process->run();
68
69 3
        if (!$process->isSuccessful()) {
70
            throw new InformationNotAccessible;
71
        }
72
73 3
        return new Str($process->getOutput());
74
    }
75
76
    /**
77
     * @return MapInterface<int, Process>
78
     */
79 3
    private function parse(Str $output): MapInterface
80
    {
81
        $lines = $output
82 3
            ->trim()
83 3
            ->split("\n");
84
        $columns = $lines
85 3
            ->first()
86 3
            ->pregSplit('~ +~')
87 3
            ->reduce(
88 3
                new Sequence,
89
                static function(Sequence $columns, Str $column): Sequence {
90 3
                    return $columns->add((string) $column);
91 3
                }
92
            );
93
94
        return $lines
95 3
            ->drop(1)
96 3
            ->reduce(
97 3
                new Sequence,
98
                static function(Sequence $lines, Str $line) use ($columns): Sequence {
99 3
                    return $lines->add(
100 3
                        $line->pregSplit('~ +~', $columns->size())
101
                    );
102 3
                }
103
            )
104
            ->map(function(StreamInterface $parts) use ($columns): Process {
105 3
                return new Process(
106 3
                    new Pid((int) (string) $parts->get($columns->indexOf('PID'))),
107 3
                    new User((string) $parts->get($columns->indexOf('USER'))),
108 3
                    new Percentage((float) (string) $parts->get($columns->indexOf('%CPU'))),
109 3
                    new Memory((float) (string) $parts->get($columns->indexOf('%MEM'))),
110 3
                    $this->clock->at(
111 3
                        (string) $parts->get(
112 3
                            $columns->indexOf(PHP_OS === 'Linux' ? 'START' : 'STARTED')
113
                        )
114
                    ),
115 3
                    new Command((string) $parts->get($columns->indexOf('COMMAND')))
116
                );
117 3
            })
118 3
            ->reduce(
119 3
                new Map('int', Process::class),
120 3
                static function(Map $processes, Process $process): Map {
121 3
                    return $processes->put(
122 3
                        $process->pid()->toInt(),
123 3
                        $process
124
                    );
125 3
                }
126
            );
127
    }
128
}
129