GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#33)
by Hennik
05:54
created

XML::recursive_parse()   C

Complexity

Conditions 8
Paths 22

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 8.2621

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 20
c 1
b 0
f 1
nc 22
nop 2
dl 0
loc 35
ccs 21
cts 25
cp 0.84
crap 8.2621
rs 5.3846
1
<?php
2
3
namespace Nathanmac\Utilities\Parser\Formats;
4
5
use Nathanmac\Utilities\Parser\Exceptions\ParserException;
6
7
/**
8
 * XML Formatter
9
 *
10
 * @package    Nathanmac\Utilities\Parser\Formats
11
 * @author     Nathan Macnamara <[email protected]>
12
 * @license    https://github.com/nathanmac/Parser/blob/master/LICENSE.md  MIT
13
 */
14
class XML implements FormatInterface
15
{
16
    /**
17
     * Parse Payload Data
18
     *
19
     * @param string $payload
20
     *
21
     * @throws ParserException
22
     * @return array
23
     *
24
     */
25 30
    public function parse($payload)
26
    {
27 30
        if ($payload) {
28
            try {
29 27
                $xml = simplexml_load_string($payload, 'SimpleXMLElement', LIBXML_NOCDATA);
30 24
                $ns = ['' => null] + $xml->getDocNamespaces(true);
31 24
                return $this->recursive_parse($xml, $ns);
32 3
            } catch (\Exception $ex) {
33 3
                throw new ParserException('Failed To Parse XML');
34
            }
35
        }
36
37 3
        return [];
38
    }
39
40 24
    protected function recursive_parse($xml, $ns)
41
    {
42 24
        $result = (string) $xml;
43
44 24
        foreach ($ns as $nsName => $nsUri) {
45 24
            foreach ($xml->attributes($nsUri) as $attName => $attValue) {
46 6
                if ( ! empty($nsName)) {
47
                    $attName = "{$nsName}:{$attName}";
48
                }
49
50 6
                $result["@{$attName}"] = $attValue;
51 24
            }
52
53 24
            foreach ($xml->children($nsUri) as $childName => $child) {
54 24
                if ( ! empty($nsName)) {
55 6
                    $childName = "{$nsName}:{$childName}";
56 6
                }
57
58 24
                $child = $this->recursive_parse($child, $ns);
59
60 24
                if (isset($result[$childName])) {
61 6
                    if (is_numeric(key($result[$childName]))) {
62
                        $result[$childName][] = $child;
63
                    } else {
64 6
                        $temp = $result[$childName];
65 6
                        $result[$childName] = [$temp, $child];
66
                    }
67 6
                } else {
68 24
                    $result[$childName] = $child;
69
                }
70 24
            }
71 24
        }
72
73 24
        return $result;
74
    }
75
}
76