Parser::process()   C
last analyzed

Complexity

Conditions 16
Paths 23

Size

Total Lines 66
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 66
rs 5.8516
cc 16
eloc 37
nc 23
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace pastuhov\xml2object;
3
4
/**
5
 * XML to object parser.
6
 */
7
class Parser
8
{
9
    /**
10
     * XML content.
11
     *
12
     * @var string
13
     */
14
    public $xml;
15
    /**
16
     * Tag names for 'to property conversion'
17
     *
18
     * @var string[]
19
     */
20
    public $convertToAttribute = [
21
        'properties',
22
        'options',
23
        'features',
24
        'rows',
25
    ];
26
    /**
27
     * Children property.
28
     *
29
     * @var string
30
     */
31
    public $childrenProperty = 'children';
32
    /**
33
     * XML parser.
34
     *
35
     * @var \XMLReader
36
     */
37
    protected $reader;
38
39
    /**
40
     * Convert.
41
     *
42
     * @return Node
43
     */
44
    public function process()
45
    {
46
        $reader = $this->getReader();
47
48
        $recipientChain = [];
49
50
        while($reader->read())
51
        {
52
            $nodeType = $reader->nodeType;
53
54
            switch ($nodeType)
55
            {
56
                case \XMLReader::END_ELEMENT:
57
                    if (!in_array($reader->name, $this->convertToAttribute, true) && count($recipientChain) !== 1) {
58
                        $recipientChain = array_slice($recipientChain, 0, -1);
59
                    }
60
61
                    break;
62
                case \XMLReader::ELEMENT:
63
                    $recipient = end($recipientChain);
64
                    $isConverted = false;
65
66
                    if ($recipient && $recipient->_childrenProperty === null) {
67
                        if ($isConverted = in_array($reader->name, $this->convertToAttribute, true)) {
68
                            $recipient->_childrenProperty = $reader->name;
69
70
                            break;
71
                        } else {
72
                            $recipient->_childrenProperty = $this->childrenProperty;
73
                        }
74
                    }
75
76
                    $node = new Node();
77
                    $node->_tagName = $reader->name;
78
79
                    $hasAttributes = $reader->hasAttributes;
80
                    $isEmptyElement = $reader->isEmptyElement;
81
82
                    if ($hasAttributes) {
83
                        while($reader->moveToNextAttribute()) {
84
                            $node->{$reader->name} = $reader->value;
85
                        }
86
                    }
87
88
                    if ($recipient) {
89
                        $recipient->{$recipient->_childrenProperty}[] = $node;
90
                    }
91
92
                    if (!$isEmptyElement && !$isConverted) {
93
                        $recipientChain[] = $node;
94
                    }
95
96
                    break;
97
                case \XMLReader::TEXT:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
98
99
                    break;
100
                case \XMLReader::ATTRIBUTE:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
101
102
                    break;
103
            }
104
        }
105
106
        $reader->close();
107
108
        return $recipientChain[0];
109
    }
110
111
    /**
112
     * @return \XMLReader
113
     */
114
    protected function getReader()
115
    {
116
        $reader = new \XMLReader();
117
        $reader->XML($this->xml);
118
119
        return $reader;
120
    }
121
122
}
123