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); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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 theid
property of an instance of theAccount
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.