JsonMessageSerializer::serialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace PSB\Core\Serialization\Json;
3
4
5
use PSB\Core\ContentTypeEnum;
6
use PSB\Core\Serialization\MessageSerializerInterface;
7
8
class JsonMessageSerializer implements MessageSerializerInterface
9
{
10
    /**
11
     * @var JsonSerializer
12
     */
13
    private $serializer;
14
15
    /**
16
     * @param JsonSerializer $serializer
17
     */
18 4
    public function __construct(JsonSerializer $serializer)
19
    {
20 4
        $this->serializer = $serializer;
21 4
    }
22
23
    /**
24
     * Serializes an object or an array of objects into a string
25
     *
26
     * @param object $message
27
     *
28
     * @return string
29
     */
30 1
    public function serialize($message)
31
    {
32 1
        return $this->serializer->serialize($message);
33
    }
34
35
    /**
36
     * Deserializes a set of messages from the given string
37
     *
38
     * @param string $string
39
     * @param string $messageType
40
     *
41
     * @return object
42
     */
43 1
    public function deserialize($string, $messageType)
44
    {
45 1
        return $this->serializer->unserialize($string);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->serializer->unserialize($string); (integer|double|string|boolean|null|object|array) is incompatible with the return type declared by the interface PSB\Core\Serialization\M...rInterface::deserialize of type object.

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...
46
    }
47
48
    /**
49
     * Returns the content type handled by this serializer
50
     *
51
     * @return string
52
     */
53 1
    public function getContentType()
54
    {
55 1
        return ContentTypeEnum::JSON;
56
    }
57
}
58