Completed
Push — master ( e5266d...7b4fa4 )
by Frank
05:26
created

src/Time/TestClock.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace EventSauce\EventSourcing\Time;
6
7
use DateTimeImmutable;
8
use DateTimeZone;
9
use EventSauce\EventSourcing\PointInTime;
10
11
class TestClock implements Clock
12
{
13
    /**
14
     * @private
15
     */
16
    const FORMAT_OF_TIME = 'Y-m-d H:i:s.uO';
17
18
    /**
19
     * @var DateTimeImmutable
20
     */
21
    private $time;
22
23
    /**
24
     * @var DateTimeZone
25
     */
26
    private $timeZone;
27
28 17
    public function __construct(DateTimeZone $timeZone = null)
29
    {
30 17
        $this->timeZone = $timeZone ?: new DateTimeZone('UTC');
31 17
        $this->tick();
32 17
    }
33
34 17
    public function tick()
35
    {
36 17
        $this->time = new DateTimeImmutable('now', $this->timeZone);
37 17
    }
38
39 1
    public function fixate(string $dateTime)
40
    {
41 1
        $preciseTime = sprintf('%s.000000', $dateTime);
42 1
        $this->time = DateTimeImmutable::createFromFormat('Y-m-d H:i:s.u', $preciseTime, $this->timeZone);
0 ignored issues
show
Documentation Bug introduced by
It seems like DateTimeImmutable::creat...eTime, $this->timeZone) can also be of type false. However, the property $time is declared as type DateTimeImmutable. 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 $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account 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.

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...
43 1
    }
44
45 8
    public function dateTime(): DateTimeImmutable
46
    {
47 8
        return $this->time;
48
    }
49
50 5
    public function pointInTime(): PointInTime
51
    {
52 5
        return PointInTime::fromDateTime($this->dateTime());
53
    }
54
}
55