Jacoco   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A findFile() 0 11 3
A addLine() 0 9 2
A findNamespace() 0 9 3
A getDescription() 0 3 1
A parseLines() 0 17 2
1
<?php
2
namespace exussum12\CoverageChecker\Loaders;
3
4
use XMLReader;
5
6
/**
7
 * Class Jacoco
8
 * Used for reading in a Jacoco coverage report
9
 * @package exussum12\CoverageChecker
10
 */
11
class Jacoco extends Clover
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function parseLines(): array
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(): string
36
    {
37
        return 'Parses xml coverage report produced by Jacoco';
38
    }
39
40
    /**
41
     * @param XMLReader $reader
42
     * @param string $currentFile
43
     */
44
    protected function addLine(XMLReader $reader, string $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
    protected function findFile(XMLReader $reader, string $currentNamespace, string $currentFile): string
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(XMLReader $reader, string $currentNamespace): string
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