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

CheckstyleLoader::parseLines()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 14
rs 9.4285
c 0
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
    public function parseLines()
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 array_keys($this->coveredLines);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getErrorsOnLine($file, $line)
53
    {
54
        $errors = [];
55
        if (isset($this->coveredLines[$file][$line])) {
56
            $errors = $this->coveredLines[$file][$line];
57
        }
58
59
        return $errors;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function handleNotFoundFile()
66
    {
67
        return true;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public static function getDescription()
74
    {
75
        return 'Parses a report in checkstyle format';
76
    }
77
78
    /**
79
     * @param $reader
80
     * @param $currentFile
81
     */
82
    protected function handleErrors($reader, $currentFile)
83
    {
84
        if ($reader->name === "error") {
85
            $this->coveredLines
86
            [$currentFile]
87
            [$reader->getAttribute('line')][]
88
                = $reader->getAttribute("message");
89
        }
90
    }
91
92
    /**
93
     * @param $reader
94
     * @param $currentFile
95
     * @return string
96
     */
97
    protected function handleFile($reader, $currentFile)
98
    {
99
        if ((
100
            $reader->name === "file" &&
101
            $reader->nodeType == XMLReader::ELEMENT
102
        )) {
103
            $currentFile = $reader->getAttribute('name');
104
            $trim = './';
105
            $currentFile = substr($currentFile, strlen($trim));
106
            $this->coveredLines[$currentFile] = [];
107
        }
108
        return $currentFile;
109
    }
110
}
111