Completed
Push — master ( 7f5a29...8ce298 )
by Scott
13s
created

PhpMdLoader::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace exussum12\CoverageChecker;
3
4
use XMLReader;
5
6
/**
7
 * Class PhpMdLoader
8
 * Used for parsing phpmd xml output
9
 * @package exussum12\CoverageChecker
10
 */
11
class PhpMdLoader implements FileChecker
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $file;
17
18
    /**
19
     * @var array
20
     */
21
    protected $errors = [];
22
23
    /**
24
     * @var array
25
     */
26
    protected $errorRanges = [];
27
28
    /**
29
     * PhpMdLoader constructor.
30
     * @param string $file the path to the phpmd xml file
31
     */
32
    public function __construct($file)
33
    {
34
        $this->file = $file;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getLines()
41
    {
42
        $this->errors = [];
43
        $this->errorRanges = [];
44
        $reader = new XMLReader;
45
        $reader->open($this->file);
46
        $currentFile = "";
47
        while ($reader->read()) {
48
            $currentFile = $this->checkForNewFile($reader, $currentFile);
49
            $this->checkForViolation($reader, $currentFile);
50
        }
51
52
        return $this->errors;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function isValidLine($file, $lineNumber)
59
    {
60
        $valid = true;
61
        foreach ($this->errorRanges[$file] as $number => $errors) {
62
            if ((
63
                $errors['start'] <= $lineNumber &&
64
                $errors['end'] >= $lineNumber
65
            )) {
66
                //unset this error
67
                unset($this->errorRanges[$file][$number]);
68
                $valid = false;
69
            }
70
        }
71
72
        return $valid;
73
    }
74
75
    /**
76
     * @param XMLReader $reader
77
     * @param string $currentFile
78
     */
79
    protected function checkForViolation(XMLReader $reader, $currentFile)
80
    {
81
        if ((
82
            $reader->name === 'violation' &&
83
            $reader->nodeType == XMLReader::ELEMENT
84
        )) {
85
            $error = trim($reader->readString());
86
            $start = $reader->getAttribute('beginline');
87
            $end = $reader->getAttribute('endline');
88
            $this->errorRanges[$currentFile][] = [
89
                'start' => $start,
90
                'end' => $end,
91
                'error' => $error,
92
            ];
93
94
            for ($i = $start; $i <= $end; $i++) {
95
                if ((
96
                    !isset($this->errors[$currentFile][$i]) ||
97
                    !in_array($error, $this->errors[$currentFile][$i])
98
                )) {
99
                    $this->errors[$currentFile][$i][] = $error;
100
                }
101
            }
102
        }
103
    }
104
105
    /**
106
     * @param XMLReader $reader
107
     * @param string $currentFile
108
     * @return string the currentFileName
109
     */
110
    protected function checkForNewFile(XMLReader $reader, $currentFile)
111
    {
112 View Code Duplication
        if ((
113
            $reader->name === 'file' &&
114
            $reader->nodeType == XMLReader::ELEMENT
115
        )
116
        ) {
117
            $currentFile = $reader->getAttribute('name');
118
            $this->errors[$currentFile] = [];
119
            return $currentFile;
120
        }
121
        return $currentFile;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function handleNotFoundFile()
128
    {
129
        return true;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public static function getDescription()
136
    {
137
        return 'Parses the xml report format of phpmd, this mode ' .
138
            'reports multi line violations once per diff, instead ' .
139
            'of on each line the violation occurs';
140
    }
141
}
142