Timer::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * Ping for Laravel.
5
 *
6
 * This class makes Ping request to a host.
7
 *
8
 * Ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to elicit an ICMP ECHO_RESPONSE from a host or gateway.
9
 *
10
 * @author  Angel Campos <[email protected]>
11
 * @requires PHP 8.0
12
 *
13
 * @version  2.1.2
14
 */
15
16
namespace Acamposm\Ping;
17
18
use Acamposm\Ping\Exceptions\TimerNotStartedException;
19
use DateTime;
20
21
/**
22
 * Utility Class to control time elapsed in commands.
23
 */
24
class Timer
25
{
26
    /**
27
     * Format for the timestamps.
28
     */
29
    public const FORMAT = 'd-m-Y H:i:s.u';
30
31
    /**
32
     * Timer START.
33
     *
34
     * @var float
35
     */
36
    protected float $start;
37
38
    /**
39
     * Timer END.
40
     *
41
     * @var float
42
     */
43
    protected float $stop;
44
45
    /**
46
     * Timer constructor.
47
     */
48
    public function __construct(protected string $format = self::FORMAT)
49
    {
50
        return $this;
51
    }
52
53
    /**
54
     * Start the Timer.
55
     *
56
     * @return float
57
     */
58
    public function Start(): float
59
    {
60
        return $this->start = microtime(true);
0 ignored issues
show
Documentation Bug introduced by
It seems like microtime(true) can also be of type string. However, the property $start is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
Bug Best Practice introduced by
The expression return $this->start = microtime(true) could return the type string which is incompatible with the type-hinted return double. Consider adding an additional type-check to rule them out.
Loading history...
61
    }
62
63
    /**
64
     * Stop the Timer.
65
     *
66
     * @throws TimerNotStartedException
67
     * @retun  float
68
     */
69
    public function Stop(): float
70
    {
71
        if (!isset($this->start)) {
72
            throw new TimerNotStartedException();
73
        }
74
75
        return $this->stop = microtime(true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->stop = microtime(true) could return the type string which is incompatible with the type-hinted return double. Consider adding an additional type-check to rule them out.
Loading history...
Documentation Bug introduced by
It seems like microtime(true) can also be of type string. However, the property $stop is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
76
    }
77
78
    /**
79
     * Returns an object with the Timer details.
80
     *
81
     * @return object
82
     */
83
    public function GetResults(): object
84
    {
85
        if (!isset($this->stop)) {
86
            $this->stop = microtime(true);
0 ignored issues
show
Documentation Bug introduced by
It seems like microtime(true) can also be of type string. However, the property $stop is declared as type double. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
87
        }
88
89
        return (object) [
90
            'start' => $this->getTimeObject($this->start),
91
            'stop'  => $this->getTimeObject($this->stop),
92
            'time'  => $this->getTimeElapsed(),
93
        ];
94
    }
95
96
    /**
97
     * Returns a DateTime instance from timestamp.
98
     *
99
     * @param float $timestamp
100
     *
101
     * @return DateTime
102
     */
103
    private static function getDateTimeObjectFromTimeStamp(float $timestamp): DateTime
104
    {
105
        return DateTime::createFromFormat('U.u', $timestamp);
106
    }
107
108
    /**
109
     * Returns an object with the timestamp as a float and as a human-readable.
110
     *
111
     * @param float $timestamp
112
     *
113
     * @return object
114
     */
115
    private function getTimeObject(float $timestamp): object
116
    {
117
        $date_time = self::getDateTimeObjectFromTimeStamp($timestamp);
118
119
        return (object) [
120
            'as_float'       => $timestamp,
121
            'human_readable' => $date_time->format($this->format),
122
        ];
123
    }
124
125
    /**
126
     * Returns the elapsed time between start and stop.
127
     *
128
     * @return float
129
     */
130
    private function getTimeElapsed(): float
131
    {
132
        $time_elapsed = $this->stop - $this->start;
133
134
        return round($time_elapsed, 3, PHP_ROUND_HALF_DOWN);
135
    }
136
}
137