Completed
Pull Request — master (#264)
by Kristof
04:49
created

EventCopied::getOriginalEventId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace CultuurNet\UDB3\Event\Events;
4
5
use CultuurNet\UDB3\Calendar;
6
use CultuurNet\UDB3\CalendarInterface;
7
use CultuurNet\UDB3\Offer\Events\AbstractEvent;
8
9
class EventCopied extends AbstractEvent
10
{
11
    /**
12
     * @var string
13
     */
14
    private $originalEventId;
15
16
    /**
17
     * @var CalendarInterface|Calendar
18
     */
19
    private $calendar;
20
21
    /**
22
     * EventCopied constructor.
23
     * @param string $eventId
24
     * @param string $originalEventId
25
     * @param CalendarInterface $calendar
26
     */
27
    public function __construct(
28
        $eventId,
29
        $originalEventId,
30
        CalendarInterface $calendar
31
    ) {
32
        parent::__construct($eventId);
33
34
        if (!is_string($originalEventId)) {
35
            throw new \InvalidArgumentException(
36
                'Expected originalEventId to be a string, received ' . gettype($originalEventId)
37
            );
38
        }
39
40
        $this->originalEventId = $originalEventId;
41
        $this->calendar = $calendar;
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function getOriginalEventId()
48
    {
49
        return $this->originalEventId;
50
    }
51
52
    /**
53
     * @return CalendarInterface
54
     */
55
    public function getCalendar()
56
    {
57
        return $this->calendar;
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function serialize()
64
    {
65
        return parent::serialize() + [
66
                'original_event_id' => $this->getOriginalEventId(),
67
                '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...
68
            ];
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public static function deserialize(array $data)
75
    {
76
        return new static(
77
            $data['item_id'],
78
            $data['original_event_id'],
79
            Calendar::deserialize($data['calendar'])
80
        );
81
    }
82
}
83