ExtensionsNormalizer::denormalize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 4
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\XApi\Serializer\Symfony\Normalizer;
13
14
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
15
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
16
use Xabbuh\XApi\Model\Extensions;
17
use Xabbuh\XApi\Model\IRI;
18
19
/**
20
 * Normalizes and denormalizes xAPI extensions.
21
 *
22
 * @author Christian Flothmann <[email protected]>
23
 */
24
final class ExtensionsNormalizer implements DenormalizerInterface, NormalizerInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function normalize($object, $format = null, array $context = array())
30
    {
31
        if (!$object instanceof Extensions) {
32
            return;
33
        }
34
35
        $extensions = $object->getExtensions();
36
37
        if (count($extensions) === 0) {
38
            return new \stdClass();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \stdClass(); (stdClass) is incompatible with the return type declared by the interface Symfony\Component\Serial...zerInterface::normalize of type array|string|integer|dou...oolean|ArrayObject|null.

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...
39
        }
40
41
        $data = array();
42
43
        foreach ($extensions as $iri) {
44
            $data[$iri->getValue()] = $extensions[$iri];
45
        }
46
47
        return $data;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function supportsNormalization($data, $format = null)
54
    {
55
       return $data instanceof Extensions;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function denormalize($data, $class, $format = null, array $context = array())
62
    {
63
        $extensions = new \SplObjectStorage();
64
65
        foreach ($data as $iri => $value) {
66
            $extensions->attach(IRI::fromString($iri), $value);
67
        }
68
69
        return new Extensions($extensions);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function supportsDenormalization($data, $type, $format = null)
76
    {
77
        return 'Xabbuh\XApi\Model\Extensions' === $type;
78
    }
79
}
80