LCOV - code coverage report
Current view: top level - ext - lsp_server_analyzers.c (source / functions) Coverage Total Hit
Test: Extension code coverage Lines: 88.4 % 638 564
Test Date: 2026-07-13 00:58:38 Functions: 98.4 % 61 60

            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            4 : extern void lsp_publish_empty_diagnostics(zend_string *uri)
      17              : {
      18              :         zval params, diagnostics;
      19              : 
      20            4 :         array_init(&params);
      21            4 :         add_assoc_str(&params, "uri", zend_string_copy(uri));
      22            4 :         array_init(&diagnostics);
      23            4 :         add_assoc_zval(&params, "diagnostics", &diagnostics);
      24            4 :         lsp_protocol_notify("textDocument/publishDiagnostics", &params);
      25            4 :         zval_ptr_dtor(&params);
      26            4 : }
      27              : 
      28       200182 : extern zend_string *lsp_join_path2(zend_string *base, const char *suffix)
      29              : {
      30              :         bool slash;
      31              : 
      32       200182 :         slash = lsp_path_has_trailing_separator(base);
      33       200182 :         return strpprintf(0, "%s%s%s", ZSTR_VAL(base), slash ? "" : "/", suffix);
      34              : }
      35              : 
      36         2764 : static inline zend_string *lsp_path_join_zstr(zend_string *base, zend_string *suffix)
      37              : {
      38              :         bool slash;
      39              : 
      40         2764 :         slash = lsp_path_has_trailing_separator(base);
      41         2764 :         return strpprintf(0, "%s%s%s", ZSTR_VAL(base), slash ? "" : "/", ZSTR_VAL(suffix));
      42              : }
      43              : 
      44          144 : static inline zend_string *lsp_resolve_path(zend_string *root, zend_string *path)
      45              : {
      46          144 :         if (lsp_path_is_absolute(path)) {
      47            0 :                 return zend_string_copy(path);
      48              :         }
      49              : 
      50          144 :         return lsp_path_join_zstr(root, path);
      51              : }
      52              : 
      53          380 : static inline bool lsp_is_php_script(zend_string *path)
      54              : {
      55          380 :         FILE *file = fopen(ZSTR_VAL(path), "rb");
      56              :         char buffer[4096];
      57              :         size_t n;
      58              :         bool result;
      59              : 
      60          380 :         if (!file) {
      61            4 :                 return false;
      62              :         }
      63              : 
      64          376 :         n = fread(buffer, 1, sizeof(buffer) - 1, file);
      65          376 :         fclose(file);
      66          376 :         buffer[n] = '\0';
      67          376 :         result = strstr(buffer, "<?php") != NULL;
      68              : 
      69          376 :         return result;
      70              : }
      71              : 
      72              : /* composer.json is consulted constantly (vendor-dir resolution walks path
      73              :  * ancestors, analysis-path checks run per request), so decoded documents are
      74              :  * memoized per project root and validated against the file's stat signature
      75              :  * instead of being re-read and re-parsed on every call. */
      76              : static HashTable lsp_composer_cache;
      77              : static bool lsp_composer_cache_live = false;
      78              : 
      79          548 : extern void lsp_composer_cache_clear(void)
      80              : {
      81          548 :         if (lsp_composer_cache_live) {
      82          284 :                 zend_hash_destroy(&lsp_composer_cache);
      83          284 :                 lsp_composer_cache_live = false;
      84              :         }
      85          548 : }
      86              : 
      87        10962 : static inline bool lsp_composer_cache_entry_is_fresh(zval *entry, zend_stat_t *st)
      88              : {
      89              :         zval *mtime, *nsec, *size;
      90              : 
      91        10962 :         if (!entry || Z_TYPE_P(entry) != IS_ARRAY) {
      92            0 :                 return false;
      93              :         }
      94              : 
      95        10962 :         mtime = zend_hash_str_find(Z_ARRVAL_P(entry), "mtime", sizeof("mtime") - 1);
      96        10962 :         nsec = zend_hash_str_find(Z_ARRVAL_P(entry), "mtimeNsec", sizeof("mtimeNsec") - 1);
      97        10962 :         size = zend_hash_str_find(Z_ARRVAL_P(entry), "size", sizeof("size") - 1);
      98              : 
      99        10962 :         return mtime && Z_TYPE_P(mtime) == IS_LONG && Z_LVAL_P(mtime) == (zend_long) st->st_mtime &&
     100        10962 :                 nsec && Z_TYPE_P(nsec) == IS_LONG && Z_LVAL_P(nsec) == lsp_stat_mtime_nsec(st) &&
     101        21924 :                 size && Z_TYPE_P(size) == IS_LONG && Z_LVAL_P(size) == (zend_long) st->st_size
     102              :         ;
     103              : }
     104              : 
     105              : /* Returns the decoded composer.json for the project root, or NULL when the
     106              :  * file is missing or invalid. The returned zval is BORROWED from the cache:
     107              :  * treat it as read-only and do not release it. */
     108        14526 : extern zval *lsp_composer_json_decoded(zend_string *project_root)
     109              : {
     110              :         zend_string *composer_json, *contents;
     111              :         zend_stat_t st;
     112              :         zval *entry, *data, new_entry, decoded;
     113              : 
     114        14526 :         composer_json = lsp_join_path2(project_root, "composer.json");
     115        14526 :         if (VCWD_STAT(ZSTR_VAL(composer_json), &st) != 0 || !S_ISREG(st.st_mode)) {
     116         3260 :                 if (lsp_composer_cache_live) {
     117          108 :                         zend_hash_del(&lsp_composer_cache, composer_json);
     118              :                 }
     119         3260 :                 zend_string_release(composer_json);
     120              : 
     121         3260 :                 return NULL;
     122              :         }
     123              : 
     124        11266 :         if (!lsp_composer_cache_live) {
     125          284 :                 zend_hash_init(&lsp_composer_cache, 8, NULL, ZVAL_PTR_DTOR, 0);
     126          284 :                 lsp_composer_cache_live = true;
     127              :         }
     128              : 
     129        11266 :         entry = zend_hash_find(&lsp_composer_cache, composer_json);
     130        11266 :         if (entry && lsp_composer_cache_entry_is_fresh(entry, &st)) {
     131        10962 :                 zend_string_release(composer_json);
     132        10962 :                 data = zend_hash_str_find(Z_ARRVAL_P(entry), "data", sizeof("data") - 1);
     133              : 
     134        10962 :                 return data && Z_TYPE_P(data) == IS_ARRAY ? data : NULL;
     135              :         }
     136              : 
     137          304 :         contents = lsp_read_file(composer_json);
     138          304 :         ZVAL_UNDEF(&decoded);
     139          304 :         if (contents != zend_empty_string) {
     140          304 :                 php_json_decode_ex(&decoded, ZSTR_VAL(contents), ZSTR_LEN(contents), PHP_JSON_OBJECT_AS_ARRAY, 512);
     141          304 :                 zend_string_release(contents);
     142              :         }
     143              : 
     144          304 :         array_init(&new_entry);
     145          304 :         add_assoc_long(&new_entry, "mtime", (zend_long) st.st_mtime);
     146          304 :         add_assoc_long(&new_entry, "mtimeNsec", lsp_stat_mtime_nsec(&st));
     147          304 :         add_assoc_long(&new_entry, "size", (zend_long) st.st_size);
     148          304 :         if (Z_TYPE(decoded) == IS_ARRAY) {
     149          304 :                 add_assoc_zval(&new_entry, "data", &decoded);
     150              :         } else {
     151              :                 /* Invalid or non-object composer.json: cache the miss so repeated
     152              :                  * lookups stay cheap until the file changes. */
     153            0 :                 add_assoc_bool(&new_entry, "data", false);
     154            0 :                 if (!Z_ISUNDEF(decoded)) {
     155            0 :                         zval_ptr_dtor(&decoded);
     156              :                 }
     157              :         }
     158              : 
     159          304 :         entry = zend_hash_update(&lsp_composer_cache, composer_json, &new_entry);
     160          304 :         zend_string_release(composer_json);
     161          304 :         data = zend_hash_str_find(Z_ARRVAL_P(entry), "data", sizeof("data") - 1);
     162              : 
     163          304 :         return data && Z_TYPE_P(data) == IS_ARRAY ? data : NULL;
     164              : }
     165              : 
     166        11650 : extern zend_string *lsp_composer_config_string(zend_string *root, const char *key)
     167              : {
     168        11650 :         zend_string *result = NULL;
     169              :         zval *decoded, *config, *value;
     170              : 
     171        11650 :         decoded = lsp_composer_json_decoded(root);
     172        11650 :         if (!decoded) {
     173         2324 :                 return NULL;
     174              :         }
     175              : 
     176         9326 :         config = zend_hash_str_find(Z_ARRVAL_P(decoded), "config", sizeof("config") - 1);
     177         9326 :         value = config && Z_TYPE_P(config) == IS_ARRAY ? zend_hash_str_find(Z_ARRVAL_P(config), key, strlen(key)) : NULL;
     178         9326 :         if (value && Z_TYPE_P(value) == IS_STRING && Z_STRLEN_P(value) > 0) {
     179          144 :                 result = zend_string_copy(Z_STR_P(value));
     180              :         }
     181              : 
     182         9326 :         return result;
     183              : }
     184              : 
     185         9030 : extern zend_string *lsp_composer_vendor_dir(zend_string *project_root)
     186              : {
     187              :         zend_string *configured, *resolved;
     188              : 
     189         9030 :         configured = lsp_composer_config_string(project_root, "vendor-dir");
     190         9030 :         if (!configured) {
     191         8946 :                 return lsp_join_path2(project_root, "vendor");
     192              :         }
     193              : 
     194           84 :         resolved = lsp_resolve_path(project_root, configured);
     195           84 :         zend_string_release(configured);
     196              : 
     197           84 :         return resolved;
     198              : }
     199              : 
     200         5226 : extern bool lsp_path_is_under_composer_vendor_dir(zend_string *path, zend_string *project_root)
     201              : {
     202              :         zend_string *vendor_dir;
     203              :         bool result;
     204              : 
     205         5226 :         vendor_dir = lsp_composer_vendor_dir(project_root);
     206         5226 :         result = lsp_path_is_same_or_under(path, vendor_dir);
     207         5226 :         zend_string_release(vendor_dir);
     208              : 
     209         5226 :         return result;
     210              : }
     211              : 
     212         6638 : extern bool lsp_path_is_in_workspace_composer_vendor(zend_string *workspace_root, zend_string *path)
     213              : {
     214              :         zend_string *dir, *composer;
     215              :         size_t workspace_root_length;
     216              :         char *buffer, *slash;
     217         6638 :         bool result = false;
     218              : 
     219         6638 :         if (!lsp_path_is_same_or_under(path, workspace_root)) {
     220           20 :                 return false;
     221              :         }
     222              : 
     223         6618 :         workspace_root_length = ZSTR_LEN(workspace_root);
     224         6618 :         buffer = estrndup(ZSTR_VAL(path), ZSTR_LEN(path));
     225        21368 :         while (strlen(buffer) >= workspace_root_length) {
     226        21368 :                 dir = zend_string_init(buffer, strlen(buffer), 0);
     227        21368 :                 composer = lsp_join_path2(dir, "composer.json");
     228              : 
     229        36118 :                 if (!zend_string_equals(dir, path) &&
     230        19964 :                         lsp_is_regular_file(composer) &&
     231         5214 :                         lsp_path_is_under_composer_vendor_dir(path, dir)
     232              :                 ) {
     233           20 :                         result = true;
     234           20 :                         zend_string_release(composer);
     235           20 :                         zend_string_release(dir);
     236           20 :                         break;
     237              :                 }
     238              : 
     239        21348 :                 zend_string_release(composer);
     240        21348 :                 zend_string_release(dir);
     241              : 
     242        21348 :                 if (strlen(buffer) == workspace_root_length && lsp_path_compare(buffer, ZSTR_VAL(workspace_root), workspace_root_length) == 0) {
     243         6598 :                         break;
     244              :                 }
     245              : 
     246        14750 :                 slash = lsp_last_path_separator(buffer);
     247        14750 :                 if (!slash || slash == buffer) {
     248              :                         break;
     249              :                 }
     250              : 
     251        14750 :                 *slash = '\0';
     252              :         }
     253         6618 :         efree(buffer);
     254              : 
     255         6618 :         return result;
     256              : }
     257              : 
     258         2620 : extern zend_string *lsp_tool_project_candidate(zend_string *root, const char *name)
     259              : {
     260         2620 :         zend_string *bin_dir, *vendor_dir = NULL, *vendor_bin, *name_string, *candidate, *resolved;
     261              : 
     262         2620 :         bin_dir = lsp_composer_config_string(root, "bin-dir");
     263         2620 :         name_string = zend_string_init(name, strlen(name), 0);
     264              : 
     265         2620 :         if (bin_dir) {
     266           60 :                 resolved = lsp_resolve_path(root, bin_dir);
     267           60 :                 candidate = lsp_path_join_zstr(resolved, name_string);
     268           60 :                 zend_string_release(resolved);
     269           60 :                 zend_string_release(bin_dir);
     270           60 :                 zend_string_release(name_string);
     271              : 
     272           60 :                 if (lsp_is_regular_file(candidate)) {
     273           36 :                         return candidate;
     274              :                 }
     275              : 
     276           24 :                 zend_string_release(candidate);
     277              : 
     278           24 :                 return NULL;
     279              :         }
     280              : 
     281         2560 :         vendor_dir = lsp_composer_vendor_dir(root);
     282         2560 :         vendor_bin = lsp_join_path2(vendor_dir, "bin");
     283         2560 :         zend_string_release(vendor_dir);
     284         2560 :         candidate = lsp_path_join_zstr(vendor_bin, name_string);
     285         2560 :         zend_string_release(vendor_bin);
     286         2560 :         zend_string_release(name_string);
     287              : 
     288         2560 :         if (lsp_is_regular_file(candidate)) {
     289          420 :                 return candidate;
     290              :         }
     291              : 
     292         2140 :         zend_string_release(candidate);
     293              : 
     294         2140 :         return NULL;
     295              : }
     296              : 
     297         5500 : static inline bool lsp_tool_path_candidate_is_usable(zend_string *candidate)
     298              : {
     299         5500 :         return !lsp_path_is_windows_batch_file(candidate) && lsp_path_is_executable_file(candidate);
     300              : }
     301              : 
     302         5500 : static inline zend_string *lsp_tool_path_candidate_in_dir(const char *dir, size_t dir_length, const char *name)
     303              : {
     304              :         zend_string *candidate;
     305              :         const char *extension;
     306              :         size_t name_length, extension_length;
     307              :         uint32_t i;
     308              : 
     309         5500 :         name_length = strlen(name);
     310        11000 :         for (i = 0; i < lsp_executable_extension_count(); i++) {
     311         5500 :                 extension = lsp_executable_extension(i);
     312         5500 :                 extension_length = strlen(extension);
     313         5500 :                 candidate = zend_string_alloc(dir_length + 1 + name_length + extension_length, 0);
     314         5500 :                 memcpy(ZSTR_VAL(candidate), dir, dir_length);
     315         5500 :                 ZSTR_VAL(candidate)[dir_length] = '/';
     316         5500 :                 memcpy(ZSTR_VAL(candidate) + dir_length + 1, name, name_length);
     317         5500 :                 memcpy(ZSTR_VAL(candidate) + dir_length + 1 + name_length, extension, extension_length);
     318         5500 :                 ZSTR_VAL(candidate)[ZSTR_LEN(candidate)] = '\0';
     319              : 
     320         5500 :                 if (lsp_tool_path_candidate_is_usable(candidate)) {
     321            0 :                         return candidate;
     322              :                 }
     323              : 
     324         5500 :                 zend_string_release(candidate);
     325              :         }
     326              : 
     327         5500 :         return NULL;
     328              : }
     329              : 
     330          936 : static inline zend_string *lsp_tool_path_candidate(const char *name)
     331              : {
     332          936 :         const char *path = getenv("PATH"), *cursor, *end, *sep;
     333              :         zend_string *candidate;
     334              :         size_t dir_length;
     335              :         char path_separator;
     336              : 
     337          936 :         if (!path || *path == '\0') {
     338            0 :                 return NULL;
     339              :         }
     340              : 
     341          936 :         path_separator = lsp_path_list_separator();
     342              : 
     343          936 :         cursor = path;
     344         6436 :         while (*cursor) {
     345         5500 :                 sep = strchr(cursor, path_separator);
     346         5500 :                 if (sep) {
     347         4564 :                         dir_length = sep - cursor;
     348         4564 :                         end = sep;
     349              :                 } else {
     350          936 :                         dir_length = strlen(cursor);
     351          936 :                         end = cursor + dir_length;
     352              :                 }
     353              : 
     354         5500 :                 if (dir_length > 0) {
     355         5500 :                         candidate = lsp_tool_path_candidate_in_dir(cursor, dir_length, name);
     356         5500 :                         if (candidate) {
     357            0 :                                 return candidate;
     358              :                         }
     359              :                 }
     360              : 
     361         5500 :                 cursor = *end == path_separator ? end + 1 : end;
     362              :         }
     363              : 
     364          936 :         return NULL;
     365              : }
     366              : 
     367         1316 : static inline zend_string *lsp_find_tool(const char *name, zend_string *root, bool *run_with_php)
     368              : {
     369         1316 :         zend_string *path = lsp_tool_project_candidate(root, name);
     370              : 
     371         1316 :         if (!path) {
     372          936 :                 path = lsp_tool_path_candidate(name);
     373              :         }
     374              : 
     375         1316 :         if (!path) {
     376          936 :                 return NULL;
     377              :         }
     378              : 
     379          380 :         *run_with_php = lsp_is_php_script(path);
     380              : 
     381          380 :         return path;
     382              : }
     383              : 
     384         1192 : extern bool lsp_tool_available(const char *name, zend_string *root)
     385              : {
     386         1192 :         bool run_with_php = false;
     387         1192 :         zend_string *path = lsp_find_tool(name, root, &run_with_php);
     388              : 
     389         1192 :         if (!path) {
     390          936 :                 return false;
     391              :         }
     392              : 
     393          256 :         zend_string_release(path);
     394              : 
     395          256 :         return true;
     396              : }
     397              : 
     398          124 : extern bool lsp_tool_command(lsp_server *server, zend_string *root, const char *name, lsp_command *command)
     399              : {
     400          124 :         bool run_with_php = false;
     401          124 :         zend_string *tool = lsp_find_tool(name, root, &run_with_php);
     402              :         uint32_t i;
     403              : 
     404          124 :         if (!tool) {
     405            0 :                 return false;
     406              :         }
     407              : 
     408          124 :         if (run_with_php) {
     409          124 :                 lsp_command_add(command, lsp_php_binary());
     410              : 
     411          128 :                 for (i = 0; i < server->options.worker_php_arg_count; i++) {
     412            4 :                         lsp_command_add_zstr(command, server->options.worker_php_args[i]);
     413              :                 }
     414              : 
     415          124 :                 lsp_command_add_zstr(command, tool);
     416              :         } else {
     417            0 :                 lsp_command_add_zstr(command, tool);
     418              :         }
     419              : 
     420          124 :         zend_string_release(tool);
     421              : 
     422          124 :         return true;
     423              : }
     424              : 
     425            0 : extern zend_string *lsp_run_command_capture(lsp_command *command, zend_string *cwd, double timeout)
     426              : {
     427            0 :         return lsp_process_run_capture(command, cwd, timeout);
     428              : }
     429              : 
     430       156166 : extern bool lsp_scan_should_skip_dir_name(const char *name)
     431              : {
     432       302576 :         return strcmp(name, ".") == 0 ||
     433       146410 :                 strcmp(name, "..") == 0 ||
     434       136658 :                 strcmp(name, ".git") == 0 ||
     435       136630 :                 strcmp(name, ".cache") == 0 ||
     436       136630 :                 strcmp(name, ".lsparrot") == 0 ||
     437       135884 :                 strcmp(name, "vendor") == 0 ||
     438       133704 :                 strcmp(name, "node_modules") == 0 ||
     439       133704 :                 strcmp(name, "var") == 0 ||
     440       133704 :                 strcmp(name, "tmp") == 0 ||
     441       436238 :                 strcmp(name, "build") == 0 ||
     442       133662 :                 strcmp(name, "dist") == 0
     443              :         ;
     444              : }
     445              : 
     446         1840 : static inline bool lsp_path_is_under_workspace_root(zend_string *path, zend_string *root)
     447              : {
     448         1840 :         return lsp_path_is_same_or_under(path, root);
     449              : }
     450              : 
     451          712 : static inline zend_string *lsp_vendor_owner_project_root(lsp_server *server, zend_string *candidate)
     452              : {
     453          712 :         zend_string *dir, *composer, *owner = NULL;
     454              :         size_t root_length;
     455              :         char *buffer, *slash;
     456              : 
     457          712 :         if (!lsp_path_is_under_workspace_root(candidate, server->root)) {
     458            0 :                 return NULL;
     459              :         }
     460              : 
     461          712 :         root_length = ZSTR_LEN(server->root);
     462          712 :         buffer = estrndup(ZSTR_VAL(candidate), ZSTR_LEN(candidate));
     463              : 
     464          712 :         slash = lsp_last_path_separator(buffer);
     465          712 :         if (!slash) {
     466            0 :                 efree(buffer);
     467            0 :                 return NULL;
     468              :         }
     469              : 
     470          712 :         if (slash == buffer) {
     471            0 :                 buffer[1] = '\0';
     472              :         } else {
     473          712 :                 *slash = '\0';
     474              :         }
     475              : 
     476          736 :         while (strlen(buffer) >= root_length) {
     477           44 :                 dir = zend_string_init(buffer, strlen(buffer), 0);
     478           44 :                 composer = lsp_join_path2(dir, "composer.json");
     479              : 
     480           88 :                 if (!zend_string_equals(dir, candidate) &&
     481           56 :                         lsp_is_regular_file(composer) &&
     482           12 :                         lsp_path_is_under_composer_vendor_dir(candidate, dir)
     483              :                 ) {
     484           12 :                         owner = dir;
     485           12 :                         zend_string_release(composer);
     486           12 :                         break;
     487              :                 }
     488              : 
     489           32 :                 zend_string_release(composer);
     490           32 :                 zend_string_release(dir);
     491              : 
     492           32 :                 if (strlen(buffer) == root_length && lsp_path_compare(buffer, ZSTR_VAL(server->root), root_length) == 0) {
     493            8 :                         break;
     494              :                 }
     495              : 
     496           24 :                 slash = lsp_last_path_separator(buffer);
     497           24 :                 if (!slash || slash == buffer) {
     498              :                         break;
     499              :                 }
     500              : 
     501           24 :                 *slash = '\0';
     502              :         }
     503              : 
     504          712 :         efree(buffer);
     505              : 
     506          712 :         return owner;
     507              : }
     508              : 
     509         1128 : extern zend_string *lsp_document_project_root(lsp_server *server, lsp_document *document)
     510              : {
     511              :         zend_string *dir, *composer, *owner;
     512         1128 :         size_t root_length = ZSTR_LEN(server->root);
     513              :         char *buffer, *slash;
     514              : 
     515         1128 :         if (!lsp_path_is_under_workspace_root(document->path, server->root)) {
     516            8 :                 return zend_string_copy(server->root);
     517              :         }
     518              : 
     519         1120 :         buffer = estrndup(ZSTR_VAL(document->path), ZSTR_LEN(document->path));
     520         1120 :         slash = lsp_last_path_separator(buffer);
     521         1120 :         if (!slash) {
     522            0 :                 efree(buffer);
     523            0 :                 return zend_string_copy(server->root);
     524              :         }
     525              : 
     526         1120 :         if (slash == buffer) {
     527            0 :                 buffer[1] = '\0';
     528              :         } else {
     529         1120 :                 *slash = '\0';
     530              :         }
     531              : 
     532         2264 :         while (strlen(buffer) >= root_length) {
     533         2264 :                 dir = zend_string_init(buffer, strlen(buffer), 0);
     534         2264 :                 composer = lsp_join_path2(dir, "composer.json");
     535              : 
     536         2264 :                 if (lsp_is_regular_file(composer)) {
     537          712 :                         owner = lsp_vendor_owner_project_root(server, dir);
     538          712 :                         zend_string_release(composer);
     539          712 :                         efree(buffer);
     540              : 
     541          712 :                         if (owner) {
     542           12 :                                 zend_string_release(dir);
     543           12 :                                 return owner;
     544              :                         }
     545              : 
     546          700 :                         return dir;
     547              :                 }
     548              : 
     549         1552 :                 zend_string_release(composer);
     550         1552 :                 zend_string_release(dir);
     551              : 
     552         1552 :                 if (strlen(buffer) == root_length && lsp_path_compare(buffer, ZSTR_VAL(server->root), root_length) == 0) {
     553          408 :                         break;
     554              :                 }
     555              : 
     556         1144 :                 slash = lsp_last_path_separator(buffer);
     557         1144 :                 if (!slash || slash == buffer) {
     558              :                         break;
     559              :                 }
     560              : 
     561         1144 :                 *slash = '\0';
     562              :         }
     563              : 
     564          408 :         efree(buffer);
     565              : 
     566          408 :         return zend_string_copy(server->root);
     567              : }
     568              : 
     569              : /* Like lsp_line_range but honoring 1-based column bounds when the analyzer
     570              :  * provides them (Psalm's column_from/column_to); PhpStorm-style precise
     571              :  * squiggles instead of whole-line noise. Invalid columns fall back to the
     572              :  * full line. */
     573            4 : extern void lsp_line_range_columns(zval *range, zend_string *text, zend_long one_based_line, zend_long column_from, zend_long column_to)
     574              : {
     575              :         const char *next;
     576            4 :         zend_long line = one_based_line > 0 ? one_based_line - 1 : 0, current;
     577              :         zval start, end;
     578            4 :         size_t offset = 0, line_start, line_end, from_offset, to_offset, length = ZSTR_LEN(text);
     579              : 
     580            4 :         for (current = 0; current < line; current++) {
     581            0 :                 next = memchr(ZSTR_VAL(text) + offset, '\n', length - offset);
     582            0 :                 if (!next) {
     583            0 :                         offset = length;
     584            0 :                         break;
     585              :                 }
     586              : 
     587            0 :                 offset = (size_t) (next - ZSTR_VAL(text)) + 1;
     588              :         }
     589              : 
     590            4 :         line_start = offset;
     591           24 :         while (offset < length && ZSTR_VAL(text)[offset] != '\n' && ZSTR_VAL(text)[offset] != '\r') {
     592           20 :                 offset++;
     593              :         }
     594            4 :         line_end = offset;
     595              : 
     596            4 :         if (column_from < 1 || column_to < column_from) {
     597            4 :                 lsp_line_range(range, text, one_based_line);
     598              : 
     599            4 :                 return;
     600              :         }
     601              : 
     602            0 :         from_offset = line_start + (size_t) (column_from - 1);
     603            0 :         to_offset = line_start + (size_t) (column_to - 1);
     604            0 :         if (from_offset > line_end) {
     605            0 :                 from_offset = line_end;
     606              :         }
     607            0 :         if (to_offset > line_end) {
     608            0 :                 to_offset = line_end;
     609              :         }
     610            0 :         if (to_offset <= from_offset) {
     611            0 :                 lsp_line_range(range, text, one_based_line);
     612              : 
     613            0 :                 return;
     614              :         }
     615              : 
     616            0 :         array_init(range);
     617            0 :         array_init(&start);
     618            0 :         add_assoc_long(&start, "line", line);
     619            0 :         add_assoc_long(&start, "character", (zend_long) lsp_byte_offset_to_utf16_units(ZSTR_VAL(text), line_start, from_offset));
     620            0 :         array_init(&end);
     621            0 :         add_assoc_long(&end, "line", line);
     622            0 :         add_assoc_long(&end, "character", (zend_long) lsp_byte_offset_to_utf16_units(ZSTR_VAL(text), line_start, to_offset));
     623            0 :         add_assoc_zval(range, "start", &start);
     624            0 :         add_assoc_zval(range, "end", &end);
     625              : }
     626              : 
     627           32 : extern void lsp_line_range(zval *range, zend_string *text, zend_long one_based_line)
     628              : {
     629              :         const char *next;
     630           32 :         zend_long line = one_based_line > 0 ? one_based_line - 1 : 0, current;
     631              :         zval start, end;
     632           32 :         size_t offset = 0, start_offset, end_offset, length = ZSTR_LEN(text);
     633              : 
     634           72 :         for (current = 0; current < line; current++) {
     635           40 :                 next = memchr(ZSTR_VAL(text) + offset, '\n', length - offset);
     636           40 :                 if (!next) {
     637            0 :                         offset = length;
     638            0 :                         break;
     639              :                 }
     640              : 
     641           40 :                 offset = (size_t) (next - ZSTR_VAL(text)) + 1;
     642              :         }
     643              : 
     644           32 :         start_offset = offset;
     645          304 :         while (offset < length && ZSTR_VAL(text)[offset] != '\n' && ZSTR_VAL(text)[offset] != '\r') {
     646          272 :                 offset++;
     647              :         }
     648           32 :         end_offset = offset;
     649              : 
     650           32 :         array_init(range);
     651           32 :         array_init(&start);
     652           32 :         add_assoc_long(&start, "line", line);
     653           32 :         add_assoc_long(&start, "character", 0);
     654           32 :         array_init(&end);
     655           32 :         add_assoc_long(&end, "line", line);
     656           64 :         add_assoc_long(&end, "character", end_offset > start_offset
     657           32 :                 ? lsp_byte_offset_to_utf16_units(ZSTR_VAL(text), start_offset, end_offset)
     658              :                 : 1);
     659           32 :         add_assoc_zval(range, "start", &start);
     660           32 :         add_assoc_zval(range, "end", &end);
     661           32 : }
     662              : 
     663           72 : static inline bool lsp_diagnostic_code_contains_ci(zend_string *code, const char *needle)
     664              : {
     665           72 :         size_t needle_length = strlen(needle), i;
     666              : 
     667           72 :         if (ZSTR_LEN(code) < needle_length) {
     668           16 :                 return false;
     669              :         }
     670              : 
     671          328 :         for (i = 0; i + needle_length <= ZSTR_LEN(code); i++) {
     672          272 :                 if (strncasecmp(ZSTR_VAL(code) + i, needle, needle_length) == 0) {
     673            0 :                         return true;
     674              :                 }
     675              :         }
     676              : 
     677           56 :         return false;
     678              : }
     679              : 
     680           32 : extern void lsp_add_analyzer_diagnostic(zval *diagnostics, const char *source, zend_string *message, zend_string *code, zval *range, zend_long severity)
     681              : {
     682              :         zval diagnostic, tags;
     683              : 
     684           32 :         array_init(&diagnostic);
     685           32 :         add_assoc_string(&diagnostic, "source", source);
     686           32 :         add_assoc_str(&diagnostic, "message", zend_string_copy(message));
     687              : 
     688           32 :         if (code && ZSTR_LEN(code) > 0) {
     689           24 :                 add_assoc_str(&diagnostic, "code", zend_string_copy(code));
     690              : 
     691              :                 /* Psalm issue types (DeprecatedMethod, UnusedVariable, ...) and
     692              :                  * PHPStan identifiers (method.deprecated, deadCode.unusedVariable)
     693              :                  * map onto LSP DiagnosticTags so editors can strike through or fade
     694              :                  * the offending code. */
     695           24 :                 if (lsp_diagnostic_code_contains_ci(code, "deprecated")) {
     696            0 :                         array_init(&tags);
     697            0 :                         add_next_index_long(&tags, 2);
     698            0 :                         add_assoc_zval(&diagnostic, "tags", &tags);
     699           24 :                 } else if (lsp_diagnostic_code_contains_ci(code, "unused") || lsp_diagnostic_code_contains_ci(code, "deadcode")) {
     700            0 :                         array_init(&tags);
     701            0 :                         add_next_index_long(&tags, 1);
     702            0 :                         add_assoc_zval(&diagnostic, "tags", &tags);
     703              :                 }
     704              :         }
     705              : 
     706           32 :         add_assoc_long(&diagnostic, "severity", severity);
     707           32 :         add_assoc_zval(&diagnostic, "range", range);
     708           32 :         add_next_index_zval(diagnostics, &diagnostic);
     709           32 : }
     710              : 
     711          204 : extern zend_string *lsp_json_slice_from(zend_string *output, char open_char)
     712              : {
     713              :         const char *value, *start, *p, *end;
     714              :         uint32_t curly_depth, square_depth;
     715              :         bool in_string, escaped;
     716              : 
     717          204 :         value = ZSTR_VAL(output);
     718          204 :         end = value + ZSTR_LEN(output);
     719          204 :         start = memchr(value, open_char, ZSTR_LEN(output));
     720          204 :         if (!start) {
     721           16 :                 return zend_string_copy(output);
     722              :         }
     723              : 
     724          188 :         curly_depth = 0;
     725          188 :         square_depth = 0;
     726          188 :         in_string = false;
     727          188 :         escaped = false;
     728        14352 :         for (p = start; p < end; p++) {
     729        14352 :                 if (in_string) {
     730        11196 :                         if (escaped) {
     731          212 :                                 escaped = false;
     732        10984 :                         } else if (*p == '\\') {
     733          212 :                                 escaped = true;
     734        10772 :                         } else if (*p == '"') {
     735          764 :                                 in_string = false;
     736              :                         }
     737        11196 :                         continue;
     738              :                 }
     739              : 
     740         3156 :                 if (*p == '"') {
     741          764 :                         in_string = true;
     742          764 :                         continue;
     743              :                 }
     744              : 
     745         2392 :                 if (*p == '{') {
     746          352 :                         curly_depth++;
     747         2040 :                 } else if (*p == '}' && curly_depth > 0) {
     748          352 :                         curly_depth--;
     749         1688 :                 } else if (*p == '[') {
     750          228 :                         square_depth++;
     751         1460 :                 } else if (*p == ']' && square_depth > 0) {
     752          228 :                         square_depth--;
     753              :                 }
     754              : 
     755         2392 :                 if (p > start && curly_depth == 0 && square_depth == 0) {
     756          188 :                         return zend_string_init(start, p - start + 1, 0);
     757              :                 }
     758              :         }
     759              : 
     760            0 :         return zend_string_init(start, end - start, 0);
     761              : }
     762              : 
     763           16 : static inline const char *lsp_analyzer_label(const char *analyzer)
     764              : {
     765           16 :         if (strcmp(analyzer, "phpstan") == 0) {
     766            8 :                 return "PHPStan";
     767              :         }
     768              : 
     769            8 :         return "Psalm";
     770              : }
     771              : 
     772           68 : static inline char lsp_analyzer_json_open_char(const char *analyzer)
     773              : {
     774           68 :         return strcmp(analyzer, "phpstan") == 0 ? '{' : '[';
     775              : }
     776              : 
     777           68 : static inline bool lsp_analyzer_output_is_json(const char *analyzer, zend_string *output)
     778              : {
     779              :         zend_string *json;
     780              :         zval decoded;
     781              :         bool ok;
     782              : 
     783           68 :         json = lsp_json_slice_from(output, lsp_analyzer_json_open_char(analyzer));
     784           68 :         ZVAL_UNDEF(&decoded);
     785           68 :         php_json_decode_ex(&decoded, ZSTR_VAL(json), ZSTR_LEN(json), PHP_JSON_OBJECT_AS_ARRAY, 512);
     786           68 :         zend_string_release(json);
     787           68 :         ok = Z_TYPE(decoded) == IS_ARRAY;
     788           68 :         if (!Z_ISUNDEF(decoded)) {
     789           68 :                 zval_ptr_dtor(&decoded);
     790              :         }
     791              : 
     792           68 :         return ok;
     793              : }
     794              : 
     795           16 : extern zend_string *lsp_analyzer_failure_message(const char *analyzer, zend_string *output)
     796              : {
     797              :         const char *value, *end, *line_start, *line_end, *marker, *label;
     798              : 
     799           16 :         value = ZSTR_VAL(output);
     800           16 :         end = value + ZSTR_LEN(output);
     801           16 :         label = lsp_analyzer_label(analyzer);
     802           16 :         marker = strstr(value, "Error:");
     803              : 
     804           16 :         if (marker && marker < end) {
     805           16 :                 line_start = marker;
     806              :         } else {
     807            0 :                 line_start = value;
     808            0 :                 while (line_start < end && isspace((unsigned char) *line_start)) {
     809            0 :                         line_start++;
     810              :                 }
     811              :         }
     812              : 
     813           16 :         line_end = line_start;
     814          496 :         while (line_end < end && *line_end != '\r' && *line_end != '\n') {
     815          480 :                 line_end++;
     816              :         }
     817              : 
     818           16 :         while (line_end > line_start && isspace((unsigned char) line_end[-1])) {
     819            0 :                 line_end--;
     820              :         }
     821              : 
     822           16 :         if (line_end <= line_start) {
     823            0 :                 return strpprintf(0, "%s diagnostics failed.", label);
     824              :         }
     825              : 
     826           16 :         return strpprintf(0, "%s diagnostics failed: %.*s", label, (int) (line_end - line_start), line_start);
     827              : }
     828              : 
     829              : 
     830          832 : extern zend_long lsp_analyzer_parallel_workers(lsp_server *server)
     831              : {
     832          832 :         long detected = 1;
     833              :         zend_long configured;
     834              : 
     835          832 :         configured = server->options.worker_count;
     836          832 :         if (configured > 0) {
     837          112 :                 return configured;
     838              :         }
     839              : 
     840              : #ifdef _SC_NPROCESSORS_ONLN
     841          720 :         detected = sysconf(_SC_NPROCESSORS_ONLN);
     842              : #endif
     843          720 :         if (detected < 1) {
     844            0 :                 detected = 1;
     845              :         }
     846              : 
     847          720 :         return (zend_long) detected;
     848              : }
     849              : 
     850          124 : extern void lsp_analyzer_add_memory_limit(lsp_command *command, lsp_server *server)
     851              : {
     852              :         zend_string *argument;
     853              : 
     854          124 :         if (!server->options.memory_limit || ZSTR_LEN(server->options.memory_limit) == 0) {
     855            0 :                 return;
     856              :         }
     857              : 
     858          124 :         argument = strpprintf(0, "--memory-limit=%s", ZSTR_VAL(server->options.memory_limit));
     859          124 :         lsp_command_add_zstr(command, argument);
     860              : 
     861          124 :         zend_string_release(argument);
     862              : }
     863              : 
     864              : 
     865          676 : static inline HashTable *lsp_analyzer_project_table(lsp_server *server, const char *analyzer)
     866              : {
     867          676 :         if (strcmp(analyzer, "phpstan") == 0) {
     868          392 :                 return &server->phpstan_projects;
     869              :         }
     870              : 
     871          284 :         if (strcmp(analyzer, "psalm-ls") == 0) {
     872           84 :                 return &server->psalm_ls_project_states;
     873              :         }
     874              : 
     875          200 :         return &server->psalm_projects;
     876              : }
     877              : 
     878           96 : extern HashTable *lsp_analyzer_job_table(lsp_server *server, const char *analyzer)
     879              : {
     880           96 :         return strcmp(analyzer, "phpstan") == 0 ? &server->phpstan_jobs : &server->psalm_jobs;
     881              : }
     882              : 
     883           96 : extern void lsp_analyzer_job_entry_destroy(zval *value)
     884              : {
     885              :         lsp_analyzer_job *job;
     886              : 
     887           96 :         if (Z_TYPE_P(value) != IS_PTR) {
     888            0 :                 return;
     889              :         }
     890              : 
     891           96 :         job = (lsp_analyzer_job *) Z_PTR_P(value);
     892           96 :         lsp_analyzer_job_destroy(job);
     893           96 :         efree(job);
     894              : }
     895              : 
     896           96 : extern lsp_analyzer_job *lsp_analyzer_job_slot(lsp_server *server, const char *analyzer, zend_string *project_root)
     897              : {
     898           96 :         HashTable *jobs = lsp_analyzer_job_table(server, analyzer);
     899              :         lsp_analyzer_job *job;
     900              :         zval *existing, value;
     901              : 
     902           96 :         existing = zend_hash_find(jobs, project_root);
     903           96 :         if (existing && Z_TYPE_P(existing) == IS_PTR) {
     904            0 :                 return (lsp_analyzer_job *) Z_PTR_P(existing);
     905              :         }
     906              : 
     907           96 :         job = ecalloc(1, sizeof(*job));
     908           96 :         ZVAL_PTR(&value, job);
     909           96 :         zend_hash_update(jobs, project_root, &value);
     910              : 
     911           96 :         return job;
     912              : }
     913              : 
     914         3385 : static inline uint32_t lsp_analyzer_job_table_running_count(HashTable *jobs)
     915              : {
     916              :         lsp_analyzer_job *job;
     917              :         zval *value;
     918         3385 :         uint32_t count = 0;
     919              : 
     920         4045 :         ZEND_HASH_FOREACH_VAL(jobs, value) {
     921          660 :                 if (Z_TYPE_P(value) != IS_PTR) {
     922            0 :                         continue;
     923              :                 }
     924              : 
     925          660 :                 job = (lsp_analyzer_job *) Z_PTR_P(value);
     926          660 :                 if (job->running && lsp_process_id_valid(job->pid)) {
     927          239 :                         count++;
     928              :                 }
     929              :         } ZEND_HASH_FOREACH_END();
     930              : 
     931         3385 :         return count;
     932              : }
     933              : 
     934         2633 : extern bool lsp_analyzer_job_table_running(HashTable *jobs)
     935              : {
     936         2633 :         return lsp_analyzer_job_table_running_count(jobs) > 0;
     937              : }
     938              : 
     939          376 : extern uint32_t lsp_analyzer_running_project_jobs(lsp_server *server)
     940              : {
     941          752 :         return lsp_analyzer_job_table_running_count(&server->phpstan_jobs) +
     942          376 :                 lsp_analyzer_job_table_running_count(&server->psalm_jobs)
     943              :         ;
     944              : }
     945              : 
     946          324 : extern zend_string *lsp_analyzer_project_output_file(zend_string *project_root, const char *analyzer)
     947              : {
     948              :         zend_string *dir, *path;
     949              : 
     950          324 :         dir = strpprintf(0, "%s/.lsparrot/%s", ZSTR_VAL(project_root), analyzer);
     951          324 :         lsp_mkdir_p(dir);
     952          324 :         path = strpprintf(0, "%s/project-diagnostics.json", ZSTR_VAL(dir));
     953          324 :         zend_string_release(dir);
     954              : 
     955          324 :         return path;
     956              : }
     957              : 
     958          344 : extern void lsp_analyzer_project_state(lsp_server *server, const char *analyzer, zend_string *project_root, zend_long state)
     959              : {
     960              :         zval state_zv;
     961              : 
     962          344 :         ZVAL_LONG(&state_zv, state);
     963          344 :         zend_hash_update(lsp_analyzer_project_table(server, analyzer), project_root, &state_zv);
     964          344 : }
     965              : 
     966          332 : extern zend_long lsp_analyzer_project_state_value(lsp_server *server, const char *analyzer, zend_string *project_root)
     967              : {
     968              :         zval *state_zv;
     969              : 
     970          332 :         state_zv = zend_hash_find(lsp_analyzer_project_table(server, analyzer), project_root);
     971              : 
     972          236 :         return state_zv &&
     973          568 :                 Z_TYPE_P(state_zv) == IS_LONG ? Z_LVAL_P(state_zv) : 0
     974              :         ;
     975              : }
     976              : 
     977          264 : extern bool lsp_analyzer_project_has_state(lsp_server *server, const char *analyzer, zend_string *project_root)
     978              : {
     979          264 :         return lsp_analyzer_project_state_value(server, analyzer, project_root) > 0;
     980              : }
     981              : 
     982              : static inline void lsp_analyzer_project_config_error(lsp_server *server, const char *analyzer, const char *message, zend_string *project_root)
     983              : {
     984              :         lsp_analyzer_project_state(server, analyzer, project_root, LSP_ANALYZER_PROJECT_ERROR);
     985              :         lsp_analyzer_project_status(analyzer, "error", message, project_root);
     986              : }
     987              : 
     988          488 : extern bool lsp_analyzer_project_has_scope(zend_string *project_root)
     989              : {
     990          488 :         return lsp_composer_project_has_analysis_paths(project_root);
     991              : }
     992              : 
     993          964 : extern bool lsp_document_is_in_analyzer_scope(lsp_document *document, zend_string *project_root)
     994              : {
     995          964 :         return lsp_path_is_in_composer_analysis_paths(document->path, project_root);
     996              : }
     997              : 
     998           52 : extern void lsp_command_add_composer_analysis_paths(lsp_command *command, zend_string *project_root)
     999              : {
    1000              :         zval paths, *path_zv;
    1001              : 
    1002           52 :         lsp_composer_analysis_paths(project_root, &paths);
    1003          104 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL(paths), path_zv) {
    1004           52 :                 if (Z_TYPE_P(path_zv) == IS_STRING) {
    1005           52 :                         lsp_command_add_zstr(command, Z_STR_P(path_zv));
    1006              :                 }
    1007              :         } ZEND_HASH_FOREACH_END();
    1008           52 :         zval_ptr_dtor(&paths);
    1009           52 : }
    1010              : 
    1011           96 : static inline lsp_process_id lsp_start_command_to_file(lsp_command *command, zend_string *cwd, zend_string *output_file)
    1012              : {
    1013           96 :         return lsp_process_spawn_to_file(command, cwd, output_file);
    1014              : }
    1015              : 
    1016           96 : extern bool lsp_start_analyzer_project_job(lsp_server *server, const char *analyzer, zend_string *project_root, lsp_command *command, zend_string *output_file)
    1017              : {
    1018              :         lsp_analyzer_job *job;
    1019              :         zend_string *temporary_file;
    1020              :         lsp_process_id pid;
    1021              : 
    1022           96 :         job = lsp_analyzer_job_slot(server, analyzer, project_root);
    1023           96 :         if (job->running) {
    1024            0 :                 return false;
    1025              :         }
    1026              : 
    1027           96 :         temporary_file = strpprintf(0, "%s.tmp.%ld", ZSTR_VAL(output_file), (long) lsp_current_process_id());
    1028              : 
    1029           96 :         pid = lsp_start_command_to_file(command, project_root, temporary_file);
    1030           96 :         if (!lsp_process_id_valid(pid)) {
    1031            0 :                 zend_string_release(temporary_file);
    1032              : 
    1033            0 :                 return false;
    1034              :         }
    1035              : 
    1036           96 :         lsp_analyzer_job_clear(job);
    1037           96 :         job->pid = pid;
    1038           96 :         job->cache_file = temporary_file;
    1039           96 :         job->project_root = zend_string_copy(project_root);
    1040           96 :         job->running = true;
    1041           96 :         lsp_analyzer_project_state(server, analyzer, project_root, LSP_ANALYZER_PROJECT_RUNNING);
    1042              : 
    1043           96 :         return true;
    1044              : }
    1045              : 
    1046              : 
    1047              : 
    1048           68 : static inline bool lsp_analyzer_output_has_content(zend_string *path)
    1049              : {
    1050              :         zend_stat_t st;
    1051              : 
    1052           68 :         return VCWD_STAT(ZSTR_VAL(path), &st) == 0 && S_ISREG(st.st_mode) && st.st_size > 0;
    1053              : }
    1054              : 
    1055           68 : static inline void lsp_commit_analyzer_project_output(const char *analyzer, zend_string *project_root, zend_string *output_file)
    1056              : {
    1057              :         zend_string *final_file;
    1058              : 
    1059           68 :         if (!output_file) {
    1060            0 :                 return;
    1061              :         }
    1062              : 
    1063           68 :         if (!lsp_analyzer_output_has_content(output_file)) {
    1064            0 :                 VCWD_UNLINK(ZSTR_VAL(output_file));
    1065            0 :                 return;
    1066              :         }
    1067              : 
    1068           68 :         final_file = lsp_analyzer_project_output_file(project_root, analyzer);
    1069           68 :         VCWD_RENAME(ZSTR_VAL(output_file), ZSTR_VAL(final_file));
    1070           68 :         zend_string_release(final_file);
    1071              : }
    1072              : 
    1073           68 : static inline void lsp_publish_project_document_diagnostics(lsp_server *server, zend_string *project_root)
    1074              : {
    1075              :         lsp_document *document;
    1076              :         zend_string *document_root;
    1077              :         zval *document_zv;
    1078              : 
    1079          136 :         ZEND_HASH_FOREACH_VAL(&server->documents, document_zv) {
    1080           68 :                 document = (lsp_document *) Z_PTR_P(document_zv);
    1081           68 :                 document_root = lsp_document_project_root(server, document);
    1082           68 :                 if (zend_string_equals(document_root, project_root)) {
    1083           68 :                         lsp_publish_document_diagnostics(server, document);
    1084              :                 }
    1085           68 :                 zend_string_release(document_root);
    1086              :         } ZEND_HASH_FOREACH_END();
    1087           68 : }
    1088              : 
    1089         2088 : static inline void lsp_schedule_workspace_analyzer_projects(lsp_server *server, zend_string *dir, uint32_t depth)
    1090              : {
    1091              :         const char *entry_name;
    1092              :         lsp_dir *handle;
    1093              :         zend_string *composer, *child;
    1094              :         zend_stat_t st;
    1095              : 
    1096         2088 :         if (depth > 8 || (depth > 0 && lsp_path_is_in_workspace_composer_vendor(server->root, dir))) {
    1097           56 :                 return;
    1098              :         }
    1099              : 
    1100         2084 :         composer = lsp_join_path2(dir, "composer.json");
    1101         2084 :         if (lsp_is_regular_file(composer) && lsp_analyzer_project_has_scope(dir)) {
    1102          260 :                 lsp_schedule_phpstan_project_analyzer(server, dir);
    1103          260 :                 lsp_schedule_psalm_project_analyzer(server, dir);
    1104          260 :                 lsp_psalm_ls_schedule_project(server, dir);
    1105              :         }
    1106              : 
    1107         2084 :         zend_string_release(composer);
    1108              : 
    1109         2084 :         handle = lsp_dir_open(dir);
    1110         2084 :         if (!handle) {
    1111           52 :                 return;
    1112              :         }
    1113              : 
    1114        40580 :         while ((entry_name = lsp_dir_read(handle)) != NULL) {
    1115        38548 :                 if (lsp_scan_should_skip_dir_name(entry_name)) {
    1116         4644 :                         continue;
    1117              :                 }
    1118              : 
    1119        33904 :                 child = lsp_join_path2(dir, entry_name);
    1120        33904 :                 if (VCWD_STAT(ZSTR_VAL(child), &st) != 0) {
    1121            0 :                         zend_string_release(child);
    1122            0 :                         continue;
    1123              :                 }
    1124              : 
    1125        33904 :                 if (S_ISDIR(st.st_mode)) {
    1126         1536 :                         lsp_schedule_workspace_analyzer_projects(server, child, depth + 1);
    1127              :                 }
    1128              : 
    1129        33904 :                 zend_string_release(child);
    1130              :         }
    1131              : 
    1132         2032 :         lsp_dir_close(handle);
    1133              : }
    1134              : 
    1135              : 
    1136              : 
    1137           68 : extern void lsp_analyzer_project_finished(lsp_server *server, const char *analyzer, zend_string *project_root, zend_string *output_file)
    1138              : {
    1139              :         zend_long state;
    1140              :         zend_string *output, *message;
    1141              :         bool output_is_json;
    1142              : 
    1143           68 :         state = lsp_analyzer_project_state_value(server, analyzer, project_root);
    1144           68 :         if (state == LSP_ANALYZER_PROJECT_PENDING) {
    1145            0 :                 if (output_file) {
    1146            0 :                         VCWD_UNLINK(ZSTR_VAL(output_file));
    1147              :                 }
    1148              :         } else {
    1149           68 :                 output = output_file && lsp_is_regular_file(output_file) ? lsp_read_file(output_file) : zend_empty_string;
    1150           68 :                 output_is_json = output != zend_empty_string && lsp_analyzer_output_is_json(analyzer, output);
    1151           68 :                 lsp_commit_analyzer_project_output(analyzer, project_root, output_file);
    1152              : 
    1153           68 :                 if (output_is_json) {
    1154           60 :                         lsp_analyzer_project_state(server, analyzer, project_root, LSP_ANALYZER_PROJECT_READY);
    1155              :                 } else {
    1156            8 :                         lsp_analyzer_project_state(server, analyzer, project_root, LSP_ANALYZER_PROJECT_ERROR);
    1157            8 :                         message = lsp_analyzer_failure_message(analyzer, output);
    1158            8 :                         lsp_analyzer_project_status(analyzer, "error", ZSTR_VAL(message), project_root);
    1159            8 :                         zend_string_release(message);
    1160              :                 }
    1161              : 
    1162           68 :                 if (output != zend_empty_string) {
    1163           68 :                         zend_string_release(output);
    1164              :                 }
    1165              : 
    1166           68 :                 lsp_publish_project_document_diagnostics(server, project_root);
    1167              :         }
    1168              : 
    1169           68 :         if (strcmp(analyzer, "phpstan") == 0) {
    1170           44 :                 lsp_start_pending_phpstan_project_analyzer(server);
    1171              :         } else {
    1172           24 :                 lsp_start_pending_psalm_project_analyzer(server);
    1173              :         }
    1174           68 : }
    1175              : 
    1176          552 : static void lsp_schedule_workspace_analyzers_root(lsp_server *server, zend_string *root, void *context)
    1177              : {
    1178              :         (void) context;
    1179              : 
    1180          552 :         lsp_schedule_workspace_analyzer_projects(server, root, 0);
    1181          552 : }
    1182              : 
    1183          548 : extern void lsp_schedule_workspace_analyzers(lsp_server *server)
    1184              : {
    1185          548 :         lsp_workspace_roots_each(server, lsp_schedule_workspace_analyzers_root, NULL);
    1186          548 : }
    1187              : 
    1188          660 : extern void lsp_schedule_project_analyzers(lsp_server *server, lsp_document *document)
    1189              : {
    1190              :         zend_string *project_root;
    1191              : 
    1192          660 :         project_root = lsp_document_project_root(server, document);
    1193          660 :         if (lsp_document_is_in_analyzer_scope(document, project_root)) {
    1194          332 :                 lsp_schedule_phpstan_project_analyzer(server, project_root);
    1195          332 :                 lsp_schedule_psalm_project_analyzer(server, project_root);
    1196          332 :                 lsp_psalm_ls_schedule_project(server, project_root);
    1197              :         }
    1198          660 :         zend_string_release(project_root);
    1199          660 : }
    1200              : 
    1201           12 : extern void lsp_reschedule_project_analyzers(lsp_server *server, lsp_document *document)
    1202              : {
    1203              :         zend_string *project_root;
    1204              : 
    1205           12 :         project_root = lsp_document_project_root(server, document);
    1206           12 :         if (lsp_document_is_in_analyzer_scope(document, project_root)) {
    1207            0 :                 lsp_reschedule_phpstan_project_analyzer(server, project_root);
    1208            0 :                 lsp_reschedule_psalm_project_analyzer(server, project_root);
    1209              :         }
    1210           12 :         zend_string_release(project_root);
    1211           12 : }
    1212              : 
    1213          660 : extern void lsp_publish_document_diagnostics(lsp_server *server, lsp_document *document)
    1214              : {
    1215              :         zval params, diagnostics, item_copy, *lsparrot_diagnostics, *lsparrot_item;
    1216              : 
    1217          660 :         lsp_schedule_project_analyzers(server, document);
    1218          660 :         lsparrot_diagnostics = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "diagnostics", sizeof("diagnostics") - 1);
    1219              : 
    1220          660 :         array_init(&params);
    1221          660 :         add_assoc_str(&params, "uri", zend_string_copy(document->uri));
    1222          660 :         array_init(&diagnostics);
    1223              : 
    1224          660 :         if (lsparrot_diagnostics && Z_TYPE_P(lsparrot_diagnostics) == IS_ARRAY) {
    1225          920 :                 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(lsparrot_diagnostics), lsparrot_item) {
    1226          260 :                         ZVAL_COPY(&item_copy, lsparrot_item);
    1227          260 :                         add_next_index_zval(&diagnostics, &item_copy);
    1228              :                 } ZEND_HASH_FOREACH_END();
    1229              :         }
    1230              : 
    1231          660 :         if (!lsparrot_diagnostics || Z_TYPE_P(lsparrot_diagnostics) != IS_ARRAY || zend_hash_num_elements(Z_ARRVAL_P(lsparrot_diagnostics)) == 0) {
    1232          400 :                 lsp_append_phpstan_cached_diagnostics(server, document, &diagnostics);
    1233          400 :                 lsp_append_psalm_cached_diagnostics(server, document, &diagnostics);
    1234          400 :                 lsp_psalm_ls_append_diagnostics(server, document, &diagnostics);
    1235              :         }
    1236              : 
    1237          660 :         add_assoc_zval(&params, "diagnostics", &diagnostics);
    1238          660 :         add_assoc_long(&params, "version", document->version);
    1239          660 :         lsp_protocol_notify("textDocument/publishDiagnostics", &params);
    1240          660 :         zval_ptr_dtor(&params);
    1241          660 : }
        

Generated by: LCOV version 2.0-1