1 | <?php |
||||
2 | |||||
3 | declare(strict_types = 1); |
||||
4 | |||||
5 | namespace Drupal\qa; |
||||
6 | |||||
7 | /** |
||||
8 | * A lifetime class. |
||||
9 | * |
||||
10 | * Properties are: Start, Access, Modify, End. |
||||
11 | * |
||||
12 | * Note: SAME is an old (ca 2005) OSInet PHP_lib class, which other modules may |
||||
13 | * have imported in a non-namespaced form. |
||||
14 | */ |
||||
15 | class Same { |
||||
16 | |||||
17 | /** |
||||
18 | * Start timestamp. |
||||
19 | * |
||||
20 | * @var float |
||||
21 | */ |
||||
22 | public $s; |
||||
23 | |||||
24 | /** |
||||
25 | * Access timestamp. |
||||
26 | * |
||||
27 | * @var float |
||||
28 | */ |
||||
29 | public $a; |
||||
30 | |||||
31 | /** |
||||
32 | * Modification timestamp. |
||||
33 | * |
||||
34 | * @var float |
||||
35 | */ |
||||
36 | public $m; |
||||
37 | |||||
38 | /** |
||||
39 | * End timestamp. |
||||
40 | * |
||||
41 | * @var float |
||||
42 | */ |
||||
43 | public $e; |
||||
44 | |||||
45 | /** |
||||
46 | * Constructor. |
||||
47 | * |
||||
48 | * S.A.M. default to current time, but E defaults to NULL. |
||||
49 | * |
||||
50 | * @param float $s |
||||
51 | * Start timestamp. |
||||
52 | * @param float $a |
||||
53 | * Access timestamp. |
||||
54 | * @param float $m |
||||
55 | * Modification timestamp. |
||||
56 | * @param float $e |
||||
57 | * End timestamp. |
||||
58 | */ |
||||
59 | public function __construct($s = 0.0, $a = 0.0, $m = 0.0, $e = 0.0) { |
||||
60 | $now = microtime(TRUE); |
||||
61 | foreach (['s', 'a', 'm'] as $ts) { |
||||
62 | $this->$ts = ($$ts) ?: $now; |
||||
63 | } |
||||
64 | $this->e = $e; |
||||
65 | } |
||||
66 | |||||
67 | /** |
||||
68 | * Update the access timestamp. |
||||
69 | * |
||||
70 | * @param float $now |
||||
71 | * The timestamp to apply. |
||||
72 | */ |
||||
73 | public function access($now = 0.0) { |
||||
74 | $this->a = $now ?: microtime(); |
||||
0 ignored issues
–
show
|
|||||
75 | } |
||||
76 | |||||
77 | /** |
||||
78 | * Update the modification timestamp. |
||||
79 | * |
||||
80 | * @param float $now |
||||
81 | * The timestamp to apply. |
||||
82 | */ |
||||
83 | public function modify($now = 0.0) { |
||||
84 | if (empty($now)) { |
||||
85 | $now = microtime(TRUE); |
||||
86 | } |
||||
87 | $this->access($now); |
||||
0 ignored issues
–
show
It seems like
$now can also be of type string ; however, parameter $now of Drupal\qa\Same::access() does only seem to accept double , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
88 | $this->m = $now; |
||||
0 ignored issues
–
show
It seems like
$now can also be of type string . However, the property $m 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 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;
}
![]() |
|||||
89 | } |
||||
90 | |||||
91 | /** |
||||
92 | * Update the end timestamp. |
||||
93 | * |
||||
94 | * @param float $now |
||||
95 | * The timestamp to apply. |
||||
96 | */ |
||||
97 | public function end($now = 0.0) { |
||||
98 | if (empty($now)) { |
||||
99 | $now = microtime(TRUE); |
||||
100 | } |
||||
101 | $this->modify($now); |
||||
0 ignored issues
–
show
It seems like
$now can also be of type string ; however, parameter $now of Drupal\qa\Same::modify() does only seem to accept double , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
102 | $this->e = $now; |
||||
0 ignored issues
–
show
It seems like
$now can also be of type string . However, the property $e 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 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;
}
![]() |
|||||
103 | } |
||||
104 | |||||
105 | /** |
||||
106 | * Return the age of the instance, in microseconds. |
||||
107 | * |
||||
108 | * @return float |
||||
109 | * The age, in microseconds. |
||||
110 | */ |
||||
111 | public function age(): float { |
||||
112 | if (!empty($this->e)) { |
||||
113 | return $this->e - $this->s; |
||||
114 | } |
||||
115 | // Modifications imply update. |
||||
116 | elseif (!empty($this->a)) { |
||||
117 | return $this->a - $this->s; |
||||
118 | } |
||||
119 | return microtime(TRUE) - $this->s; |
||||
120 | } |
||||
121 | |||||
122 | /** |
||||
123 | * Return the age of the instance in seconds, for use with UNIX timestamps. |
||||
124 | * |
||||
125 | * @return int |
||||
126 | * The age, in seconds. |
||||
127 | */ |
||||
128 | public function unixAge(): int { |
||||
129 | return intval($this->age() / 1E6); |
||||
130 | } |
||||
131 | |||||
132 | /** |
||||
133 | * {@inheritdoc} |
||||
134 | */ |
||||
135 | public function __toString(): string { |
||||
136 | return (string) $this->age(); |
||||
137 | } |
||||
138 | |||||
139 | } |
||||
140 |
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.