UserEvent   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 37
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A event() 0 4 1
A occurredOn() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the BenGorUser package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorUser\CarlosBuenosvinosDddBridge\Domain\Model;
14
15
use BenGorUser\User\Domain\Model\Event\UserEvent as BaseUserEvent;
16
use Ddd\Domain\DomainEvent;
17
use Ddd\Domain\Event\PublishableDomainEvent;
18
19
/**
20
 * User domain event class.
21
 *
22
 * @author Beñat Espiña <[email protected]>
23
 * @author Gorka Laucirica <[email protected]>
24
 */
25
class UserEvent implements DomainEvent, PublishableDomainEvent
26
{
27
    /**
28
     * The domain event.
29
     *
30
     * @var BaseUserEvent
31
     */
32
    private $event;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param BaseUserEvent $anEvent The domain event
38
     */
39
    public function __construct(BaseUserEvent $anEvent)
40
    {
41
        $this->event = $anEvent;
42
    }
43
44
    /**
45
     * Gets the domain event.
46
     *
47
     * @return BaseUserEvent
48
     */
49
    public function event()
50
    {
51
        return $this->event;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function occurredOn()
58
    {
59
        return $this->event->occurredOn();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->event->occurredOn(); (DateTimeImmutable) is incompatible with the return type declared by the interface Ddd\Domain\DomainEvent::occurredOn of type DateTime.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
60
    }
61
}
62