Completed
Pull Request — master (#12)
by Christian
03:53
created

ResultNormalizer::normalize()   D

Complexity

Conditions 9
Paths 129

Size

Total Lines 38
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 14
cts 14
cp 1
rs 4.6666
c 0
b 0
f 0
cc 9
eloc 19
nc 129
nop 3
crap 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 11
    public function normalize($object, $format = null, array $context = array())
27
    {
28 11
        if (!$object instanceof Result) {
29
            return null;
30
        }
31
32 11
        $data = array();
33
34 11
        if (null !== $object->getScore()) {
35 7
            $data['score'] = $this->normalizeAttribute($object->getScore(), 'Xabbuh\XApi\Model\Score', $context);
36
        }
37
38 11
        if (null !== $success = $object->getSuccess()) {
39 2
            $data['success'] = $success;
40
        }
41
42 11
        if (null !== $completion = $object->getCompletion()) {
43 2
            $data['completion'] = $completion;
44
        }
45
46 11
        if (null !== $response = $object->getResponse()) {
47 2
            $data['response'] = $response;
48
        }
49
50 11
        if (null !== $duration = $object->getDuration()) {
51 3
            $data['duration'] = $duration;
52
        }
53
54 11
        if (null !== $extensions = $object->getExtensions()) {
0 ignored issues
show
Bug introduced by
The method getExtensions() does not seem to exist on object<Xabbuh\XApi\Model\Result>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
            $data['extensions'] = $this->normalizeAttribute($extensions, 'Xabbuh\XApi\Model\Extensions', $context);
56
        }
57
58
        if (empty($data)) {
59
            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
        return $data;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 74
    public function supportsNormalization($data, $format = null)
69
    {
70 74
        return $data instanceof Result;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 11
    public function denormalize($data, $class, $format = null, array $context = array())
77
    {
78 11
        $score = isset($data['score']) ? $this->denormalizeData($data['score'], 'Xabbuh\XApi\Model\Score', $format, $context) : null;
79 11
        $success = isset($data['success']) ? $data['success'] : null;
80 11
        $completion = isset($data['completion']) ? $data['completion'] : null;
81 11
        $response = isset($data['response']) ? $data['response'] : null;
82 11
        $duration = isset($data['duration']) ? $data['duration'] : null;
83 11
        $extensions = isset($data['extensions']) ? $this->denormalizeData($data['extensions'], 'Xabbuh\XApi\Model\Extensions', $format, $context) : null;
84
85 11
        return new Result($score, $success, $completion, $response, $duration, $extensions);
0 ignored issues
show
Unused Code introduced by
The call to Result::__construct() has too many arguments starting with $extensions.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 75
    public function supportsDenormalization($data, $type, $format = null)
92
    {
93 75
        return 'Xabbuh\XApi\Model\Result' === $type;
94
    }
95
}
96