Humbug   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A parseLines() 0 17 4
A getDescription() 0 3 1
A handleNotFoundFile() 0 3 1
A getErrorsOnLine() 0 8 2
A __construct() 0 6 2
1
<?php
2
namespace exussum12\CoverageChecker\Loaders;
3
4
use exussum12\CoverageChecker\FileChecker;
5
use InvalidArgumentException;
6
7
/**
8
 * Class HumbugLoader
9
 * Used for reading json output from humbug
10
 * @package exussum12\CoverageChecker
11
 */
12
class Humbug 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
    public function __construct($filePath)
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(): array
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
    public function getErrorsOnLine(string $file, int $lineNumber)
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(): string
89
    {
90
        return 'Parses the json report format of humbug (mutation testing)';
91
    }
92
}
93