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

UnixDisk::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 1

Importance

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

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\Disk;
5
6
use Innmind\Server\Status\{
7
    Server\Disk,
8
    Server\Disk\Volume\MountPoint,
9
    Server\Disk\Volume\Usage,
10
    Server\Memory\Bytes,
11
    Exception\DiskUsageNotAccessible
12
};
13
use Innmind\Immutable\{
14
    MapInterface,
15
    Str,
16
    StreamInterface,
17
    Sequence,
18
    Map
19
};
20
use Symfony\Component\Process\Process;
21
22
final class UnixDisk implements Disk
23
{
24
    private static $columns = [
25
        'Size' => 'size',
26
        'Used' => 'used',
27
        'Avail' => 'available',
28
        'Use%' => 'usage',
29
        'Capacity' => 'usage',
30
        'Mounted' => 'mountPoint',
31
    ];
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 6
    public function volumes(): MapInterface
37
    {
38 6
        return $this->parse(
39 6
            $this->run('df -lh')
40
        );
41
    }
42
43 3
    public function get(MountPoint $point): Volume
44
    {
45
        return $this
46 3
            ->volumes()
47 3
            ->get((string) $point);
48
    }
49
50 6
    private function run(string $command): Str
51
    {
52 6
        $process = new Process($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

52
        $process = new Process(/** @scrutinizer ignore-type */ $command);
Loading history...
53 6
        $process->run();
54
55 6
        if (!$process->isSuccessful()) {
56
            throw new DiskUsageNotAccessible;
57
        }
58
59 6
        return new Str($process->getOutput());
60
    }
61
62
    /**
63
     * @return MapInterface<string, Volume>
64
     */
65 6
    private function parse(Str $output): MapInterface
66
    {
67
        $lines = $output
68 6
            ->trim()
69 6
            ->split("\n");
70
        $columns = $lines
71 6
            ->first()
72 6
            ->pregSplit('~ +~')
73 6
            ->reduce(
74 6
                new Sequence,
75
                static function(Sequence $columns, Str $column): Sequence {
76 6
                    $column = (string) $column;
77
78 6
                    return $columns->add(self::$columns[$column] ?? $column);
79 6
                }
80
            );
81
82
        return $lines
83 6
            ->drop(1)
84 6
            ->reduce(
85 6
                new Sequence,
86
                static function(Sequence $lines, Str $line) use ($columns): Sequence {
87 6
                    return $lines->add(
88 6
                        $line->pregSplit('~ +~', $columns->size())
89
                    );
90 6
                }
91
            )
92
            ->map(function(StreamInterface $parts) use ($columns): Volume {
93 6
                return new Volume(
94 6
                    new MountPoint(
95 6
                        (string) $parts->get($columns->indexOf('mountPoint'))
96
                    ),
97 6
                    Bytes::of(
98 6
                        (string) $parts->get($columns->indexOf('size'))
99
                    ),
100 6
                    Bytes::of(
101 6
                        (string) $parts->get($columns->indexOf('available'))
102
                    ),
103 6
                    Bytes::of(
104 6
                        (string) $parts->get($columns->indexOf('used'))
105
                    ),
106 6
                    new Usage(
107 6
                        (float) (string) $parts->get($columns->indexOf('usage'))
108
                    )
109
                );
110 6
            })
111 6
            ->reduce(
112 6
                new Map('string', Volume::class),
113
                static function(Map $volumes, Volume $volume): Map {
114 6
                    return $volumes->put(
115 6
                        (string) $volume->mountPoint(),
116 4
                        $volume
117
                    );
118 6
                }
119
            );
120
    }
121
}
122