Completed
Push — master ( e19b31...4da4bd )
by Scott
02:04
created

CheckstyleLoader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 95
Duplicated Lines 14.74 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getLines() 14 14 2
A isValidLine() 0 4 1
A handleNotFoundFile() 0 4 1
A getDescription() 0 4 1
A handleErrors() 0 9 2
A handleFile() 0 13 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 CheckstyleLoader
8
 * Used for reading in a report in checkstyle format
9
 * @package exussum12\CoverageChecker
10
 */
11
class CheckstyleLoader 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 View Code Duplication
    public function getLines()
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...
35
    {
36
        $this->coveredLines = [];
37
        $reader = new XMLReader;
38
        $reader->open($this->file);
39
        $currentFile = '';
40
        while ($reader->read()) {
41
            $currentFile = $this->handleFile($reader, $currentFile);
42
43
            $this->handleErrors($reader, $currentFile);
44
        }
45
46
        return $this->coveredLines;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function isValidLine($file, $line)
53
    {
54
        return empty($this->coveredLines[$file][$line]);
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function handleNotFoundFile()
61
    {
62
        return true;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public static function getDescription()
69
    {
70
        return 'Parses a report in checkstyle format';
71
    }
72
73
    /**
74
     * @param $reader
75
     * @param $currentFile
76
     */
77
    protected function handleErrors($reader, $currentFile)
78
    {
79
        if ($reader->name === "error") {
80
            $this->coveredLines
81
            [$currentFile]
82
            [$reader->getAttribute('line')]
83
                = $reader->getAttribute("message");
84
        }
85
    }
86
87
    /**
88
     * @param $reader
89
     * @param $currentFile
90
     * @return string
91
     */
92
    protected function handleFile($reader, $currentFile)
93
    {
94
        if ((
95
            $reader->name === "file" &&
96
            $reader->nodeType == XMLReader::ELEMENT
97
        )) {
98
            $currentFile = $reader->getAttribute('name');
99
            $trim = './';
100
            $currentFile = substr($currentFile, strlen($trim));
101
            $this->coveredLines[$currentFile] = [];
102
        }
103
        return $currentFile;
104
    }
105
}
106