Passed
Pull Request — master (#26)
by Scott
02:25
created

CloverLoader   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 104
Duplicated Lines 10.58 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parseLines() 0 14 2
A getErrorsOnLine() 0 10 3
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 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