Completed
Push — master ( ec1658...e39047 )
by Tomasz
04:01
created

XmlFormat   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 92
Duplicated Lines 3.26 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 2
Metric Value
wmc 20
c 5
b 1
f 2
lcom 0
cbo 1
dl 3
loc 92
ccs 55
cts 55
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 4 1
A doSerialize() 0 11 4
C serializeValue() 3 31 7
A unserialize() 0 9 1
C parse() 0 30 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Thunder\Serializard\Format;
3
4
use Thunder\Serializard\HandlerContainer\HandlerContainerInterface as Handlers;
5
6
/**
7
 * @author Tomasz Kowalczyk <[email protected]>
8
 */
9
final class XmlFormat implements FormatInterface
10
{
11 2
    public function serialize($var, Handlers $handlers)
12
    {
13 2
        return $this->doSerialize($var, $handlers);
14
    }
15
16 2
    private function doSerialize($var, Handlers $handlers, $doc = null, $parent = null, $key = null, array $state = array(), array $classes = array())
17
    {
18 2
        $isRoot = ($doc === null);
19 2
        $doc = $doc ?: new \DOMDocument('1.0', 'utf-8');
20 2
        $doc->formatOutput = true;
21 2
        $parent = $parent ?: $doc;
22
23 2
        $this->serializeValue($var, $handlers, $doc, $parent, $key, $state, $classes);
24
25 2
        return $isRoot ? $doc->saveXML() : null;
26
    }
27
28 2
    private function serializeValue($var, Handlers $handlers, $doc, $parent, $key, array $state = array(), array $classes = array())
29
    {
30
        /** @var \DOMDocument|\DOMElement $doc */
31
        /** @var \DOMDocument|\DOMElement $parent */
32 2
        if(is_object($var)) {
33 2
            $handler = $handlers->getHandler(get_class($var));
34 2
            $arr = $handler($var);
35
36 2
            $newState = array_merge($state, array(spl_object_hash($var)));
37 2
            $newClasses = array_merge($classes, array(get_class($var)));
38 2 View Code Duplication
            if(count(array_keys($state, spl_object_hash($var), true)) > 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39 1
                throw new \RuntimeException('Nesting cycle: '.implode(' -> ', $newClasses));
40
            }
41
42 2
            $this->doSerialize($arr, $handlers, $doc, $parent, $handlers->getRoot(get_class($var)), $newState, $newClasses);
43
44 2
            return;
45
        }
46
47 2
        if(is_array($var)) {
48 2
            $item = $key ? $doc->createElement($key) : $parent;
49 2
            foreach($var as $index => $value) {
50 2
                $this->doSerialize($value, $handlers, $doc, $item, $index, $state, $classes);
51 2
            }
52 2
            !$key ?: $parent->appendChild($item);
53
54 2
            return;
55
        }
56
57 2
        $parent->appendChild($doc->createElement($key, $var));
58 2
    }
59
60 1
    public function unserialize($var, $class, Handlers $handlers)
61
    {
62 1
        $doc = new \DOMDocument();
63 1
        $doc->loadXML($var);
64 1
        $data = $this->parse($doc, $doc);
65 1
        $hydrator = $handlers->getHandler($class);
66
67 1
        return $hydrator($data[$handlers->getRoot($class)], $handlers);
68
    }
69
70 1
    private function parse(\DOMDocument $doc, $parent = null)
71
    {
72 1
        $ret = array();
73
        /** @var \DOMElement $parent */
74
        /** @var \DOMElement $node */
75 1
        $tags = array();
76 1
        foreach($parent->childNodes as $node) {
77 1
            if($node->nodeName === '#text') {
78 1
                continue;
79
            }
80 1
            if($node->childNodes->length === 1 && $node->childNodes->item(0) instanceof \DOMText) {
81 1
                $ret[$node->tagName] = $node->childNodes->item(0)->nodeValue;
82 1
                continue;
83
            }
84 1
            $result = $this->parse($doc, $node);
85 1
            if(array_key_exists($node->tagName, $ret)) {
86 1
                $tags[] = $node->tagName;
87 1
                $ret = array($ret[$node->tagName]);
88 1
                $ret[] = $result;
89 1
                continue;
90
            }
91 1
            if(in_array($node->tagName, $tags)) {
92 1
                $ret[] = $result;
93 1
                continue;
94
            }
95 1
            $ret[$node->tagName] = $result;
96 1
        }
97
98 1
        return $ret;
99
    }
100
}
101