Completed
Push — master ( 6aef24...286712 )
by Márk
06:17
created

NativeParser::parse()   C

Complexity

Conditions 11
Paths 14

Size

Total Lines 40
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 40
ccs 0
cts 35
cp 0
rs 5.2653
cc 11
eloc 26
nc 14
nop 2
crap 132

How to fix   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
3
namespace Fxmlrpc\Serialization\Parser;
4
5
use Fxmlrpc\Serialization\Parser;
6
use Fxmlrpc\Serialization\Value\Base64Value;
7
8
/**
9
 * Parser to parse XML responses into its PHP representation using XML RPC extension.
10
 *
11
 * @author Lars Strojny <[email protected]>
12
 */
13
final class NativeParser implements Parser
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function parse($xmlString, &$isFault)
19
    {
20
        $result = xmlrpc_decode($xmlString, 'UTF-8');
21
22
        $isFault = false;
23
24
        $toBeVisited = [&$result];
25
        while (isset($toBeVisited[0]) && $value = &$toBeVisited[0]) {
26
            $type = gettype($value);
27
            if ($type === 'object') {
28
                $xmlRpcType = $value->{'xmlrpc_type'};
29
                if ($xmlRpcType === 'datetime') {
30
                    $value = \DateTime::createFromFormat(
31
                        'Ymd\TH:i:s',
32
                        $value->scalar,
33
                        isset($timezone) ? $timezone : $timezone = new \DateTimeZone('UTC')
34
                    );
35
                } elseif ($xmlRpcType === 'base64') {
36
                    if ($value->scalar !== '') {
37
                        $value = Base64Value::serialize($value->scalar);
38
                    } else {
39
                        $value = null;
40
                    }
41
                }
42
            } elseif ($type === 'array') {
43
                foreach ($value as &$element) {
44
                    $toBeVisited[] = &$element;
45
                }
46
            }
47
48
            array_shift($toBeVisited);
49
        }
50
51
        if (is_array($result)) {
52
            reset($result);
53
            $isFault = xmlrpc_is_fault($result);
54
        }
55
56
        return $result;
57
    }
58
}
59