Completed
Pull Request — master (#14)
by Scott
02:44
created

CheckstyleLoader   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 75
Duplicated Lines 8 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 6
loc 75
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getLines() 6 27 5
A isValidLine() 0 4 1
A handleNotFoundFile() 0 4 1
A getDescription() 0 4 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 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
    public function getLines()
35
    {
36
        $this->coveredLines = [];
37
        $reader = new XMLReader;
38
        $reader->open($this->file);
39
        $currentFile = '';
40
        while ($reader->read()) {
41
            if ((
42
                $reader->name === "file" &&
43
                $reader->nodeType == XMLReader::ELEMENT
44
            )) {
45
                $currentFile = $reader->getAttribute('name');
46
                $trim = './';
47
                $currentFile = substr($currentFile, strlen($trim));
48
                $this->coveredLines[$currentFile] = [];
49
            }
50
51 View Code Duplication
            if ($reader->name === "error") {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
52
                $this->coveredLines
53
                    [$currentFile]
54
                    [$reader->getAttribute('line')]
55
                    = $reader->getAttribute("message");
56
            }
57
        }
58
59
        return $this->coveredLines;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function isValidLine($file, $line)
66
    {
67
        return empty($this->coveredLines[$file][$line]);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function handleNotFoundFile()
74
    {
75
        return true;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public static function getDescription()
82
    {
83
        return 'Parses a report in checkstyle format';
84
    }
85
}
86