AbstractEvent   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 17
dl 0
loc 96
rs 10
c 2
b 1
f 0
wmc 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getEventType() 0 3 1
A __unserialize() 0 3 1
A reconstitute() 0 9 2
A __construct() 0 6 1
A getAllowedInterfaces() 0 3 1
A __wakeup() 0 3 1
A occurred() 0 5 1
A __serialize() 0 3 1
A __sleep() 0 3 1
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\Exception\EventException;
17
use Gears\Event\Time\SystemTimeProvider;
18
use Gears\Event\Time\TimeProvider;
19
20
/**
21
 * Abstract immutable event.
22
 */
23
abstract class AbstractEvent implements Event
24
{
25
    use EventBehaviour;
26
27
    /**
28
     * AbstractEvent constructor.
29
     *
30
     * @param array<string, mixed> $payload
31
     * @param \DateTimeImmutable   $createdAt
32
     */
33
    private function __construct(array $payload, \DateTimeImmutable $createdAt)
34
    {
35
        $this->assertImmutable();
36
37
        $this->setPayload($payload);
38
        $this->createdAt = $createdAt->setTimezone(new \DateTimeZone('UTC'));
0 ignored issues
show
Documentation Bug introduced by
It seems like $createdAt->setTimezone(new DateTimeZone('UTC')) can also be of type false. However, the property $createdAt 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...
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getEventType(): string
45
    {
46
        return static::class;
47
    }
48
49
    /**
50
     * Instantiate new event.
51
     *
52
     * @param array<string, mixed> $payload
53
     * @param TimeProvider         $timeProvider
54
     *
55
     * @return mixed|self
56
     */
57
    final protected static function occurred(array $payload, ?TimeProvider $timeProvider = null)
58
    {
59
        $timeProvider = $timeProvider ?? new SystemTimeProvider();
60
61
        return new static($payload, $timeProvider->getCurrentTime());
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     *
67
     * @return mixed|self
68
     */
69
    public static function reconstitute(array $payload, \DateTimeImmutable $createdAt, array $attributes)
70
    {
71
        $event = new static($payload, $createdAt);
72
73
        if (isset($attributes['metadata'])) {
74
            $event->addMetadata($attributes['metadata']);
75
        }
76
77
        return $event;
78
    }
79
80
    /**
81
     * @return string[]
82
     */
83
    final public function __sleep(): array
84
    {
85
        throw new EventException(\sprintf('Event "%s" cannot be serialized.', static::class));
86
    }
87
88
    final public function __wakeup(): void
89
    {
90
        throw new EventException(\sprintf('Event "%s" cannot be unserialized.', static::class));
91
    }
92
93
    /**
94
     * @return array<string, mixed>
95
     */
96
    final public function __serialize(): array
97
    {
98
        throw new EventException(\sprintf('Event "%s" cannot be serialized.', static::class));
99
    }
100
101
    /**
102
     * @param array<string, mixed> $data
103
     *
104
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
105
     */
106
    final public function __unserialize(array $data): void
107
    {
108
        throw new EventException(\sprintf('Event "%s" cannot be unserialized.', static::class));
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     *
114
     * @return string[]
115
     */
116
    final protected function getAllowedInterfaces(): array
117
    {
118
        return [Event::class];
119
    }
120
}
121