| 1 | <?php |
||
| 9 | class MicroDate |
||
| 10 | { |
||
| 11 | /** @var int */ |
||
| 12 | public $sec; |
||
| 13 | /** @var int */ |
||
| 14 | public $uSec; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @param int $sec |
||
| 18 | * @param int $uSec |
||
| 19 | */ |
||
| 20 | 5 | public function __construct(int $sec, int $uSec) |
|
| 25 | |||
| 26 | /** |
||
| 27 | * @param \DateTime $date |
||
| 28 | * |
||
| 29 | * @return MicroDate |
||
| 30 | */ |
||
| 31 | 1 | public static function createFromDateTime(DateTime $date): MicroDate |
|
| 35 | |||
| 36 | /** |
||
| 37 | * @return MicroDate |
||
| 38 | */ |
||
| 39 | 3 | public static function now(): MicroDate |
|
| 45 | |||
| 46 | /** |
||
| 47 | * @param DateTimeZone|null $timeZone |
||
| 48 | * |
||
| 49 | * @return DateTime |
||
| 50 | */ |
||
| 51 | 2 | public function toDateTime(DateTimeZone $timeZone = null): DateTime |
|
| 60 | } |
||
| 61 |
This error can happen if you refactor code and forget to move the variable initialization.
Let’s take a look at a simple example:
The above code is perfectly fine. Now imagine that we re-order the statements:
In that case,
$xwould be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.