Passed
Pull Request — master (#26)
by Scott
02:25
created

JacocoReport::findFile()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 2
nop 3
dl 12
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace exussum12\CoverageChecker;
3
4
use XMLReader;
5
6
/**
7
 * Class JacocoReport
8
 * Used for reading in a Jacoco coverage report
9
 * @package exussum12\CoverageChecker
10
 */
11
class JacocoReport extends CloverLoader
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function parseLines()
17
    {
18
        $this->coveredLines = [];
19
        $reader = new XMLReader;
20
        $reader->open($this->file);
21
        $currentNamespace = '';
22
        $currentFile = '';
23
24
        while ($reader->read()) {
25
            $currentNamespace = $this->findNamespace($reader, $currentNamespace);
26
27
            $currentFile = $this->findFile($reader, $currentNamespace, $currentFile);
28
29
            $this->addLine($reader, $currentFile);
30
        }
31
32
        return array_keys($this->coveredLines);
33
    }
34
35
    public static function getDescription()
36
    {
37
        return 'Parses xml coverage report produced by Jacoco';
38
    }
39
40
    /**
41
     * @param $reader
42
     * @param $currentFile
43
     */
44
    protected function addLine($reader, $currentFile)
45
    {
46
        if ((
47
            $reader->name === "line"
48
        )) {
49
            $this->coveredLines
50
            [$currentFile]
51
            [$reader->getAttribute('nr')]
52
                = $reader->getAttribute("mi") == 0;
53
        }
54
    }
55
56 View Code Duplication
    protected function findFile($reader, $currentNamespace, $currentFile)
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...
57
    {
58
        if ((
59
            $reader->name === "sourcefile" &&
60
            $reader->nodeType == XMLReader::ELEMENT
61
        )) {
62
            $currentFile = $currentNamespace . '/' . $reader->getAttribute('name');
63
            $this->coveredLines[$currentFile] = [];
64
        }
65
66
        return $currentFile;
67
    }
68
69
    protected function findNamespace($reader, $currentNamespace)
70
    {
71
        if ((
72
            $reader->name === "package" &&
73
            $reader->nodeType == XMLReader::ELEMENT
74
        )) {
75
            $currentNamespace = $reader->getAttribute('name');
76
        }
77
        return $currentNamespace;
78
    }
79
}
80