Completed
Push — master ( 69fb76...ff5317 )
by
unknown
04:59
created

AbstractFacilitiesUpdated   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getFacilities() 0 4 1
A deserialize() 0 10 2
A serialize() 0 12 2
1
<?php
2
3
namespace CultuurNet\UDB3\Offer\Events;
4
5
use CultuurNet\UDB3\Facility;
6
7
abstract class AbstractFacilitiesUpdated extends AbstractEvent
8
{
9
    /**
10
     * The new facilities.
11
     * @var array
12
     */
13
    protected $facilities;
14
15
    /**
16
     * @param string $id
17
     * @param array $facilities
18
     */
19
    public function __construct($id, array $facilities)
20
    {
21
        parent::__construct($id);
22
        $this->facilities = $facilities;
23
    }
24
25
    /**
26
     * @return array
27
     */
28
    public function getFacilities()
29
    {
30
        return $this->facilities;
31
    }
32
33
    /**
34
     * @param array $data
35
     * @return mixed The object instance
36
     */
37
    public static function deserialize(array $data)
38
    {
39
40
        $facilities = array();
41
        foreach ($data['facilities'] as $facility) {
42
            $facilities[] = Facility::deserialize($facility);
43
        }
44
45
        return new static($data['item_id'], $facilities);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function serialize()
52
    {
53
54
        $facilities = array();
55
        foreach ($this->facilities as $facility) {
56
            $facilities[] = $facility->serialize();
57
        }
58
59
        return parent::serialize() + array(
0 ignored issues
show
Best Practice introduced by
The expression return parent::serialize...ities' => $facilities); seems to be an array, but some of its elements' types (array) are incompatible with the return type of the parent method CultuurNet\UDB3\Offer\Ev...bstractEvent::serialize of type array<string,string>.

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 new Author('Johannes');
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return '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
                'facilities' => $facilities,
61
            );
62
    }
63
}
64