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

ResultNormalizer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 75
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 75
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A supportsNormalization() 0 4 1
B denormalize() 0 11 7
A supportsDenormalization() 0 4 1
D normalize() 0 38 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\Result;
15
16
/**
17
 * Normalizes and denormalizes xAPI statement results.
18
 *
19
 * @author Christian Flothmann <[email protected]>
20
 */
21
final class ResultNormalizer extends Normalizer
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 16
    public function normalize($object, $format = null, array $context = array())
27
    {
28 16
        if (!$object instanceof Result) {
29
            return null;
30
        }
31
32 16
        $data = array();
33
34 16
        if (null !== $object->getScore()) {
35 8
            $data['score'] = $this->normalizeAttribute($object->getScore(), 'Xabbuh\XApi\Model\Score', $context);
36
        }
37
38 16
        if (null !== $success = $object->getSuccess()) {
39 3
            $data['success'] = $success;
40
        }
41
42 16
        if (null !== $completion = $object->getCompletion()) {
43 3
            $data['completion'] = $completion;
44
        }
45
46 16
        if (null !== $response = $object->getResponse()) {
47 3
            $data['response'] = $response;
48
        }
49
50 16
        if (null !== $duration = $object->getDuration()) {
51 4
            $data['duration'] = $duration;
52
        }
53
54 16
        if (null !== $extensions = $object->getExtensions()) {
55 3
            $data['extensions'] = $this->normalizeAttribute($extensions, 'Xabbuh\XApi\Model\Extensions', $context);
56
        }
57
58 16
        if (empty($data)) {
59 2
            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...
60
        }
61
62 14
        return $data;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 89
    public function supportsNormalization($data, $format = null)
69
    {
70 89
        return $data instanceof Result;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 16
    public function denormalize($data, $class, $format = null, array $context = array())
77
    {
78 16
        $score = isset($data['score']) ? $this->denormalizeData($data['score'], 'Xabbuh\XApi\Model\Score', $format, $context) : null;
79 16
        $success = isset($data['success']) ? $data['success'] : null;
80 16
        $completion = isset($data['completion']) ? $data['completion'] : null;
81 16
        $response = isset($data['response']) ? $data['response'] : null;
82 16
        $duration = isset($data['duration']) ? $data['duration'] : null;
83 16
        $extensions = isset($data['extensions']) ? $this->denormalizeData($data['extensions'], 'Xabbuh\XApi\Model\Extensions', $format, $context) : null;
84
85 16
        return new Result($score, $success, $completion, $response, $duration, $extensions);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 90
    public function supportsDenormalization($data, $type, $format = null)
92
    {
93 90
        return 'Xabbuh\XApi\Model\Result' === $type;
94
    }
95
}
96