Clover   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A addLine() 0 8 2
A getErrorsOnLine() 0 8 3
A handleStatement() 0 7 3
A getDescription() 0 3 1
A parseLines() 0 13 2
A handleNotFoundFile() 0 3 1
A checkForNewFiles() 0 10 3
1
<?php
2
namespace exussum12\CoverageChecker\Loaders;
3
4
use exussum12\CoverageChecker\FileChecker;
5
use XMLReader;
6
7
/**
8
 * Class XMLReport
9
 * Used for reading in a phpunit clover XML file
10
 * @package exussum12\CoverageChecker
11
 */
12
class Clover 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->checkForNewFiles($reader, $currentFile);
43
44
            $this->handleStatement($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
        if (!isset($this->coveredLines[$file][$line])) {
56
            return null;
57
        }
58
        return $this->coveredLines[$file][$line] > 0 ?
59
            []:
60
            ['No unit test covering this line']
61
        ;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function handleNotFoundFile()
68
    {
69
        return null;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public static function getDescription(): string
76
    {
77
        return 'Parses text output in clover (xml) format';
78
    }
79
80
    protected function checkForNewFiles(XMLReader $reader, string $currentFile)
81
    {
82
        if ((
83
            $reader->name === "file" &&
84
            $reader->nodeType == XMLReader::ELEMENT
85
        )) {
86
            $currentFile = $reader->getAttribute('name');
87
            $this->coveredLines[$currentFile] = [];
88
        }
89
        return $currentFile;
90
    }
91
92
    protected function addLine(XMLReader $reader, string $currentFile)
93
    {
94
        $covered = $reader->getAttribute('count') > 0;
95
96
        $this->coveredLines
97
        [$currentFile]
98
        [$reader->getAttribute('num')]
99
            = $covered ?: "No test coverage";
100
    }
101
102
    protected function handleStatement(XMLReader $reader, string $currentFile)
103
    {
104
        if ((
105
            $reader->name === "line" &&
106
            $reader->getAttribute("type") == "stmt"
107
        )) {
108
            $this->addLine($reader, $currentFile);
109
        }
110
    }
111
}
112