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 ( 1cea92...5cd982 )
by Nelson
04:20
created

DomDoc::getNodeAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace Baruica\Xml\Adapter\Reader;
4
5
use Baruica\Xml\Reader;
6
7
class DomDoc implements Reader
8
{
9
    /** @var \DOMXPath */
10
    private $domXpath;
11
12
    private function __construct(\DOMDocument $doc)
13
    {
14
        $this->domXpath = new \DOMXPath($doc);
15
    }
16
17 View Code Duplication
    public static function fromFile(string $filePath) : Reader
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
    {
19
        $doc = new \DOMDocument();
20
21
        try {
22
            if (false === file_exists($filePath) || false === $doc->load($filePath)) {
23
                throw new \RuntimeException(sprintf('Could not load xml file [%s].', $filePath));
24
            }
25
        } catch (\Exception $e) {
26
            throw new \RuntimeException($e->getMessage());
27
        }
28
29
        return new self($doc);
30
    }
31
32 View Code Duplication
    public static function fromString(string $xmlStr) : Reader
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
    {
34
        $doc = new \DOMDocument();
35
36
        try {
37
            if (false === $doc->loadXML($xmlStr)) {
38
                throw new \RuntimeException(sprintf('Could not load XML from string [%s]', $xmlStr));
39
            }
40
        } catch (\Exception $e) {
41
            throw new \RuntimeException($e->getMessage());
42
        }
43
44
        return new self($doc);
45
    }
46
47
    public function getList(string $xpath) : array
48
    {
49
        $list = [];
50
51
        if (null === $nodeList = $this->getNodeList($xpath)) {
52
            return $list;
53
        }
54
55
        foreach ($nodeList as $node) {
56
            $list[] = $this->getNodeValue($node);
57
        }
58
59
        return $list;
60
    }
61
62
    public function getNodeList(string $xpath, \DOMNode $contextNode = null) : \DOMNodeList
63
    {
64
        $valNL = (null === $contextNode)
65
            ? $this->domXpath->query($xpath)
66
            : $this->domXpath->query($xpath, $contextNode);
67
68
        if (false === $valNL) {
69
            return new \DOMNodeList();
70
        }
71
72
        return $valNL;
73
    }
74
75
    public function getFirstNode(string $xpath, \DOMNode $contextNode = null) : \DOMElement
76
    {
77
        $nodeList = $this->getNodeList($xpath, $contextNode);
78
79
        if (0 === $nodeList->length) {
80
            return;
81
        }
82
83
        return $nodeList->item(0);
84
    }
85
86
    public function getLastNode(string $xpath, \DOMNode $contextNode = null) : \DOMElement
87
    {
88
        $nodeList = $this->getNodeList($xpath, $contextNode);
89
90
        if (0 === $nodeList->length) {
91
            return;
92
        }
93
94
        $lastIndex = $nodeList->length - 1;
95
96
        return $nodeList->item($lastIndex);
97
    }
98
99
    public function getNodeValue(\DOMElement $node = null) : string
100
    {
101
        if (null !== $node) {
102
            return $node->nodeValue;
103
        }
104
105
        return;
106
    }
107
108
    public function getNodeAttribute(string $att, \DOMElement $node = null) : string
109
    {
110
        if (null !== $node) {
111
            return $node->getAttribute($att);
112
        }
113
114
        return;
115
    }
116
117
    public function getNeighborNodeValue(string $neighborNodeName, \DOMElement $node = null) : string
118
    {
119
        if (null !== $node) {
120
            return $this->getNodeValue(
121
                $node->parentNode->getElementsByTagName($neighborNodeName)->item(0)
122
            );
123
        }
124
125
        return;
126
    }
127
128
    public function getValue(string $xpath, \DOMNode $contextNode = null) : string
129
    {
130
        return $this->getNodeValue($this->getFirstNode($xpath, $contextNode));
131
    }
132
133
    public function getValues(\DOMNodeList $contextNodes, string $keyNodeName, array $valNodes = [], \Closure $fn = null, array $fnParams = []) : array
134
    {
135
        $values = [];
136
137
        foreach ($contextNodes as $node) {
138
            $keyNodeValue = $this->getValue($keyNodeName, $node);
139
            if (!array_key_exists($keyNodeValue, $values)) {
140
                $values[$keyNodeValue] = [];
141
            }
142
            foreach ($valNodes as $valNodeName) {
143
                if (null !== $fn) {
144
                    $params = $fnParams;
145
                    array_unshift($params, $this->getValue($valNodeName, $node));
146
                    $values[$keyNodeValue] = $fn($params);
147
                } else {
148
                    $values[$keyNodeValue] = $this->getValue($valNodeName, $node);
149
                }
150
            }
151
        }
152
153
        ksort($values);
154
155
        return $values;
156
    }
157
158
    public function getLastValue(string $xpath, \DOMNode $contextNode = null) : string
159
    {
160
        return $this->getNodeValue($this->getLastNode($xpath, $contextNode));
161
    }
162
}
163