Completed
Push — master ( 4da4bd...5ab290 )
by Scott
14s
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 parseLines()
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 array_keys($this->errors);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getErrorsOnLine($file, $lineNumber)
59
    {
60
        $errors = [];
61
        foreach ($this->errorRanges[$file] as $number => $error) {
62
            if ((
63
                $error['start'] <= $lineNumber &&
64
                $error['end'] >= $lineNumber
65
            )) {
66
                $errors[] = $error['error'];
67
                unset($this->errorRanges[$file][$number]);
68
            }
69
        }
70
71
        return $errors;
72
    }
73
74
    /**
75
     * @param XMLReader $reader
76
     * @param string $currentFile
77
     */
78
    protected function checkForViolation(XMLReader $reader, $currentFile)
79
    {
80
        if ((
81
            $reader->name === 'violation' &&
82
            $reader->nodeType == XMLReader::ELEMENT
83
        )) {
84
            $error = trim($reader->readString());
85
            $start = $reader->getAttribute('beginline');
86
            $end = $reader->getAttribute('endline');
87
            $this->errorRanges[$currentFile][] = [
88
                'start' => $start,
89
                'end' => $end,
90
                'error' => $error,
91
            ];
92
93
            $this->addForAllLines($currentFile, $start, $end, $error);
94
        }
95
    }
96
97
    /**
98
     * @param XMLReader $reader
99
     * @param string $currentFile
100
     * @return string the currentFileName
101
     */
102 View Code Duplication
    protected function checkForNewFile(XMLReader $reader, $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...
103
    {
104
        if ((
105
            $reader->name === 'file' &&
106
            $reader->nodeType == XMLReader::ELEMENT
107
        )
108
        ) {
109
            $currentFile = $reader->getAttribute('name');
110
            $this->errors[$currentFile] = [];
111
            return $currentFile;
112
        }
113
        return $currentFile;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function handleNotFoundFile()
120
    {
121
        return true;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public static function getDescription()
128
    {
129
        return 'Parses the xml report format of phpmd, this mode ' .
130
            'reports multi line violations once per diff, instead ' .
131
            'of on each line the violation occurs';
132
    }
133
134
    protected function addForAllLines($currentFile, $start, $end, $error)
135
    {
136
        for ($i = $start; $i <= $end; $i++) {
137
            if ((
138
                !isset($this->errors[$currentFile][$i]) ||
139
                !in_array($error, $this->errors[$currentFile][$i])
140
            )) {
141
                $this->errors[$currentFile][$i][] = $error;
142
            }
143
        }
144
    }
145
}
146