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 (#35)
by
unknown
15:20
created

XML   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 87.88%

Importance

Changes 6
Bugs 2 Features 2
Metric Value
c 6
b 2
f 2
dl 0
loc 68
ccs 29
cts 33
cp 0.8788
rs 10
wmc 13
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 14 3
D recursive_parse() 0 41 10
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
        $xml_array = (array) $xml;
43
        if(count($xml_array) == 1 and is_numeric(key($xml_array))) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
44 24
            $result = (string) $xml;
45 24
        }else{
46 6
            $result = null;
47
        }
48
        unset($xml_array);
49
50 6
        foreach ($ns as $nsName => $nsUri) {
51 24
            foreach ($xml->attributes($nsUri) as $attName => $attValue) {
52
                if ( ! empty($nsName)) {
53 24
                    $attName = "{$nsName}:{$attName}";
54 24
                }
55 6
56 6
                $result["@{$attName}"] = (string) $attValue;
57
            }
58 24
59
            foreach ($xml->children($nsUri) as $childName => $child) {
60 24
                if ( ! empty($nsName)) {
61 6
                    $childName = "{$nsName}:{$childName}";
62
                }
63
64 6
                $child = $this->recursive_parse($child, $ns);
65 6
66
                if (isset($result[$childName])) {
67 6
                    if (is_numeric(key($result[$childName]))) {
68 24
                        $result[$childName][] = $child;
69
                    } else {
70 24
                        $temp = $result[$childName];
71 24
                        $result[$childName] = [$temp, $child];
72
                    }
73 24
                } else {
74
                    $result[$childName] = $child;
75
                }
76
            }
77
        }
78
79
        return $result;
80
    }
81
}
82