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

OSXFacade::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\Facade\Memory;
5
6
use Innmind\Server\Status\{
7
    Server\Memory,
8
    Server\Memory\Bytes,
9
    Exception\MemoryUsageNotAccessible
10
};
11
use Innmind\Immutable\Str;
12
use Symfony\Component\Process\Process;
13
14
final class OSXFacade
15
{
16 1
    public function __invoke(): Memory
17
    {
18
        $total = $this
19 1
            ->run('sysctl hw.memsize')
20
            ->trim()
21
            ->capture('~^hw.memsize: (?P<total>\d+)$~')
22
            ->get('total');
23
        $swap = $this
24
            ->run('sysctl vm.swapusage')
25
            ->trim()
26
            ->capture('~used = (?P<swap>\d+\.?\d*[KMGTP])~')
27
            ->get('swap');
28
        $amounts = $this
29
            ->run('top -l 1 -s 0 | grep PhysMem')
30
            ->trim()
31
            ->capture(
32
                '~^PhysMem: (?P<used>\d+[KMGTP]) used \((?P<wired>\d+[KMGTP]) wired\), (?P<unused>\d+[KMGTP]) unused.$~'
33
            );
34
        $active = $this
35
            ->run('vm_stat | grep \'Pages active\'')
36
            ->trim()
37
            ->capture('~(?P<active>\d+)~')
38
            ->get('active');
39
40
        return new Memory(
41
            new Bytes((int) (string) $total),
42
            $this->parse($amounts->get('wired')),
43
            new Bytes(((int) (string) $active) * 4096),
44
            $this->parse($amounts->get('unused')),
45
            $this->parse($swap),
46
            $this->parse($amounts->get('used'))
47
        );
48
    }
49
50 1 View Code Duplication
    private function run(string $command): Str
0 ignored issues
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...
51
    {
52 1
        $process = new Process($command);
53 1
        $process->run();
54
55 1
        if (!$process->isSuccessful()) {
56 1
            throw new MemoryUsageNotAccessible;
57
        }
58
59
        return new Str($process->getOutput());
60
    }
61
62
    private function parse(Str $str): Bytes
63
    {
64
        switch ($str->substring(-1)) {
65
            case 'K':
66
                $multiplier = Bytes::BYTES;
67
                break;
68
69
            case 'M':
70
                $multiplier = Bytes::KILOBYTES;
71
                break;
72
73
            case 'G':
74
                $multiplier = Bytes::MEGABYTES;
75
                break;
76
77
            case 'T':
78
                $multiplier = Bytes::GIGABYTES;
79
                break;
80
81
            case 'P':
82
                $multiplier = Bytes::TERABYTES;
83
                break;
84
        }
85
86
        return new Bytes(
87
            (int) (((float) (string) $str->substring(0, -1)) * $multiplier)
0 ignored issues
show
Bug introduced by
The variable $multiplier does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
88
        );
89
    }
90
}
91