forked from vimeo/psalm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shepherd.php
222 lines (196 loc) · 7.33 KB
/
Shepherd.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace Psalm\Plugin;
use Psalm\Config;
use Psalm\Internal\Analyzer\IssueData;
use Psalm\Plugin\EventHandler\AfterAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AfterAnalysisEvent;
use function array_filter;
use function array_merge;
use function array_values;
use function curl_close;
use function curl_exec;
use function curl_getinfo;
use function curl_init;
use function curl_setopt;
use function function_exists;
use function fwrite;
use function is_array;
use function json_encode;
use function parse_url;
use function str_replace;
use function strlen;
use function var_export;
use const CURLINFO_HEADER_OUT;
use const CURLOPT_HTTPHEADER;
use const CURLOPT_POST;
use const CURLOPT_POSTFIELDS;
use const CURLOPT_RETURNTRANSFER;
use const JSON_THROW_ON_ERROR;
use const PHP_EOL;
use const PHP_URL_SCHEME;
use const STDERR;
final class Shepherd implements AfterAnalysisInterface
{
/**
* Called after analysis is complete
*/
public static function afterAnalysis(
AfterAnalysisEvent $event
): void {
$codebase = $event->getCodebase();
$issues = $event->getIssues();
$build_info = $event->getBuildInfo();
$source_control_info = $event->getSourceControlInfo();
if (!function_exists('curl_init')) {
fwrite(STDERR, 'No curl found, cannot send data to ' . $codebase->config->shepherd_host . PHP_EOL);
return;
}
$source_control_data = $source_control_info ? $source_control_info->toArray() : [];
if (!$source_control_data && isset($build_info['git']) && is_array($build_info['git'])) {
$source_control_data = $build_info['git'];
}
unset($build_info['git']);
if ($build_info) {
$normalized_data = $issues === [] ? [] : array_filter(
array_merge(...array_values($issues)),
static fn(IssueData $i): bool => $i->severity === 'error'
);
$data = [
'build' => $build_info,
'git' => $source_control_data,
'issues' => $normalized_data,
'coverage' => $codebase->analyzer->getTotalTypeCoverage($codebase),
'level' => Config::getInstance()->level
];
$payload = json_encode($data, JSON_THROW_ON_ERROR);
$base_address = $codebase->config->shepherd_host;
if (parse_url($base_address, PHP_URL_SCHEME) === null) {
$base_address = 'https://' . $base_address;
}
// Prepare new cURL resource
$ch = curl_init($base_address . '/hooks/psalm');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
// Set HTTP Header for POST request
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
[
'Content-Type: application/json',
'Content-Length: ' . strlen($payload),
]
);
// Submit the POST request
$return = curl_exec($ch);
if ($return !== '') {
fwrite(STDERR, 'Error with Psalm Shepherd:' . PHP_EOL);
if ($return === false) {
fwrite(STDERR, self::getCurlErrorMessage($ch) . PHP_EOL);
} else {
echo $return . PHP_EOL;
echo 'Git args: '
. var_export($source_control_data, true)
. PHP_EOL;
echo 'CI args: '
. var_export($build_info, true)
. PHP_EOL;
}
} else {
$short_address = str_replace('https://', '', $base_address);
fwrite(STDERR, "🐑 results sent to $short_address 🐑" . PHP_EOL);
}
// Close cURL session handle
curl_close($ch);
}
}
/**
* @param mixed $ch
*
* @psalm-pure
*/
public static function getCurlErrorMessage($ch): string
{
/**
* @psalm-suppress MixedArgument
* @var array
*/
$curl_info = curl_getinfo($ch);
if (isset($curl_info['ssl_verify_result'])
&& $curl_info['ssl_verify_result'] !== 0
) {
switch ($curl_info['ssl_verify_result']) {
case 2:
return 'unable to get issuer certificate';
case 3:
return 'unable to get certificate CRL';
case 4:
return 'unable to decrypt certificate’s signature';
case 5:
return 'unable to decrypt CRL’s signature';
case 6:
return 'unable to decode issuer public key';
case 7:
return 'certificate signature failure';
case 8:
return 'CRL signature failure';
case 9:
return 'certificate is not yet valid';
case 10:
return 'certificate has expired';
case 11:
return 'CRL is not yet valid';
case 12:
return 'CRL has expired';
case 13:
return 'format error in certificate’s notBefore field';
case 14:
return 'format error in certificate’s notAfter field';
case 15:
return 'format error in CRL’s lastUpdate field';
case 16:
return 'format error in CRL’s nextUpdate field';
case 17:
return 'out of memory';
case 18:
return 'self signed certificate';
case 19:
return 'self signed certificate in certificate chain';
case 20:
return 'unable to get local issuer certificate';
case 21:
return 'unable to verify the first certificate';
case 22:
return 'certificate chain too long';
case 23:
return 'certificate revoked';
case 24:
return 'invalid CA certificate';
case 25:
return 'path length constraint exceeded';
case 26:
return 'unsupported certificate purpose';
case 27:
return 'certificate not trusted';
case 28:
return 'certificate rejected';
case 29:
return 'subject issuer mismatch';
case 30:
return 'authority and subject key identifier mismatch';
case 31:
return 'authority and issuer serial number mismatch';
case 32:
return 'key usage does not include certificate signing';
case 50:
return 'application verification failure';
}
return '';
}
/**
* @psalm-suppress MixedArgument
*/
return var_export(curl_getinfo($ch), true);
}
}