Completed
Pull Request — master (#17)
by Christian
03:36
created

DefinitionNormalizer::supportsNormalization()   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 2
crap 1
1
<?php
2
3
namespace Xabbuh\XApi\Serializer\Normalizer;
4
5
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
6
use Xabbuh\XApi\Model\Definition;
7
use Xabbuh\XApi\Model\Interaction\ChoiceInteraction;
8
use Xabbuh\XApi\Model\Interaction\FillInInteraction;
9
use Xabbuh\XApi\Model\Interaction\Interaction;
10
use Xabbuh\XApi\Model\Interaction\LikertInteraction;
11
use Xabbuh\XApi\Model\Interaction\LongFillInInteraction;
12
use Xabbuh\XApi\Model\Interaction\MatchingInteraction;
13
use Xabbuh\XApi\Model\Interaction\NumericInteraction;
14
use Xabbuh\XApi\Model\Interaction\OtherInteraction;
15
use Xabbuh\XApi\Model\Interaction\PerformanceInteraction;
16
use Xabbuh\XApi\Model\Interaction\SequencingInteraction;
17
use Xabbuh\XApi\Model\Interaction\TrueFalseInteraction;
18
19
/**
20
 * Denormalizes PHP arrays to {@link Definition} instances.
21
 *
22
 * @author Christian Flothmann <[email protected]>
23
 */
24
final class DefinitionNormalizer extends Normalizer
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 11
    public function normalize($object, $format = null, array $context = array())
30
    {
31 11
        if (!$object instanceof Definition) {
32
            return;
33
        }
34
35 11
        $data = array();
36
37 11
        if (null !== $name = $object->getName()) {
38 3
            $data['name'] = $this->normalizeAttribute($name, $format, $context);
39
        }
40
41 11
        if (null !== $description = $object->getDescription()) {
42 3
            $data['description'] = $this->normalizeAttribute($description, $format, $context);
43
        }
44
45 11
        if (null !== $type = $object->getType()) {
46 3
            $data['type'] = $type;
47
        }
48
49 11
        if (null !== $moreInfo = $object->getMoreInfo()) {
50 2
            $data['moreInfo'] = $moreInfo;
51
        }
52
53 11
        if ($object instanceof Interaction) {
54
            if (null !== $correctResponsesPattern = $object->getCorrectResponsesPattern()) {
55
                $data['correctResponsesPattern'] = $object->getCorrectResponsesPattern();
56
            }
57
58
            switch (true) {
59
                case $object instanceof ChoiceInteraction:
60
                    $data['interactionType'] = 'choice';
61
62
                    if (null !== $choices = $object->getChoices()) {
63
                        $data['choices'] = $this->normalizeAttribute($choices, $format, $context);
64
                    }
65
                    break;
66
                case $object instanceof FillInInteraction:
67
                    $data['interactionType'] = 'fill-in';
68
                    break;
69
                case $object instanceof LikertInteraction:
70
                    $data['interactionType'] = 'likert';
71
72
                    if (null !== $scale = $object->getScale()) {
73
                        $data['scale'] = $this->normalizeAttribute($scale, $format, $context);
74
                    }
75
                    break;
76
                case $object instanceof LongFillInInteraction:
77
                    $data['interactionType'] = 'long-fill-in';
78
                    break;
79
                case $object instanceof MatchingInteraction:
80
                    $data['interactionType'] = 'matching';
81
82
                    if (null !== $source = $object->getSource()) {
83
                        $data['source'] = $this->normalizeAttribute($source, $format, $context);
84
                    }
85
86
                    if (null !== $target = $object->getTarget()) {
87
                        $data['target'] = $this->normalizeAttribute($target, $format, $context);
88
                    }
89
                    break;
90
                case $object instanceof NumericInteraction:
91
                    $data['interactionType'] = 'numeric';
92
                    break;
93
                case $object instanceof OtherInteraction:
94
                    $data['interactionType'] = 'other';
95
                    break;
96
                case $object instanceof PerformanceInteraction:
97
                    $data['interactionType'] = 'performance';
98
99
                    if (null !== $steps = $object->getSteps()) {
100
                        $data['steps'] = $this->normalizeAttribute($steps, $format, $context);
101
                    }
102
                    break;
103
                case $object instanceof SequencingInteraction:
104
                    $data['interactionType'] = 'sequencing';
105
106
                    if (null !== $choices = $object->getChoices()) {
107
                        $data['choices'] = $this->normalizeAttribute($choices, $format, $context);
108
                    }
109
                    break;
110
                case $object instanceof TrueFalseInteraction:
111
                    $data['interactionType'] = 'true-false';
112
                    break;
113
            }
114
        }
115
116 11
        if (empty($data)) {
117 5
            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...
118
        }
119
120 6
        return $data;
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126 126
    public function supportsNormalization($data, $format = null)
127
    {
128 126
        return $data instanceof Definition;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 13
    public function denormalize($data, $class, $format = null, array $context = array())
135
    {
136 13
        $name = null;
137 13
        $description = null;
138 13
        $type = isset($data['type']) ? $data['type'] : null;
139 13
        $moreInfo = isset($data['moreInfo']) ? $data['moreInfo'] : null;
140
141 13
        if (isset($data['name'])) {
142 5
            $name = $this->denormalizeData($data['name'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
143
        }
144
145 13
        if (isset($data['description'])) {
146 3
            $description = $this->denormalizeData($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
147
        }
148
149 13
        if (isset($data['interactionType'])) {
150
            $definition = null;
0 ignored issues
show
Unused Code introduced by
$definition is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
151
152
            switch ($data['interactionType']) {
153
                case 'choice':
154
                    $definition = new ChoiceInteraction($name, $description, $type, $moreInfo);
155
                    break;
156
                case 'fill-in':
157
                    $definition = new FillInInteraction($name, $description, $type, $moreInfo);
158
                    break;
159
                case 'likert':
160
                    $definition = new LikertInteraction($name, $description, $type, $moreInfo);
161
                    break;
162
                case 'long-fill-in':
163
                    $definition = new LongFillInInteraction($name, $description, $type, $moreInfo);
164
                    break;
165
                case 'matching':
166
                    $definition = new MatchingInteraction($name, $description, $type, $moreInfo);
167
                    break;
168
                case 'numeric':
169
                    $definition = new NumericInteraction($name, $description, $type, $moreInfo);
170
                    break;
171
                case 'other':
172
                    $definition = new OtherInteraction($name, $description, $type, $moreInfo);
173
                    break;
174
                case 'performance':
175
                    $definition = new PerformanceInteraction($name, $description, $type, $moreInfo);
176
                    break;
177
                case 'sequencing':
178
                    $definition = new SequencingInteraction($name, $description, $type, $moreInfo);
179
                    break;
180
                case 'true-false':
181
                    $definition = new TrueFalseInteraction($name, $description, $type, $moreInfo);
182
                    break;
183
                default:
184
                    throw new InvalidArgumentException(sprintf('The interaction type "%s" is not supported.', $data['interactionType']));
185
            }
186
187
            if (isset($data['correctResponsesPattern'])) {
188
                $definition = $definition->withCorrectResponsesPattern($data['correctResponsesPattern']);
189
            }
190
191
            return $definition;
192
        }
193
194 13
        return new Definition($name, $description, $type, $moreInfo);
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200 132
    public function supportsDenormalization($data, $type, $format = null)
201
    {
202
        $supportedDefinitionClasses = array(
203 132
            'Xabbuh\XApi\Model\Definition',
204
            'Xabbuh\XApi\Model\Interaction\ChoiceInteraction',
205
            'Xabbuh\XApi\Model\Interaction\FillInInteraction',
206
            'Xabbuh\XApi\Model\Interaction\LikertInteraction',
207
            'Xabbuh\XApi\Model\Interaction\LongFillInInteraction',
208
            'Xabbuh\XApi\Model\Interaction\MatchingInteraction',
209
            'Xabbuh\XApi\Model\Interaction\NumericInteraction',
210
            'Xabbuh\XApi\Model\Interaction\OtherInteraction',
211
            'Xabbuh\XApi\Model\Interaction\PerformanceInteraction',
212
            'Xabbuh\XApi\Model\Interaction\SequencingInteraction',
213
            'Xabbuh\XApi\Model\Interaction\TrueFalseInteraction',
214
        );
215
216 132
        return in_array($type, $supportedDefinitionClasses, true);
217
    }
218
}
219