DefinitionNormalizer::normalize()   F
last analyzed

Complexity

Conditions 26
Paths 2369

Size

Total Lines 97

Duplication

Lines 3
Ratio 3.09 %

Importance

Changes 0
Metric Value
dl 3
loc 97
rs 0
c 0
b 0
f 0
cc 26
nc 2369
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Xabbuh\XApi\Serializer\Symfony\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
use Xabbuh\XApi\Model\IRI;
19
use Xabbuh\XApi\Model\IRL;
20
21
/**
22
 * Normalizes and denormalizes PHP arrays to {@link Definition} instances.
23
 *
24
 * @author Christian Flothmann <[email protected]>
25
 */
26
final class DefinitionNormalizer extends Normalizer
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function normalize($object, $format = null, array $context = array())
32
    {
33
        if (!$object instanceof Definition) {
34
            return;
35
        }
36
37
        $data = array();
38
39
        if (null !== $name = $object->getName()) {
40
            $data['name'] = $this->normalizeAttribute($name, $format, $context);
41
        }
42
43
        if (null !== $description = $object->getDescription()) {
44
            $data['description'] = $this->normalizeAttribute($description, $format, $context);
45
        }
46
47
        if (null !== $type = $object->getType()) {
48
            $data['type'] = $type->getValue();
49
        }
50
51
        if (null !== $moreInfo = $object->getMoreInfo()) {
52
            $data['moreInfo'] = $moreInfo->getValue();
53
        }
54
55 View Code Duplication
        if (null !== $extensions = $object->getExtensions()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
            $data['extensions'] = $this->normalizeAttribute($extensions, $format, $context);
57
        }
58
59
        if ($object instanceof InteractionDefinition) {
60
            if (null !== $correctResponsesPattern = $object->getCorrectResponsesPattern()) {
61
                $data['correctResponsesPattern'] = $object->getCorrectResponsesPattern();
62
            }
63
64
            switch (true) {
65
                case $object instanceof ChoiceInteractionDefinition:
66
                    $data['interactionType'] = 'choice';
67
68
                    if (null !== $choices = $object->getChoices()) {
69
                        $data['choices'] = $this->normalizeAttribute($choices, $format, $context);
70
                    }
71
                    break;
72
                case $object instanceof FillInInteractionDefinition:
73
                    $data['interactionType'] = 'fill-in';
74
                    break;
75
                case $object instanceof LikertInteractionDefinition:
76
                    $data['interactionType'] = 'likert';
77
78
                    if (null !== $scale = $object->getScale()) {
79
                        $data['scale'] = $this->normalizeAttribute($scale, $format, $context);
80
                    }
81
                    break;
82
                case $object instanceof LongFillInInteractionDefinition:
83
                    $data['interactionType'] = 'long-fill-in';
84
                    break;
85
                case $object instanceof MatchingInteractionDefinition:
86
                    $data['interactionType'] = 'matching';
87
88
                    if (null !== $source = $object->getSource()) {
89
                        $data['source'] = $this->normalizeAttribute($source, $format, $context);
90
                    }
91
92
                    if (null !== $target = $object->getTarget()) {
93
                        $data['target'] = $this->normalizeAttribute($target, $format, $context);
94
                    }
95
                    break;
96
                case $object instanceof NumericInteractionDefinition:
97
                    $data['interactionType'] = 'numeric';
98
                    break;
99
                case $object instanceof OtherInteractionDefinition:
100
                    $data['interactionType'] = 'other';
101
                    break;
102
                case $object instanceof PerformanceInteractionDefinition:
103
                    $data['interactionType'] = 'performance';
104
105
                    if (null !== $steps = $object->getSteps()) {
106
                        $data['steps'] = $this->normalizeAttribute($steps, $format, $context);
107
                    }
108
                    break;
109
                case $object instanceof SequencingInteractionDefinition:
110
                    $data['interactionType'] = 'sequencing';
111
112
                    if (null !== $choices = $object->getChoices()) {
113
                        $data['choices'] = $this->normalizeAttribute($choices, $format, $context);
114
                    }
115
                    break;
116
                case $object instanceof TrueFalseInteractionDefinition:
117
                    $data['interactionType'] = 'true-false';
118
                    break;
119
            }
120
        }
121
122
        if (empty($data)) {
123
            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...
124
        }
125
126
        return $data;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function supportsNormalization($data, $format = null)
133
    {
134
        return $data instanceof Definition;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function denormalize($data, $class, $format = null, array $context = array())
141
    {
142
        if (isset($data['interactionType'])) {
143
            switch ($data['interactionType']) {
144
                case 'choice':
145
                    $definition = new ChoiceInteractionDefinition();
146
147 View Code Duplication
                    if (isset($data['choices'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
                        $definition = $definition->withChoices($this->denormalizeData($data['choices'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
149
                    }
150
                    break;
151
                case 'fill-in':
152
                    $definition = new FillInInteractionDefinition();
153
                    break;
154
                case 'likert':
155
                    $definition = new LikertInteractionDefinition();
156
157
                    if (isset($data['scale'])) {
158
                        $definition = $definition->withScale($this->denormalizeData($data['scale'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
159
                    }
160
                    break;
161
                case 'long-fill-in':
162
                    $definition = new LongFillInInteractionDefinition();
163
                    break;
164
                case 'matching':
165
                    $definition = new MatchingInteractionDefinition();
166
167
                    if (isset($data['source'])) {
168
                        $definition = $definition->withSource($this->denormalizeData($data['source'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
169
                    }
170
171
                    if (isset($data['target'])) {
172
                        $definition = $definition->withTarget($this->denormalizeData($data['target'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
173
                    }
174
                    break;
175
                case 'numeric':
176
                    $definition = new NumericInteractionDefinition();
177
                    break;
178
                case 'other':
179
                    $definition = new OtherInteractionDefinition();
180
                    break;
181
                case 'performance':
182
                    $definition = new PerformanceInteractionDefinition();
183
184
                    if (isset($data['steps'])) {
185
                        $definition = $definition->withSteps($this->denormalizeData($data['steps'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
186
                    }
187
                    break;
188
                case 'sequencing':
189
                    $definition = new SequencingInteractionDefinition();
190
191 View Code Duplication
                    if (isset($data['choices'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
                        $definition = $definition->withChoices($this->denormalizeData($data['choices'], 'Xabbuh\XApi\Model\Interaction\InteractionComponent[]', $format, $context));
193
                    }
194
                    break;
195
                case 'true-false':
196
                    $definition = new TrueFalseInteractionDefinition();
197
                    break;
198
                default:
199
                    throw new InvalidArgumentException(sprintf('The interaction type "%s" is not supported.', $data['interactionType']));
200
            }
201
202
            if (isset($data['correctResponsesPattern'])) {
203
                $definition = $definition->withCorrectResponsesPattern($data['correctResponsesPattern']);
204
            }
205
        } else {
206
            $definition = new Definition();
207
        }
208
209 View Code Duplication
        if (isset($data['name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
210
            $name = $this->denormalizeData($data['name'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
211
            $definition = $definition->withName($name);
212
        }
213
214 View Code Duplication
        if (isset($data['description'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
215
            $description = $this->denormalizeData($data['description'], 'Xabbuh\XApi\Model\LanguageMap', $format, $context);
216
            $definition = $definition->withDescription($description);
217
        }
218
219
        if (isset($data['type'])) {
220
            $definition = $definition->withType(IRI::fromString($data['type']));
221
        }
222
223
        if (isset($data['moreInfo'])) {
224
            $definition = $definition->withMoreInfo(IRL::fromString($data['moreInfo']));
225
        }
226
227 View Code Duplication
        if (isset($data['extensions'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
228
            $extensions = $this->denormalizeData($data['extensions'], 'Xabbuh\XApi\Model\Extensions', $format, $context);
229
            $definition = $definition->withExtensions($extensions);
230
        }
231
232
        return $definition;
233
    }
234
235
    /**
236
     * {@inheritdoc}
237
     */
238
    public function supportsDenormalization($data, $type, $format = null)
239
    {
240
        $supportedDefinitionClasses = array(
241
            'Xabbuh\XApi\Model\Definition',
242
            'Xabbuh\XApi\Model\Interaction\ChoiceInteractionDefinition',
243
            'Xabbuh\XApi\Model\Interaction\FillInInteractionDefinition',
244
            'Xabbuh\XApi\Model\Interaction\LikertInteractionDefinition',
245
            'Xabbuh\XApi\Model\Interaction\LongFillInInteractionDefinition',
246
            'Xabbuh\XApi\Model\Interaction\MatchingInteractionDefinition',
247
            'Xabbuh\XApi\Model\Interaction\NumericInteractionDefinition',
248
            'Xabbuh\XApi\Model\Interaction\OtherInteractionDefinition',
249
            'Xabbuh\XApi\Model\Interaction\PerformanceInteractionDefinition',
250
            'Xabbuh\XApi\Model\Interaction\SequencingInteractionDefinition',
251
            'Xabbuh\XApi\Model\Interaction\TrueFalseInteractionDefinition',
252
        );
253
254
        return in_array($type, $supportedDefinitionClasses, true);
255
    }
256
}
257