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

JacocoReport   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 17.39 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 12
loc 69
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 4 1
A addLine() 0 11 2
A findFile() 12 12 3
A findNamespace() 0 10 3
A parseLines() 0 18 2

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
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