Completed
Push — master ( e19b31...4da4bd )
by Scott
02:04
created

CheckstyleLoader::getLines()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 14
loc 14
rs 9.4285
c 1
b 0
f 0
1
<?php
2
namespace exussum12\CoverageChecker;
3
4
use XMLReader;
5
6
/**
7
 * Class CheckstyleLoader
8
 * Used for reading in a report in checkstyle format
9
 * @package exussum12\CoverageChecker
10
 */
11
class CheckstyleLoader implements FileChecker
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $file;
17
    /**
18
     * @var array
19
     */
20
    protected $coveredLines;
21
22
    /**
23
     * XMLReport constructor.
24
     * @param string $file the path the to phpunit clover file
25
     */
26
    public function __construct($file)
27
    {
28
        $this->file = $file;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 View Code Duplication
    public function getLines()
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...
35
    {
36
        $this->coveredLines = [];
37
        $reader = new XMLReader;
38
        $reader->open($this->file);
39
        $currentFile = '';
40
        while ($reader->read()) {
41
            $currentFile = $this->handleFile($reader, $currentFile);
42
43
            $this->handleErrors($reader, $currentFile);
44
        }
45
46
        return $this->coveredLines;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function isValidLine($file, $line)
53
    {
54
        return empty($this->coveredLines[$file][$line]);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function handleNotFoundFile()
61
    {
62
        return true;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public static function getDescription()
69
    {
70
        return 'Parses a report in checkstyle format';
71
    }
72
73
    /**
74
     * @param $reader
75
     * @param $currentFile
76
     */
77
    protected function handleErrors($reader, $currentFile)
78
    {
79
        if ($reader->name === "error") {
80
            $this->coveredLines
81
            [$currentFile]
82
            [$reader->getAttribute('line')]
83
                = $reader->getAttribute("message");
84
        }
85
    }
86
87
    /**
88
     * @param $reader
89
     * @param $currentFile
90
     * @return string
91
     */
92
    protected function handleFile($reader, $currentFile)
93
    {
94
        if ((
95
            $reader->name === "file" &&
96
            $reader->nodeType == XMLReader::ELEMENT
97
        )) {
98
            $currentFile = $reader->getAttribute('name');
99
            $trim = './';
100
            $currentFile = substr($currentFile, strlen($trim));
101
            $this->coveredLines[$currentFile] = [];
102
        }
103
        return $currentFile;
104
    }
105
}
106