Completed
Push — master ( 5678fc...0141b8 )
by Ivannis Suárez
02:34
created

DomainEvent::fromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Domain\EventSourcing;
13
14
use Cubiche\Domain\EventPublisher\DomainEvent as BaseDomainEvent;
15
use Cubiche\Domain\Model\IdInterface;
16
17
/**
18
 * DomainEvent class.
19
 *
20
 * @author Ivannis Suárez Jerez <[email protected]>
21
 */
22
class DomainEvent extends BaseDomainEvent implements DomainEventInterface
23
{
24
    /**
25
     * @var DomainEventId
26
     */
27
    protected $eventId;
28
29
    /**
30
     * EntityDomainEvent constructor.
31
     *
32
     * @param IdInterface $aggregateId
33
     */
34
    public function __construct(IdInterface $aggregateId)
35
    {
36
        parent::__construct();
37
38
        $this->setMetadata('aggregateId', $aggregateId);
0 ignored issues
show
Bug introduced by
The method setMetadata() does not seem to exist on object<Cubiche\Domain\EventSourcing\DomainEvent>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
        $this->setVersion(0);
40
        $this->eventId = DomainEventId::next();
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function eventId()
47
    {
48
        return $this->eventId;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function aggregateId()
55
    {
56
        return $this->getMetadata('aggregateId');
0 ignored issues
show
Bug introduced by
The method getMetadata() does not seem to exist on object<Cubiche\Domain\EventSourcing\DomainEvent>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function version()
63
    {
64
        return $this->getMetadata('version');
0 ignored issues
show
Bug introduced by
The method getMetadata() does not seem to exist on object<Cubiche\Domain\EventSourcing\DomainEvent>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function setVersion($version)
71
    {
72
        $this->setMetadata('version', $version);
0 ignored issues
show
Bug introduced by
The method setMetadata() does not seem to exist on object<Cubiche\Domain\EventSourcing\DomainEvent>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public static function fromArray(array $data)
79
    {
80
        /** @var DomainEvent $domainEvent */
81
        $domainEvent = parent::fromArray($data);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Cubiche\Domain\EventPublisher\DomainEvent as the method fromArray() does only exist in the following sub-classes of Cubiche\Domain\EventPublisher\DomainEvent: Cubiche\Domain\EventSourcing\DomainEvent, Cubiche\Domain\EventSour...ent\PostTitleWasChanged, Cubiche\Domain\EventSour...es\Event\PostWasCreated, Cubiche\Domain\EventSour...\Event\PostWasPublished, Cubiche\Domain\EventSour...es\Event\PostWasRemoved, Cubiche\Domain\EventSour...vent\PostWasUnPublished. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
82
        $domainEvent->eventId = $data['eventId'];
83
84
        return $domainEvent;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function toArray()
91
    {
92
        return array_merge(parent::toArray(), array(
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Cubiche\Domain\EventPublisher\DomainEvent as the method toArray() does only exist in the following sub-classes of Cubiche\Domain\EventPublisher\DomainEvent: Cubiche\Domain\EventSourcing\DomainEvent, Cubiche\Domain\EventSour...ent\PostTitleWasChanged, Cubiche\Domain\EventSour...es\Event\PostWasCreated, Cubiche\Domain\EventSour...\Event\PostWasPublished, Cubiche\Domain\EventSour...es\Event\PostWasRemoved, Cubiche\Domain\EventSour...vent\PostWasUnPublished. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
93
            'eventId' => $this->eventId(),
94
        ));
95
    }
96
}
97