ContextNormalizer::normalize()   F
last analyzed

Complexity

Conditions 12
Paths 1025

Size

Total Lines 50

Duplication

Lines 3
Ratio 6 %

Importance

Changes 0
Metric Value
dl 3
loc 50
rs 2.8
c 0
b 0
f 0
cc 12
nc 1025
nop 3

How to fix   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
/*
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 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
    public function normalize($object, $format = null, array $context = array())
24
    {
25
        if (!$object instanceof Context) {
26
            return;
27
        }
28
29
        $data = array();
30
31
        if (null !== $registration = $object->getRegistration()) {
32
            $data['registration'] = $registration;
33
        }
34
35
        if (null !== $instructor = $object->getInstructor()) {
36
            $data['instructor'] = $this->normalizeAttribute($instructor, $format, $context);
37
        }
38
39
        if (null !== $team = $object->getTeam()) {
40
            $data['team'] = $this->normalizeAttribute($team, $format, $context);
41
        }
42
43
        if (null !== $contextActivities = $object->getContextActivities()) {
44
            $data['contextActivities'] = $this->normalizeAttribute($contextActivities, $format, $context);
45
        }
46
47
        if (null !== $revision = $object->getRevision()) {
48
            $data['revision'] = $revision;
49
        }
50
51
        if (null !== $platform = $object->getPlatform()) {
52
            $data['platform'] = $platform;
53
        }
54
55
        if (null !== $language = $object->getLanguage()) {
56
            $data['language'] = $language;
57
        }
58
59
        if (null !== $statement = $object->getStatement()) {
60
            $data['statement'] = $this->normalizeAttribute($statement, $format, $context);
61
        }
62
63 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...
64
            $data['extensions'] = $this->normalizeAttribute($extensions, $format, $context);
65
        }
66
67
        if (empty($data)) {
68
            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...
69
        }
70
71
        return $data;
72
    }
73
74
    public function supportsNormalization($data, $format = null)
75
    {
76
        return $data instanceof Context;
77
    }
78
79
    public function denormalize($data, $class, $format = null, array $context = array())
80
    {
81
        $statementContext = new Context();
82
83
        if (isset($data['registration'])) {
84
            $statementContext = $statementContext->withRegistration($data['registration']);
85
        }
86
87
        if (isset($data['instructor'])) {
88
            $statementContext = $statementContext->withInstructor($this->denormalizeData($data['instructor'], 'Xabbuh\XApi\Model\Actor', $format, $context));
89
        }
90
91
        if (isset($data['team'])) {
92
            $statementContext = $statementContext->withTeam($this->denormalizeData($data['team'], 'Xabbuh\XApi\Model\Group', $format, $context));
93
        }
94
95
        if (isset($data['contextActivities'])) {
96
            $statementContext = $statementContext->withContextActivities($this->denormalizeData($data['contextActivities'], 'Xabbuh\XApi\Model\ContextActivities', $format, $context));
97
        }
98
99
        if (isset($data['revision'])) {
100
            $statementContext = $statementContext->withRevision($data['revision']);
101
        }
102
103
        if (isset($data['platform'])) {
104
            $statementContext = $statementContext->withPlatform($data['platform']);
105
        }
106
107
        if (isset($data['language'])) {
108
            $statementContext = $statementContext->withLanguage($data['language']);
109
        }
110
111
        if (isset($data['statement'])) {
112
            $statementContext = $statementContext->withStatement($this->denormalizeData($data['statement'], 'Xabbuh\XApi\Model\StatementReference', $format, $context));
113
        }
114
115 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...
116
            $statementContext = $statementContext->withExtensions($this->denormalizeData($data['extensions'], 'Xabbuh\XApi\Model\Extensions', $format, $context));
117
        }
118
119
        return $statementContext;
120
    }
121
122
    public function supportsDenormalization($data, $type, $format = null)
123
    {
124
        return 'Xabbuh\XApi\Model\Context' === $type;
125
    }
126
}
127