Completed
Push — master ( 4a26c6...5bced3 )
by Christian
02:21
created

ContextNormalizer::supportsDenormalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 3
crap 1
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\Normalizer;
13
14
use Xabbuh\XApi\Model\Context;
15
16
/**
17
 * Normalizes and denormalizes xAPI statement contexts.
18
 *
19
 * @author Christian Flothmann <[email protected]>
20
 */
21
final class ContextNormalizer extends Normalizer
22
{
23 16
    public function normalize($object, $format = null, array $context = array())
24
    {
25 16
        if (!$object instanceof Context) {
26
            return;
27
        }
28
29 16
        $data = array();
30
31 16
        if (null !== $registration = $object->getRegistration()) {
32 1
            $data['registration'] = $registration;
33
        }
34
35 16
        if (null !== $instructor = $object->getInstructor()) {
36 6
            $data['instructor'] = $this->normalizeAttribute($instructor, $format, $context);
37
        }
38
39 16
        if (null !== $team = $object->getTeam()) {
40 2
            $data['team'] = $this->normalizeAttribute($team, $format, $context);
41
        }
42
43 16
        if (null !== $contextActivities = $object->getContextActivities()) {
44 4
            $data['contextActivities'] = $this->normalizeAttribute($contextActivities, $format, $context);
45
        }
46
47 16
        if (null !== $revision = $object->getRevision()) {
48 1
            $data['revision'] = $revision;
49
        }
50
51 16
        if (null !== $platform = $object->getPlatform()) {
52 1
            $data['platform'] = $platform;
53
        }
54
55 16
        if (null !== $language = $object->getLanguage()) {
56 1
            $data['language'] = $language;
57
        }
58
59 16
        if (null !== $statement = $object->getStatement()) {
60 2
            $data['statement'] = $this->normalizeAttribute($statement, $format, $context);
61
        }
62
63 16
        if (null !== $extensions = $object->getExtensions()) {
64 3
            $data['extensions'] = $this->normalizeAttribute($extensions, $format, $context);
65
        }
66
67 16
        if (empty($data)) {
68 3
            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|integer|double|string|boolean.

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...
69
        }
70
71 13
        return $data;
72
    }
73
74 113
    public function supportsNormalization($data, $format = null)
75
    {
76 113
        return $data instanceof Context;
77
    }
78
79 16
    public function denormalize($data, $class, $format = null, array $context = array())
80
    {
81 16
        $statementContext = new Context();
82
83 16
        if (isset($data['registration'])) {
84 1
            $statementContext = $statementContext->withRegistration($data['registration']);
85
        }
86
87 16
        if (isset($data['instructor'])) {
88 6
            $statementContext = $statementContext->withInstructor($this->denormalizeData($data['instructor'], 'Xabbuh\XApi\Model\Actor', $format, $context));
89
        }
90
91 16
        if (isset($data['team'])) {
92 2
            $statementContext = $statementContext->withTeam($this->denormalizeData($data['team'], 'Xabbuh\XApi\Model\Group', $format, $context));
93
        }
94
95 16
        if (isset($data['contextActivities'])) {
96 4
            $statementContext = $statementContext->withContextActivities($this->denormalizeData($data['contextActivities'], 'Xabbuh\XApi\Model\ContextActivities', $format, $context));
97
        }
98
99 16
        if (isset($data['revision'])) {
100 1
            $statementContext = $statementContext->withRevision($data['revision']);
101
        }
102
103 16
        if (isset($data['platform'])) {
104 1
            $statementContext = $statementContext->withPlatform($data['platform']);
105
        }
106
107 16
        if (isset($data['language'])) {
108 1
            $statementContext = $statementContext->withLanguage($data['language']);
109
        }
110
111 16
        if (isset($data['statement'])) {
112 2
            $statementContext = $statementContext->withStatement($this->denormalizeData($data['statement'], 'Xabbuh\XApi\Model\StatementReference', $format, $context));
113
        }
114
115 16
        if (isset($data['extensions'])) {
116 3
            $statementContext = $statementContext->withExtensions($this->denormalizeData($data['extensions'], 'Xabbuh\XApi\Model\Extensions', $format, $context));
117
        }
118
119 16
        return $statementContext;
120
    }
121
122 114
    public function supportsDenormalization($data, $type, $format = null)
123
    {
124 114
        return 'Xabbuh\XApi\Model\Context' === $type;
125
    }
126
}
127