|
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
|
|
|
|