1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\GuzzleHttp\Middleware\Storage; |
4
|
|
|
|
5
|
|
|
class Counter implements \Serializable, \JsonSerializable, \Countable |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var bool |
9
|
|
|
*/ |
10
|
|
|
private $useMicroseconds; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var float |
14
|
|
|
*/ |
15
|
|
|
private $expiresIn; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var float |
19
|
|
|
*/ |
20
|
|
|
private $expiresAt; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
private $counter; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Counter constructor. |
29
|
|
|
* @param float $expiresIn |
30
|
|
|
*/ |
31
|
|
|
public function __construct(float $expiresIn) |
32
|
|
|
{ |
33
|
|
|
$this->useMicroseconds = intval($expiresIn) != $expiresIn; |
34
|
|
|
$this->expiresIn = $this->useMicroseconds ? $expiresIn : intval($expiresIn); |
|
|
|
|
35
|
|
|
$this->reset(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function now() |
39
|
|
|
{ |
40
|
|
|
return $this->useMicroseconds ? microtime(true) : time(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function reset() |
44
|
|
|
{ |
45
|
|
|
$this->counter = 0; |
46
|
|
|
$this->expiresAt = null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Increment counter. |
51
|
|
|
*/ |
52
|
|
|
public function increment() |
53
|
|
|
{ |
54
|
|
|
$this->counter++; |
55
|
|
|
if (1 === $this->counter) { |
56
|
|
|
$this->expiresAt = $this->now() + $this->expiresIn; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return int |
62
|
|
|
*/ |
63
|
|
|
public function count(): int |
64
|
|
|
{ |
65
|
|
|
if ($this->isExpired()) { |
66
|
|
|
$this->reset(); |
67
|
|
|
} |
68
|
|
|
return $this->counter; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return float |
73
|
|
|
*/ |
74
|
|
|
public function getRemainingTime() |
75
|
|
|
{ |
76
|
|
|
return (float) max(0, $this->expiresAt - $this->now()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function isExpired() |
83
|
|
|
{ |
84
|
|
|
return null !== $this->expiresAt && 0.0 === $this->getRemainingTime(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @inheritDoc |
89
|
|
|
*/ |
90
|
|
|
public function serialize() |
91
|
|
|
{ |
92
|
|
|
return json_encode($this); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @inheritDoc |
97
|
|
|
*/ |
98
|
|
|
public function unserialize($serialized) |
99
|
|
|
{ |
100
|
|
|
$data = json_decode($serialized, true); |
101
|
|
|
$this->expiresAt = $data['e']; |
102
|
|
|
$this->expiresIn = $data['i']; |
103
|
|
|
$this->counter = $data['n']; |
104
|
|
|
$this->useMicroseconds = $data['m']; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritDoc |
109
|
|
|
*/ |
110
|
|
|
public function jsonSerialize() |
111
|
|
|
{ |
112
|
|
|
return [ |
113
|
|
|
'm' => $this->useMicroseconds, |
114
|
|
|
'i' => $this->expiresIn, |
115
|
|
|
'e' => $this->expiresAt, |
116
|
|
|
'n' => $this->counter, |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function __debugInfo() |
121
|
|
|
{ |
122
|
|
|
return [ |
123
|
|
|
'counter' => $this->counter, |
124
|
|
|
'microseconds' => $this->useMicroseconds, |
125
|
|
|
'expiresIn' => $this->expiresIn, |
126
|
|
|
'expiresAt' => $this->expiresAt, |
127
|
|
|
'now' => $this->now(), |
128
|
|
|
'remaining' => $this->useMicroseconds ? $this->getRemainingTime() : round($this->getRemainingTime()), |
129
|
|
|
'expired' => $this->isExpired(), |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
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.