XMLReader::getDomFromString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of PHPProject - A pure PHP library for reading and writing
4
 * project management files.
5
 *
6
 * PHPProject is free software distributed under the terms of the GNU Lesser
7
 * General Public License version 3 as published by the Free Software Foundation.
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code. For the full list of
11
 * contributors, visit https://github.com/PHPOffice/PHPProject/contributors.
12
 *
13
 * @link        https://github.com/PHPOffice/PHPProject
14
 * @copyright   2010-2014 PHPProject contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpProject\Shared;
19
20
/**
21
 * XML Reader wrapper
22
 *
23
 * @since   0.10.0
24
 */
25
class XMLReader
26
{
27
    /**
28
     * DOMDocument object
29
     *
30
     * @var \DOMDocument
31
     */
32
    private $dom = null;
33
34
    /**
35
     * DOMXpath object
36
     *
37
     * @var \DOMXpath
38
     */
39
    private $xpath = null;
40
41
    /**
42
     * Get DOMDocument from ZipArchive
43
     *
44
     * @param string $zipFile
45
     * @param string $xmlFile
46
     * @return \DOMDocument|false
47
     * @throws \Exception
48
     */
49 10
    public function getDomFromZip($zipFile, $xmlFile)
50
    {
51 10
        if (file_exists($zipFile) === false) {
52 1
            throw new \Exception('Cannot find archive file.');
53
        }
54
55 9
        $zip = new \ZipArchive();
56 9
        $zip->open($zipFile);
57 9
        $content = $zip->getFromName($xmlFile);
58 9
        $zip->close();
59
60 9
        if ($content === false) {
61 1
            return false;
62
        } else {
63 8
            return $this->getDomFromString($content);
64
        }
65
    }
66
67
    /**
68
     * Get DOMDocument from content string
69
     *
70
     * @param string $content
71
     * @return \DOMDocument
72
     */
73 10
    public function getDomFromString($content)
74
    {
75 10
        $this->dom = new \DOMDocument();
76 10
        $this->dom->loadXML($content);
77
78 10
        return $this->dom;
79
    }
80
81
    /**
82
     * Get elements
83
     *
84
     * @param string $path
85
     * @param \DOMElement $contextNode
86
     * @return \DOMNodeList
87
     */
88 11
    public function getElements($path, \DOMElement $contextNode = null)
89
    {
90 11
        if ($this->dom === null) {
91 1
            return array();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array(); (array) is incompatible with the return type documented by PhpOffice\PhpProject\Shared\XMLReader::getElements of type DOMNodeList.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
92
        }
93 10
        if ($this->xpath === null) {
94 10
            $this->xpath = new \DOMXpath($this->dom);
95 10
        }
96
97 10
        return $this->xpath->query($path, $contextNode);
98
    }
99
100
    /**
101
     * Get element
102
     *
103
     * @param string $path
104
     * @param \DOMElement $contextNode
105
     * @return \DOMElement|null
106
     */
107 4
    public function getElement($path, \DOMElement $contextNode = null)
108
    {
109 4
        $elements = $this->getElements($path, $contextNode);
110 4
        if ($elements->length > 0) {
111 3
            return $elements->item(0);
112
        } else {
113 1
            return null;
114
        }
115
    }
116
117
    /**
118
     * Get element attribute
119
     *
120
     * @param string $attribute
121
     * @param \DOMElement $contextNode
122
     * @param string $path
123
     * @return string|null
124
     */
125 2
    public function getAttribute($attribute, \DOMElement $contextNode = null, $path = null)
126
    {
127 2
        $return = null;
128 2
        if ($path !== null) {
129 1
            $elements = $this->getElements($path, $contextNode);
130 1
            if ($elements->length > 0) {
131
                /** @var \DOMElement $node Type hint */
132 1
                $node = $elements->item(0);
133 1
                $return = $node->getAttribute($attribute);
134 1
            }
135 1
        } else {
136 1
            if ($contextNode !== null) {
137 1
                $return = $contextNode->getAttribute($attribute);
138 1
            }
139
        }
140
141 2
        return ($return == '') ? null : $return;
142
    }
143
144
    /**
145
     * Get element value
146
     *
147
     * @param string $path
148
     * @param \DOMElement $contextNode
149
     * @return string|null
150
     */
151 2
    public function getValue($path, \DOMElement $contextNode = null)
152
    {
153 2
        $elements = $this->getElements($path, $contextNode);
154 2
        if ($elements->length > 0) {
155 1
            return $elements->item(0)->nodeValue;
156
        } else {
157 1
            return null;
158
        }
159
    }
160
161
    /**
162
     * Count elements
163
     *
164
     * @param string $path
165
     * @param \DOMElement $contextNode
166
     * @return integer
167
     */
168 1
    public function countElements($path, \DOMElement $contextNode = null)
169
    {
170 1
        $elements = $this->getElements($path, $contextNode);
171
172 1
        return $elements->length;
173
    }
174
175
    /**
176
     * Element exists
177
     *
178
     * @param string $path
179
     * @param \DOMElement $contextNode
180
     * @return boolean
181
     */
182 1
    public function elementExists($path, \DOMElement $contextNode = null)
183
    {
184 1
        return $this->getElements($path, $contextNode)->length > 0;
185
    }
186
}
187