1
|
|
|
<?php |
2
|
|
|
namespace exussum12\CoverageChecker; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class CodeClimateLoader |
6
|
|
|
* Used for parsing reports in CodeClimate format |
7
|
|
|
* @package exussum12\CoverageChecker |
8
|
|
|
*/ |
9
|
|
|
class CodeClimateLoader implements FileChecker |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $file; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $errors = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $file the path to the codeclimate file |
23
|
|
|
*/ |
24
|
|
|
public function __construct($file) |
25
|
|
|
{ |
26
|
|
|
$json = $this->convertToJson(file_get_contents($file)); |
27
|
|
|
$this->file = json_decode($json); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function parseLines() |
34
|
|
|
{ |
35
|
|
|
foreach ($this->file as $line) { |
|
|
|
|
36
|
|
|
$this->addError($line); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return array_keys($this->errors); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function getErrorsOnLine($file, $lineNumber) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$errors = []; |
48
|
|
|
if (isset($this->errors[$file][$lineNumber])) { |
49
|
|
|
$errors = $this->errors[$file][$lineNumber]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $errors; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function handleNotFoundFile() |
59
|
|
|
{ |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
public static function getDescription() |
67
|
|
|
{ |
68
|
|
|
return 'Parse codeclimate output'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function addError($line) |
72
|
|
|
{ |
73
|
|
|
$trim = './'; |
74
|
|
|
$fileName = substr($line->location->path, strlen($trim)); |
75
|
|
|
$start = $line->location->lines->begin; |
76
|
|
|
$end = $line->location->lines->end; |
77
|
|
|
$message = $line->description; |
78
|
|
|
|
79
|
|
|
for ($lineNumber = $start; $lineNumber <= $end; $lineNumber++) { |
80
|
|
|
$this->errors[$fileName][$lineNumber][] = $message; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function convertToJson($codeClimateFormat) |
85
|
|
|
{ |
86
|
|
|
$codeClimateFormat = str_replace("\0", ',', $codeClimateFormat); |
87
|
|
|
|
88
|
|
|
return '[' . $codeClimateFormat . ']'; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|