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 ( 78c84d...9bdc70 )
by Baptiste
04:48
created

UnixDisk   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 100
Duplicated Lines 11 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 97.87%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 11
dl 11
loc 100
c 0
b 0
f 0
ccs 46
cts 47
cp 0.9787
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A volumes() 0 6 1
A get() 0 6 1
A run() 11 11 2
A parse() 0 56 1

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\Disk;
5
6
use Innmind\Server\Status\{
7
    Server\Disk,
8
    Server\Disk\Volume,
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Innmind\Server\Status\Server\Disk\Volume.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
    Server\Disk\Volume\MountPoint,
10
    Server\Disk\Volume\Usage,
11
    Server\Memory\Bytes,
12
    Exception\DiskUsageNotAccessible
13
};
14
use Innmind\Immutable\{
15
    MapInterface,
16
    Str,
17
    StreamInterface,
18
    Sequence,
19
    Map
20
};
21
use Symfony\Component\Process\Process;
22
23
final class UnixDisk implements Disk
24
{
25
    private static $columns = [
26
        'Size' => 'size',
27
        'Used' => 'used',
28
        'Avail' => 'available',
29
        'Use%' => 'usage',
30
        'Capacity' => 'usage',
31
        'Mounted' => 'mountPoint',
32
    ];
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 2
    public function volumes(): MapInterface
38
    {
39 2
        return $this->parse(
40 2
            $this->run('df -lh')
41
        );
42
    }
43
44 1
    public function get(MountPoint $point): Volume
45
    {
46
        return $this
47 1
            ->volumes()
48 1
            ->get((string) $point);
1 ignored issue
show
Documentation introduced by
(string) $point is of type string, 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...
49
    }
50
51 2 View Code Duplication
    private function run(string $command): Str
52
    {
53 2
        $process = new Process($command);
54 2
        $process->run();
55
56 2
        if (!$process->isSuccessful()) {
57
            throw new DiskUsageNotAccessible;
58
        }
59
60 2
        return new Str($process->getOutput());
61
    }
62
63
    /**
64
     * @return MapInterface<string, Volume>
1 ignored issue
show
Documentation introduced by
The doc-type MapInterface<string, 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...
65
     */
66 2
    private function parse(Str $output): MapInterface
67
    {
68
        $lines = $output
69 2
            ->trim()
70 2
            ->split("\n");
71
        $columns = $lines
72 2
            ->first()
73 2
            ->pregSplit('~ +~')
74 2
            ->reduce(
75 2
                new Sequence,
76
                static function(Sequence $columns, Str $column): Sequence {
77 2
                    $column = (string) $column;
78
79 2
                    return $columns->add(self::$columns[$column] ?? $column);
80 2
                }
81
            );
82
83
        return $lines
84 2
            ->drop(1)
85 2
            ->reduce(
86 2
                new Sequence,
87
                static function(Sequence $lines, Str $line) use ($columns): Sequence {
88 2
                    return $lines->add(
89 2
                        $line->pregSplit('~ +~', $columns->size())
90
                    );
91 2
                }
92
            )
93
            ->map(function(StreamInterface $parts) use ($columns): Volume {
94 2
                return new Volume(
95 2
                    new MountPoint(
96 2
                        (string) $parts->get($columns->indexOf('mountPoint'))
97
                    ),
98 2
                    Bytes::fromString(
99 2
                        (string) $parts->get($columns->indexOf('size'))
100
                    ),
101 2
                    Bytes::fromString(
102 2
                        (string) $parts->get($columns->indexOf('available'))
103
                    ),
104 2
                    Bytes::fromString(
105 2
                        (string) $parts->get($columns->indexOf('used'))
106
                    ),
107 2
                    new Usage(
108 2
                        (float) (string) $parts->get($columns->indexOf('usage'))
109
                    )
110
                );
111 2
            })
112 2
            ->reduce(
113 2
                new Map('string', Volume::class),
114 2
                static function(Map $volumes, Volume $volume): Map {
115 2
                    return $volumes->put(
116 2
                        (string) $volume->mountPoint(),
1 ignored issue
show
Documentation introduced by
(string) $volume->mountPoint() is of type string, 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...
117 2
                        $volume
1 ignored issue
show
Documentation introduced by
$volume is of type object<Innmind\Server\Status\Server\Disk\Volume>, 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...
118
                    );
119 2
                }
120
            );
121
    }
122
}
123