Completed
Push — master ( 56185f...c80cc1 )
by Scott
13s
created

CloverLoader::getLines()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 20
c 1
b 0
f 0
nc 7
nop 0
dl 0
loc 30
rs 6.7272
1
<?php
2
namespace exussum12\CoverageChecker;
3
4
use XMLReader;
5
6
/**
7
 * Class XMLReport
8
 * Used for reading in a phpunit clover XML file
9
 * @package exussum12\CoverageChecker
10
 */
11
class CloverLoader implements FileChecker
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $file;
17
    /**
18
     * @var array
19
     */
20
    protected $coveredLines;
21
22
    /**
23
     * XMLReport constructor.
24
     * @param string $file the path the to phpunit clover file
25
     */
26
    public function __construct($file)
27
    {
28
        $this->file = $file;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function getLines()
35
    {
36
        $this->coveredLines = [];
37
        $reader = new XMLReader;
38
        $reader->open($this->file);
39
        $currentFile = '';
40
        while ($reader->read()) {
41
            if ((
42
                $reader->name === "file" &&
43
                $reader->nodeType == XMLReader::ELEMENT
44
            )) {
45
                $currentFile = $reader->getAttribute('name');
46
                $this->coveredLines[$currentFile] = [];
47
            }
48
49
            if ((
50
                $reader->name === "line" &&
51
                $reader->getAttribute("type") == "stmt"
52
            )) {
53
                $covered = $reader->getAttribute('count') > 0;
54
55
                $this->coveredLines
56
                    [$currentFile]
57
                    [$reader->getAttribute('num')]
58
                    = $covered ?: "No test coverage";
59
            }
60
        }
61
62
        return $this->coveredLines;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function isValidLine($file, $line)
69
    {
70
        if (!isset($this->coveredLines[$file][$line])) {
71
            return null;
72
        }
73
74
        return $this->coveredLines[$file][$line] > 0;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function handleNotFoundFile()
81
    {
82
        return null;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public static function getDescription()
89
    {
90
        return 'Parses text output in clover (xml) format';
91
    }
92
}
93