Completed
Push — master ( 8865c9...169b65 )
by Christian
03:22
created

ExtensionsNormalizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 0
cbo 1
dl 0
loc 56
ccs 18
cts 19
cp 0.9474
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 20 4
A supportsNormalization() 0 4 1
A denormalize() 0 10 2
A supportsDenormalization() 0 4 1
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 Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
15
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
16
use Xabbuh\XApi\Model\Extensions;
17
use Xabbuh\XApi\Model\IRI;
18
19
/**
20
 * Normalizes and denormalizes xAPI extensions.
21
 *
22
 * @author Christian Flothmann <[email protected]>
23
 */
24
final class ExtensionsNormalizer implements DenormalizerInterface, NormalizerInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 15
    public function normalize($object, $format = null, array $context = array())
30
    {
31 15
        if (!$object instanceof Extensions) {
32
            return;
33
        }
34
35 15
        $extensions = $object->getExtensions();
36
37 15
        if (count($extensions) === 0) {
38 4
            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...
39
        }
40
41 11
        $data = array();
42
43 11
        foreach ($extensions as $iri) {
44 11
            $data[$iri->getValue()] = $extensions[$iri];
45
        }
46
47 11
        return $data;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 129
    public function supportsNormalization($data, $format = null)
54
    {
55 129
       return $data instanceof Extensions;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 15
    public function denormalize($data, $class, $format = null, array $context = array())
62
    {
63 15
        $extensions = new \SplObjectStorage();
64
65 15
        foreach ($data as $iri => $value) {
66 11
            $extensions->attach(IRI::fromString($iri), $value);
67
        }
68
69 15
        return new Extensions($extensions);
0 ignored issues
show
Documentation introduced by
$extensions is of type object<SplObjectStorage>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 133
    public function supportsDenormalization($data, $type, $format = null)
76
    {
77 133
        return 'Xabbuh\XApi\Model\Extensions' === $type;
78
    }
79
}
80