Completed
Push — master ( 412c0b...ce3826 )
by Scott
11s
created

PhpMdLoader   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 136
Duplicated Lines 9.56 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 13
loc 136
rs 10
wmc 19
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getLines() 0 14 2
A isValidLine() 0 16 4
A checkForViolation() 0 18 3
A checkForNewFile() 13 13 3
A handleNotFoundFile() 0 4 1
A getDescription() 0 6 1
A addForAllLines() 0 11 4

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 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
            $this->addForAllLines($currentFile, $start, $end, $error);
95
        }
96
    }
97
98
    /**
99
     * @param XMLReader $reader
100
     * @param string $currentFile
101
     * @return string the currentFileName
102
     */
103 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...
104
    {
105
        if ((
106
            $reader->name === 'file' &&
107
            $reader->nodeType == XMLReader::ELEMENT
108
        )
109
        ) {
110
            $currentFile = $reader->getAttribute('name');
111
            $this->errors[$currentFile] = [];
112
            return $currentFile;
113
        }
114
        return $currentFile;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function handleNotFoundFile()
121
    {
122
        return true;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public static function getDescription()
129
    {
130
        return 'Parses the xml report format of phpmd, this mode ' .
131
            'reports multi line violations once per diff, instead ' .
132
            'of on each line the violation occurs';
133
    }
134
135
    protected function addForAllLines($currentFile, $start, $end, $error)
136
    {
137
        for ($i = $start; $i <= $end; $i++) {
138
            if ((
139
                !isset($this->errors[$currentFile][$i]) ||
140
                !in_array($error, $this->errors[$currentFile][$i])
141
            )) {
142
                $this->errors[$currentFile][$i][] = $error;
143
            }
144
        }
145
    }
146
}
147