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

HumbugLoader   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 11.84 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 2
A getLines() 0 18 4
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 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 getLines()
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 $this->invalidLines;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function isValidLine($file, $lineNumber)
68
    {
69
        return empty($this->invalidLines[$file][$lineNumber]);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function handleNotFoundFile()
76
    {
77
        return true;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public static function getDescription()
84
    {
85
        return 'Parses the json report format of humbug (mutation testing)';
86
    }
87
}
88