|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* event (https://github.com/phpgears/event). |
|
5
|
|
|
* Event handling. |
|
6
|
|
|
* |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* @link https://github.com/phpgears/event |
|
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Gears\Event; |
|
15
|
|
|
|
|
16
|
|
|
use Gears\Event\Time\SystemTimeProvider; |
|
17
|
|
|
use Gears\Event\Time\TimeProvider; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Abstract immutable event. |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class AbstractEvent implements Event |
|
23
|
|
|
{ |
|
24
|
|
|
use EventBehaviour; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* AbstractEvent constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param array<string, mixed> $payload |
|
30
|
|
|
* @param array<string, mixed> $metadata |
|
31
|
|
|
* @param \DateTimeImmutable $createdAt |
|
32
|
|
|
*/ |
|
33
|
|
|
private function __construct(array $payload, array $metadata, \DateTimeImmutable $createdAt) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->checkImmutability(); |
|
36
|
|
|
|
|
37
|
|
|
$this->setPayload($payload); |
|
38
|
|
|
$this->setMetadata($metadata); |
|
39
|
|
|
$this->createdAt = $createdAt->setTimezone(new \DateTimeZone('UTC')); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Instantiate new event. |
|
44
|
|
|
* |
|
45
|
|
|
* @param array<string, mixed> $payload |
|
46
|
|
|
* @param TimeProvider $timeProvider |
|
|
|
|
|
|
47
|
|
|
* |
|
48
|
|
|
* @return mixed|self |
|
|
|
|
|
|
49
|
|
|
*/ |
|
50
|
|
|
final protected static function occurred(array $payload, ?TimeProvider $timeProvider = null) |
|
51
|
|
|
{ |
|
52
|
|
|
$timeProvider = $timeProvider ?? new SystemTimeProvider(); |
|
53
|
|
|
|
|
54
|
|
|
return new static($payload, [], $timeProvider->getCurrentTime()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
* |
|
60
|
|
|
* @return mixed|self |
|
|
|
|
|
|
61
|
|
|
*/ |
|
62
|
|
|
public static function reconstitute(array $payload, array $attributes) |
|
63
|
|
|
{ |
|
64
|
|
|
return new static($payload, $attributes['metadata'], $attributes['createdAt']); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
* |
|
70
|
|
|
* @return string[] |
|
71
|
|
|
*/ |
|
72
|
|
|
final protected function getAllowedInterfaces(): array |
|
73
|
|
|
{ |
|
74
|
|
|
return [Event::class]; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
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.