Checkstyle   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrorsOnLine() 0 8 2
A handleNotFoundFile() 0 3 1
A handleErrors() 0 7 2
A handleFile() 0 12 3
A __construct() 0 3 1
A getDescription() 0 3 1
A parseLines() 0 13 2
1
<?php
2
namespace exussum12\CoverageChecker\Loaders;
3
4
use exussum12\CoverageChecker\FileChecker;
5
use XMLReader;
6
7
/**
8
 * Class CheckstyleLoader
9
 * Used for reading in a report in checkstyle format
10
 * @package exussum12\CoverageChecker
11
 */
12
class Checkstyle implements FileChecker
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $file;
18
    /**
19
     * @var array
20
     */
21
    protected $coveredLines;
22
23
    /**
24
     * XMLReport constructor.
25
     * @param string $file the path the to phpunit clover file
26
     */
27
    public function __construct($file)
28
    {
29
        $this->file = $file;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function parseLines(): array
36
    {
37
        $this->coveredLines = [];
38
        $reader = new XMLReader;
39
        $reader->open($this->file);
40
        $currentFile = '';
41
        while ($reader->read()) {
42
            $currentFile = $this->handleFile($reader, $currentFile);
43
44
            $this->handleErrors($reader, $currentFile);
45
        }
46
47
        return array_keys($this->coveredLines);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getErrorsOnLine(string $file, int $line)
54
    {
55
        $errors = [];
56
        if (isset($this->coveredLines[$file][$line])) {
57
            $errors = $this->coveredLines[$file][$line];
58
        }
59
60
        return $errors;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function handleNotFoundFile()
67
    {
68
        return true;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public static function getDescription(): string
75
    {
76
        return 'Parses a report in checkstyle format';
77
    }
78
79
    protected function handleErrors(XMLReader $reader, string $currentFile)
80
    {
81
        if ($reader->name === "error") {
82
            $this->coveredLines
83
            [$currentFile]
84
            [$reader->getAttribute('line')][]
85
                = $reader->getAttribute("message");
86
        }
87
    }
88
89
    protected function handleFile(XMLReader $reader, string $currentFile): string
90
    {
91
        if ((
92
            $reader->name === "file" &&
93
            $reader->nodeType == XMLReader::ELEMENT
94
        )) {
95
            $currentFile = $reader->getAttribute('name');
96
            $trim = './';
97
            $currentFile = substr($currentFile, strlen($trim));
98
            $this->coveredLines[$currentFile] = [];
99
        }
100
        return $currentFile;
101
    }
102
}
103