Completed
Push — master ( 648d4c...817611 )
by Alexpts
05:47
created

MicroDate::createFromDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace PTS\Tools;
5
6
use DateTime;
7
use DateTimeZone;
8
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)
21
    {
22 5
        $this->sec = $sec;
23 5
        $this->uSec = $uSec;
24 5
    }
25
26
    /**
27
     * @param \DateTime $date
28
     *
29
     * @return MicroDate
30
     */
31 1
    public static function createFromDateTime(DateTime $date): MicroDate
32
    {
33 1
        return new self($date->getTimestamp(), (int)$date->format('u'));
34
    }
35
36
    /**
37
     * @return MicroDate
38
     */
39 3
    public static function now(): MicroDate
40
    {
41 3
        [$uSec, $sec] = explode(' ', microtime());
0 ignored issues
show
Bug introduced by
The variable $uSec seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Bug introduced by
The variable $sec does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
42 3
        $uSec = (float)$uSec * 100000;
0 ignored issues
show
Bug introduced by
The variable $uSec seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
43 3
        return new self((int)$sec, (int)$uSec);
44
    }
45
46
    /**
47
     * @param DateTimeZone|null $timeZone
48
     *
49
     * @return DateTime
50
     */
51 2
    public function toDateTime(DateTimeZone $timeZone = null): DateTime
52
    {
53 2
        $date = \DateTime::createFromFormat('U.u', $this->sec . '.' . $this->uSec);
54 2
        if ($timeZone) {
55 1
            $date->setTimezone($timeZone);
56
        }
57
58 2
        return $date;
59
    }
60
}
61