1
|
|
|
<?php |
2
|
|
|
namespace exussum12\CoverageChecker; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
use stdClass; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class PhpCsLoader |
9
|
|
|
* Used for reading json output from phpcs |
10
|
|
|
* @package exussum12\CoverageChecker |
11
|
|
|
*/ |
12
|
|
|
class PhpCsLoader implements FileChecker |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $json; |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $invalidLines; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
protected $failOnTypes = [ |
28
|
|
|
'ERROR', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $wholeFileErrors = [ |
35
|
|
|
'PSR1.Files.SideEffects.FoundWithSymbols', |
36
|
|
|
'Generic.Files.LineEndings.InvalidEOLChar', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $invalidFiles = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* PhpCsLoader constructor. |
47
|
|
|
* @param string $filePath the file path to the json output from phpcs |
48
|
|
|
*/ |
49
|
|
View Code Duplication |
public function __construct($filePath) |
|
|
|
|
50
|
|
|
{ |
51
|
|
|
$this->json = json_decode(file_get_contents($filePath)); |
52
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
53
|
|
|
throw new InvalidArgumentException( |
54
|
|
|
"Can't Parse phpcs json - " . json_last_error_msg() |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function parseLines() |
63
|
|
|
{ |
64
|
|
|
$this->invalidLines = []; |
65
|
|
|
foreach ($this->json->files as $fileName => $file) { |
66
|
|
|
foreach ($file->messages as $message) { |
67
|
|
|
$this->addInvalidLine($fileName, $message); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return array_merge( |
72
|
|
|
array_keys($this->invalidLines), |
73
|
|
|
array_keys($this->invalidFiles) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function getErrorsOnLine($file, $lineNumber) |
81
|
|
|
{ |
82
|
|
|
$errors = []; |
83
|
|
|
if (!empty($this->invalidFiles[$file])) { |
84
|
|
|
$errors = $this->invalidFiles[$file]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (!empty($this->invalidLines[$file][$lineNumber])) { |
88
|
|
|
$errors = array_merge($errors, $this->invalidLines[$file][$lineNumber]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $errors; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $file |
96
|
|
|
* @param stdClass $message |
97
|
|
|
*/ |
98
|
|
|
protected function addInvalidLine($file, $message) |
99
|
|
|
{ |
100
|
|
|
if (!in_array($message->type, $this->failOnTypes)) { |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
$line = $message->line; |
104
|
|
|
|
105
|
|
|
if (!isset($this->invalidLines[$file][$line])) { |
106
|
|
|
$this->invalidLines[$file][$line] = []; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->invalidLines[$file][$line][] = $message->message; |
110
|
|
|
|
111
|
|
|
if (in_array($message->source, $this->wholeFileErrors)) { |
112
|
|
|
$this->invalidFiles[$file][] = $message->message; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function handleNotFoundFile() |
120
|
|
|
{ |
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
|
public static function getDescription() |
128
|
|
|
{ |
129
|
|
|
return 'Parses the json report format of phpcs, this mode ' . |
130
|
|
|
'only reports errors as violations'; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
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.