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

ContextActivitiesNormalizer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 0
cbo 2
dl 0
loc 81
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsNormalization() 0 4 1
A supportsDenormalization() 0 4 1
C normalize() 0 30 7
D denormalize() 0 25 9
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\ContextActivities;
15
16
/**
17
 * Normalizes and denormalizes xAPI statement context activities.
18
 *
19
 * @author Christian Flothmann <[email protected]>
20
 */
21
final class ContextActivitiesNormalizer extends Normalizer
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 12
    public function normalize($object, $format = null, array $context = array())
27
    {
28 12
        if (!$object instanceof ContextActivities) {
29
            return;
30
        }
31
32 12
        $data = array();
33
34 12
        if (null !== $categoryActivities = $object->getCategoryActivities()) {
35 6
            $data['category'] = $this->normalizeAttribute($categoryActivities);
36
        }
37
38 12
        if (null !== $parentActivities = $object->getParentActivities()) {
39 6
            $data['parent'] = $this->normalizeAttribute($parentActivities);
40
        }
41
42 12
        if (null !== $groupingActivities = $object->getGroupingActivities()) {
43 6
            $data['grouping'] = $this->normalizeAttribute($groupingActivities);
44
        }
45
46 12
        if (null !== $otherActivities = $object->getOtherActivities()) {
47 6
            $data['other'] = $this->normalizeAttribute($otherActivities);
48
        }
49
50 12
        if (empty($data)) {
51 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...
52
        }
53
54 9
        return $data;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 106
    public function supportsNormalization($data, $format = null)
61
    {
62 106
        return $data instanceof ContextActivities;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 12
    public function denormalize($data, $class, $format = null, array $context = array())
69
    {
70 12
        $parentActivities = null;
71 12
        $groupingActivities = null;
72 12
        $categoryActivities = null;
73 12
        $otherActivities = null;
74
75 12
        if (isset($data['parent']) && null !== $data['parent']) {
76 6
            $parentActivities = $this->denormalizeData($data['parent'], 'Xabbuh\XApi\Model\Activity[]', $format, $context);
77
        }
78
79 12
        if (isset($data['grouping']) && null !== $data['grouping']) {
80 6
            $groupingActivities = $this->denormalizeData($data['grouping'], 'Xabbuh\XApi\Model\Activity[]', $format, $context);
81
        }
82
83 12
        if (isset($data['category']) && null !== $data['category']) {
84 6
            $categoryActivities = $this->denormalizeData($data['category'], 'Xabbuh\XApi\Model\Activity[]', $format, $context);
85
        }
86
87 12
        if (isset($data['other']) && null !== $data['other']) {
88 6
            $otherActivities = $this->denormalizeData($data['other'], 'Xabbuh\XApi\Model\Activity[]', $format, $context);
89
        }
90
91 12
        return new ContextActivities($parentActivities, $groupingActivities, $categoryActivities, $otherActivities);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 107
    public function supportsDenormalization($data, $type, $format = null)
98
    {
99 107
        return 'Xabbuh\XApi\Model\ContextActivities' === $type;
100
    }
101
}
102