Completed
Push — master ( 696320...2ffb28 )
by Julián
05:01
created

AbstractEmptyEvent::getEventType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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 empty immutable event.
21
 */
22
abstract class AbstractEmptyEvent implements Event
23
{
24
    use EventBehaviour;
25
26
    /**
27
     * AbstractEmptyEvent constructor.
28
     *
29
     * @param \DateTimeImmutable $createdAt
30
     */
31
    private function __construct(\DateTimeImmutable $createdAt)
32
    {
33
        $this->assertImmutable();
34
35
        $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...
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getEventType(): string
42
    {
43
        return \get_called_class();
44
    }
45
46
    /**
47
     * Instantiate new event.
48
     *
49
     * @param TimeProvider $timeProvider
50
     *
51
     * @return mixed|self
52
     */
53
    final protected static function occurred(?TimeProvider $timeProvider = null)
54
    {
55
        $timeProvider = $timeProvider ?? new SystemTimeProvider();
56
57
        return new static($timeProvider->getCurrentTime());
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     *
63
     * @return mixed|self
64
     *
65
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
66
     */
67
    public static function reconstitute(array $payload, \DateTimeImmutable $createdAt, array $attributes)
68
    {
69
        $event = new static($createdAt);
70
71
        if (isset($attributes['metadata'])) {
72
            $event->addMetadata($attributes['metadata']);
73
        }
74
75
        return $event;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     *
81
     * @return string[]
82
     */
83
    final protected function getAllowedInterfaces(): array
84
    {
85
        return [Event::class];
86
    }
87
}
88