Normalizer   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
cbo 1
dl 0
loc 53
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B denormalize() 0 20 7
A doDenormalize() 0 14 3
A formatAttribute() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of the XabbuhPandaClient 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\PandaClient\Serializer\Symfony;
13
14
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
15
16
/**
17
 * Extends the Symfony GetSetMethodNormalizer to be able to denormalize data
18
 * collections.
19
 *
20
 * @author Christian Flothmann <[email protected]>
21
 */
22
class Normalizer extends GetSetMethodNormalizer
23
{
24
    /**
25
     * {@inheritDoc}
26
     */
27 18
    public function denormalize($data, $class, $format = null, array $context = array())
28
    {
29
        // prematurely return in case of an empty collection
30 18
        if (is_array($data) && count($data) == 0) {
31 4
            return array();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type declared by the interface Symfony\Component\Serial...rInterface::denormalize of type object.

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...
32
        }
33
34
        // check if the data is a collection
35 14
        if (is_array($data) && isset($data[0]) && is_array($data[0])) {
36 4
            $collection = array();
37
38 4
            foreach ($data as &$item) {
39 4
                $collection[] = $this->doDenormalize($item, $class);
40
            }
41
42 4
            return $collection;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $collection; (array) is incompatible with the return type declared by the interface Symfony\Component\Serial...rInterface::denormalize of type object.

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...
43
        }
44
45 10
        return $this->doDenormalize($data, $class);
46
    }
47
48 14
    private function doDenormalize($data, $class)
49
    {
50 14
        $object = new $class;
51
52 14
        foreach ($data as $attribute => $value) {
53 14
            $setter = 'set'.$this->formatAttribute($attribute);
54
55 14
            if (method_exists($object, $setter)) {
56 14
                $object->$setter($value);
57
            }
58
        }
59
60 14
        return $object;
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66 14
    protected function formatAttribute($attributeName)
67
    {
68 14
        return preg_replace_callback(
69
            '/(^|_|\.)+(.)/', function ($match) {
70 14
                return ('.' === $match[1] ? '_' : '').strtoupper($match[2]);
71 14
            }, $attributeName
72
        );
73
    }
74
}
75