Test Failed
Push — master ( 892598...558941 )
by Antonio Carlos
11:17
created

ServerUptime   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 155
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 94.55%

Importance

Changes 0
Metric Value
dl 155
loc 155
ccs 52
cts 55
cp 0.9455
rs 10
c 0
b 0
f 0
wmc 20
lcom 1
cbo 5

8 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 17 17 1
A executeUptimeCommand() 12 12 4
A getCurrentUptime() 4 4 1
A normalizeMatches() 21 21 4
A parseUptimeString() 17 17 1
A uptimeInSeconds() 8 8 4
A makeMessage() 8 8 1
A toUptimeString() 8 8 4

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
3
namespace PragmaRX\Health\Checkers;
4
5
use DomainException;
6
use Carbon\CarbonInterval;
7
use PragmaRX\Health\Support\Result;
8
use PragmaRX\Health\Support\Traits\Database;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, PragmaRX\Health\Checkers\Database.

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
10 View Code Duplication
class ServerUptime extends Base
0 ignored issues
show
Duplication introduced by
This class 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...
11
{
12
    use Database;
13
14
    /**
15
     * Check resource.
16
     *
17
     * @return Result
18
     */
19 1
    public function check()
20
    {
21 1
        $this->loadDatabase();
22
23 1
        $current = $this->getCurrentUptime();
24
25 1
        $this->persist($current);
26
27
        $rebooted =
28 1
            ($cs = $this->uptimeInSeconds($current)) <
29 1
            ($ss = $this->uptimeInSeconds($this->database));
30
31 1
        return $this->makeResult(
32 1
            !$rebooted,
33 1
            $this->makeMessage($current, $this->database)
34
        );
35
    }
36
37
    /**
38
     * Execute command to get an uptime string.
39
     *
40
     * @return mixed|string
41
     * @throws Exception
42
     */
43 1
    private function executeUptimeCommand()
44
    {
45 1
        $error = exec($this->target->command, $system_string, $output);
0 ignored issues
show
Bug introduced by
The property command does not seem to exist in PragmaRX\Health\Support\Target.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
46
47 1
        if ($output !== 0) {
48
            throw new DomainException((string) $error);
49
        }
50
51 1
        return (!is_array($system_string) || empty($system_string))
52
            ? ''
53 1
            : $system_string[0];
54
    }
55
56
    /**
57
     * Get current uptime.
58
     *
59
     * @return array
60
     * @throws Exception
61
     */
62 1
    protected function getCurrentUptime()
63
    {
64 1
        return $this->parseUptimeString($this->executeUptimeCommand());
65
    }
66
67
    /**
68
     * Normalize uptime matches.
69
     *
70
     * @param $matches
71
     * @return \Illuminate\Support\Collection
72
     */
73 1
    protected function normalizeMatches($matches)
74
    {
75 1
        return collect($matches)
76
            ->filter(function ($item, $key) {
77 1
                return !is_numeric($key);
78 1
            })
79
            ->map(function ($item, $key) {
80 1
                $return = $item[0];
81
82 1
                if (starts_with($key, 'load')) {
83 1
                    $return = floatval($return);
84 1
                } elseif (is_numeric($return)) {
85 1
                    $return = (int) $return;
86
                } elseif (empty($return)) {
87 1
                    $return = null;
88
                }
89
90 1
                return $return;
91 1
            })
92 1
            ->toArray();
93
    }
94
95
    /**
96
     * Parse the uptime string.
97
     *
98
     * @param $system_string
99
     * @return array
100
     */
101 1
    protected function parseUptimeString($system_string)
102
    {
103 1
        $matches = [];
104
105 1
        preg_match(
106 1
            $this->target->regex,
0 ignored issues
show
Bug introduced by
The property regex does not seem to exist in PragmaRX\Health\Support\Target.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
107 1
            $system_string,
108 1
            $matches,
109 1
            PREG_OFFSET_CAPTURE
110
        );
111
112 1
        $matches = $this->normalizeMatches($matches);
113
114 1
        $matches['uptime_string'] = $system_string;
115
116 1
        return $matches;
117
    }
118
119
    /**
120
     * Convert uptime to seconds.
121
     *
122
     * @param $date
123
     * @return int
124
     */
125 1
    protected function uptimeInSeconds($date)
126
    {
127
        return (
128 1
            (isset($date['up_days']) ? $date['up_days'] * 24 * 60 : 0) +
129 1
            (isset($date['up_hours']) ? $date['up_hours'] * 60 : 0) +
130 1
            (isset($date['up_minutes']) ? $date['up_minutes'] : 0)
131
        );
132
    }
133
134
    /**
135
     * Make uptime message.
136
     *
137
     * @param $current
138
     * @param $saved
139
     * @return string
140
     */
141 1
    protected function makeMessage($current, $saved = null)
142
    {
143 1
        $current = $this->toUptimeString($current);
144
145 1
        $saved = $this->toUptimeString($saved);
146
147 1
        return sprintf($this->target->getErrorMessage(), $current, $saved);
148
    }
149
150
    /**
151
     * Convert uptime to human readable string.
152
     *
153
     * @param $uptime
154
     * @return string
155
     */
156 1
    public function toUptimeString($uptime)
157
    {
158 1
        return (string) CarbonInterval::days(
159 1
            isset($uptime['up_days']) ? $uptime['up_days'] : 0
160
        )
161 1
            ->hours(isset($uptime['up_hours']) ? $uptime['up_hours'] : 0)
162 1
            ->minutes(isset($uptime['up_minutes']) ? $uptime['up_minutes'] : 0);
163
    }
164
}
165