AbstractEmptyEvent   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 95
rs 10
c 1
b 0
f 0
wmc 10

9 Methods

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