Completed
Push — master ( c0ce1f...577dcc )
by Andreas
8s
created

JUnitParser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 84.62 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 7
c 2
b 0
f 2
lcom 0
cbo 0
dl 44
loc 52
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C parseFile() 44 44 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    /**
35
     * @param string $filename
36
     *
37
     * @return array
38
     */
39 View Code Duplication
    public function parseFile($filename)
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...
40
    {
41
        if (! is_readable($filename)) {
42
            throw new \UnexpectedValueException(sprintf(
43
                'File %s is not readable',
44
                basename($filename)
45
            ));
46
        }
47
        $dom = new \DOMDocument(1.0, 'UTF-8');
48
        if (false === @$dom->load($filename)) {
49
            throw new \UnexpectedValueException(sprintf(
50
                'File %s seems not to be a JUnit-File',
51
                basename($filename)
52
            ));
53
        }
54
55
        $xpath = new \DOMXPath($dom);
56
57
        $result = [];
58
59
        $items = $xpath->query('//testcase');
60
        foreach ($items as $item) {
61
62
            if ($item->hasAttribute('class')) {
63
                $class = $item->getAttribute('class');
64
            } else {
65
                $class = explode('::', $item->parentNode->getAttribute('name'))[0];
66
            }
67
68
            $type = 'success';
69
70
            foreach ($item->childNodes as $child) {
71
                if ($child->nodeType != XML_ELEMENT_NODE) {
72
                    continue;
73
                }
74
                $type = $child->nodeName;
75
            }
76
77
            $result[$class . '::' . $item->getAttribute('name')] = $type;
78
79
        }
80
81
        return $result;
82
    }
83
}