Completed
Push — master ( 5e72fe...19cec1 )
by Yanick
01:32
created

Serializer::denormalize()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
cc 5
nc 4
nop 4
1
<?php
2
3
/*
4
 * (c) Kévin Dunglas <[email protected]>
5
 *
6
 * This source file is subject to the MIT license that is bundled
7
 * with this source code in the file LICENSE.
8
 */
9
10
namespace Dunglas\DoctrineJsonOdm;
11
12
use Symfony\Component\Serializer\Serializer as BaseSerializer;
13
14
final class Serializer extends BaseSerializer
15
{
16
    private const KEY_TYPE = '#type';
17
    private const KEY_SCALAR = '#scalar';
18
19
    public function normalize($data, $format = null, array $context = [])
20
    {
21
        $normalizedData = parent::normalize($data, $format, $context);
22
23
        if (is_object($data)) {
24
            $typeData = [self::KEY_TYPE => get_class($data)];
25
            $valueData = is_scalar($normalizedData) ? [self::KEY_SCALAR => $normalizedData] : $normalizedData;
26
            $normalizedData = array_merge($typeData, $valueData);
27
        }
28
29
        return $normalizedData;
30
    }
31
32
    public function denormalize($data, $class, $format = null, array $context = [])
33
    {
34
        if (is_array($data) && (isset($data[self::KEY_TYPE]))) {
35
            $type = $data[self::KEY_TYPE];
36
            unset($data[self::KEY_TYPE]);
37
38
            $data = $data[self::KEY_SCALAR] ?? $data;
39
            $data = $this->denormalize($data, $type, $format, $context);
40
41
            return parent::denormalize($data, $type, $format, $context);
42
        }
43
44
        if (is_iterable($data)) {
45
            $class = ($class === '') ? 'stdClass' : $class;
46
47
            return parent::denormalize($data, $class.'[]', $format, $context);
48
        }
49
50
        return $data;
0 ignored issues
show
Bug Best Practice introduced by Antonio J. García Lagar
The return type of return $data; (object|integer|double|string|null|boolean|array) is incompatible with the return type declared by the interface Symfony\Component\Serial...rInterface::denormalize 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...
51
    }
52
}
53