Line data Source code
1 : /*
2 : +----------------------------------------------------------------------+
3 : | LSParrot PHP LSP Extension |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) LSParrot GitHub Organization |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to the 0BSD license that is |
8 : | bundled with this package in the file LICENSE. |
9 : +----------------------------------------------------------------------+
10 : | Author: Go Kudo <zeriyoshi@gmail.com> |
11 : +----------------------------------------------------------------------+
12 : */
13 :
14 : #include "lsp_internal.h"
15 :
16 36 : static inline void lsp_psalm_add_parallel_args(lsp_command *command, zend_long workers)
17 : {
18 : zend_string *threads_arg, *scan_threads_arg;
19 :
20 36 : if (workers < 2) {
21 0 : return;
22 : }
23 :
24 36 : threads_arg = strpprintf(0, "--threads=" ZEND_LONG_FMT, workers);
25 36 : scan_threads_arg = strpprintf(0, "--scan-threads=" ZEND_LONG_FMT, workers);
26 36 : lsp_command_add_zstr(command, threads_arg);
27 36 : lsp_command_add_zstr(command, scan_threads_arg);
28 36 : zend_string_release(scan_threads_arg);
29 36 : zend_string_release(threads_arg);
30 : }
31 :
32 36 : static inline void lsp_psalm_add_configured_level(lsp_command *command, zend_string *project_config, zend_long level)
33 : {
34 : zend_string *argument;
35 :
36 36 : if (project_config) {
37 12 : return;
38 : }
39 :
40 24 : if (level < 1) {
41 0 : level = 6;
42 : }
43 :
44 24 : argument = strpprintf(0, "--error-level=" ZEND_LONG_FMT, level);
45 24 : lsp_command_add_zstr(command, argument);
46 :
47 24 : zend_string_release(argument);
48 : }
49 :
50 20 : static inline bool lsp_psalm_type_expression_safe(zend_string *expression)
51 : {
52 : const char *value;
53 : size_t i;
54 :
55 20 : if (!expression || ZSTR_LEN(expression) == 0 || ZSTR_VAL(expression)[0] != '$') {
56 0 : return false;
57 : }
58 :
59 20 : value = ZSTR_VAL(expression);
60 120 : for (i = 0; i < ZSTR_LEN(expression); i++) {
61 100 : if (value[i] == '\r' || value[i] == '\n' || value[i] == ';') {
62 0 : return false;
63 : }
64 : }
65 :
66 20 : return true;
67 : }
68 :
69 4 : static inline bool lsp_psalm_trace_is_lhs_assignment(zend_string *text, zend_string *expression, size_t offset)
70 : {
71 : const char *value;
72 : size_t length, line_start, line_end, word_start, word_end, i;
73 :
74 4 : if (!text || !expression || ZSTR_LEN(expression) == 0) {
75 0 : return false;
76 : }
77 :
78 4 : value = ZSTR_VAL(text);
79 4 : length = ZSTR_LEN(text);
80 4 : if (offset > length) {
81 0 : offset = length;
82 : }
83 :
84 4 : line_start = offset;
85 44 : while (line_start > 0 && value[line_start - 1] != '\n' && value[line_start - 1] != '\r') {
86 40 : line_start--;
87 : }
88 :
89 4 : line_end = offset;
90 132 : while (line_end < length && value[line_end] != '\n' && value[line_end] != '\r') {
91 128 : line_end++;
92 : }
93 :
94 4 : word_start = offset;
95 8 : while (word_start > line_start && lsp_doc_is_identifier_char(value[word_start - 1])) {
96 4 : word_start--;
97 : }
98 4 : if (word_start > line_start && value[word_start - 1] == '$') {
99 4 : word_start--;
100 : }
101 :
102 4 : word_end = offset;
103 12 : while (word_end < line_end && lsp_doc_is_identifier_char(value[word_end])) {
104 8 : word_end++;
105 : }
106 :
107 4 : if (word_end <= word_start || word_end - word_start != ZSTR_LEN(expression) || memcmp(value + word_start, ZSTR_VAL(expression), ZSTR_LEN(expression)) != 0) {
108 0 : return false;
109 : }
110 :
111 4 : i = word_end;
112 8 : while (i < line_end && isspace((unsigned char) value[i])) {
113 4 : i++;
114 : }
115 :
116 4 : if (i >= line_end || value[i] != '=') {
117 0 : return false;
118 : }
119 :
120 4 : if ((i + 1 < line_end && (value[i + 1] == '=' || value[i + 1] == '>')) || (i > line_start && (value[i - 1] == '!' || value[i - 1] == '<' || value[i - 1] == '>'))) {
121 0 : return false;
122 : }
123 :
124 4 : return true;
125 : }
126 :
127 4 : static inline void lsp_psalm_trace_insert_location(zend_string *text, size_t offset, size_t *line_start, size_t *line_end, zend_long *one_based_line)
128 : {
129 : const char *value;
130 : size_t i, limit;
131 :
132 4 : value = ZSTR_VAL(text);
133 4 : limit = offset > ZSTR_LEN(text) ? ZSTR_LEN(text) : offset;
134 4 : *line_start = limit;
135 44 : while (*line_start > 0 && value[*line_start - 1] != '\n') {
136 40 : (*line_start)--;
137 : }
138 :
139 4 : *line_end = limit;
140 132 : while (*line_end < ZSTR_LEN(text) && value[*line_end] != '\r' && value[*line_end] != '\n') {
141 128 : (*line_end)++;
142 : }
143 :
144 4 : *one_based_line = 1;
145 1336 : for (i = 0; i < *line_start; i++) {
146 1332 : if (value[i] == '\n') {
147 84 : (*one_based_line)++;
148 : }
149 : }
150 4 : }
151 :
152 4 : static inline zend_string *lsp_psalm_type_shadow_file(zend_string *project_root, lsp_document *document, zend_string *expression, size_t offset, zend_long *trace_line)
153 : {
154 : FILE *handle;
155 : const char *value;
156 : zend_string *cache_dir, *file;
157 4 : smart_str contents = {0};
158 : size_t line_start, line_end, text_length;
159 : zend_long one_based_line;
160 : bool keep_assignment_line;
161 :
162 4 : value = ZSTR_VAL(document->text);
163 4 : text_length = ZSTR_LEN(document->text);
164 4 : lsp_psalm_trace_insert_location(document->text, offset, &line_start, &line_end, &one_based_line);
165 4 : keep_assignment_line = lsp_psalm_trace_is_lhs_assignment(document->text, expression, offset);
166 :
167 4 : cache_dir = strpprintf(0, "%s/.lsparrot/shadow/psalm-type", ZSTR_VAL(project_root));
168 4 : lsp_mkdir_p(cache_dir);
169 4 : file = strpprintf(0, "%s/%ld-" ZEND_LONG_FMT "-%s", ZSTR_VAL(cache_dir), (long) lsp_current_process_id(), document->version, lsp_path_basename(document->path));
170 4 : zend_string_release(cache_dir);
171 :
172 4 : smart_str_appendl(&contents, value, line_start);
173 4 : if (keep_assignment_line) {
174 4 : smart_str_appendl(&contents, value + line_start, line_end - line_start);
175 4 : smart_str_appendc(&contents, '\n');
176 4 : *trace_line = one_based_line + 2;
177 : } else {
178 0 : *trace_line = one_based_line + 1;
179 : }
180 4 : smart_str_appendl(&contents, "/** @psalm-trace ", sizeof("/** @psalm-trace ") - 1);
181 4 : smart_str_append(&contents, expression);
182 4 : smart_str_appendl(&contents, " */\n", sizeof(" */\n") - 1);
183 4 : smart_str_append(&contents, expression);
184 4 : smart_str_appendl(&contents, ";\n", sizeof(";\n") - 1);
185 4 : smart_str_appendl(&contents, value + line_end, text_length - line_end);
186 4 : smart_str_0(&contents);
187 :
188 4 : handle = fopen(ZSTR_VAL(file), "wb");
189 4 : if (handle) {
190 4 : fwrite(ZSTR_VAL(contents.s), 1, ZSTR_LEN(contents.s), handle);
191 4 : fclose(handle);
192 : }
193 :
194 4 : smart_str_free(&contents);
195 :
196 4 : return file;
197 : }
198 :
199 4 : static inline zend_string *lsp_psalm_trace_issue_type(zval *issue_zv, zend_string *expression, zend_long trace_line)
200 : {
201 : const char *message, *type_start;
202 : zend_string *type;
203 : zval *line_zv, *message_zv, *type_zv;
204 : size_t expression_length, message_length, type_length;
205 :
206 4 : if (Z_TYPE_P(issue_zv) != IS_ARRAY) {
207 0 : return NULL;
208 : }
209 :
210 4 : type_zv = zend_hash_str_find(Z_ARRVAL_P(issue_zv), "type", sizeof("type") - 1);
211 4 : if (!type_zv || Z_TYPE_P(type_zv) != IS_STRING || !zend_string_equals_literal(Z_STR_P(type_zv), "Trace")) {
212 0 : return NULL;
213 : }
214 :
215 4 : line_zv = zend_hash_str_find(Z_ARRVAL_P(issue_zv), "line_from", sizeof("line_from") - 1);
216 4 : if (line_zv && Z_TYPE_P(line_zv) == IS_LONG && Z_LVAL_P(line_zv) != trace_line) {
217 0 : return NULL;
218 : }
219 :
220 4 : message_zv = zend_hash_str_find(Z_ARRVAL_P(issue_zv), "message", sizeof("message") - 1);
221 4 : if (!message_zv || Z_TYPE_P(message_zv) != IS_STRING) {
222 0 : return NULL;
223 : }
224 :
225 4 : message = Z_STRVAL_P(message_zv);
226 4 : message_length = Z_STRLEN_P(message_zv);
227 4 : expression_length = ZSTR_LEN(expression);
228 4 : if (message_length <= expression_length + 2 || memcmp(message, ZSTR_VAL(expression), expression_length) != 0 || message[expression_length] != ':' || message[expression_length + 1] != ' ') {
229 0 : return NULL;
230 : }
231 :
232 4 : type_start = message + expression_length + 2;
233 4 : type_length = message_length - expression_length - 2;
234 4 : type = zend_string_init(type_start, type_length, 0);
235 4 : if (lsp_type_is_unhelpful(type)) {
236 0 : zend_string_release(type);
237 :
238 0 : return NULL;
239 : }
240 :
241 4 : return type;
242 : }
243 :
244 4 : static inline zend_string *lsp_psalm_trace_type_from_decoded(zval *decoded, zend_string *expression, zend_long trace_line)
245 : {
246 : zend_string *type;
247 : zval *issue_zv;
248 :
249 4 : if (Z_TYPE_P(decoded) != IS_ARRAY) {
250 0 : return NULL;
251 : }
252 :
253 4 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(decoded), issue_zv) {
254 4 : type = lsp_psalm_trace_issue_type(issue_zv, expression, trace_line);
255 4 : if (type) {
256 4 : return type;
257 : }
258 : } ZEND_HASH_FOREACH_END();
259 :
260 0 : return NULL;
261 : }
262 :
263 4 : extern zend_string *lsp_psalm_parse_type_output(zend_string *output, zend_string *expression, zend_long trace_line)
264 : {
265 : zend_string *json, *type;
266 : zval decoded;
267 :
268 4 : if (!output || !expression) {
269 0 : return NULL;
270 : }
271 :
272 4 : json = lsp_json_slice_from(output, '[');
273 4 : ZVAL_UNDEF(&decoded);
274 4 : php_json_decode_ex(&decoded, ZSTR_VAL(json), ZSTR_LEN(json), PHP_JSON_OBJECT_AS_ARRAY, 512);
275 4 : zend_string_release(json);
276 :
277 4 : type = NULL;
278 4 : if (Z_TYPE(decoded) == IS_ARRAY) {
279 4 : type = lsp_psalm_trace_type_from_decoded(&decoded, expression, trace_line);
280 : }
281 :
282 4 : if (!Z_ISUNDEF(decoded)) {
283 4 : zval_ptr_dtor(&decoded);
284 : }
285 :
286 4 : return type;
287 : }
288 :
289 20 : extern zend_string *lsp_psalm_type_for_expression(lsp_server *server, lsp_document *document, zend_string *expression, size_t offset)
290 : {
291 : lsp_command command;
292 : zend_long trace_line, level;
293 : zend_string *project_root, *analysis_file, *config, *generated_config, *output, *type, *cache_key, *root_arg, *config_arg;
294 : zval *cached, cache_value, descriptor;
295 20 : bool deferred = false;
296 :
297 20 : if (!server->psalm_enabled || !lsp_psalm_type_expression_safe(expression)) {
298 0 : return NULL;
299 : }
300 :
301 20 : cache_key = lsp_type_cache_key("psalm", document, expression, offset);
302 20 : cached = zend_hash_find(&server->type_cache, cache_key);
303 20 : if (cached) {
304 4 : if (Z_TYPE_P(cached) == IS_STRING) {
305 4 : type = zend_string_copy(Z_STR_P(cached));
306 4 : zend_string_release(cache_key);
307 :
308 4 : return type;
309 : }
310 :
311 : /* IS_DOUBLE marks a query still running in the background; retry
312 : * only once its deadline has passed. */
313 0 : if (Z_TYPE_P(cached) != IS_DOUBLE || lsp_now_seconds() < Z_DVAL_P(cached)) {
314 0 : zend_string_release(cache_key);
315 :
316 0 : return NULL;
317 : }
318 :
319 0 : zend_hash_del(&server->type_cache, cache_key);
320 : }
321 :
322 16 : project_root = lsp_document_project_root(server, document);
323 16 : if (!lsp_document_is_in_analyzer_scope(document, project_root)) {
324 12 : zend_string_release(project_root);
325 12 : ZVAL_FALSE(&cache_value);
326 12 : zend_hash_update(&server->type_cache, cache_key, &cache_value);
327 12 : zend_string_release(cache_key);
328 :
329 12 : return NULL;
330 : }
331 :
332 4 : config = lsp_psalm_config_file(project_root);
333 4 : level = lsp_project_psalm_level(server, project_root);
334 4 : generated_config = lsp_psalm_type_config_file(project_root, config, level, server->options.psalm_live_dead_code_diagnostics);
335 4 : if (!generated_config && !config) {
336 0 : if (config) {
337 0 : zend_string_release(config);
338 : }
339 0 : zend_string_release(project_root);
340 0 : ZVAL_FALSE(&cache_value);
341 0 : zend_hash_update(&server->type_cache, cache_key, &cache_value);
342 0 : zend_string_release(cache_key);
343 :
344 0 : return NULL;
345 : }
346 :
347 4 : lsp_command_init(&command);
348 4 : if (!lsp_tool_command(server, project_root, "psalm", &command)) {
349 0 : lsp_command_destroy(&command);
350 0 : if (generated_config) {
351 0 : zend_string_release(generated_config);
352 : }
353 0 : if (config) {
354 0 : zend_string_release(config);
355 : }
356 0 : zend_string_release(project_root);
357 0 : ZVAL_FALSE(&cache_value);
358 0 : zend_hash_update(&server->type_cache, cache_key, &cache_value);
359 0 : zend_string_release(cache_key);
360 :
361 0 : return NULL;
362 : }
363 :
364 4 : analysis_file = lsp_psalm_type_shadow_file(project_root, document, expression, offset, &trace_line);
365 4 : root_arg = strpprintf(0, "--root=%s", ZSTR_VAL(project_root));
366 4 : config_arg = strpprintf(0, "--config=%s", ZSTR_VAL(generated_config ? generated_config : config));
367 4 : lsp_command_add(&command, "--output-format=json");
368 4 : lsp_command_add(&command, "--no-progress");
369 4 : lsp_command_add(&command, "--show-info=true");
370 4 : lsp_analyzer_add_memory_limit(&command, server);
371 4 : lsp_psalm_add_parallel_args(&command, lsp_analyzer_parallel_workers(server));
372 4 : lsp_psalm_add_configured_level(&command, config, level);
373 4 : lsp_command_add_zstr(&command, root_arg);
374 4 : lsp_command_add_zstr(&command, config_arg);
375 4 : lsp_command_add_zstr(&command, analysis_file);
376 4 : zend_string_release(root_arg);
377 4 : zend_string_release(config_arg);
378 4 : if (generated_config) {
379 4 : zend_string_release(generated_config);
380 : }
381 4 : if (config) {
382 0 : zend_string_release(config);
383 : }
384 :
385 4 : array_init(&descriptor);
386 4 : add_assoc_string(&descriptor, "analyzer", "psalm");
387 4 : add_assoc_str(&descriptor, "cacheKey", zend_string_copy(cache_key));
388 4 : add_assoc_str(&descriptor, "analysisFile", zend_string_copy(analysis_file));
389 4 : add_assoc_long(&descriptor, "markerLine", trace_line);
390 4 : add_assoc_str(&descriptor, "expression", zend_string_copy(expression));
391 4 : add_assoc_str(&descriptor, "uri", zend_string_copy(document->uri));
392 :
393 4 : output = lsp_runner_run_capture_deferred(server, "psalm", project_root, &command, project_root,
394 : server->options.analyzer_type_query_timeout, server->options.analyzer_diagnostics_timeout, &descriptor, &deferred);
395 4 : lsp_command_destroy(&command);
396 4 : zend_string_release(project_root);
397 :
398 4 : if (deferred) {
399 : /* The runner keeps working; mark the key as in flight (retry after
400 : * the deadline) and let the pump fill the real value in. The shadow
401 : * file must survive until the background analysis finishes. */
402 0 : ZVAL_DOUBLE(&cache_value, lsp_now_seconds() + server->options.analyzer_diagnostics_timeout + 10.0);
403 0 : zend_hash_update(&server->type_cache, cache_key, &cache_value);
404 0 : zend_string_release(cache_key);
405 0 : zend_string_release(analysis_file);
406 :
407 0 : return NULL;
408 : }
409 :
410 4 : zval_ptr_dtor(&descriptor);
411 :
412 4 : type = lsp_psalm_parse_type_output(output, expression, trace_line);
413 4 : if (output && output != zend_empty_string) {
414 4 : zend_string_release(output);
415 : }
416 :
417 : /* One shadow copy is written per probe; without the unlink they
418 : * accumulate in the project for the whole session. */
419 4 : VCWD_UNLINK(ZSTR_VAL(analysis_file));
420 4 : zend_string_release(analysis_file);
421 4 : if (lsp_type_is_unhelpful(type)) {
422 0 : if (type) {
423 0 : zend_string_release(type);
424 : }
425 0 : type = NULL;
426 : }
427 :
428 4 : if (type) {
429 4 : ZVAL_STR_COPY(&cache_value, type);
430 : } else {
431 0 : ZVAL_FALSE(&cache_value);
432 : }
433 4 : zend_hash_update(&server->type_cache, cache_key, &cache_value);
434 4 : zend_string_release(cache_key);
435 :
436 4 : return type;
437 : }
438 :
439 32 : static inline bool lsp_start_psalm_project_analyzer(lsp_server *server, zend_string *project_root)
440 : {
441 : lsp_command command;
442 : zend_string *config, *generated_config, *output_file, *root_arg, *config_arg;
443 : zend_long level;
444 : bool started;
445 :
446 32 : config = lsp_psalm_config_file(project_root);
447 32 : level = lsp_project_psalm_level(server, project_root);
448 :
449 32 : if (!lsp_analyzer_project_has_scope(project_root)) {
450 0 : if (config) {
451 0 : zend_string_release(config);
452 : }
453 :
454 0 : return false;
455 : }
456 :
457 32 : lsp_command_init(&command);
458 32 : if (!lsp_tool_command(server, project_root, "psalm", &command)) {
459 0 : lsp_command_destroy(&command);
460 0 : if (config) {
461 0 : zend_string_release(config);
462 : }
463 :
464 0 : return false;
465 : }
466 :
467 32 : output_file = lsp_analyzer_project_output_file(project_root, "psalm");
468 32 : root_arg = strpprintf(0, "--root=%s", ZSTR_VAL(project_root));
469 :
470 32 : generated_config = lsp_psalm_ls_config_file(project_root, config, level, server->options.psalm_live_dead_code_diagnostics);
471 32 : if (!generated_config && !config) {
472 0 : zend_string_release(root_arg);
473 0 : zend_string_release(output_file);
474 0 : lsp_command_destroy(&command);
475 :
476 0 : return false;
477 : }
478 :
479 32 : config_arg = strpprintf(0, "--config=%s", ZSTR_VAL(generated_config ? generated_config : config));
480 32 : lsp_command_add(&command, "--output-format=json");
481 32 : lsp_command_add(&command, "--no-progress");
482 32 : lsp_command_add(&command, server->options.psalm_show_info ? "--show-info=true" : "--show-info=false");
483 32 : lsp_analyzer_add_memory_limit(&command, server);
484 32 : lsp_psalm_add_parallel_args(&command, lsp_analyzer_parallel_workers(server));
485 32 : lsp_psalm_add_configured_level(&command, config, level);
486 32 : lsp_command_add_zstr(&command, root_arg);
487 32 : lsp_command_add_zstr(&command, config_arg);
488 32 : zend_string_release(root_arg);
489 32 : zend_string_release(config_arg);
490 :
491 32 : if (generated_config) {
492 32 : zend_string_release(generated_config);
493 : }
494 :
495 32 : if (config) {
496 12 : zend_string_release(config);
497 : }
498 :
499 32 : started = lsp_start_analyzer_project_job(server, "psalm", project_root, &command, output_file);
500 32 : if (started) {
501 32 : lsp_analyzer_project_status("psalm", "running", "Prewarming Psalm project diagnostics.", project_root);
502 : }
503 :
504 32 : zend_string_release(output_file);
505 32 : lsp_command_destroy(&command);
506 :
507 32 : return started;
508 : }
509 :
510 56 : extern void lsp_start_pending_psalm_project_analyzer(lsp_server *server)
511 : {
512 56 : zend_string *project_root = NULL, *candidate;
513 : zval *state_zv;
514 : zend_long capacity;
515 :
516 : /* Independent projects analyze concurrently up to the worker budget. */
517 56 : capacity = lsp_analyzer_parallel_workers(server);
518 56 : if (capacity < 1) {
519 0 : capacity = 1;
520 : }
521 :
522 88 : while ((zend_long) lsp_analyzer_running_project_jobs(server) < capacity) {
523 88 : candidate = NULL;
524 144 : ZEND_HASH_FOREACH_STR_KEY_VAL(&server->psalm_projects, project_root, state_zv) {
525 88 : if (project_root && Z_TYPE_P(state_zv) == IS_LONG && Z_LVAL_P(state_zv) == LSP_ANALYZER_PROJECT_PENDING) {
526 32 : candidate = zend_string_copy(project_root);
527 32 : break;
528 : }
529 : } ZEND_HASH_FOREACH_END();
530 :
531 88 : if (!candidate) {
532 56 : return;
533 : }
534 :
535 32 : if (!lsp_start_psalm_project_analyzer(server, candidate)) {
536 0 : zend_hash_del(&server->psalm_projects, candidate);
537 : }
538 :
539 32 : zend_string_release(candidate);
540 : }
541 : }
542 :
543 592 : extern void lsp_schedule_psalm_project_analyzer(lsp_server *server, zend_string *project_root)
544 : {
545 592 : if (!server->psalm_enabled || lsp_analyzer_project_has_state(server, "psalm", project_root)) {
546 560 : return;
547 : }
548 :
549 32 : if (!lsp_analyzer_project_has_scope(project_root)) {
550 0 : return;
551 : }
552 :
553 32 : if (!lsp_tool_available("psalm", project_root)) {
554 0 : return;
555 : }
556 :
557 32 : lsp_analyzer_project_state(server, "psalm", project_root, LSP_ANALYZER_PROJECT_PENDING);
558 32 : lsp_start_pending_psalm_project_analyzer(server);
559 : }
560 :
561 400 : extern void lsp_append_psalm_cached_diagnostics(lsp_server *server, lsp_document *document, zval *diagnostics)
562 : {
563 : zend_long line, severity;
564 : zend_string *project_root, *output_file, *output, *json, *failure_message;
565 : zval decoded, *file_zv, *issue_zv, *line_zv, *message_text_zv, *type_zv, *severity_zv, *column_from_zv, *column_to_zv, range;
566 :
567 400 : if (!server->psalm_enabled) {
568 368 : return;
569 : }
570 :
571 68 : project_root = lsp_document_project_root(server, document);
572 68 : if (!lsp_document_is_in_analyzer_scope(document, project_root)) {
573 12 : zend_string_release(project_root);
574 :
575 12 : return;
576 : }
577 :
578 56 : output_file = lsp_analyzer_project_output_file(project_root, "psalm");
579 56 : if (!lsp_is_regular_file(output_file)) {
580 20 : zend_string_release(output_file);
581 20 : zend_string_release(project_root);
582 :
583 20 : return;
584 : }
585 :
586 36 : output = lsp_read_file(output_file);
587 36 : zend_string_release(output_file);
588 36 : zend_string_release(project_root);
589 36 : if (output == zend_empty_string) {
590 0 : return;
591 : }
592 :
593 36 : json = lsp_json_slice_from(output, '[');
594 :
595 36 : ZVAL_UNDEF(&decoded);
596 36 : php_json_decode_ex(&decoded, ZSTR_VAL(json), ZSTR_LEN(json), PHP_JSON_OBJECT_AS_ARRAY, 512);
597 36 : zend_string_release(json);
598 36 : if (Z_TYPE(decoded) != IS_ARRAY) {
599 4 : if (!Z_ISUNDEF(decoded)) {
600 4 : zval_ptr_dtor(&decoded);
601 : }
602 :
603 4 : failure_message = lsp_analyzer_failure_message("psalm", output);
604 4 : lsp_line_range(&range, document->text, 1);
605 4 : lsp_add_analyzer_diagnostic(diagnostics, "psalm", failure_message, NULL, &range, 1);
606 4 : zend_string_release(failure_message);
607 4 : zend_string_release(output);
608 :
609 4 : return;
610 : }
611 32 : zend_string_release(output);
612 :
613 36 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL(decoded), issue_zv) {
614 4 : severity = 2;
615 4 : if (Z_TYPE_P(issue_zv) != IS_ARRAY) {
616 0 : continue;
617 : }
618 :
619 4 : file_zv = zend_hash_str_find(Z_ARRVAL_P(issue_zv), "file_path", sizeof("file_path") - 1);
620 4 : if (!file_zv || Z_TYPE_P(file_zv) != IS_STRING || !zend_string_equals(Z_STR_P(file_zv), document->path)) {
621 0 : continue;
622 : }
623 :
624 4 : message_text_zv = zend_hash_str_find(Z_ARRVAL_P(issue_zv), "message", sizeof("message") - 1);
625 4 : if (!message_text_zv || Z_TYPE_P(message_text_zv) != IS_STRING) {
626 0 : continue;
627 : }
628 :
629 4 : line_zv = zend_hash_str_find(Z_ARRVAL_P(issue_zv), "line_from", sizeof("line_from") - 1);
630 4 : type_zv = zend_hash_str_find(Z_ARRVAL_P(issue_zv), "type", sizeof("type") - 1);
631 :
632 4 : severity_zv = zend_hash_str_find(Z_ARRVAL_P(issue_zv), "severity", sizeof("severity") - 1);
633 4 : if (severity_zv && Z_TYPE_P(severity_zv) == IS_STRING) {
634 4 : if (zend_string_equals_literal(Z_STR_P(severity_zv), "info")) {
635 0 : severity = 3;
636 4 : } else if (zend_string_equals_literal(Z_STR_P(severity_zv), "error")) {
637 4 : severity = 1;
638 : }
639 : }
640 :
641 4 : column_from_zv = zend_hash_str_find(Z_ARRVAL_P(issue_zv), "column_from", sizeof("column_from") - 1);
642 4 : column_to_zv = zend_hash_str_find(Z_ARRVAL_P(issue_zv), "column_to", sizeof("column_to") - 1);
643 :
644 4 : line = line_zv && Z_TYPE_P(line_zv) == IS_LONG ? Z_LVAL_P(line_zv) : 1;
645 4 : lsp_line_range_columns(&range, document->text, line,
646 0 : column_from_zv && Z_TYPE_P(column_from_zv) == IS_LONG ? Z_LVAL_P(column_from_zv) : 0,
647 0 : column_to_zv && Z_TYPE_P(column_to_zv) == IS_LONG ? Z_LVAL_P(column_to_zv) : 0
648 : );
649 8 : lsp_add_analyzer_diagnostic(diagnostics, "psalm", Z_STR_P(message_text_zv),
650 4 : type_zv && Z_TYPE_P(type_zv) == IS_STRING ? Z_STR_P(type_zv) : NULL, &range, severity
651 : );
652 : } ZEND_HASH_FOREACH_END();
653 :
654 32 : zval_ptr_dtor(&decoded);
655 : }
656 :
657 0 : extern void lsp_reschedule_psalm_project_analyzer(lsp_server *server, zend_string *project_root)
658 : {
659 : zend_long state;
660 :
661 0 : if (!server->psalm_enabled) {
662 0 : return;
663 : }
664 :
665 0 : if (!lsp_analyzer_project_has_scope(project_root)) {
666 0 : return;
667 : }
668 :
669 0 : if (!lsp_tool_available("psalm", project_root)) {
670 0 : return;
671 : }
672 :
673 0 : state = lsp_analyzer_project_state_value(server, "psalm", project_root);
674 0 : if (state == LSP_ANALYZER_PROJECT_RUNNING) {
675 0 : lsp_analyzer_project_state(server, "psalm", project_root, LSP_ANALYZER_PROJECT_PENDING);
676 0 : return;
677 : }
678 :
679 0 : zend_hash_del(&server->psalm_projects, project_root);
680 0 : lsp_schedule_psalm_project_analyzer(server, project_root);
681 : }
|