| 1 | <?php |
||
| 2 | |||
| 3 | namespace Lagdo\DbAdmin\Service; |
||
| 4 | |||
| 5 | use function max; |
||
| 6 | use function microtime; |
||
| 7 | |||
| 8 | class TimerService |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var float |
||
| 12 | */ |
||
| 13 | private float $startTimestamp = 0; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var float |
||
| 17 | */ |
||
| 18 | private float $endTimestamp = 0; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @return void |
||
| 22 | */ |
||
| 23 | public function start(): void |
||
| 24 | { |
||
| 25 | $this->startTimestamp = microtime(true); |
||
|
0 ignored issues
–
show
|
|||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @return void |
||
| 30 | */ |
||
| 31 | public function stop(): void |
||
| 32 | { |
||
| 33 | $this->endTimestamp = microtime(true); |
||
|
0 ignored issues
–
show
It seems like
microtime(true) can also be of type string. However, the property $endTimestamp 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;
}
Loading history...
|
|||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @return float |
||
| 38 | */ |
||
| 39 | public function duration(): float |
||
| 40 | { |
||
| 41 | return max(0, $this->endTimestamp - $this->startTimestamp); |
||
| 42 | } |
||
| 43 | } |
||
| 44 |
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
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. 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.