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

HumbugLoader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 22.22 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 18
loc 81
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 2
A parseLines() 0 18 4
A getErrorsOnLine() 9 9 2
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 InvalidArgumentException;
5
use stdClass;
6
7
/**
8
 * Class HumbugLoader
9
 * Used for reading json output from humbug
10
 * @package exussum12\CoverageChecker
11
 */
12
class HumbugLoader implements FileChecker
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $json;
18
    /**
19
     * @var array
20
     */
21
    protected $invalidLines;
22
23
    protected $errorMethods = [
24
        'errored',
25
        'escaped',
26
    ];
27
28
    /**
29
     * HumbugLoader constructor.
30
     * @param string $filePath the file path to json output of humbug
31
     */
32 View Code Duplication
    public function __construct($filePath)
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...
33
    {
34
        $this->json = json_decode(file_get_contents($filePath));
35
        if (json_last_error() !== JSON_ERROR_NONE) {
36
            throw new InvalidArgumentException(
37
                "Can't Parse Humbug json - " . json_last_error_msg()
38
            );
39
        }
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function parseLines()
46
    {
47
        $this->invalidLines = [];
48
        foreach ($this->errorMethods as $failures) {
49
            foreach ($this->json->$failures as $errors) {
50
                $fileName = $errors->file;
51
                $lineNumber = $errors->line;
52
                $error = "Failed on $failures check";
53
                if (!empty($errors->diff)) {
54
                    $error .= "\nDiff:\n" . $errors->diff;
55
                }
56
57
                $this->invalidLines[$fileName][$lineNumber] = $error;
58
            }
59
        }
60
61
        return array_keys($this->invalidLines);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 View Code Duplication
    public function getErrorsOnLine($file, $lineNumber)
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...
68
    {
69
        $errors = [];
70
        if (isset($this->invalidLines[$file][$lineNumber])) {
71
            $errors = (array) $this->invalidLines[$file][$lineNumber];
72
        }
73
74
        return $errors;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function handleNotFoundFile()
81
    {
82
        return true;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public static function getDescription()
89
    {
90
        return 'Parses the json report format of humbug (mutation testing)';
91
    }
92
}
93