Completed
Push — master ( 412c0b...ce3826 )
by Scott
11s
created

CloverLoader   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 102
Duplicated Lines 10.78 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 102
rs 10
wmc 15
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getLines() 0 14 2
A isValidLine() 0 8 2
A handleNotFoundFile() 0 4 1
A getDescription() 0 4 1
A checkForNewFiles() 11 11 3
A addLine() 0 9 2
A handleStatement() 0 9 3

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 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
            $currentFile = $this->checkForNewFiles($reader, $currentFile);
42
43
            $this->handleStatement($reader, $currentFile);
44
        }
45
46
        return $this->coveredLines;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function isValidLine($file, $line)
53
    {
54
        if (!isset($this->coveredLines[$file][$line])) {
55
            return null;
56
        }
57
58
        return $this->coveredLines[$file][$line] > 0;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function handleNotFoundFile()
65
    {
66
        return null;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public static function getDescription()
73
    {
74
        return 'Parses text output in clover (xml) format';
75
    }
76
77 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...
78
    {
79
        if ((
80
            $reader->name === "file" &&
81
            $reader->nodeType == XMLReader::ELEMENT
82
        )) {
83
            $currentFile = $reader->getAttribute('name');
84
            $this->coveredLines[$currentFile] = [];
85
        }
86
        return $currentFile;
87
    }
88
89
    /**
90
     * @param $reader
91
     * @param $currentFile
92
     */
93
    protected function addLine($reader, $currentFile)
94
    {
95
        $covered = $reader->getAttribute('count') > 0;
96
97
        $this->coveredLines
98
        [$currentFile]
99
        [$reader->getAttribute('num')]
100
            = $covered ?: "No test coverage";
101
    }
102
103
    protected function handleStatement($reader, $currentFile)
104
    {
105
        if ((
106
            $reader->name === "line" &&
107
            $reader->getAttribute("type") == "stmt"
108
        )) {
109
            $this->addLine($reader, $currentFile);
110
        }
111
    }
112
}
113