PhpMndXml::handleErrors()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
namespace exussum12\CoverageChecker\Loaders;
3
4
use XMLReader;
5
use exussum12\CoverageChecker\FileChecker;
6
7
class PhpMndXml implements FileChecker
8
{
9
    protected $currentLine;
10
    private $invalidLines = [];
11
12
    private $file;
13
14
    public function __construct($filename)
15
    {
16
        $this->file = $filename;
17
    }
18
19
    /**
20
     * @inheritdoc
21
     */
22
    public function parseLines(): array
23
    {
24
        $reader = new XMLReader;
25
        $reader->open($this->file);
26
        $currentFile = '';
27
        while ($reader->read()) {
28
            $currentFile = $this->checkForNewFiles($reader, $currentFile);
29
30
            $this->handleLine($reader, $currentFile);
31
            $this->handleErrors($reader, $currentFile);
32
        }
33
34
        return array_keys($this->invalidLines);
35
    }
36
37
    protected function checkForNewFiles(XMLReader $reader, $currentFile)
38
    {
39
        if ((
40
            $reader->name === "file" &&
41
            $reader->nodeType == XMLReader::ELEMENT
42
        )) {
43
            $currentFile = $reader->getAttribute('path');
44
            $this->invalidLines[$currentFile] = [];
45
        }
46
        return $currentFile;
47
    }
48
49
    protected function handleLine(XMLReader $reader, $currentFile)
50
    {
51
        if ($reader->name === "entry") {
52
            $this->currentLine = $reader->getAttribute("line");
53
            if (!isset($this->invalidLines[$currentFile][$this->currentLine])) {
54
                $this->invalidLines[$currentFile][$this->currentLine] = [];
55
            }
56
        }
57
    }
58
59
    protected function handleErrors(XMLReader $reader, $currentFile)
60
    {
61
        if ((
62
            $reader->name === "snippet" &&
63
            $reader->nodeType == XMLReader::ELEMENT
64
        )) {
65
            $this->invalidLines[$currentFile][$this->currentLine][] = $reader->readString();
66
        }
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function getErrorsOnLine(string $file, int $lineNumber)
73
    {
74
        $errors = [];
75
        if (isset($this->invalidLines[$file][$lineNumber])) {
76
            $errors = $this->invalidLines[$file][$lineNumber];
77
        }
78
79
        return $errors;
80
    }
81
82
    /**
83
     * return as true to include files, phpmnd only shows files with errors
84
     */
85
    public function handleNotFoundFile()
86
    {
87
        return true;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public static function getDescription(): string
94
    {
95
        return 'Parses the XML output of phpmnd (Magic Number Detection)';
96
    }
97
}
98