IssueXML   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 30
c 2
b 1
f 0
dl 0
loc 98
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A addDirectory() 0 26 4
A __construct() 0 8 1
A addXMLFile() 0 8 3
A query() 0 9 3
1
<?php
2
3
/**
4
 * Issue XML Document
5
 *
6
 * PHP Version 5.3.2
7
 *
8
 * Copyright (c) 2007-2009, Mayflower GmbH
9
 * All rights reserved.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 *
15
 *   * Redistributions of source code must retain the above copyright
16
 *     notice, this list of conditions and the following disclaimer.
17
 *
18
 *   * Redistributions in binary form must reproduce the above copyright
19
 *     notice, this list of conditions and the following disclaimer in
20
 *     the documentation and/or other materials provided with the
21
 *     distribution.
22
 *
23
 *   * Neither the name of Mayflower GmbH nor the names of his
24
 *     contributors may be used to endorse or promote products derived
25
 *     from this software without specific prior written permission.
26
 *
27
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
 * POSSIBILITY OF SUCH DAMAGE.
39
 *
40
 * @category PHP_CodeBrowser
41
 *
42
 * @author Elger Thiele <[email protected]>
43
 * @author Michel Hartmann <[email protected]>
44
 *
45
 * @copyright 2007-2010 Mayflower GmbH
46
 *
47
 * @license http://www.opensource.org/licenses/bsd-license.php  BSD License
48
 *
49
 * @version SVN: $Id$
50
 *
51
 * @link http://www.phpunit.de/
52
 *
53
 * @since File available since 0.1.0
54
 */
55
56
namespace PHPCodeBrowser;
57
58
use DOMDocument;
59
use DOMDocumentType;
60
use DOMNode;
61
use DOMNodeList;
62
use DOMXPath;
63
use SebastianBergmann\FileIterator\Factory as FileIteratorFactory;
64
65
/**
66
 * IssueXML
67
 *
68
 * This class is a wrapper around DOMDocument to provide additional features
69
 * like simple xpath queries.
70
 * It is used to merge issue XML files and execute plugins
71
 * against it to retrieve the issues from them.
72
 *
73
 * @category PHP_CodeBrowser
74
 *
75
 * @author Elger Thiele <[email protected]>
76
 * @author Michel Hartmann <[email protected]>
77
 *
78
 * @copyright 2007-2010 Mayflower GmbH
79
 *
80
 * @license http://www.opensource.org/licenses/bsd-license.php  BSD License
81
 *
82
 * @version Release: @package_version@
83
 *
84
 * @link http://www.phpunit.de/
85
 *
86
 * @since Class available since 0.1.0
87
 */
88
class IssueXML extends DOMDocument
89
{
90
    /**
91
     *
92
     *
93
     * @var DOMXPath
94
     */
95
    protected $xpath;
96
97
    /**
98
     * Default constructor
99
     *
100
     * @param string $version  The version definition for DomDocument
101
     * @param string $encoding The used encoding for DomDocument
102
     */
103
    public function __construct(string $version = '1.0', string $encoding = 'UTF-8')
104
    {
105
        parent::__construct($version, $encoding);
106
        $this->appendChild(
107
            $this->createElement('codebrowser')
108
        );
109
        $this->preserveWhiteSpace = false;
110
        $this->formatOutput       = true;
111
    }
112
113
    /**
114
     * Parses directory for XML report files, generating a single DomDocument
115
     * inheriting all files and issues.
116
     *
117
     * @param string $directory The path to directory where xml files are stored
118
     *
119
     * @return IssueXML This object
120
     */
121
    public function addDirectory(string $directory): IssueXML
122
    {
123
        $factory  = new FileIteratorFactory();
124
        $iterator = $factory->getFileIterator($directory, 'xml');
125
126
        foreach ($iterator as $current) {
127
            $realFileName         = \realpath($current);
128
            $xml                  = new DOMDocument('1.0', 'UTF-8');
129
            $xml->validateOnParse = true;
130
131
            if (@$xml->load(\realpath($current))) {
132
                $this->addXMLFile($xml);
133
            } else {
134
                \error_log(
135
                    "[Warning] Could not read file '{$realFileName}'. ".'Make sure it contains valid xml.'
136
                );
137
            }
138
139
            unset($xml);
140
        }
141
142
        if (!$this->documentElement->hasChildNodes()) {
143
            \error_log("[Warning] No valid log files found in '{$directory}'");
144
        }
145
146
        return $this;
147
    }
148
149
    /**
150
     * Add xml file to merge
151
     *
152
     * @param DOMDocument $domDocument The DOMDocument to merge.
153
     *
154
     * @return void
155
     */
156
    public function addXMLFile(DOMDocument $domDocument): void
157
    {
158
        foreach ($domDocument->childNodes as $node) {
159
            if ($node instanceof DOMDocumentType) {
160
                continue;
161
            }
162
163
            $this->documentElement->appendChild($this->importNode($node, true));
164
        }
165
    }
166
167
    /**
168
     * Perform a XPath-Query on the document.
169
     *
170
     * @see DOMXPath::query
171
     *
172
     * @param string       $expression  Xpath expression to query for.
173
     * @param DOMNode|null $contextNode Node to use as context (optional)
174
     *
175
     * @return DOMNodeList List of all matching nodes.
176
     */
177
    public function query(string $expression, ?DOMNode $contextNode = null): DOMNodeList
178
    {
179
        if (null === $this->xpath) {
180
            $this->xpath = new DOMXPath($this);
181
        }
182
183
        $result = $contextNode ? $this->xpath->query($expression, $contextNode) : $this->xpath->query($expression);
184
185
        return $result;
186
    }
187
}
188