Completed
Pull Request — master (#17)
by Christian
07:24
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\ChoiceInteractionDefinition;
8
use Xabbuh\XApi\Model\Interaction\FillInInteractionDefinition;
9
use Xabbuh\XApi\Model\Interaction\InteractionDefinition;
10
use Xabbuh\XApi\Model\Interaction\LikertInteractionDefinition;
11
use Xabbuh\XApi\Model\Interaction\LongFillInInteractionDefinition;
12
use Xabbuh\XApi\Model\Interaction\MatchingInteractionDefinition;
13
use Xabbuh\XApi\Model\Interaction\NumericInteractionDefinition;
14
use Xabbuh\XApi\Model\Interaction\OtherInteractionDefinition;
15
use Xabbuh\XApi\Model\Interaction\PerformanceInteractionDefinition;
16
use Xabbuh\XApi\Model\Interaction\SequencingInteractionDefinition;
17
use Xabbuh\XApi\Model\Interaction\TrueFalseInteractionDefinition;
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 InteractionDefinition) {
54
            if (null !== $correctResponsesPattern = $object->getCorrectResponsesPattern()) {
55
                $data['correctResponsesPattern'] = $object->getCorrectResponsesPattern();
56
            }
57
58
            switch (true) {
59
                case $object instanceof ChoiceInteractionDefinition:
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 FillInInteractionDefinition:
67
                    $data['interactionType'] = 'fill-in';
68
                    break;
69
                case $object instanceof LikertInteractionDefinition:
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 LongFillInInteractionDefinition:
77
                    $data['interactionType'] = 'long-fill-in';
78
                    break;
79
                case $object instanceof MatchingInteractionDefinition:
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 NumericInteractionDefinition:
91
                    $data['interactionType'] = 'numeric';
92
                    break;
93
                case $object instanceof OtherInteractionDefinition:
94
                    $data['interactionType'] = 'other';
95
                    break;
96
                case $object instanceof PerformanceInteractionDefinition:
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 SequencingInteractionDefinition:
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 TrueFalseInteractionDefinition:
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 ChoiceInteractionDefinition($name, $description, $type, $moreInfo);
155
156
                    if (isset($data['choices'])) {
157
                        $definition = $definition->withChoices($this->denormalizeData($data['choices'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
158
                    }
159
160
                    break;
161
                case 'fill-in':
162
                    $definition = new FillInInteractionDefinition($name, $description, $type, $moreInfo);
163
                    break;
164
                case 'likert':
165
                    $definition = new LikertInteractionDefinition($name, $description, $type, $moreInfo);
166
167
                    if (isset($data['scale'])) {
168
                        $definition = $definition->withScale($this->denormalizeData($data['scale'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
169
                    }
170
171
                    break;
172
                case 'long-fill-in':
173
                    $definition = new LongFillInInteractionDefinition($name, $description, $type, $moreInfo);
174
                    break;
175
                case 'matching':
176
                    $definition = new MatchingInteractionDefinition($name, $description, $type, $moreInfo);
177
178
                    if (isset($data['source'])) {
179
                        $definition = $definition->withSource($this->denormalizeData($data['source'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
180
                    }
181
182
                    if (isset($data['target'])) {
183
                        $definition = $definition->withTarget($this->denormalizeData($data['target'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
184
                    }
185
186
                    break;
187
                case 'numeric':
188
                    $definition = new NumericInteractionDefinition($name, $description, $type, $moreInfo);
189
                    break;
190
                case 'other':
191
                    $definition = new OtherInteractionDefinition($name, $description, $type, $moreInfo);
192
                    break;
193
                case 'performance':
194
                    $definition = new PerformanceInteractionDefinition($name, $description, $type, $moreInfo);
195
196
                    if (isset($data['steps'])) {
197
                        $definition = $definition->withSteps($this->denormalizeData($data['steps'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
198
                    }
199
200
                    break;
201
                case 'sequencing':
202
                    $definition = new SequencingInteractionDefinition($name, $description, $type, $moreInfo);
203
204
                    if (isset($data['choices'])) {
205
                        $definition = $definition->withChoices($this->denormalizeData($data['choices'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
206
                    }
207
208
                    break;
209
                case 'true-false':
210
                    $definition = new TrueFalseInteractionDefinition($name, $description, $type, $moreInfo);
211
                    break;
212
                default:
213
                    throw new InvalidArgumentException(sprintf('The interaction type "%s" is not supported.', $data['interactionType']));
214
            }
215
216
            if (isset($data['correctResponsesPattern'])) {
217
                $definition = $definition->withCorrectResponsesPattern($data['correctResponsesPattern']);
218
            }
219
220
            return $definition;
221
        }
222
223 13
        return new Definition($name, $description, $type, $moreInfo);
224
    }
225
226
    /**
227
     * {@inheritdoc}
228
     */
229 132
    public function supportsDenormalization($data, $type, $format = null)
230
    {
231
        $supportedDefinitionClasses = array(
232 132
            'Xabbuh\XApi\Model\Definition',
233
            'Xabbuh\XApi\Model\Interaction\ChoiceInteractionDefinition',
234
            'Xabbuh\XApi\Model\Interaction\FillInInteractionDefinition',
235
            'Xabbuh\XApi\Model\Interaction\LikertInteractionDefinition',
236
            'Xabbuh\XApi\Model\Interaction\LongFillInInteractionDefinition',
237
            'Xabbuh\XApi\Model\Interaction\MatchingInteractionDefinition',
238
            'Xabbuh\XApi\Model\Interaction\NumericInteractionDefinition',
239
            'Xabbuh\XApi\Model\Interaction\OtherInteractionDefinition',
240
            'Xabbuh\XApi\Model\Interaction\PerformanceInteractionDefinition',
241
            'Xabbuh\XApi\Model\Interaction\SequencingInteractionDefinition',
242
            'Xabbuh\XApi\Model\Interaction\TrueFalseInteractionDefinition',
243
        );
244
245 132
        return in_array($type, $supportedDefinitionClasses, true);
246
    }
247
}
248