JUnitParser::parseFile()   C
last analyzed

Complexity

Conditions 12
Paths 69

Size

Total Lines 71

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 71
rs 6.206
c 0
b 0
f 0
cc 12
nc 69
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Copyright (c) Andreas Heigl<[email protected]>
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @author    Andreas Heigl<[email protected]>
24
 * @copyright Andreas Heigl
25
 * @license   http://www.opensource.org/licenses/mit-license.php MIT-License
26
 * @since     16.06.2016
27
 * @link      http://github.com/heiglandreas/org.heigl.junitdiff
28
 */
29
30
namespace Org_Heigl\JUnitDiff;
31
32
class JUnitParser
33
{
34
    public function __construct()
35
    {
36
        libxml_use_internal_errors(true);
37
    }
38
39
    public function getLastLibXmlError()
40
    {
41
        $error = libxml_get_last_error();
42
        if (! $error instanceof \LibXMLError) {
43
            return false;
44
        }
45
46
        libxml_clear_errors();
47
        return $error;
48
    }
49
50
    /**
51
     * @param string $filename
52
     *
53
     * @return array
54
     */
55
    public function parseFile($filename)
56
    {
57
        if (! is_readable($filename)) {
58
            throw new \UnexpectedValueException(sprintf(
59
                'File %s is not readable',
60
                basename($filename)
61
            ));
62
        }
63
        $dom = new \DOMDocument(1.0, 'UTF-8');
64
        $dom->load($filename);
65
66
        if ($this->getLastLibXmlError()) {
67
            throw new \UnexpectedValueException(sprintf(
68
                'File %s seems not to be a JUnit-File',
69
                basename($filename)
70
            ));
71
        }
72
73
        $xpath = new \DOMXPath($dom);
74
75
        $result = [];
76
77
        $items = $xpath->query('//testcase');
78
        foreach ($items as $item) {
79
            $class = $item->getAttribute('class');
80
            if (! $class) {
81
                $class = explode('::', $item->parentNode->getAttribute('name'))[0];
82
            }
83
84
            $type = 'success';
85
86
            $element = new \DOMNode();
87
88
            foreach ($item->childNodes as $child) {
89
                if ($child->nodeType != XML_ELEMENT_NODE) {
90
                    continue;
91
                }
92
                $type = $child->nodeName;
93
                $element = clone($child);
94
            }
95
96
            $message = $ftype = $addInfo = '';
97
98
            switch ($type) {
99
                case 'error':
100
                case 'failure':
101
                    if (! $element->hasAttributes()) {
102
                        break;
103
                    }
104
                    $message = $element->attributes->getNamedItem('message');
105
                    if ($message instanceof \DOMAttr) {
106
                        $message = $message->value;
107
                    }
108
                    $ftype   = $element->attributes->getNamedItem('type');
109
                    if ($ftype instanceof \DOMAttr) {
110
                        $ftype = $ftype->value;
111
                    }
112
                    $addInfo = $element->textContent;
113
                    break;
114
            }
115
116
            $result[$class . '::' . $item->getAttribute('name')] = [
117
                'result'    => $type,
118
                'message' => $message,
119
                'type'   => $ftype,
120
                'info'    => $addInfo,
121
            ];
122
        }
123
124
        return $result;
125
    }
126
}
127