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
Push — master ( 8eeac2...c7d448 )
by Nathan
10s
created

XML   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 95.35%

Importance

Changes 10
Bugs 2 Features 2
Metric Value
c 10
b 2
f 2
dl 0
loc 76
ccs 41
cts 43
cp 0.9535
rs 10
wmc 17
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 14 3
C recursive_parse() 0 49 14
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 48
    public function parse($payload)
26
    {
27 48
        if ($payload) {
28
            try {
29 45
                $xml = simplexml_load_string($payload, 'SimpleXMLElement', LIBXML_NOCDATA);
30 42
                $ns = ['' => null] + $xml->getDocNamespaces(true);
31 42
                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 42
    protected function recursive_parse($xml, $ns)
41
    {
42 42
        $xml_string = (string)$xml;
43
44 42
        if ($xml->count() == 0 and $xml_string != '') {
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...
45 42
            if (count($xml->attributes()) == 0) {
46 39
                if (trim($xml_string) == '') {
47 3
                    $result = null;
48 3
                } else {
49 39
                    $result = $xml_string;
50
                }
51 39
            } else {
52 6
                $result = array('#text' => $xml_string);
53
            }
54 42
        } else {
55 39
            $result = null;
56
        }
57
58 42
        foreach ($ns as $nsName => $nsUri) {
59 42
            foreach ($xml->attributes($nsUri) as $attName => $attValue) {
60 24
                if (!empty($nsName)) {
61
                    $attName = "{$nsName}:{$attName}";
62
                }
63
64 24
                $result["@{$attName}"] = (string)$attValue;
65 42
            }
66
67 42
            foreach ($xml->children($nsUri) as $childName => $child) {
68 39
                if (!empty($nsName)) {
69 6
                    $childName = "{$nsName}:{$childName}";
70 6
                }
71
72 39
                $child = $this->recursive_parse($child, $ns);
73
74 39
                if (is_array($result) and array_key_exists($childName, $result)) {
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...
75 18
                    if (is_array($result[$childName]) and is_numeric(key($result[$childName]))) {
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...
76 12
                        $result[$childName][] = $child;
77 12
                    } else {
78 18
                        $temp = $result[$childName];
79 18
                        $result[$childName] = [$temp, $child];
80
                    }
81 18
                } else {
82 39
                    $result[$childName] = $child;
83
                }
84 42
            }
85 42
        }
86
87 42
        return $result;
88
    }
89
}
90