Passed
Pull Request — master (#9)
by Scott
03:15 queued 57s
created

XMLReport   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 81
Duplicated Lines 8.64 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 7
loc 81
rs 10
c 2
b 0
f 0
wmc 11
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getLines() 7 28 6
A isValidLine() 0 8 2
A handleNotFoundFile() 0 4 1
A getDescription() 0 5 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 XMLReport 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 View Code Duplication
            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
                $this->coveredLines
54
                    [$currentFile]
55
                    [$reader->getAttribute('num')]
56
                    = (int) $reader->getAttribute("count");
57
            }
58
        }
59
60
        return $this->coveredLines;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function isValidLine($file, $line)
67
    {
68
        if (!isset($this->coveredLines[$file][$line])) {
69
            return;
70
        }
71
72
        return $this->coveredLines[$file][$line] > 0;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function handleNotFoundFile()
79
    {
80
        return null;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public static function getDescription()
87
    {
88
        return 'Parses text output in clover (xml) format designed for ' .
89
            'phpunit but any tool which outputs in this format should work';
90
    }
91
}
92