Completed
Push — master ( 4da4bd...5ab290 )
by Scott
14s
created

CloverLoader::parseLines()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
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 parseLines()
35
    {
36
        $this->coveredLines = [];
37
        $reader = new XMLReader;
38
        $reader->open($this->file);
39
        $currentFile = '';
40
        while ($reader->read()) {
41
            $currentFile = $this->checkForNewFiles($reader, $currentFile);
42
43
            $this->handleStatement($reader, $currentFile);
44
        }
45
46
        return array_keys($this->coveredLines);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getErrorsOnLine($file, $line)
53
    {
54
        if (!isset($this->coveredLines[$file][$line])) {
55
            return null;
56
        }
57
        return $this->coveredLines[$file][$line] > 0 ?
58
            []:
59
            ['No unit test covering this line']
60
        ;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function handleNotFoundFile()
67
    {
68
        return null;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public static function getDescription()
75
    {
76
        return 'Parses text output in clover (xml) format';
77
    }
78
79 View Code Duplication
    protected function checkForNewFiles($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...
80
    {
81
        if ((
82
            $reader->name === "file" &&
83
            $reader->nodeType == XMLReader::ELEMENT
84
        )) {
85
            $currentFile = $reader->getAttribute('name');
86
            $this->coveredLines[$currentFile] = [];
87
        }
88
        return $currentFile;
89
    }
90
91
    /**
92
     * @param $reader
93
     * @param $currentFile
94
     */
95
    protected function addLine($reader, $currentFile)
96
    {
97
        $covered = $reader->getAttribute('count') > 0;
98
99
        $this->coveredLines
100
        [$currentFile]
101
        [$reader->getAttribute('num')]
102
            = $covered ?: "No test coverage";
103
    }
104
105
    protected function handleStatement($reader, $currentFile)
106
    {
107
        if ((
108
            $reader->name === "line" &&
109
            $reader->getAttribute("type") == "stmt"
110
        )) {
111
            $this->addLine($reader, $currentFile);
112
        }
113
    }
114
}
115