Completed
Pull Request — master (#279)
by Luc
13:49 queued 06:54
created

EventCopied   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 82
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A getParentAggregateId() 0 4 1
A getOriginalEventId() 0 4 1
A getCalendar() 0 4 1
A serialize() 0 7 1
A deserialize() 0 8 1
1
<?php
2
3
namespace CultuurNet\UDB3\Event\Events;
4
5
use CultuurNet\UDB3\Calendar;
6
use CultuurNet\UDB3\CalendarInterface;
7
use CultuurNet\UDB3\EventSourcing\AggregateCopiedEventInterface;
8
use CultuurNet\UDB3\Offer\Events\AbstractEvent;
9
10
class EventCopied extends AbstractEvent implements AggregateCopiedEventInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $originalEventId;
16
17
    /**
18
     * @var CalendarInterface|Calendar
19
     */
20
    private $calendar;
21
22
    /**
23
     * EventCopied constructor.
24
     * @param string $eventId
25
     * @param string $originalEventId
26
     * @param CalendarInterface $calendar
27
     */
28
    public function __construct(
29
        $eventId,
30
        $originalEventId,
31
        CalendarInterface $calendar
32
    ) {
33
        parent::__construct($eventId);
34
35
        if (!is_string($originalEventId)) {
36
            throw new \InvalidArgumentException(
37
                'Expected originalEventId to be a string, received ' . gettype($originalEventId)
38
            );
39
        }
40
41
        $this->originalEventId = $originalEventId;
42
        $this->calendar = $calendar;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function getParentAggregateId()
49
    {
50
        return $this->originalEventId;
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getOriginalEventId()
57
    {
58
        return $this->originalEventId;
59
    }
60
61
    /**
62
     * @return CalendarInterface
63
     */
64
    public function getCalendar()
65
    {
66
        return $this->calendar;
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function serialize()
73
    {
74
        return parent::serialize() + [
75
                'original_event_id' => $this->getOriginalEventId(),
76
                'calendar' => $this->calendar->serialize()
0 ignored issues
show
Bug introduced by
The method serialize does only exist in CultuurNet\UDB3\Calendar, but not in CultuurNet\UDB3\CalendarInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
77
            ];
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public static function deserialize(array $data)
84
    {
85
        return new static(
86
            $data['item_id'],
87
            $data['original_event_id'],
88
            Calendar::deserialize($data['calendar'])
89
        );
90
    }
91
}
92