XmlParser   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 130
Duplicated Lines 25.38 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 23
c 2
b 0
f 0
lcom 1
cbo 2
dl 33
loc 130
ccs 90
cts 90
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parseValues() 13 13 3
B parseArrays() 4 24 5
C parseMaps() 12 39 7
B parseTuples() 4 29 6
A parse() 0 18 2

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\SimilarWebApi\Parser;
3
4
use Thunder\SimilarWebApi\AbstractParser;
5
use Thunder\SimilarWebApi\RawResponse;
6
7
/**
8
 * @author Tomasz Kowalczyk <[email protected]>
9
 */
10
final class XmlParser extends AbstractParser
11
    {
12 32 View Code Duplication
    private function parseValues(\SimpleXMLElement $xml)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
13
        {
14 32
        $values = array();
15 32
        if(array_key_exists('values', $this->mapping))
16 32
            {
17 21
            foreach($this->mapping['values'] as $key => $item)
18
                {
19 21
                $values[$key] = (string)$xml->{$item['xml']['field']};
20 21
                }
21 21
            }
22
23 32
        return $values;
24
        }
25
26 32
    private function parseArrays(\SimpleXMLElement $xml)
27
        {
28 32
        $arrays = array();
29 32
        if(array_key_exists('arrays', $this->mapping))
30 32
            {
31 3
            foreach($this->mapping['arrays'] as $key => $item)
32
                {
33 3
                $array = array();
34 3
                $parts = explode('.', $item['xml']['field']);
35 3
                $items = array();
36 3 View Code Duplication
                if(2 == count($parts))
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...
37 3
                    {
38 3
                    $items = $xml->{$parts[0]}->{$parts[1]};
39 3
                    }
40 3
                foreach($items as $element)
41
                    {
42 3
                    $array[] = $element;
43 3
                    }
44 3
                $arrays[$key] = $array;
45 3
                }
46 3
            }
47
48 32
        return $arrays;
49
        }
50
51 32
    private function parseMaps(\SimpleXMLElement $xml)
52
        {
53 32
        $maps = array();
54 32
        if(array_key_exists('maps', $this->mapping))
55 32
            {
56 15
            foreach($this->mapping['maps'] as $key => $item)
57
                {
58
                /* --- XML NS --- */
59 15
                if('MobileRelatedApps' == $this->name)
60 15
                    {
61
                    /** @var $element \SimpleXMLElement */
62 1
                    $element = $xml->{'RelatedApps'};
63 1
                    $ns = $xml->getNamespaces(true);
64 1
                    $map = array();
65 1 View Code Duplication
                    foreach($element->children($ns['d2p1']) as $element)
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...
66
                        {
67 1
                        $map[(string)$element->{$item['xml']['key']}] = (string)$element->{$item['xml']['value']};
68 1
                        }
69 1
                    $maps[$key] = $map;
70 1
                    continue;
71
                    }
72
                /* --- XML NS --- */
73 14
                $map = array();
74 14
                $parts = explode('.', $item['xml']['field']);
75 14
                $items = array();
76 14 View Code Duplication
                if(2 == count($parts))
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...
77 14
                    {
78 14
                    $items = $xml->{$parts[0]}->{$parts[1]};
79 14
                    }
80 14 View Code Duplication
                foreach($items as $element)
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...
81
                    {
82 14
                    $map[(string)$element->{$item['xml']['key']}] = (string)$element->{$item['xml']['value']};
83 14
                    }
84 14
                $maps[$key] = $map;
85 15
                }
86 15
            }
87
88 32
        return $maps;
89
        }
90
91 32
    private function parseTuples(\SimpleXMLElement $xml)
92
        {
93 32
        $tuples = array();
94 32
        if(array_key_exists('tuples', $this->mapping))
95 32
            {
96 3
            foreach($this->mapping['tuples'] as $key => $item)
97
                {
98 3
                $parts = explode('.', $item['xml']['field']);
99 3
                $items = array();
100 3 View Code Duplication
                if(2 == count($parts))
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...
101 3
                    {
102 3
                    $items = $xml->{$parts[0]}->{$parts[1]};
103 3
                    }
104 3
                $tuple = array();
105 3
                foreach($items as $element)
106
                    {
107 3
                    $array = array();
108 3
                    foreach($item['xml']['items'] as $itemData)
109
                        {
110 3
                        $array[$itemData['value']] = (string)$element->{$itemData['key']};
111 3
                        }
112 3
                    $tuple[(string)$element->{$item['xml']['index']}] = $array;
113 3
                    }
114 3
                $tuples[$key] = $tuple;
115 3
                }
116 3
            }
117
118 32
        return $tuples;
119
        }
120
121 34
    protected function parse($content)
122
        {
123 34
        libxml_use_internal_errors(true);
124
        try
125
            {
126 34
            $xml = new \SimpleXMLElement($content);
127
            }
128 34
        catch(\Exception $e)
129
            {
130 2
            throw new \RuntimeException(sprintf('Failed to parse XML data: "%s(...)"!', substr($content, 0, 100)));
131
            }
132
133 32
        return new RawResponse($content,
134 32
            $this->parseValues($xml),
135 32
            $this->parseArrays($xml),
136 32
            $this->parseMaps($xml),
137 32
            $this->parseTuples($xml));
138
        }
139
    }
140