LCOV - code coverage report
Current view: top level - ext - lsp_server_phpstan.c (source / functions) Coverage Total Hit
Test: Extension code coverage Lines: 78.7 % 315 248
Test Date: 2026-07-13 00:58:38 Functions: 92.9 % 14 13

            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           88 : static inline void lsp_phpstan_add_configured_level(lsp_command *command, zend_string *project_config, zend_long level)
      17              : {
      18              :         zend_string *argument;
      19              : 
      20           88 :         if (project_config) {
      21           12 :                 return;
      22              :         }
      23              : 
      24           76 :         if (level < 0) {
      25            0 :                 level = 6;
      26              :         }
      27              : 
      28           76 :         argument = strpprintf(0, "--level=" ZEND_LONG_FMT, level);
      29           76 :         lsp_command_add_zstr(command, argument);
      30              : 
      31           76 :         zend_string_release(argument);
      32              : }
      33              : 
      34           64 : static inline bool lsp_start_phpstan_project_analyzer(lsp_server *server, zend_string *project_root)
      35              : {
      36              :         lsp_command command;
      37              :         zend_string *config, *generated_config, *output_file;
      38              :         bool started;
      39              : 
      40           64 :         if (!lsp_analyzer_project_has_scope(project_root)) {
      41            0 :                 return false;
      42              :         }
      43              : 
      44           64 :         lsp_command_init(&command);
      45           64 :         if (!lsp_tool_command(server, project_root, "phpstan", &command)) {
      46            0 :                 lsp_command_destroy(&command);
      47              : 
      48            0 :                 return false;
      49              :         }
      50              : 
      51           64 :         output_file = lsp_analyzer_project_output_file(project_root, "phpstan");
      52           64 :         lsp_command_add(&command, "analyse");
      53           64 :         config = lsp_phpstan_config_file(project_root);
      54           64 :         generated_config = lsp_phpstan_lsp_config_file(project_root, config, lsp_analyzer_parallel_workers(server));
      55              : 
      56           64 :         if (!config) {
      57           52 :                 lsp_command_add_composer_analysis_paths(&command, project_root);
      58              :         }
      59              : 
      60           64 :         lsp_command_add(&command, "--error-format=json");
      61           64 :         lsp_command_add(&command, "--no-progress");
      62           64 :         lsp_analyzer_add_memory_limit(&command, server);
      63           64 :         lsp_phpstan_add_configured_level(&command, config, lsp_project_phpstan_level(server, project_root));
      64              : 
      65           64 :         if (generated_config) {
      66           64 :                 lsp_command_add(&command, "-c");
      67           64 :                 lsp_command_add_zstr(&command, generated_config);
      68           64 :                 zend_string_release(generated_config);
      69            0 :         } else if (config) {
      70            0 :                 lsp_command_add(&command, "-c");
      71            0 :                 lsp_command_add_zstr(&command, config);
      72              :         }
      73              : 
      74           64 :         if (config) {
      75           12 :                 zend_string_release(config);
      76              :         }
      77              : 
      78           64 :         started = lsp_start_analyzer_project_job(server, "phpstan", project_root, &command, output_file);
      79           64 :         if (started) {
      80           64 :                 lsp_analyzer_project_status("phpstan", "running", "Prewarming PHPStan project diagnostics.", project_root);
      81              :         }
      82              : 
      83           64 :         zend_string_release(output_file);
      84           64 :         lsp_command_destroy(&command);
      85              : 
      86           64 :         return started;
      87              : }
      88              : 
      89          108 : extern void lsp_start_pending_phpstan_project_analyzer(lsp_server *server)
      90              : {
      91          108 :         zend_string *project_root = NULL, *candidate;
      92              :         zval *state_zv;
      93              :         zend_long capacity;
      94              : 
      95              :         /* Independent projects analyze concurrently up to the worker budget. */
      96          108 :         capacity = lsp_analyzer_parallel_workers(server);
      97          108 :         if (capacity < 1) {
      98            0 :                 capacity = 1;
      99              :         }
     100              : 
     101          172 :         while ((zend_long) lsp_analyzer_running_project_jobs(server) < capacity) {
     102          172 :                 candidate = NULL;
     103          280 :                 ZEND_HASH_FOREACH_STR_KEY_VAL(&server->phpstan_projects, project_root, state_zv) {
     104          172 :                         if (project_root && Z_TYPE_P(state_zv) == IS_LONG && Z_LVAL_P(state_zv) == LSP_ANALYZER_PROJECT_PENDING) {
     105           64 :                                 candidate = zend_string_copy(project_root);
     106           64 :                                 break;
     107              :                         }
     108              :                 } ZEND_HASH_FOREACH_END();
     109              : 
     110          172 :                 if (!candidate) {
     111          108 :                         return;
     112              :                 }
     113              : 
     114           64 :                 if (!lsp_start_phpstan_project_analyzer(server, candidate)) {
     115            0 :                         zend_hash_del(&server->phpstan_projects, candidate);
     116              :                 }
     117              : 
     118           64 :                 zend_string_release(candidate);
     119              :         }
     120              : }
     121              : 
     122          592 : extern void lsp_schedule_phpstan_project_analyzer(lsp_server *server, zend_string *project_root)
     123              : {
     124          592 :         if (!server->phpstan_enabled || lsp_analyzer_project_has_state(server, "phpstan", project_root)) {
     125          528 :                 return;
     126              :         }
     127              : 
     128           64 :         if (!lsp_analyzer_project_has_scope(project_root)) {
     129            0 :                 return;
     130              :         }
     131              : 
     132           64 :         if (!lsp_tool_available("phpstan", project_root)) {
     133            0 :                 return;
     134              :         }
     135              : 
     136           64 :         lsp_analyzer_project_state(server, "phpstan", project_root, LSP_ANALYZER_PROJECT_PENDING);
     137           64 :         lsp_start_pending_phpstan_project_analyzer(server);
     138              : }
     139              : 
     140           84 : static inline bool lsp_phpstan_type_expression_safe(zend_string *expression)
     141              : {
     142              :         const char *value;
     143              :         size_t i;
     144              : 
     145           84 :         if (!expression || ZSTR_LEN(expression) == 0) {
     146            0 :                 return false;
     147              :         }
     148              : 
     149           84 :         value = ZSTR_VAL(expression);
     150         1184 :         for (i = 0; i < ZSTR_LEN(expression); i++) {
     151         1100 :                 if (value[i] == '\r' || value[i] == '\n' || value[i] == ';') {
     152            0 :                         return false;
     153              :                 }
     154              :         }
     155              : 
     156           84 :         return true;
     157              : }
     158              : 
     159           24 : static inline void lsp_phpstan_dump_insert_location(zend_string *text, size_t offset, size_t *line_start, size_t *line_end, zend_long *one_based_line)
     160              : {
     161              :         const char *value;
     162              :         size_t i, limit;
     163              : 
     164           24 :         value = ZSTR_VAL(text);
     165           24 :         limit = offset > ZSTR_LEN(text) ? ZSTR_LEN(text) : offset;
     166           24 :         *line_start = limit;
     167          280 :         while (*line_start > 0 && value[*line_start - 1] != '\n') {
     168          256 :                 (*line_start)--;
     169              :         }
     170              : 
     171           24 :         *line_end = limit;
     172          252 :         while (*line_end < ZSTR_LEN(text) && value[*line_end] != '\r' && value[*line_end] != '\n') {
     173          228 :                 (*line_end)++;
     174              :         }
     175              : 
     176           24 :         *one_based_line = 1;
     177         5912 :         for (i = 0; i < *line_start; i++) {
     178         5888 :                 if (value[i] == '\n') {
     179          368 :                         (*one_based_line)++;
     180              :                 }
     181              :         }
     182           24 : }
     183              : 
     184           24 : static inline zend_string *lsp_phpstan_type_shadow_file(zend_string *project_root, lsp_document *document, zend_string *expression, size_t offset, zend_long *dump_line)
     185              : {
     186              :         FILE *handle;
     187              :         const char *value;
     188              :         zend_string *cache_dir, *file;
     189           24 :         smart_str contents = {0};
     190              :         size_t line_start, line_end, text_length;
     191              : 
     192           24 :         value = ZSTR_VAL(document->text);
     193           24 :         text_length = ZSTR_LEN(document->text);
     194           24 :         lsp_phpstan_dump_insert_location(document->text, offset, &line_start, &line_end, dump_line);
     195              : 
     196           24 :         cache_dir = strpprintf(0, "%s/.lsparrot/shadow/phpstan-type", ZSTR_VAL(project_root));
     197           24 :         lsp_mkdir_p(cache_dir);
     198           24 :         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));
     199           24 :         zend_string_release(cache_dir);
     200              : 
     201           24 :         smart_str_appendl(&contents, value, line_start);
     202           24 :         smart_str_appendl(&contents, "\\PHPStan\\dumpType(", sizeof("\\PHPStan\\dumpType(") - 1);
     203           24 :         smart_str_append(&contents, expression);
     204           24 :         smart_str_appendl(&contents, ");\n", sizeof(");\n") - 1);
     205           24 :         smart_str_appendl(&contents, value + line_end, text_length - line_end);
     206           24 :         smart_str_0(&contents);
     207              : 
     208           24 :         handle = fopen(ZSTR_VAL(file), "wb");
     209           24 :         if (handle) {
     210           24 :                 fwrite(ZSTR_VAL(contents.s), 1, ZSTR_LEN(contents.s), handle);
     211           24 :                 fclose(handle);
     212              :         }
     213              : 
     214           24 :         smart_str_free(&contents);
     215              : 
     216           24 :         return file;
     217              : }
     218              : 
     219           24 : static inline zend_string *lsp_phpstan_dump_message_type(zval *message_zv, zend_long dump_line)
     220              : {
     221              :         const char *prefix, *message;
     222              :         zend_string *type;
     223              :         zval *line_zv, *message_text_zv, *identifier_zv;
     224              :         size_t prefix_length;
     225              : 
     226           24 :         prefix = "Dumped type: ";
     227           24 :         prefix_length = sizeof("Dumped type: ") - 1;
     228              : 
     229           24 :         if (Z_TYPE_P(message_zv) != IS_ARRAY) {
     230            0 :                 return NULL;
     231              :         }
     232              : 
     233           24 :         identifier_zv = zend_hash_str_find(Z_ARRVAL_P(message_zv), "identifier", sizeof("identifier") - 1);
     234           24 :         if (identifier_zv && Z_TYPE_P(identifier_zv) == IS_STRING && !zend_string_equals_literal(Z_STR_P(identifier_zv), "phpstan.dumpType")) {
     235            0 :                 return NULL;
     236              :         }
     237              : 
     238           24 :         message_text_zv = zend_hash_str_find(Z_ARRVAL_P(message_zv), "message", sizeof("message") - 1);
     239           24 :         if (!message_text_zv || Z_TYPE_P(message_text_zv) != IS_STRING || Z_STRLEN_P(message_text_zv) <= prefix_length) {
     240            0 :                 return NULL;
     241              :         }
     242              : 
     243           24 :         message = Z_STRVAL_P(message_text_zv);
     244           24 :         if (memcmp(message, prefix, prefix_length) != 0) {
     245            0 :                 return NULL;
     246              :         }
     247              : 
     248           24 :         line_zv = zend_hash_str_find(Z_ARRVAL_P(message_zv), "line", sizeof("line") - 1);
     249           24 :         if (line_zv && Z_TYPE_P(line_zv) == IS_LONG && Z_LVAL_P(line_zv) != dump_line) {
     250            0 :                 return NULL;
     251              :         }
     252              : 
     253           24 :         type = zend_string_init(message + prefix_length, Z_STRLEN_P(message_text_zv) - prefix_length, 0);
     254           24 :         if (lsp_type_is_unhelpful(type)) {
     255            0 :                 zend_string_release(type);
     256              : 
     257            0 :                 return NULL;
     258              :         }
     259              : 
     260           24 :         return type;
     261              : }
     262              : 
     263           24 : static inline zend_string *lsp_phpstan_dump_type_from_messages(zval *messages, zend_long dump_line)
     264              : {
     265              :         zend_string *fallback, *type;
     266              :         zval *message_zv, *line_zv;
     267              : 
     268           24 :         if (!messages || Z_TYPE_P(messages) != IS_ARRAY) {
     269            0 :                 return NULL;
     270              :         }
     271              : 
     272           24 :         fallback = NULL;
     273           24 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(messages), message_zv) {
     274           24 :                 type = lsp_phpstan_dump_message_type(message_zv, dump_line);
     275           24 :                 if (!type) {
     276            0 :                         continue;
     277              :                 }
     278              : 
     279           24 :                 line_zv = zend_hash_str_find(Z_ARRVAL_P(message_zv), "line", sizeof("line") - 1);
     280           24 :                 if (!line_zv || Z_TYPE_P(line_zv) != IS_LONG || Z_LVAL_P(line_zv) == dump_line) {
     281           24 :                         if (fallback) {
     282            0 :                                 zend_string_release(fallback);
     283              :                         }
     284              : 
     285           24 :                         return type;
     286              :                 }
     287              : 
     288            0 :                 if (!fallback) {
     289            0 :                         fallback = type;
     290              :                 } else {
     291            0 :                         zend_string_release(type);
     292              :                 }
     293              :         } ZEND_HASH_FOREACH_END();
     294              : 
     295            0 :         return fallback;
     296              : }
     297              : 
     298           24 : static inline zend_string *lsp_phpstan_dump_type_from_decoded(zval *decoded, zend_string *analysis_file, zend_long dump_line)
     299              : {
     300              :         zend_string *type;
     301              :         zval *files, *file_result, *messages, *other_file_result;
     302              : 
     303           24 :         files = zend_hash_str_find(Z_ARRVAL_P(decoded), "files", sizeof("files") - 1);
     304           24 :         if (!files || Z_TYPE_P(files) != IS_ARRAY) {
     305            0 :                 return NULL;
     306              :         }
     307              : 
     308           24 :         file_result = zend_hash_find(Z_ARRVAL_P(files), analysis_file);
     309           24 :         messages = file_result && Z_TYPE_P(file_result) == IS_ARRAY ? zend_hash_str_find(Z_ARRVAL_P(file_result), "messages", sizeof("messages") - 1) : NULL;
     310           24 :         type = lsp_phpstan_dump_type_from_messages(messages, dump_line);
     311           24 :         if (type) {
     312           24 :                 return type;
     313              :         }
     314              : 
     315            0 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(files), other_file_result) {
     316            0 :                 if (Z_TYPE_P(other_file_result) != IS_ARRAY) {
     317            0 :                         continue;
     318              :                 }
     319              : 
     320            0 :                 messages = zend_hash_str_find(Z_ARRVAL_P(other_file_result), "messages", sizeof("messages") - 1);
     321            0 :                 type = lsp_phpstan_dump_type_from_messages(messages, dump_line);
     322            0 :                 if (type) {
     323            0 :                         return type;
     324              :                 }
     325              :         } ZEND_HASH_FOREACH_END();
     326              : 
     327            0 :         return NULL;
     328              : }
     329              : 
     330           24 : extern zend_string *lsp_phpstan_parse_type_output(zend_string *output, zend_string *analysis_file, zend_long dump_line)
     331              : {
     332              :         zend_string *json, *type;
     333              :         zval decoded;
     334              : 
     335           24 :         if (!output || !analysis_file) {
     336            0 :                 return NULL;
     337              :         }
     338              : 
     339           24 :         json = lsp_json_slice_from(output, '{');
     340           24 :         ZVAL_UNDEF(&decoded);
     341           24 :         php_json_decode_ex(&decoded, ZSTR_VAL(json), ZSTR_LEN(json), PHP_JSON_OBJECT_AS_ARRAY, 512);
     342           24 :         zend_string_release(json);
     343              : 
     344           24 :         type = NULL;
     345           24 :         if (Z_TYPE(decoded) == IS_ARRAY) {
     346           24 :                 type = lsp_phpstan_dump_type_from_decoded(&decoded, analysis_file, dump_line);
     347              :         }
     348              : 
     349           24 :         if (!Z_ISUNDEF(decoded)) {
     350           24 :                 zval_ptr_dtor(&decoded);
     351              :         }
     352              : 
     353           24 :         return type;
     354              : }
     355              : 
     356           84 : extern zend_string *lsp_phpstan_type_for_expression(lsp_server *server, lsp_document *document, zend_string *expression, size_t offset)
     357              : {
     358              :         lsp_command command;
     359              :         zend_long dump_line;
     360              :         zend_string *project_root, *analysis_file, *config, *generated_config, *output, *type, *cache_key;
     361              :         zval *cached, cache_value, descriptor;
     362           84 :         bool deferred = false;
     363              : 
     364           84 :         if (!server->phpstan_enabled || !lsp_phpstan_type_expression_safe(expression)) {
     365            0 :                 return NULL;
     366              :         }
     367              : 
     368           84 :         cache_key = lsp_type_cache_key("phpstan", document, expression, offset);
     369           84 :         cached = zend_hash_find(&server->type_cache, cache_key);
     370           84 :         if (cached) {
     371           20 :                 if (Z_TYPE_P(cached) == IS_STRING) {
     372           12 :                         type = zend_string_copy(Z_STR_P(cached));
     373           12 :                         zend_string_release(cache_key);
     374              : 
     375           12 :                         return type;
     376              :                 }
     377              : 
     378              :                 /* IS_DOUBLE marks a query still running in the background; retry
     379              :                  * only once its deadline has passed. */
     380            8 :                 if (Z_TYPE_P(cached) != IS_DOUBLE || lsp_now_seconds() < Z_DVAL_P(cached)) {
     381            8 :                         zend_string_release(cache_key);
     382              : 
     383            8 :                         return NULL;
     384              :                 }
     385              : 
     386            0 :                 zend_hash_del(&server->type_cache, cache_key);
     387              :         }
     388              : 
     389           64 :         project_root = lsp_document_project_root(server, document);
     390           64 :         if (!lsp_document_is_in_analyzer_scope(document, project_root)) {
     391           40 :                 zend_string_release(project_root);
     392           40 :                 ZVAL_FALSE(&cache_value);
     393           40 :                 zend_hash_update(&server->type_cache, cache_key, &cache_value);
     394           40 :                 zend_string_release(cache_key);
     395              : 
     396           40 :                 return NULL;
     397              :         }
     398              : 
     399           24 :         lsp_command_init(&command);
     400           24 :         if (!lsp_tool_command(server, project_root, "phpstan", &command)) {
     401            0 :                 lsp_command_destroy(&command);
     402            0 :                 zend_string_release(project_root);
     403            0 :                 ZVAL_FALSE(&cache_value);
     404            0 :                 zend_hash_update(&server->type_cache, cache_key, &cache_value);
     405            0 :                 zend_string_release(cache_key);
     406              : 
     407            0 :                 return NULL;
     408              :         }
     409              : 
     410           24 :         analysis_file = lsp_phpstan_type_shadow_file(project_root, document, expression, offset, &dump_line);
     411           24 :         lsp_command_add(&command, "analyse");
     412           24 :         lsp_command_add_zstr(&command, analysis_file);
     413           24 :         lsp_command_add(&command, "--error-format=json");
     414           24 :         lsp_command_add(&command, "--no-progress");
     415           24 :         config = lsp_phpstan_config_file(project_root);
     416           24 :         generated_config = lsp_phpstan_lsp_config_file(project_root, config, lsp_analyzer_parallel_workers(server));
     417           24 :         lsp_analyzer_add_memory_limit(&command, server);
     418           24 :         lsp_phpstan_add_configured_level(&command, config, lsp_project_phpstan_level(server, project_root));
     419              : 
     420           24 :         if (generated_config) {
     421           24 :                 lsp_command_add(&command, "-c");
     422           24 :                 lsp_command_add_zstr(&command, generated_config);
     423           24 :                 zend_string_release(generated_config);
     424            0 :         } else if (config) {
     425            0 :                 lsp_command_add(&command, "-c");
     426            0 :                 lsp_command_add_zstr(&command, config);
     427              :         }
     428              : 
     429           24 :         if (config) {
     430            0 :                 zend_string_release(config);
     431              :         }
     432              : 
     433           24 :         array_init(&descriptor);
     434           24 :         add_assoc_string(&descriptor, "analyzer", "phpstan");
     435           24 :         add_assoc_str(&descriptor, "cacheKey", zend_string_copy(cache_key));
     436           24 :         add_assoc_str(&descriptor, "analysisFile", zend_string_copy(analysis_file));
     437           24 :         add_assoc_long(&descriptor, "markerLine", dump_line);
     438           24 :         add_assoc_str(&descriptor, "uri", zend_string_copy(document->uri));
     439              : 
     440           24 :         output = lsp_runner_run_capture_deferred(server, "phpstan", project_root, &command, project_root,
     441              :                 server->options.analyzer_type_query_timeout, server->options.analyzer_diagnostics_timeout, &descriptor, &deferred);
     442           24 :         lsp_command_destroy(&command);
     443           24 :         zend_string_release(project_root);
     444              : 
     445           24 :         if (deferred) {
     446              :                 /* The runner keeps working; mark the key as in flight (retry after
     447              :                  * the deadline) and let the pump fill the real value in. The shadow
     448              :                  * file must survive until the background analysis finishes. */
     449            4 :                 ZVAL_DOUBLE(&cache_value, lsp_now_seconds() + server->options.analyzer_diagnostics_timeout + 10.0);
     450            4 :                 zend_hash_update(&server->type_cache, cache_key, &cache_value);
     451            4 :                 zend_string_release(cache_key);
     452            4 :                 zend_string_release(analysis_file);
     453              : 
     454            4 :                 return NULL;
     455              :         }
     456              : 
     457           20 :         zval_ptr_dtor(&descriptor);
     458              : 
     459           20 :         type = lsp_phpstan_parse_type_output(output, analysis_file, dump_line);
     460           20 :         if (output && output != zend_empty_string) {
     461           20 :                 zend_string_release(output);
     462              :         }
     463              : 
     464              :         /* One shadow copy is written per probe; without the unlink they
     465              :          * accumulate in the project for the whole session. */
     466           20 :         VCWD_UNLINK(ZSTR_VAL(analysis_file));
     467           20 :         zend_string_release(analysis_file);
     468              : 
     469           20 :         if (lsp_type_is_unhelpful(type)) {
     470            0 :                 if (type) {
     471            0 :                         zend_string_release(type);
     472              :                 }
     473            0 :                 type = NULL;
     474              :         }
     475              : 
     476           20 :         if (type) {
     477           20 :                 ZVAL_STR_COPY(&cache_value, type);
     478              :         } else {
     479            0 :                 ZVAL_FALSE(&cache_value);
     480              :         }
     481           20 :         zend_hash_update(&server->type_cache, cache_key, &cache_value);
     482           20 :         zend_string_release(cache_key);
     483              : 
     484           20 :         return type;
     485              : }
     486              : 
     487          400 : extern void lsp_append_phpstan_cached_diagnostics(lsp_server *server, lsp_document *document, zval *diagnostics)
     488              : {
     489              :         zend_long line;
     490              :         zend_string *project_root, *output_file, *output, *json, *failure_message;
     491              :         zval decoded, *files, *file_result, *messages, *message_zv,
     492              :                 *line_zv, *message_text_zv, *identifier_zv, range
     493              :         ;
     494              : 
     495          400 :         if (!server->phpstan_enabled) {
     496          332 :                 return;
     497              :         }
     498              : 
     499          144 :         project_root = lsp_document_project_root(server, document);
     500          144 :         if (!lsp_document_is_in_analyzer_scope(document, project_root)) {
     501           40 :                 zend_string_release(project_root);
     502              : 
     503           40 :                 return;
     504              :         }
     505              : 
     506          104 :         output_file = lsp_analyzer_project_output_file(project_root, "phpstan");
     507          104 :         if (!lsp_is_regular_file(output_file)) {
     508           32 :                 zend_string_release(output_file);
     509           32 :                 zend_string_release(project_root);
     510              : 
     511           32 :                 return;
     512              :         }
     513              : 
     514           72 :         output = lsp_read_file(output_file);
     515           72 :         zend_string_release(output_file);
     516           72 :         zend_string_release(project_root);
     517           72 :         if (output == zend_empty_string) {
     518            0 :                 return;
     519              :         }
     520              : 
     521           72 :         json = lsp_json_slice_from(output, '{');
     522              : 
     523           72 :         ZVAL_UNDEF(&decoded);
     524           72 :         php_json_decode_ex(&decoded, ZSTR_VAL(json), ZSTR_LEN(json), PHP_JSON_OBJECT_AS_ARRAY, 512);
     525           72 :         zend_string_release(json);
     526           72 :         if (Z_TYPE(decoded) != IS_ARRAY) {
     527            4 :                 if (!Z_ISUNDEF(decoded)) {
     528            4 :                         zval_ptr_dtor(&decoded);
     529              :                 }
     530              : 
     531            4 :                 failure_message = lsp_analyzer_failure_message("phpstan", output);
     532            4 :                 lsp_line_range(&range, document->text, 1);
     533            4 :                 lsp_add_analyzer_diagnostic(diagnostics, "phpstan", failure_message, NULL, &range, 1);
     534            4 :                 zend_string_release(failure_message);
     535            4 :                 zend_string_release(output);
     536              : 
     537            4 :                 return;
     538              :         }
     539           68 :         zend_string_release(output);
     540              : 
     541           68 :         files = zend_hash_str_find(Z_ARRVAL(decoded), "files", sizeof("files") - 1);
     542           68 :         file_result = files && Z_TYPE_P(files) == IS_ARRAY ? zend_hash_find(Z_ARRVAL_P(files), document->path) : NULL;
     543              : 
     544           68 :         messages = file_result && Z_TYPE_P(file_result) == IS_ARRAY ? zend_hash_str_find(Z_ARRVAL_P(file_result), "messages", sizeof("messages") - 1) : NULL;
     545           68 :         if (messages && Z_TYPE_P(messages) == IS_ARRAY) {
     546           40 :                 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(messages), message_zv) {
     547           20 :                         if (Z_TYPE_P(message_zv) != IS_ARRAY) {
     548            0 :                                 continue;
     549              :                         }
     550              : 
     551           20 :                         line_zv = zend_hash_str_find(Z_ARRVAL_P(message_zv), "line", sizeof("line") - 1);
     552           20 :                         message_text_zv = zend_hash_str_find(Z_ARRVAL_P(message_zv), "message", sizeof("message") - 1);
     553           20 :                         identifier_zv = zend_hash_str_find(Z_ARRVAL_P(message_zv), "identifier", sizeof("identifier") - 1);
     554              : 
     555           20 :                         if (!message_text_zv || Z_TYPE_P(message_text_zv) != IS_STRING) {
     556            0 :                                 continue;
     557              :                         }
     558              : 
     559           20 :                         line = line_zv && Z_TYPE_P(line_zv) == IS_LONG ? Z_LVAL_P(line_zv) : 1;
     560           20 :                         lsp_line_range(&range, document->text, line);
     561              :                         /* PHPStan reports genuine rule violations without a severity
     562              :                          * axis; PhpStorm and the official VSCode integrations surface
     563              :                          * them as errors, so do the same. */
     564           40 :                         lsp_add_analyzer_diagnostic(diagnostics, "phpstan", Z_STR_P(message_text_zv),
     565           20 :                                 identifier_zv && Z_TYPE_P(identifier_zv) == IS_STRING ? Z_STR_P(identifier_zv) : NULL, &range, 1
     566              :                         );
     567              :                 } ZEND_HASH_FOREACH_END();
     568              :         }
     569              : 
     570           68 :         zval_ptr_dtor(&decoded);
     571              : }
     572              : 
     573            0 : extern void lsp_reschedule_phpstan_project_analyzer(lsp_server *server, zend_string *project_root)
     574              : {
     575              :         zend_long state;
     576              : 
     577            0 :         if (!server->phpstan_enabled || !lsp_tool_available("phpstan", project_root)) {
     578            0 :                 return;
     579              :         }
     580              : 
     581            0 :         if (!lsp_analyzer_project_has_scope(project_root)) {
     582            0 :                 return;
     583              :         }
     584              : 
     585            0 :         state = lsp_analyzer_project_state_value(server, "phpstan", project_root);
     586            0 :         if (state == LSP_ANALYZER_PROJECT_RUNNING) {
     587            0 :                 lsp_analyzer_project_state(server, "phpstan", project_root, LSP_ANALYZER_PROJECT_PENDING);
     588              : 
     589            0 :                 return;
     590              :         }
     591              : 
     592            0 :         zend_hash_del(&server->phpstan_projects, project_root);
     593            0 :         lsp_schedule_phpstan_project_analyzer(server, project_root);
     594              : }
        

Generated by: LCOV version 2.0-1