LCOV - code coverage report
Current view: top level - ext - lsp_server_refactor.c (source / functions) Coverage Total Hit
Test: Extension code coverage Lines: 76.7 % 1476 1132
Test Date: 2026-07-13 00:58:38 Functions: 95.2 % 83 79

            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              : #include <Zend/zend_language_parser.h>
      17              : 
      18              : #define LSP_INLAY_TYPE_HINT_LIMIT 64
      19              : 
      20              : static inline bool lsp_refactor_add_scoped_matches(zval *items, zend_string *location_path, lsp_document *document, zend_string *word, size_t offset);
      21              : static inline zval *lsp_refactor_prev_significant_token(HashTable *tokens, uint32_t index);
      22              : static inline bool lsp_refactor_token_is_object_operator(zval *token);
      23              : 
      24           12 : static inline bool lsp_refactor_string_equals_ci(const char *left, size_t left_length, const char *right, size_t right_length)
      25              : {
      26           12 :         return left_length == right_length && strncasecmp(left, right, left_length) == 0;
      27              : }
      28              : 
      29           12 : static inline bool lsp_refactor_string_equals_zstr_ci(const char *left, size_t left_length, zend_string *right)
      30              : {
      31              :         const char *right_value;
      32              :         size_t right_length;
      33              : 
      34           12 :         right_value = ZSTR_VAL(right);
      35           12 :         right_length = ZSTR_LEN(right);
      36           12 :         if (right_length > 0 && right_value[0] == '\\') {
      37            0 :                 right_value++;
      38            0 :                 right_length--;
      39              :         }
      40              : 
      41           12 :         return lsp_refactor_string_equals_ci(left, left_length, right_value, right_length);
      42              : }
      43              : 
      44           28 : static inline int lsp_refactor_zstr_casecmp(zend_string *left, zend_string *right)
      45              : {
      46              :         unsigned char left_char, right_char;
      47              :         size_t i, min_length;
      48              : 
      49           28 :         min_length = ZSTR_LEN(left) < ZSTR_LEN(right) ? ZSTR_LEN(left) : ZSTR_LEN(right);
      50          472 :         for (i = 0; i < min_length; i++) {
      51          460 :                 left_char = (unsigned char) tolower((unsigned char) ZSTR_VAL(left)[i]);
      52          460 :                 right_char = (unsigned char) tolower((unsigned char) ZSTR_VAL(right)[i]);
      53          460 :                 if (left_char < right_char) {
      54            4 :                         return -1;
      55              :                 }
      56          456 :                 if (left_char > right_char) {
      57           12 :                         return 1;
      58              :                 }
      59              :         }
      60              : 
      61           12 :         if (ZSTR_LEN(left) < ZSTR_LEN(right)) {
      62            0 :                 return -1;
      63              :         }
      64           12 :         if (ZSTR_LEN(left) > ZSTR_LEN(right)) {
      65            0 :                 return 1;
      66              :         }
      67              : 
      68           12 :         return 0;
      69              : }
      70              : 
      71           24 : static inline bool lsp_refactor_zstr_equals_ci(zend_string *left, zend_string *right)
      72              : {
      73           24 :         return lsp_refactor_zstr_casecmp(left, right) == 0;
      74              : }
      75              : 
      76           56 : static inline bool lsp_refactor_word_is_variable(zend_string *word)
      77              : {
      78           56 :         return ZSTR_LEN(word) > 0 && ZSTR_VAL(word)[0] == '$';
      79              : }
      80              : 
      81           16 : static inline bool lsp_refactor_word_is_qualified(zend_string *word)
      82              : {
      83           16 :         return memchr(ZSTR_VAL(word), '\\', ZSTR_LEN(word)) != NULL;
      84              : }
      85              : 
      86          728 : static inline bool lsp_refactor_token_is_searchable(zval *token, bool variable)
      87              : {
      88          728 :         if (variable) {
      89            0 :                 return lsp_token_name_equals(token, "T_VARIABLE");
      90              :         }
      91              : 
      92         1356 :         return lsp_token_name_equals(token, "T_STRING") ||
      93         1256 :                 lsp_token_name_equals(token, "T_NAME_FULLY_QUALIFIED") ||
      94         1984 :                 lsp_token_name_equals(token, "T_NAME_RELATIVE") ||
      95          628 :                 lsp_token_name_equals(token, "T_NAME_QUALIFIED")
      96              :         ;
      97              : }
      98              : 
      99          460 : static inline bool lsp_refactor_match_word_at(const char *value, size_t value_length, size_t start, zend_string *word, bool variable, size_t *match_end)
     100              : {
     101              :         const char *word_value;
     102              :         size_t word_length;
     103              : 
     104          460 :         word_value = ZSTR_VAL(word);
     105          460 :         word_length = ZSTR_LEN(word);
     106          460 :         if (!variable && word_length > 0 && word_value[0] == '\\') {
     107            0 :                 word_value++;
     108            0 :                 word_length--;
     109              :         }
     110              : 
     111          460 :         if (word_length == 0 || start + word_length > value_length) {
     112          416 :                 return false;
     113              :         }
     114              : 
     115           44 :         if (start > 0 && lsp_doc_is_identifier_char(value[start - 1])) {
     116           12 :                 return false;
     117              :         }
     118              : 
     119           32 :         if (start + word_length < value_length && lsp_doc_is_identifier_char(value[start + word_length])) {
     120            4 :                 return false;
     121              :         }
     122              : 
     123           28 :         if (variable) {
     124            0 :                 if (memcmp(value + start, word_value, word_length) != 0) {
     125            0 :                         return false;
     126              :                 }
     127           28 :         } else if (strncasecmp(value + start, word_value, word_length) != 0) {
     128           16 :                 return false;
     129              :         }
     130              : 
     131           12 :         *match_end = start + word_length;
     132              : 
     133           12 :         return true;
     134              : }
     135              : 
     136          100 : static inline bool lsp_refactor_next_word_match(zend_string *text, zend_string *word, bool variable, size_t *cursor, size_t *match_start, size_t *match_end)
     137              : {
     138              :         const char *value;
     139              :         size_t i, end;
     140              : 
     141          100 :         value = ZSTR_VAL(text);
     142          100 :         i = *cursor;
     143          548 :         while (i < ZSTR_LEN(text)) {
     144          460 :                 if (lsp_refactor_match_word_at(value, ZSTR_LEN(text), i, word, variable, &end)) {
     145           12 :                         *cursor = end;
     146           12 :                         *match_start = i;
     147           12 :                         *match_end = end;
     148              : 
     149           12 :                         return true;
     150              :                 }
     151          448 :                 i++;
     152              :         }
     153              : 
     154           88 :         return false;
     155              : }
     156              : 
     157          728 : static inline bool lsp_refactor_token_match(zval *token, zend_string *word, bool variable, size_t *match_start, size_t *match_end)
     158              : {
     159              :         zend_string *text;
     160              :         size_t cursor, local_start, local_end, offset;
     161              : 
     162          728 :         text = lsp_token_string(token, "text");
     163          728 :         if (!text || !lsp_refactor_token_is_searchable(token, variable)) {
     164          628 :                 return false;
     165              :         }
     166              : 
     167          100 :         cursor = 0;
     168          100 :         if (!lsp_refactor_next_word_match(text, word, variable, &cursor, &local_start, &local_end)) {
     169           88 :                 return false;
     170              :         }
     171              : 
     172           12 :         offset = (size_t) lsp_token_long(token, "offset", 0);
     173           12 :         *match_start = offset + local_start;
     174           12 :         *match_end = offset + local_end;
     175              : 
     176           12 :         return true;
     177              : }
     178              : 
     179           12 : static inline bool lsp_refactor_word_bounds(zend_string *text, size_t offset, size_t *word_start, size_t *word_end)
     180              : {
     181              :         const char *value;
     182              :         size_t start, end, length;
     183              : 
     184           12 :         value = ZSTR_VAL(text);
     185           12 :         length = ZSTR_LEN(text);
     186           12 :         if (offset > length) {
     187            0 :                 offset = length;
     188              :         }
     189              : 
     190           12 :         start = offset;
     191           20 :         while (start > 0 && (lsp_doc_is_identifier_char(value[start - 1]) || value[start - 1] == '$' || value[start - 1] == '\\')) {
     192            8 :                 start--;
     193              :         }
     194              : 
     195           12 :         end = offset;
     196           88 :         while (end < length && (lsp_doc_is_identifier_char(value[end]) || value[end] == '$' || value[end] == '\\')) {
     197           76 :                 end++;
     198              :         }
     199              : 
     200           12 :         if (end <= start) {
     201            0 :                 return false;
     202              :         }
     203              : 
     204           12 :         *word_start = start;
     205           12 :         *word_end = end;
     206              : 
     207           12 :         return true;
     208              : }
     209              : 
     210           12 : static inline void lsp_refactor_location_from_offsets(zend_string *path, zend_string *contents, size_t start_offset, size_t end_offset, zval *location)
     211              : {
     212              :         zend_string *uri;
     213              :         zval range;
     214              : 
     215           12 :         uri = lsp_uri_from_path(path);
     216           12 :         array_init(location);
     217           12 :         add_assoc_str(location, "uri", uri);
     218           12 :         lsp_range_from_offsets(contents, start_offset, end_offset, &range);
     219           12 :         add_assoc_zval(location, "range", &range);
     220           12 : }
     221              : 
     222            0 : static inline void lsp_refactor_add_token_locations(zval *locations, zend_string *path, zend_string *contents, zend_string *word)
     223              : {
     224              :         zval tokens_zv, *token, location;
     225              :         HashTable *tokens;
     226              :         uint32_t i, count;
     227              :         size_t start, end;
     228              :         bool variable;
     229              : 
     230            0 :         variable = lsp_refactor_word_is_variable(word);
     231            0 :         ZVAL_UNDEF(&tokens_zv);
     232            0 :         lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
     233            0 :         if (Z_TYPE(tokens_zv) != IS_ARRAY) {
     234            0 :                 if (!Z_ISUNDEF(tokens_zv)) {
     235            0 :                         zval_ptr_dtor(&tokens_zv);
     236              :                 }
     237              : 
     238            0 :                 return;
     239              :         }
     240              : 
     241            0 :         tokens = Z_ARRVAL(tokens_zv);
     242            0 :         count = zend_hash_num_elements(tokens);
     243            0 :         for (i = 0; i < count; i++) {
     244            0 :                 token = zend_hash_index_find(tokens, i);
     245            0 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_refactor_token_match(token, word, variable, &start, &end)) {
     246            0 :                         continue;
     247              :                 }
     248              : 
     249            0 :                 lsp_refactor_location_from_offsets(path, contents, start, end, &location);
     250            0 :                 add_next_index_zval(locations, &location);
     251              :         }
     252              : 
     253            0 :         zval_ptr_dtor(&tokens_zv);
     254              : }
     255              : 
     256            0 : static inline void lsp_refactor_add_document_highlights(zval *items, zend_string *contents, zend_string *word)
     257              : {
     258              :         zval tokens_zv, *token, item, range;
     259              :         HashTable *tokens;
     260              :         uint32_t i, count;
     261              :         size_t start, end;
     262              :         bool variable;
     263              : 
     264            0 :         variable = lsp_refactor_word_is_variable(word);
     265            0 :         ZVAL_UNDEF(&tokens_zv);
     266            0 :         lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
     267            0 :         if (Z_TYPE(tokens_zv) != IS_ARRAY) {
     268            0 :                 if (!Z_ISUNDEF(tokens_zv)) {
     269            0 :                         zval_ptr_dtor(&tokens_zv);
     270              :                 }
     271              : 
     272            0 :                 return;
     273              :         }
     274              : 
     275            0 :         tokens = Z_ARRVAL(tokens_zv);
     276            0 :         count = zend_hash_num_elements(tokens);
     277            0 :         for (i = 0; i < count; i++) {
     278            0 :                 token = zend_hash_index_find(tokens, i);
     279            0 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_refactor_token_match(token, word, variable, &start, &end)) {
     280            0 :                         continue;
     281              :                 }
     282              : 
     283            0 :                 array_init(&item);
     284            0 :                 lsp_range_from_offsets(contents, start, end, &range);
     285            0 :                 add_assoc_zval(&item, "range", &range);
     286            0 :                 add_assoc_long(&item, "kind", 1);
     287            0 :                 add_next_index_zval(items, &item);
     288              :         }
     289              : 
     290            0 :         zval_ptr_dtor(&tokens_zv);
     291              : }
     292              : 
     293            0 : static inline void lsp_refactor_add_project_reference_locations(lsp_server *server, zval *locations, lsp_document *primary_document, zend_string *word)
     294              : {
     295              :         lsp_symbol_index_header *header;
     296              :         lsp_document *document;
     297              :         HashTable visited;
     298              :         zend_string *path_string, *contents;
     299              :         uint32_t i;
     300              :         size_t fqcn_length, path_length;
     301              :         char *cursor, *end, kind, *fqcn, *path;
     302              :         bool owns_contents;
     303              : 
     304            0 :         lsp_index_join_worker(server);
     305              : 
     306            0 :         if (!server->symbol_index.available || !server->symbol_index.addr) {
     307            0 :                 return;
     308              :         }
     309              : 
     310            0 :         header = (lsp_symbol_index_header *) server->symbol_index.addr;
     311            0 :         if (header->magic != LSP_SYMBOL_INDEX_MAGIC || header->used > header->capacity) {
     312            0 :                 return;
     313              :         }
     314              : 
     315            0 :         zend_hash_init(&visited, 128, NULL, NULL, 0);
     316            0 :         zend_hash_add_empty_element(&visited, primary_document->path);
     317            0 :         cursor = ((char *) server->symbol_index.addr) + sizeof(lsp_symbol_index_header);
     318            0 :         end = ((char *) server->symbol_index.addr) + header->used;
     319            0 :         for (i = 0; i < header->symbol_count && cursor < end; i++) {
     320            0 :                 kind = *cursor++;
     321            0 :                 fqcn = cursor;
     322            0 :                 fqcn_length = strlen(fqcn);
     323            0 :                 path = fqcn + fqcn_length + 1;
     324            0 :                 if (path >= end) {
     325            0 :                         break;
     326              :                 }
     327              : 
     328            0 :                 path_length = strlen(path);
     329            0 :                 cursor = path + path_length + 1;
     330            0 :                 if (cursor > end ||
     331            0 :                         lsp_path_value_contains_analysis_helper(path, path_length) ||
     332            0 :                         lsp_path_value_contains_vendor(path, path_length) ||
     333              :                         kind == '\0'
     334              :                 ) {
     335            0 :                         continue;
     336              :                 }
     337              : 
     338            0 :                 path_string = zend_string_init(path, path_length, 0);
     339            0 :                 if (zend_hash_exists(&visited, path_string)) {
     340            0 :                         zend_string_release(path_string);
     341            0 :                         continue;
     342              :                 }
     343              : 
     344            0 :                 zend_hash_add_empty_element(&visited, path_string);
     345              : 
     346            0 :                 document = lsp_document_for_path(server, path_string);
     347            0 :                 contents = document ? zend_string_copy(document->text) : lsp_read_file(path_string);
     348            0 :                 owns_contents = document || contents != zend_empty_string;
     349            0 :                 if (owns_contents) {
     350            0 :                         lsp_refactor_add_token_locations(locations, path_string, contents, word);
     351            0 :                         zend_string_release(contents);
     352              :                 }
     353              : 
     354            0 :                 zend_string_release(path_string);
     355              :         }
     356              : 
     357            0 :         zend_hash_destroy(&visited);
     358              : }
     359              : 
     360            8 : static inline bool lsp_refactor_range_start_offset(zend_string *text, zval *range, size_t *offset)
     361              : {
     362              :         zend_long line, character;
     363              :         zval *start;
     364              : 
     365            8 :         if (!range || Z_TYPE_P(range) != IS_ARRAY) {
     366            0 :                 return false;
     367              :         }
     368              : 
     369            8 :         start = lsp_array_find(range, "start");
     370            8 :         if (!start || Z_TYPE_P(start) != IS_ARRAY) {
     371            0 :                 return false;
     372              :         }
     373              : 
     374            8 :         lsp_position_from_zval(start, &line, &character);
     375            8 :         *offset = lsp_offset_at(text, line, character);
     376              : 
     377            8 :         return true;
     378              : }
     379              : 
     380              : static inline bool lsp_refactor_range_offsets(zend_string *text, zval *range, size_t *start_offset, size_t *end_offset)
     381              : {
     382              :         zend_long line, character;
     383              :         zval *start, *end;
     384              : 
     385              :         if (!range || Z_TYPE_P(range) != IS_ARRAY) {
     386              :                 return false;
     387              :         }
     388              : 
     389              :         start = lsp_array_find(range, "start");
     390              :         end = lsp_array_find(range, "end");
     391              :         if (!start || Z_TYPE_P(start) != IS_ARRAY || !end || Z_TYPE_P(end) != IS_ARRAY) {
     392              :                 return false;
     393              :         }
     394              : 
     395              :         lsp_position_from_zval(start, &line, &character);
     396              :         *start_offset = lsp_offset_at(text, line, character);
     397              :         lsp_position_from_zval(end, &line, &character);
     398              :         *end_offset = lsp_offset_at(text, line, character);
     399              :         if (*end_offset < *start_offset) {
     400              :                 *end_offset = *start_offset;
     401              :         }
     402              : 
     403              :         return true;
     404              : }
     405              : 
     406           24 : static inline bool lsp_refactor_context_only_has(zval *params, const char *kind)
     407              : {
     408              :         zval *context, *only, *entry;
     409              : 
     410           24 :         context = lsp_array_find(params, "context");
     411           24 :         if (!context || Z_TYPE_P(context) != IS_ARRAY) {
     412            0 :                 return true;
     413              :         }
     414              : 
     415           24 :         only = lsp_array_find(context, "only");
     416           24 :         if (!only || Z_TYPE_P(only) != IS_ARRAY || zend_hash_num_elements(Z_ARRVAL_P(only)) == 0) {
     417            8 :                 return true;
     418              :         }
     419              : 
     420           24 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(only), entry) {
     421           16 :                 if (Z_TYPE_P(entry) == IS_STRING && php_ver_abstract.string_equals_cstr(Z_STR_P(entry), kind, strlen(kind))) {
     422            8 :                         return true;
     423              :                 }
     424              :         } ZEND_HASH_FOREACH_END();
     425              : 
     426            8 :         return false;
     427              : }
     428              : 
     429              : static inline void lsp_refactor_workspace_edit_single(zval *edit, zend_string *uri, zval *text_edits)
     430              : {
     431              :         zval changes;
     432              : 
     433              :         array_init(edit);
     434              :         array_init(&changes);
     435              : 
     436              :         add_assoc_zval_ex(&changes, ZSTR_VAL(uri), ZSTR_LEN(uri), text_edits);
     437              :         add_assoc_zval(edit, "changes", &changes);
     438              : }
     439              : 
     440              : static inline void lsp_refactor_add_single_text_edit(zval *text_edits, zend_string *contents, size_t start_offset, size_t end_offset, zend_string *new_text)
     441              : {
     442              :         zval edit, range;
     443              : 
     444              :         array_init(&edit);
     445              :         lsp_range_from_offsets(contents, start_offset, end_offset, &range);
     446              :         add_assoc_zval(&edit, "range", &range);
     447              :         add_assoc_str(&edit, "newText", zend_string_copy(new_text));
     448              :         add_next_index_zval(text_edits, &edit);
     449              : }
     450              : 
     451           12 : static inline zend_string *lsp_refactor_trim_line(const char *start, const char *end)
     452              : {
     453           12 :         while (start < end && isspace((unsigned char) *start)) {
     454            0 :                 start++;
     455              :         }
     456              : 
     457           12 :         while (end > start && isspace((unsigned char) end[-1])) {
     458            0 :                 end--;
     459              :         }
     460              : 
     461           12 :         return zend_string_init(start, end - start, 0);
     462              : }
     463              : 
     464          224 : static inline bool lsp_refactor_trimmed_line_starts_with(const char *start, const char *end, const char *keyword)
     465              : {
     466              :         size_t keyword_length;
     467              : 
     468          224 :         keyword_length = strlen(keyword);
     469          224 :         while (start < end && isspace((unsigned char) *start)) {
     470            0 :                 start++;
     471              :         }
     472              : 
     473          360 :         return (size_t) (end - start) >= keyword_length &&
     474          244 :                 strncasecmp(start, keyword, keyword_length) == 0 &&
     475           20 :                 ((size_t) (end - start) == keyword_length || isspace((unsigned char) start[keyword_length]))
     476              :         ;
     477              : }
     478              : 
     479           12 : static inline bool lsp_refactor_line_contains_semicolon(const char *start, const char *end)
     480              : {
     481          244 :         while (start < end) {
     482          244 :                 if (*start == ';') {
     483           12 :                         return true;
     484              :                 }
     485          232 :                 start++;
     486              :         }
     487              : 
     488            0 :         return false;
     489              : }
     490              : 
     491           28 : static inline bool lsp_refactor_line_starts_declaration(const char *start, const char *end)
     492              : {
     493           56 :         return lsp_refactor_trimmed_line_starts_with(start, end, "class") ||
     494           56 :                 lsp_refactor_trimmed_line_starts_with(start, end, "interface") ||
     495           56 :                 lsp_refactor_trimmed_line_starts_with(start, end, "trait") ||
     496           56 :                 lsp_refactor_trimmed_line_starts_with(start, end, "enum") ||
     497           52 :                 lsp_refactor_trimmed_line_starts_with(start, end, "function") ||
     498           80 :                 lsp_refactor_trimmed_line_starts_with(start, end, "final") ||
     499           20 :                 lsp_refactor_trimmed_line_starts_with(start, end, "abstract")
     500              :         ;
     501              : }
     502              : 
     503           12 : static inline void lsp_refactor_sorted_line_insert(zend_string ***lines, uint32_t *count, uint32_t *capacity, zend_string *line)
     504              : {
     505              :         uint32_t i, j;
     506              : 
     507           16 :         for (i = 0; i < *count; i++) {
     508            8 :                 if (lsp_refactor_zstr_equals_ci((*lines)[i], line)) {
     509            4 :                         zend_string_release(line);
     510            4 :                         return;
     511              :                 }
     512              :         }
     513              : 
     514            8 :         if (*count + 1 > *capacity) {
     515            4 :                 *capacity = *capacity == 0 ? 8 : *capacity * 2;
     516            4 :                 *lines = erealloc(*lines, sizeof(zend_string *) * (*capacity));
     517              :         }
     518              : 
     519            8 :         i = 0;
     520            8 :         while (i < *count && lsp_refactor_zstr_casecmp((*lines)[i], line) < 0) {
     521            0 :                 i++;
     522              :         }
     523              : 
     524           12 :         for (j = *count; j > i; j--) {
     525            4 :                 (*lines)[j] = (*lines)[j - 1];
     526              :         }
     527              : 
     528            8 :         (*lines)[i] = line;
     529            8 :         (*count)++;
     530              : }
     531              : 
     532            8 : static inline void lsp_refactor_lines_destroy(zend_string **lines, uint32_t count)
     533              : {
     534              :         uint32_t i;
     535              : 
     536           16 :         for (i = 0; i < count; i++) {
     537            8 :                 zend_string_release(lines[i]);
     538              :         }
     539              : 
     540            8 :         if (lines) {
     541            4 :                 efree(lines);
     542              :         }
     543            8 : }
     544              : 
     545            8 : static inline bool lsp_refactor_organize_imports_edit(lsp_document *document, zval *text_edit)
     546              : {
     547              :         const char *value, *line_start, *line_end;
     548              :         zend_string **lines, *line;
     549            8 :         smart_str new_text = {0};
     550              :         zval range;
     551              :         uint32_t count, capacity, i;
     552              :         size_t offset, next_offset, first_use_offset, last_use_end, original_length;
     553              :         bool has_newline;
     554              : 
     555            8 :         value = ZSTR_VAL(document->text);
     556            8 :         lines = NULL;
     557            8 :         count = 0;
     558            8 :         capacity = 0;
     559            8 :         offset = 0;
     560            8 :         first_use_offset = (size_t) -1;
     561            8 :         last_use_end = 0;
     562           40 :         while (offset < ZSTR_LEN(document->text)) {
     563           40 :                 line_start = value + offset;
     564           40 :                 line_end = memchr(line_start, '\n', ZSTR_LEN(document->text) - offset);
     565           40 :                 has_newline = line_end != NULL;
     566           40 :                 if (!line_end) {
     567            0 :                         line_end = value + ZSTR_LEN(document->text);
     568            0 :                         next_offset = ZSTR_LEN(document->text);
     569              :                 } else {
     570           40 :                         next_offset = (size_t) (line_end - value) + 1;
     571              :                 }
     572              : 
     573           40 :                 if (lsp_refactor_trimmed_line_starts_with(line_start, line_end, "use") && lsp_refactor_line_contains_semicolon(line_start, line_end)) {
     574           12 :                         if (first_use_offset == (size_t) -1) {
     575            4 :                                 first_use_offset = offset;
     576              :                         }
     577           12 :                         last_use_end = next_offset;
     578           12 :                         line = lsp_refactor_trim_line(line_start, line_end);
     579           12 :                         lsp_refactor_sorted_line_insert(&lines, &count, &capacity, line);
     580           28 :                 } else if (lsp_refactor_line_starts_declaration(line_start, line_end)) {
     581            8 :                         break;
     582              :                 }
     583              : 
     584           32 :                 offset = next_offset;
     585           32 :                 if (!has_newline) {
     586            0 :                         break;
     587              :                 }
     588              :         }
     589              : 
     590            8 :         if (count == 0 || first_use_offset == (size_t) -1 || last_use_end <= first_use_offset) {
     591            4 :                 lsp_refactor_lines_destroy(lines, count);
     592            4 :                 return false;
     593              :         }
     594              : 
     595           12 :         for (i = 0; i < count; i++) {
     596            8 :                 smart_str_append(&new_text, lines[i]);
     597            8 :                 smart_str_appendc(&new_text, '\n');
     598              :         }
     599              : 
     600            4 :         smart_str_0(&new_text);
     601            4 :         lsp_refactor_lines_destroy(lines, count);
     602              : 
     603            4 :         original_length = last_use_end - first_use_offset;
     604            4 :         if (new_text.s && ZSTR_LEN(new_text.s) == original_length && memcmp(ZSTR_VAL(new_text.s), value + first_use_offset, original_length) == 0) {
     605            0 :                 smart_str_free(&new_text);
     606            0 :                 return false;
     607              :         }
     608              : 
     609            4 :         array_init(text_edit);
     610              : 
     611            4 :         lsp_range_from_offsets(document->text, first_use_offset, last_use_end, &range);
     612            4 :         add_assoc_zval(text_edit, "range", &range);
     613            4 :         add_assoc_str(text_edit, "newText", new_text.s);
     614              : 
     615            4 :         return true;
     616              : }
     617              : 
     618            8 : static inline void lsp_refactor_add_organize_imports_action(zval *actions, lsp_document *document)
     619              : {
     620              :         zval action, edit, changes, text_edits, text_edit;
     621              : 
     622            8 :         ZVAL_UNDEF(&text_edit);
     623            8 :         if (!lsp_refactor_organize_imports_edit(document, &text_edit)) {
     624            4 :                 return;
     625              :         }
     626              : 
     627            4 :         array_init(&text_edits);
     628            4 :         add_next_index_zval(&text_edits, &text_edit);
     629            4 :         array_init(&edit);
     630            4 :         array_init(&changes);
     631            4 :         add_assoc_zval_ex(&changes, ZSTR_VAL(document->uri), ZSTR_LEN(document->uri), &text_edits);
     632            4 :         add_assoc_zval(&edit, "changes", &changes);
     633              : 
     634            4 :         array_init(&action);
     635            4 :         add_assoc_string(&action, "title", "Organize Imports");
     636            4 :         add_assoc_string(&action, "kind", "source.organizeImports");
     637            4 :         add_assoc_zval(&action, "edit", &edit);
     638            4 :         add_next_index_zval(actions, &action);
     639              : }
     640              : 
     641            4 : static inline void lsp_refactor_add_import_action(zval *actions, lsp_document *document, char kind, const char *fqcn, size_t fqcn_length)
     642              : {
     643              :         zend_string *current_namespace, *import_text, *title;
     644              :         zval action, edit, changes, text_edits, text_edit, range;
     645              :         size_t insert_offset;
     646              :         bool after_existing_use;
     647              : 
     648              :         /* The cached derived imports fall back to token scanning while the
     649              :          * document does not parse — exactly the state a missing-import quick
     650              :          * fix is requested in. The AST-only lsp_document_has_import() returned
     651              :          * false for every mid-edit document, offering duplicate imports. */
     652            4 :         current_namespace = lsp_document_namespace_cached(document);
     653            8 :         if (lsp_symbol_in_current_namespace(current_namespace, kind, fqcn, fqcn_length) ||
     654            8 :                 lsp_document_has_import_cached(document, kind, fqcn, fqcn_length) ||
     655            4 :                 lsp_document_import_binds_short_name(document, kind, fqcn, fqcn_length)
     656              :         ) {
     657            0 :                 return;
     658              :         }
     659              : 
     660            4 :         insert_offset = lsp_import_insert_offset(document->text, &after_existing_use);
     661            4 :         import_text = lsp_symbol_import_text(kind, fqcn, after_existing_use);
     662            4 :         array_init(&text_edit);
     663            4 :         lsp_range_from_offsets(document->text, insert_offset, insert_offset, &range);
     664            4 :         add_assoc_zval(&text_edit, "range", &range);
     665            4 :         add_assoc_str(&text_edit, "newText", import_text);
     666            4 :         array_init(&text_edits);
     667            4 :         add_next_index_zval(&text_edits, &text_edit);
     668            4 :         array_init(&changes);
     669            4 :         add_assoc_zval_ex(&changes, ZSTR_VAL(document->uri), ZSTR_LEN(document->uri), &text_edits);
     670            4 :         array_init(&edit);
     671            4 :         add_assoc_zval(&edit, "changes", &changes);
     672            4 :         title = strpprintf(0, "Import %.*s", (int) fqcn_length, fqcn);
     673            4 :         array_init(&action);
     674            4 :         add_assoc_str(&action, "title", title);
     675            4 :         add_assoc_string(&action, "kind", "quickfix");
     676            4 :         add_assoc_zval(&action, "edit", &edit);
     677            4 :         add_next_index_zval(actions, &action);
     678              : }
     679              : 
     680            8 : static inline void lsp_refactor_add_import_actions(lsp_server *server, zval *actions, lsp_document *document, zend_string *word)
     681              : {
     682              :         const char *label_value;
     683              :         lsp_symbol_index_header *header;
     684              :         uint32_t i, added;
     685              :         size_t fqcn_length, path_length, label_length;
     686              :         char *cursor, *end, kind, *fqcn, *path;
     687              : 
     688            8 :         lsp_index_join_worker(server);
     689              : 
     690            8 :         if (!server->symbol_index.available || !server->symbol_index.addr || ZSTR_LEN(word) == 0 || lsp_refactor_word_is_variable(word)) {
     691            0 :                 return;
     692              :         }
     693              : 
     694            8 :         header = (lsp_symbol_index_header *) server->symbol_index.addr;
     695            8 :         if (header->magic != LSP_SYMBOL_INDEX_MAGIC || header->used > header->capacity) {
     696            0 :                 return;
     697              :         }
     698              : 
     699            8 :         added = 0;
     700            8 :         cursor = ((char *) server->symbol_index.addr) + sizeof(lsp_symbol_index_header);
     701            8 :         end = ((char *) server->symbol_index.addr) + header->used;
     702           20 :         for (i = 0; i < header->symbol_count && cursor < end && added < 20; i++) {
     703           12 :                 kind = *cursor++;
     704           12 :                 fqcn = cursor;
     705           12 :                 fqcn_length = strlen(fqcn);
     706           12 :                 path = fqcn + fqcn_length + 1;
     707           12 :                 if (path >= end) {
     708            0 :                         break;
     709              :                 }
     710              : 
     711           12 :                 path_length = strlen(path);
     712           12 :                 cursor = path + path_length + 1;
     713           12 :                 if (cursor > end || lsp_path_value_contains_analysis_helper(path, path_length)) {
     714            0 :                         continue;
     715              :                 }
     716              : 
     717           12 :                 label_value = lsp_basename_from_fqcn(fqcn, fqcn_length, &label_length);
     718           12 :                 if (!lsp_refactor_string_equals_zstr_ci(label_value, label_length, word)) {
     719            8 :                         continue;
     720              :                 }
     721              : 
     722            4 :                 lsp_refactor_add_import_action(actions, document, kind, fqcn, fqcn_length);
     723            4 :                 added++;
     724              :         }
     725              : }
     726              : 
     727           16 : static inline bool lsp_refactor_header_has_name(zend_string *contents, size_t class_start, size_t body_start, const char *keyword, zend_string *target)
     728              : {
     729              :         const char *value, *header_end, *p, *name_start, *name_end;
     730              :         zend_string *raw, *resolved;
     731              :         size_t keyword_end;
     732              :         bool found;
     733              : 
     734           16 :         value = ZSTR_VAL(contents);
     735           16 :         header_end = value + (body_start > 0 ? body_start - 1 : body_start);
     736           16 :         p = value + class_start;
     737           16 :         found = false;
     738          296 :         while (p < header_end) {
     739          284 :                 if (!lsp_keyword_at_slice(value, (size_t) (p - value), (size_t) (header_end - value), keyword, &keyword_end)) {
     740          280 :                         p++;
     741          280 :                         continue;
     742              :                 }
     743              : 
     744            4 :                 p = value + keyword_end;
     745            4 :                 while (p < header_end && *p != '{') {
     746            8 :                         while (p < header_end && (isspace((unsigned char) *p) || *p == ',')) {
     747            4 :                                 p++;
     748              :                         }
     749              : 
     750            4 :                         name_start = p;
     751           36 :                         while (p < header_end && (lsp_doc_is_identifier_char(*p) || *p == '\\')) {
     752           32 :                                 p++;
     753              :                         }
     754            4 :                         name_end = p;
     755            4 :                         if (name_end > name_start) {
     756            4 :                                 raw = zend_string_init(name_start, name_end - name_start, 0);
     757            4 :                                 resolved = lsp_resolve_class_name(contents, raw);
     758            4 :                                 zend_string_release(raw);
     759            4 :                                 if (resolved && lsp_refactor_zstr_equals_ci(resolved, target)) {
     760            4 :                                         zend_string_release(resolved);
     761            4 :                                         found = true;
     762            4 :                                         break;
     763              :                                 }
     764            0 :                                 if (resolved) {
     765            0 :                                         zend_string_release(resolved);
     766              :                                 }
     767              :                         }
     768              : 
     769            0 :                         while (p < header_end && *p != ',' && *p != '{') {
     770            0 :                                 p++;
     771              :                         }
     772            0 :                         if (p < header_end && *p == '{') {
     773            0 :                                 break;
     774              :                         }
     775              :                 }
     776            4 :                 break;
     777              :         }
     778              : 
     779           16 :         return found;
     780              : }
     781              : 
     782            8 : static inline bool lsp_refactor_class_matches_implementation(zend_string *contents, zend_string *candidate, zend_string *target)
     783              : {
     784              :         zend_long body_depth;
     785              :         size_t class_start, body_start, body_end;
     786              : 
     787            8 :         body_depth = 0;
     788            8 :         class_start = 0;
     789            8 :         body_start = 0;
     790            8 :         body_end = 0;
     791            8 :         if (!lsp_find_class_header_for_name(contents, candidate, &class_start, &body_start, &body_end, &body_depth)) {
     792            0 :                 return false;
     793              :         }
     794              : 
     795           16 :         return lsp_refactor_header_has_name(contents, class_start, body_start, "extends", target) ||
     796            8 :                 lsp_refactor_header_has_name(contents, class_start, body_start, "implements", target)
     797              :         ;
     798              : }
     799              : 
     800            4 : static inline bool lsp_refactor_class_location(zend_string *path, zend_string *contents, zend_string *class_name, zval *location)
     801              : {
     802              :         zend_long body_depth;
     803              :         zval tokens_zv, *token, *name_token;
     804              :         HashTable *tokens;
     805              :         uint32_t i, count, name_index;
     806              :         size_t class_start, body_start, body_end, name_start, name_end;
     807              :         bool found;
     808              : 
     809            4 :         body_depth = 0;
     810            4 :         class_start = 0;
     811            4 :         body_start = 0;
     812            4 :         body_end = 0;
     813            4 :         found = false;
     814            4 :         ZVAL_UNDEF(&tokens_zv);
     815            4 :         lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
     816            4 :         if (Z_TYPE(tokens_zv) == IS_ARRAY && lsp_find_class_header_for_name(contents, class_name, &class_start, &body_start, &body_end, &body_depth)) {
     817            4 :                 tokens = Z_ARRVAL(tokens_zv);
     818            4 :                 count = zend_hash_num_elements(tokens);
     819           36 :                 for (i = 0; i < count; i++) {
     820           36 :                         token = zend_hash_index_find(tokens, i);
     821           36 :                         if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_is_class_like(token)) {
     822           32 :                                 continue;
     823              :                         }
     824              : 
     825            4 :                         if (!lsp_token_in_bounds(token, class_start, body_start)) {
     826            0 :                                 continue;
     827              :                         }
     828              : 
     829            4 :                         name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
     830            4 :                         if (!name_token) {
     831            0 :                                 continue;
     832              :                         }
     833              : 
     834            4 :                         name_start = (size_t) lsp_token_long(name_token, "offset", lsp_token_long(token, "offset", 0));
     835            4 :                         name_end = name_start + (size_t) lsp_token_long(name_token, "length", 1);
     836            4 :                         lsp_refactor_location_from_offsets(path, contents, name_start, name_end, location);
     837            4 :                         found = true;
     838            4 :                         break;
     839              :                 }
     840              :         }
     841              : 
     842            4 :         if (!Z_ISUNDEF(tokens_zv)) {
     843            4 :                 zval_ptr_dtor(&tokens_zv);
     844              :         }
     845              : 
     846            4 :         return found;
     847              : }
     848              : 
     849            4 : static inline void lsp_refactor_add_implementation_locations_ex(lsp_server *server, zval *locations, zend_string *target, zend_string *method)
     850              : {
     851              :         lsp_symbol_index_header *header;
     852              :         lsp_document *document;
     853              :         zend_string *candidate, *path_string, *contents;
     854              :         zval location;
     855              :         uint32_t i;
     856              :         size_t fqcn_length, path_length;
     857              :         char *cursor, *end, kind, *fqcn, *path;
     858              :         bool owns_contents;
     859              : 
     860            4 :         lsp_index_join_worker(server);
     861              : 
     862            4 :         if (!server->symbol_index.available || !server->symbol_index.addr) {
     863            0 :                 return;
     864              :         }
     865              : 
     866            4 :         header = (lsp_symbol_index_header *) server->symbol_index.addr;
     867            4 :         if (header->magic != LSP_SYMBOL_INDEX_MAGIC || header->used > header->capacity) {
     868            0 :                 return;
     869              :         }
     870              : 
     871            4 :         cursor = ((char *) server->symbol_index.addr) + sizeof(lsp_symbol_index_header);
     872            4 :         end = ((char *) server->symbol_index.addr) + header->used;
     873           16 :         for (i = 0; i < header->symbol_count && cursor < end; i++) {
     874           12 :                 kind = *cursor++;
     875           12 :                 fqcn = cursor;
     876           12 :                 fqcn_length = strlen(fqcn);
     877           12 :                 path = fqcn + fqcn_length + 1;
     878           12 :                 if (path >= end) {
     879            0 :                         break;
     880              :                 }
     881              : 
     882           12 :                 path_length = strlen(path);
     883           12 :                 cursor = path + path_length + 1;
     884           12 :                 if (cursor > end ||
     885           16 :                         (kind != LSP_SYMBOL_CLASS && kind != LSP_SYMBOL_INTERFACE) ||
     886           12 :                         lsp_path_value_contains_analysis_helper(path, path_length)
     887              :                 ) {
     888            0 :                         continue;
     889              :                 }
     890              : 
     891           12 :                 candidate = zend_string_init(fqcn, fqcn_length, 0);
     892           12 :                 if (lsp_refactor_zstr_equals_ci(candidate, target)) {
     893            4 :                         zend_string_release(candidate);
     894            4 :                         continue;
     895              :                 }
     896              : 
     897            8 :                 path_string = zend_string_init(path, path_length, 0);
     898            8 :                 document = lsp_document_for_path(server, path_string);
     899            8 :                 contents = document ? zend_string_copy(document->text) : lsp_read_file(path_string);
     900            8 :                 owns_contents = document || contents != zend_empty_string;
     901            8 :                 if (owns_contents && lsp_refactor_class_matches_implementation(contents, candidate, target)) {
     902            4 :                         ZVAL_UNDEF(&location);
     903            8 :                         if (method
     904            0 :                                 ? lsp_project_method_definition_for_class(server, candidate, method, &location, 0)
     905            4 :                                 : lsp_refactor_class_location(path_string, contents, candidate, &location)
     906              :                         ) {
     907            4 :                                 add_next_index_zval(locations, &location);
     908            0 :                         } else if (!Z_ISUNDEF(location)) {
     909            0 :                                 zval_ptr_dtor(&location);
     910              :                         }
     911              :                 }
     912              : 
     913            8 :                 if (owns_contents) {
     914            8 :                         zend_string_release(contents);
     915              :                 }
     916            8 :                 zend_string_release(path_string);
     917            8 :                 zend_string_release(candidate);
     918              :         }
     919              : }
     920              : 
     921           20 : static inline uint32_t lsp_refactor_add_text_edits_for_word(zval *text_edits, zend_string *contents, zend_string *word, zend_string *new_text)
     922              : {
     923              :         zval tokens_zv, *token, edit, range;
     924              :         HashTable *tokens;
     925              :         uint32_t i, count, added;
     926              :         size_t start, end;
     927              :         bool variable;
     928              : 
     929           20 :         variable = lsp_refactor_word_is_variable(word);
     930           20 :         added = 0;
     931           20 :         ZVAL_UNDEF(&tokens_zv);
     932           20 :         lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
     933           20 :         if (Z_TYPE(tokens_zv) != IS_ARRAY) {
     934            0 :                 if (!Z_ISUNDEF(tokens_zv)) {
     935            0 :                         zval_ptr_dtor(&tokens_zv);
     936              :                 }
     937              : 
     938            0 :                 return 0;
     939              :         }
     940              : 
     941           20 :         tokens = Z_ARRVAL(tokens_zv);
     942           20 :         count = zend_hash_num_elements(tokens);
     943          748 :         for (i = 0; i < count; i++) {
     944          728 :                 token = zend_hash_index_find(tokens, i);
     945          728 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_refactor_token_match(token, word, variable, &start, &end)) {
     946          716 :                         continue;
     947              :                 }
     948              : 
     949           12 :                 array_init(&edit);
     950           12 :                 lsp_range_from_offsets(contents, start, end, &range);
     951           12 :                 add_assoc_zval(&edit, "range", &range);
     952           12 :                 add_assoc_str(&edit, "newText", zend_string_copy(new_text));
     953           12 :                 add_next_index_zval(text_edits, &edit);
     954           12 :                 added++;
     955              :         }
     956              : 
     957           20 :         zval_ptr_dtor(&tokens_zv);
     958              : 
     959           20 :         return added;
     960              : }
     961              : 
     962           20 : static inline void lsp_refactor_add_file_rename_changes(zval *changes, zend_string *uri, zend_string *contents, zend_string *word, zend_string *new_text)
     963              : {
     964              :         zval text_edits;
     965              : 
     966           20 :         array_init(&text_edits);
     967           20 :         if (lsp_refactor_add_text_edits_for_word(&text_edits, contents, word, new_text) == 0) {
     968            8 :                 zval_ptr_dtor(&text_edits);
     969            8 :                 return;
     970              :         }
     971              : 
     972           12 :         add_assoc_zval_ex(changes, ZSTR_VAL(uri), ZSTR_LEN(uri), &text_edits);
     973              : }
     974              : 
     975              : /* ------------------------------------------------------------------------
     976              :  * Type-directed method rename. A method rename applies within one
     977              :  * inheritance family (declaring class, its ancestors/interfaces/traits, and
     978              :  * every class connected to them), never to unrelated classes that merely
     979              :  * declare a same-named method. Receivers that cannot be resolved keep the
     980              :  * historical rename-by-name behavior so call sites are not silently missed.
     981              :  * ------------------------------------------------------------------------ */
     982              : 
     983           12 : static inline void lsp_rename_set_add(HashTable *set, zend_string *fqcn)
     984              : {
     985           12 :         zend_string *key = zend_string_tolower(fqcn);
     986              : 
     987           12 :         zend_hash_add_empty_element(set, key);
     988           12 :         zend_string_release(key);
     989           12 : }
     990              : 
     991           12 : static inline bool lsp_rename_set_has(HashTable *set, zend_string *fqcn)
     992              : {
     993           12 :         zend_string *key = zend_string_tolower(fqcn);
     994           12 :         bool exists = zend_hash_exists(set, key);
     995              : 
     996           12 :         zend_string_release(key);
     997              : 
     998           12 :         return exists;
     999              : }
    1000              : 
    1001              : /* Parse comma-separated names after `keyword` in a class header slice and
    1002              :  * add their resolved FQCNs. */
    1003              : static inline void lsp_rename_collect_header_names(lsp_server *server, zend_string *contents, size_t class_start, size_t body_start, const char *keyword, HashTable *out, uint32_t depth);
    1004              : 
    1005           12 : static void lsp_rename_collect_ancestors(lsp_server *server, zend_string *class_fqcn, HashTable *out, uint32_t depth)
    1006              : {
    1007              :         lsp_document *open_document;
    1008           12 :         zend_long body_depth = 0;
    1009              :         zend_string *path, *contents, *trait_name;
    1010              :         zval traits, *trait_zv;
    1011           12 :         size_t class_start = 0, body_start = 0, body_end = 0;
    1012              :         bool owns_contents;
    1013              : 
    1014           12 :         if (depth > 8 || lsp_rename_set_has(out, class_fqcn)) {
    1015            0 :                 return;
    1016              :         }
    1017              : 
    1018           12 :         lsp_rename_set_add(out, class_fqcn);
    1019              : 
    1020           12 :         path = lsp_find_project_symbol_path(server, LSP_SYMBOL_CLASS, class_fqcn);
    1021           12 :         if (!path) {
    1022            0 :                 return;
    1023              :         }
    1024              : 
    1025           12 :         open_document = lsp_document_for_path(server, path);
    1026           12 :         contents = open_document ? zend_string_copy(open_document->text) : lsp_read_file(path);
    1027           12 :         owns_contents = open_document || contents != zend_empty_string;
    1028           12 :         zend_string_release(path);
    1029           12 :         if (!owns_contents || contents == zend_empty_string) {
    1030            0 :                 if (owns_contents) {
    1031            0 :                         zend_string_release(contents);
    1032              :                 }
    1033              : 
    1034            0 :                 return;
    1035              :         }
    1036              : 
    1037           12 :         if (lsp_find_class_header_for_name(contents, class_fqcn, &class_start, &body_start, &body_end, &body_depth)) {
    1038           12 :                 lsp_rename_collect_header_names(server, contents, class_start, body_start, "extends", out, depth);
    1039           12 :                 lsp_rename_collect_header_names(server, contents, class_start, body_start, "implements", out, depth);
    1040              : 
    1041           12 :                 array_init(&traits);
    1042           12 :                 lsp_collect_class_trait_names_in_bounds(contents, body_start, body_end, body_depth, &traits);
    1043           12 :                 ZEND_HASH_FOREACH_VAL(Z_ARRVAL(traits), trait_zv) {
    1044            0 :                         if (Z_TYPE_P(trait_zv) == IS_STRING) {
    1045            0 :                                 trait_name = Z_STR_P(trait_zv);
    1046            0 :                                 lsp_rename_collect_ancestors(server, trait_name, out, depth + 1);
    1047              :                         }
    1048              :                 } ZEND_HASH_FOREACH_END();
    1049           12 :                 zval_ptr_dtor(&traits);
    1050              :         }
    1051              : 
    1052           12 :         zend_string_release(contents);
    1053              : }
    1054              : 
    1055           24 : static inline void lsp_rename_collect_header_names(lsp_server *server, zend_string *contents, size_t class_start, size_t body_start, const char *keyword, HashTable *out, uint32_t depth)
    1056              : {
    1057           24 :         const char *value = ZSTR_VAL(contents), *name_start;
    1058              :         zend_string *raw, *resolved;
    1059              :         size_t p, header_end, keyword_end, name_end;
    1060              : 
    1061           24 :         header_end = body_start > 0 ? body_start - 1 : body_start;
    1062          304 :         for (p = class_start; p < header_end; p++) {
    1063          280 :                 if (!lsp_keyword_at_slice(value, p, header_end, keyword, &keyword_end)) {
    1064          280 :                         continue;
    1065              :                 }
    1066              : 
    1067            0 :                 p = keyword_end;
    1068              :                 for (;;) {
    1069            0 :                         while (p < header_end && (isspace((unsigned char) value[p]) || value[p] == ',')) {
    1070            0 :                                 p++;
    1071              :                         }
    1072              : 
    1073            0 :                         name_start = value + p;
    1074            0 :                         while (p < header_end && (lsp_doc_is_identifier_char(value[p]) || value[p] == '\\')) {
    1075            0 :                                 p++;
    1076              :                         }
    1077            0 :                         name_end = p;
    1078            0 :                         if (value + name_end <= name_start) {
    1079            0 :                                 break;
    1080              :                         }
    1081              : 
    1082            0 :                         raw = zend_string_init(name_start, (size_t) (value + name_end - name_start), 0);
    1083            0 :                         resolved = lsp_resolve_class_name(contents, raw);
    1084            0 :                         zend_string_release(raw);
    1085            0 :                         if (resolved) {
    1086            0 :                                 lsp_rename_collect_ancestors(server, resolved, out, depth + 1);
    1087            0 :                                 zend_string_release(resolved);
    1088              :                         }
    1089              : 
    1090            0 :                         while (p < header_end && isspace((unsigned char) value[p])) {
    1091            0 :                                 p++;
    1092              :                         }
    1093            0 :                         if (p >= header_end || value[p] != ',') {
    1094              :                                 break;
    1095              :                         }
    1096              :                 }
    1097              : 
    1098            0 :                 return;
    1099              :         }
    1100              : }
    1101              : 
    1102              : /* Is `candidate` in the family: itself or any ancestor intersects owner_set? */
    1103           16 : static inline bool lsp_rename_class_in_family(lsp_server *server, zend_string *candidate, HashTable *owner_set, HashTable *cache)
    1104              : {
    1105              :         HashTable *ancestors;
    1106              :         zend_string *key, *name;
    1107              :         zval *cached, ptr;
    1108           16 :         bool related = false;
    1109              : 
    1110           16 :         key = zend_string_tolower(candidate);
    1111           16 :         cached = zend_hash_find(cache, key);
    1112           16 :         if (cached) {
    1113            8 :                 ancestors = (HashTable *) Z_PTR_P(cached);
    1114              :         } else {
    1115            8 :                 ancestors = emalloc(sizeof(HashTable));
    1116            8 :                 zend_hash_init(ancestors, 8, NULL, NULL, 0);
    1117            8 :                 lsp_rename_collect_ancestors(server, candidate, ancestors, 0);
    1118            8 :                 ZVAL_PTR(&ptr, ancestors);
    1119            8 :                 zend_hash_update(cache, key, &ptr);
    1120              :         }
    1121           16 :         zend_string_release(key);
    1122              : 
    1123           24 :         ZEND_HASH_FOREACH_STR_KEY(ancestors, name) {
    1124           16 :                 if (name && zend_hash_exists(owner_set, name)) {
    1125            8 :                         related = true;
    1126            8 :                         break;
    1127              :                 }
    1128              :         } ZEND_HASH_FOREACH_END();
    1129              : 
    1130           16 :         return related;
    1131              : }
    1132              : 
    1133              : /* The class whose method the cursor targets, or NULL when the cursor is not
    1134              :  * on a method declaration/access. */
    1135            8 : static inline zend_string *lsp_rename_method_owner(lsp_server *server, lsp_document *document, size_t offset, zend_string *word)
    1136              : {
    1137            8 :         const char *value = ZSTR_VAL(document->text);
    1138            8 :         zend_long body_depth = 0;
    1139              :         zend_string *owner, *member_prefix;
    1140            8 :         size_t word_start, before, kw_start, class_start = 0, body_start = 0, body_end = 0;
    1141              :         bool public_only;
    1142              : 
    1143            8 :         word_start = offset > ZSTR_LEN(document->text) ? ZSTR_LEN(document->text) : offset;
    1144           24 :         while (word_start > 0 && lsp_doc_is_identifier_char(value[word_start - 1])) {
    1145           16 :                 word_start--;
    1146              :         }
    1147              : 
    1148            8 :         before = word_start;
    1149           16 :         while (before > 0 && isspace((unsigned char) value[before - 1])) {
    1150            8 :                 before--;
    1151              :         }
    1152              : 
    1153              :         /* Declaration: `function word`. */
    1154            8 :         kw_start = before;
    1155           60 :         while (kw_start > 0 && lsp_doc_is_identifier_char(value[kw_start - 1])) {
    1156           52 :                 kw_start--;
    1157              :         }
    1158            8 :         if (before - kw_start == sizeof("function") - 1 &&
    1159            4 :                 strncasecmp(value + kw_start, "function", sizeof("function") - 1) == 0
    1160              :         ) {
    1161            4 :                 if (!lsp_find_enclosing_class_header(document->text, offset, &class_start, &body_start, &body_end, &body_depth)) {
    1162            0 :                         return NULL;
    1163              :                 }
    1164              : 
    1165            4 :                 return lsp_class_declared_name(document->text, class_start, body_start);
    1166              :         }
    1167              : 
    1168              :         /* Instance access: `...->word`. */
    1169            4 :         if (before >= 2 && value[before - 1] == '>' && (value[before - 2] == '-' ||
    1170            0 :                 (before >= 3 && value[before - 2] == '?' && value[before - 3] == '-'))
    1171              :         ) {
    1172            0 :                 owner = NULL;
    1173            0 :                 if (lsp_member_access_class_context(server, document, word_start + ZSTR_LEN(word), word, &owner, &member_prefix)) {
    1174            0 :                         zend_string_release(member_prefix);
    1175              : 
    1176            0 :                         return owner;
    1177              :                 }
    1178              : 
    1179            0 :                 if (lsp_find_enclosing_class_header(document->text, offset, &class_start, &body_start, &body_end, &body_depth)) {
    1180            0 :                         return lsp_class_declared_name(document->text, class_start, body_start);
    1181              :                 }
    1182              : 
    1183            0 :                 return NULL;
    1184              :         }
    1185              : 
    1186              :         /* Static access: `Receiver::word`. */
    1187            4 :         if (before >= 2 && value[before - 1] == ':' && value[before - 2] == ':') {
    1188            0 :                 if (lsp_static_member_receiver_class(document, offset, word, &owner, &public_only)) {
    1189            0 :                         return owner;
    1190              :                 }
    1191              :         }
    1192              : 
    1193            4 :         return NULL;
    1194              : }
    1195              : 
    1196              : /* Receiver-aware method rename inside one file's contents. */
    1197           12 : static inline void lsp_rename_add_method_changes_in_contents(lsp_server *server, zval *changes, lsp_document *open_document, zend_string *uri, zend_string *contents, zend_string *word, zend_string *new_text, HashTable *owner_set, HashTable *cache)
    1198              : {
    1199           12 :         const char *value = ZSTR_VAL(contents);
    1200           12 :         zend_long body_depth = 0;
    1201              :         zend_string *text, *receiver, *enclosing, *resolved;
    1202              :         zval tokens_zv, *token, *previous, text_edits, edit, range;
    1203              :         HashTable *tokens;
    1204           12 :         uint32_t i, count, added = 0;
    1205           12 :         size_t token_offset, recv_end, recv_start, class_start = 0, body_start = 0, body_end = 0;
    1206              :         bool include;
    1207              : 
    1208           12 :         ZVAL_UNDEF(&tokens_zv);
    1209           12 :         lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
    1210           12 :         if (Z_TYPE(tokens_zv) != IS_ARRAY) {
    1211            0 :                 if (!Z_ISUNDEF(tokens_zv)) {
    1212            0 :                         zval_ptr_dtor(&tokens_zv);
    1213              :                 }
    1214              : 
    1215            4 :                 return;
    1216              :         }
    1217              : 
    1218           12 :         array_init(&text_edits);
    1219           12 :         tokens = Z_ARRVAL(tokens_zv);
    1220           12 :         count = zend_hash_num_elements(tokens);
    1221          628 :         for (i = 0; i < count; i++) {
    1222          616 :                 token = zend_hash_index_find(tokens, i);
    1223          616 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_STRING")) {
    1224          536 :                         continue;
    1225              :                 }
    1226              : 
    1227           80 :                 text = lsp_token_string(token, "text");
    1228           80 :                 if (!text ||
    1229           80 :                         ZSTR_LEN(text) != ZSTR_LEN(word) ||
    1230           20 :                         strncasecmp(ZSTR_VAL(text), ZSTR_VAL(word), ZSTR_LEN(word)) != 0
    1231              :                 ) {
    1232           60 :                         continue;
    1233              :                 }
    1234              : 
    1235           20 :                 token_offset = (size_t) lsp_token_long(token, "offset", 0);
    1236           20 :                 previous = lsp_refactor_prev_significant_token(tokens, i);
    1237           20 :                 include = false;
    1238              : 
    1239           20 :                 if (previous && (lsp_token_name_equals(previous, "T_FUNCTION") || lsp_token_is_char(previous, '&'))) {
    1240              :                         /* Declaration: rename when the declaring class is family. */
    1241           16 :                         if (lsp_find_enclosing_class_header(contents, token_offset, &class_start, &body_start, &body_end, &body_depth)) {
    1242            8 :                                 enclosing = lsp_class_declared_name(contents, class_start, body_start);
    1243            8 :                                 if (enclosing) {
    1244            8 :                                         include = lsp_rename_class_in_family(server, enclosing, owner_set, cache);
    1245            8 :                                         zend_string_release(enclosing);
    1246              :                                 }
    1247              :                         }
    1248           12 :                 } else if (previous && lsp_refactor_token_is_object_operator(previous)) {
    1249           12 :                         recv_end = (size_t) lsp_token_long(previous, "offset", 0);
    1250           12 :                         while (recv_end > 0 && isspace((unsigned char) value[recv_end - 1])) {
    1251            0 :                                 recv_end--;
    1252              :                         }
    1253           12 :                         recv_start = recv_end;
    1254           76 :                         while (recv_start > 0 && (lsp_doc_is_identifier_char(value[recv_start - 1]) || value[recv_start - 1] == '$')) {
    1255           64 :                                 recv_start--;
    1256              :                         }
    1257              : 
    1258           12 :                         if (recv_end > recv_start && value[recv_start] == '$') {
    1259           12 :                                 receiver = zend_string_init(value + recv_start, recv_end - recv_start, 0);
    1260           12 :                                 if (zend_string_equals_literal(receiver, "$this")) {
    1261            8 :                                         include = true;
    1262            8 :                                         if (lsp_find_enclosing_class_header(contents, token_offset, &class_start, &body_start, &body_end, &body_depth)) {
    1263            8 :                                                 enclosing = lsp_class_declared_name(contents, class_start, body_start);
    1264            8 :                                                 if (enclosing) {
    1265            8 :                                                         include = lsp_rename_class_in_family(server, enclosing, owner_set, cache);
    1266            8 :                                                         zend_string_release(enclosing);
    1267              :                                                 }
    1268              :                                         }
    1269            4 :                                 } else if (open_document) {
    1270            0 :                                         resolved = lsp_infer_receiver_class(server, open_document, receiver, token_offset);
    1271            0 :                                         include = !resolved || lsp_rename_class_in_family(server, resolved, owner_set, cache);
    1272            0 :                                         if (resolved) {
    1273            0 :                                                 zend_string_release(resolved);
    1274              :                                         }
    1275              :                                 } else {
    1276              :                                         /* Unresolvable receiver in a closed file: keep the
    1277              :                                          * historical rename-by-name behavior. */
    1278            4 :                                         include = true;
    1279              :                                 }
    1280           12 :                                 zend_string_release(receiver);
    1281              :                         } else {
    1282            0 :                                 include = true;
    1283              :                         }
    1284            0 :                 } else if (previous && lsp_token_name_equals(previous, "T_DOUBLE_COLON")) {
    1285            0 :                         recv_end = (size_t) lsp_token_long(previous, "offset", 0);
    1286            0 :                         while (recv_end > 0 && isspace((unsigned char) value[recv_end - 1])) {
    1287            0 :                                 recv_end--;
    1288              :                         }
    1289            0 :                         recv_start = recv_end;
    1290            0 :                         while (recv_start > 0 && (lsp_doc_is_identifier_char(value[recv_start - 1]) || value[recv_start - 1] == '\\')) {
    1291            0 :                                 recv_start--;
    1292              :                         }
    1293              : 
    1294            0 :                         resolved = NULL;
    1295            0 :                         if (recv_end > recv_start) {
    1296            0 :                                 receiver = zend_string_init(value + recv_start, recv_end - recv_start, 0);
    1297            0 :                                 if (zend_string_equals_literal_ci(receiver, "self") ||
    1298            0 :                                         zend_string_equals_literal_ci(receiver, "static") ||
    1299            0 :                                         zend_string_equals_literal_ci(receiver, "parent")
    1300              :                                 ) {
    1301            0 :                                         if (lsp_find_enclosing_class_header(contents, token_offset, &class_start, &body_start, &body_end, &body_depth)) {
    1302            0 :                                                 resolved = zend_string_equals_literal_ci(receiver, "parent")
    1303            0 :                                                         ? lsp_class_extends_name(contents, class_start, body_start)
    1304            0 :                                                         : lsp_class_declared_name(contents, class_start, body_start)
    1305              :                                                 ;
    1306              :                                         }
    1307              :                                 } else {
    1308            0 :                                         resolved = lsp_resolve_class_name(contents, receiver);
    1309              :                                 }
    1310            0 :                                 zend_string_release(receiver);
    1311              :                         }
    1312              : 
    1313            0 :                         include = !resolved || lsp_rename_class_in_family(server, resolved, owner_set, cache);
    1314            0 :                         if (resolved) {
    1315            0 :                                 zend_string_release(resolved);
    1316              :                         }
    1317              :                 }
    1318              : 
    1319           20 :                 if (!include) {
    1320            8 :                         continue;
    1321              :                 }
    1322              : 
    1323           12 :                 array_init(&edit);
    1324           12 :                 lsp_range_from_offsets(contents, token_offset, token_offset + ZSTR_LEN(text), &range);
    1325           12 :                 add_assoc_zval(&edit, "range", &range);
    1326           12 :                 add_assoc_str(&edit, "newText", zend_string_copy(new_text));
    1327           12 :                 add_next_index_zval(&text_edits, &edit);
    1328           12 :                 added++;
    1329              :         }
    1330              : 
    1331           12 :         zval_ptr_dtor(&tokens_zv);
    1332              : 
    1333           12 :         if (added == 0) {
    1334            4 :                 zval_ptr_dtor(&text_edits);
    1335              : 
    1336            4 :                 return;
    1337              :         }
    1338              : 
    1339            8 :         add_assoc_zval_ex(changes, ZSTR_VAL(uri), ZSTR_LEN(uri), &text_edits);
    1340              : }
    1341              : 
    1342            4 : static inline void lsp_rename_ancestor_cache_destroy(HashTable *cache)
    1343              : {
    1344              :         zval *entry;
    1345              :         HashTable *ancestors;
    1346              : 
    1347           12 :         ZEND_HASH_FOREACH_VAL(cache, entry) {
    1348            8 :                 ancestors = (HashTable *) Z_PTR_P(entry);
    1349            8 :                 if (ancestors) {
    1350            8 :                         zend_hash_destroy(ancestors);
    1351            8 :                         efree(ancestors);
    1352              :                 }
    1353              :         } ZEND_HASH_FOREACH_END();
    1354            4 :         zend_hash_destroy(cache);
    1355            4 : }
    1356              : 
    1357              : /* Full type-directed method rename across the project. Returns false when
    1358              :  * the cursor is not on a method, leaving the generic rename to run. */
    1359            8 : static inline bool lsp_refactor_add_method_rename_changes(lsp_server *server, zval *changes, lsp_document *primary_document, size_t offset, zend_string *word, zend_string *new_text)
    1360              : {
    1361              :         lsp_symbol_index_header *header;
    1362              :         lsp_document *open_document;
    1363              :         zend_string *owner, *path_string, *uri, *contents;
    1364              :         HashTable owner_set, cache, visited;
    1365              :         uint32_t i;
    1366              :         size_t fqcn_length, path_length;
    1367              :         char *cursor, *end, kind, *fqcn, *path;
    1368              :         bool owns_contents;
    1369              : 
    1370            8 :         owner = lsp_rename_method_owner(server, primary_document, offset, word);
    1371            8 :         if (!owner) {
    1372            4 :                 return false;
    1373              :         }
    1374              : 
    1375            4 :         lsp_index_join_worker(server);
    1376              : 
    1377            4 :         zend_hash_init(&owner_set, 8, NULL, NULL, 0);
    1378            4 :         zend_hash_init(&cache, 8, NULL, ZVAL_PTR_DTOR, 0);
    1379            4 :         lsp_rename_collect_ancestors(server, owner, &owner_set, 0);
    1380            4 :         zend_string_release(owner);
    1381              : 
    1382              :         /* Primary document first. */
    1383            4 :         lsp_rename_add_method_changes_in_contents(server, changes, primary_document, primary_document->uri, primary_document->text, word, new_text, &owner_set, &cache);
    1384              : 
    1385            4 :         if (server->symbol_index.available && server->symbol_index.addr) {
    1386            4 :                 header = (lsp_symbol_index_header *) server->symbol_index.addr;
    1387            4 :                 if (header->magic == LSP_SYMBOL_INDEX_MAGIC && header->used <= header->capacity) {
    1388            4 :                         zend_hash_init(&visited, 128, NULL, NULL, 0);
    1389            4 :                         zend_hash_add_empty_element(&visited, primary_document->path);
    1390            4 :                         cursor = ((char *) server->symbol_index.addr) + sizeof(lsp_symbol_index_header);
    1391            4 :                         end = ((char *) server->symbol_index.addr) + header->used;
    1392           16 :                         for (i = 0; i < header->symbol_count && cursor < end; i++) {
    1393           12 :                                 kind = *cursor++;
    1394           12 :                                 fqcn = cursor;
    1395           12 :                                 fqcn_length = strlen(fqcn);
    1396           12 :                                 path = fqcn + fqcn_length + 1;
    1397           12 :                                 if (path >= end) {
    1398            0 :                                         break;
    1399              :                                 }
    1400              : 
    1401           12 :                                 path_length = strlen(path);
    1402           12 :                                 cursor = path + path_length + 1;
    1403              :                                 (void) kind;
    1404              :                                 (void) fqcn_length;
    1405           12 :                                 if (cursor > end || lsp_path_value_contains_analysis_helper(path, path_length) || lsp_path_value_contains_vendor(path, path_length)) {
    1406            0 :                                         continue;
    1407              :                                 }
    1408              : 
    1409           12 :                                 path_string = zend_string_init(path, path_length, 0);
    1410           12 :                                 if (zend_hash_exists(&visited, path_string)) {
    1411            4 :                                         zend_string_release(path_string);
    1412            4 :                                         continue;
    1413              :                                 }
    1414            8 :                                 zend_hash_add_empty_element(&visited, path_string);
    1415              : 
    1416            8 :                                 open_document = lsp_document_for_path(server, path_string);
    1417            8 :                                 contents = open_document ? zend_string_copy(open_document->text) : lsp_read_file(path_string);
    1418            8 :                                 owns_contents = open_document || contents != zend_empty_string;
    1419            8 :                                 if (owns_contents && contents != zend_empty_string) {
    1420            8 :                                         uri = lsp_uri_from_path(path_string);
    1421            8 :                                         lsp_rename_add_method_changes_in_contents(server, changes, open_document, uri, contents, word, new_text, &owner_set, &cache);
    1422            8 :                                         zend_string_release(uri);
    1423              :                                 }
    1424            8 :                                 if (owns_contents) {
    1425            8 :                                         zend_string_release(contents);
    1426              :                                 }
    1427            8 :                                 zend_string_release(path_string);
    1428              :                         }
    1429            4 :                         zend_hash_destroy(&visited);
    1430              :                 }
    1431              :         }
    1432              : 
    1433            4 :         zend_hash_destroy(&owner_set);
    1434            4 :         lsp_rename_ancestor_cache_destroy(&cache);
    1435              : 
    1436            4 :         return true;
    1437              : }
    1438              : 
    1439            4 : static inline void lsp_refactor_add_project_rename_changes(lsp_server *server, zval *changes, lsp_document *primary_document, zend_string *word, zend_string *new_text)
    1440              : {
    1441              :         lsp_symbol_index_header *header;
    1442              :         lsp_document *document;
    1443              :         zend_string *path_string, *uri, *contents;
    1444              :         HashTable visited;
    1445              :         uint32_t i;
    1446              :         size_t fqcn_length, path_length;
    1447              :         char *cursor, *end, kind, *fqcn, *path;
    1448              :         bool owns_contents;
    1449              : 
    1450            4 :         lsp_index_join_worker(server);
    1451              : 
    1452            4 :         if (!server->symbol_index.available || !server->symbol_index.addr) {
    1453            0 :                 return;
    1454              :         }
    1455              : 
    1456            4 :         header = (lsp_symbol_index_header *) server->symbol_index.addr;
    1457            4 :         if (header->magic != LSP_SYMBOL_INDEX_MAGIC || header->used > header->capacity) {
    1458            0 :                 return;
    1459              :         }
    1460              : 
    1461            4 :         zend_hash_init(&visited, 128, NULL, NULL, 0);
    1462            4 :         zend_hash_add_empty_element(&visited, primary_document->path);
    1463            4 :         cursor = ((char *) server->symbol_index.addr) + sizeof(lsp_symbol_index_header);
    1464            4 :         end = ((char *) server->symbol_index.addr) + header->used;
    1465           24 :         for (i = 0; i < header->symbol_count && cursor < end; i++) {
    1466           20 :                 kind = *cursor++;
    1467           20 :                 fqcn = cursor;
    1468           20 :                 fqcn_length = strlen(fqcn);
    1469           20 :                 path = fqcn + fqcn_length + 1;
    1470           20 :                 if (path >= end) {
    1471            0 :                         break;
    1472              :                 }
    1473              : 
    1474           20 :                 path_length = strlen(path);
    1475           20 :                 cursor = path + path_length + 1;
    1476           20 :                 if (cursor > end || lsp_path_value_contains_analysis_helper(path, path_length) || lsp_path_value_contains_vendor(path, path_length)) {
    1477            0 :                         continue;
    1478              :                 }
    1479              : 
    1480           20 :                 path_string = zend_string_init(path, path_length, 0);
    1481           20 :                 if (zend_hash_exists(&visited, path_string)) {
    1482            4 :                         zend_string_release(path_string);
    1483            4 :                         continue;
    1484              :                 }
    1485              : 
    1486           16 :                 zend_hash_add_empty_element(&visited, path_string);
    1487              : 
    1488           16 :                 document = lsp_document_for_path(server, path_string);
    1489           16 :                 contents = document ? zend_string_copy(document->text) : lsp_read_file(path_string);
    1490           16 :                 owns_contents = document || contents != zend_empty_string;
    1491           16 :                 if (owns_contents) {
    1492           16 :                         uri = lsp_uri_from_path(path_string);
    1493           16 :                         lsp_refactor_add_file_rename_changes(changes, uri, contents, word, new_text);
    1494           16 :                         zend_string_release(uri);
    1495           16 :                         zend_string_release(contents);
    1496              :                 }
    1497              : 
    1498           16 :                 zend_string_release(path_string);
    1499              :         }
    1500              : 
    1501            4 :         zend_hash_destroy(&visited);
    1502              : }
    1503              : 
    1504           12 : static inline zend_string *lsp_refactor_rename_text(zend_string *word, zend_string *new_name)
    1505              : {
    1506           12 :         if (lsp_refactor_word_is_variable(word) && (ZSTR_LEN(new_name) == 0 || ZSTR_VAL(new_name)[0] != '$')) {
    1507            4 :                 return strpprintf(0, "$%s", ZSTR_VAL(new_name));
    1508              :         }
    1509              : 
    1510            8 :         return zend_string_copy(new_name);
    1511              : }
    1512              : 
    1513           60 : static inline zend_string *lsp_inlay_key(zend_string *name)
    1514              : {
    1515              :         zend_string *key;
    1516              :         size_t i;
    1517              : 
    1518           60 :         key = zend_string_alloc(ZSTR_LEN(name), 0);
    1519              : 
    1520          456 :         for (i = 0; i < ZSTR_LEN(name); i++) {
    1521          396 :                 ZSTR_VAL(key)[i] = (char) tolower((unsigned char) ZSTR_VAL(name)[i]);
    1522              :         }
    1523              : 
    1524           60 :         ZSTR_VAL(key)[ZSTR_LEN(name)] = '\0';
    1525              : 
    1526           60 :         return key;
    1527              : }
    1528              : 
    1529           60 : static inline zval *lsp_inlay_next_non_whitespace(HashTable *tokens, uint32_t start, uint32_t *index)
    1530              : {
    1531              :         zval *token;
    1532              :         uint32_t i, count;
    1533              : 
    1534           60 :         count = zend_hash_num_elements(tokens);
    1535           80 :         for (i = start; i < count; i++) {
    1536           80 :                 token = zend_hash_index_find(tokens, i);
    1537           80 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || lsp_token_name_equals(token, "T_WHITESPACE")) {
    1538           20 :                         continue;
    1539              :                 }
    1540              : 
    1541           60 :                 *index = i;
    1542              : 
    1543           60 :                 return token;
    1544              :         }
    1545              : 
    1546            0 :         return NULL;
    1547              : }
    1548              : 
    1549           84 : static inline bool lsp_inlay_token_is_call_name(HashTable *tokens, uint32_t index)
    1550              : {
    1551              :         zval *previous;
    1552              :         uint32_t previous_index;
    1553              : 
    1554           84 :         if (index == 0) {
    1555            0 :                 return true;
    1556              :         }
    1557              : 
    1558           84 :         previous = NULL;
    1559           84 :         previous_index = 0;
    1560          148 :         while (index > 0) {
    1561          148 :                 index--;
    1562              : 
    1563          148 :                 previous = zend_hash_index_find(tokens, index);
    1564          148 :                 if (!previous || Z_TYPE_P(previous) != IS_ARRAY || lsp_token_name_equals(previous, "T_WHITESPACE")) {
    1565           64 :                         continue;
    1566              :                 }
    1567              : 
    1568           84 :                 previous_index = index;
    1569           84 :                 break;
    1570              :         }
    1571              : 
    1572           84 :         if (!previous) {
    1573            0 :                 return true;
    1574              :         }
    1575              : 
    1576              :         (void) previous_index;
    1577           84 :         return !lsp_token_name_equals(previous, "T_FUNCTION") &&
    1578           64 :                 !lsp_token_name_equals(previous, "T_CLASS") &&
    1579           56 :                 !lsp_token_name_equals(previous, "T_INTERFACE") &&
    1580           56 :                 !lsp_token_name_equals(previous, "T_TRAIT") &&
    1581           56 :                 !lsp_token_name_equals(previous, "T_ENUM") &&
    1582          196 :                 !lsp_token_name_equals(previous, "T_NEW") &&
    1583           48 :                 !lsp_token_name_equals(previous, "T_NS_SEPARATOR")
    1584              :         ;
    1585              : }
    1586              : 
    1587            8 : static inline bool lsp_inlay_find_call_parens(HashTable *tokens, uint32_t name_index, uint32_t *open_index, size_t *open_offset, size_t *close_offset)
    1588              : {
    1589              :         zend_string *text;
    1590              :         zval *token;
    1591              :         uint32_t next_index;
    1592              : 
    1593            8 :         token = lsp_inlay_next_non_whitespace(tokens, name_index + 1, &next_index);
    1594            8 :         if (!token || !lsp_token_is_char(token, '(')) {
    1595            0 :                 return false;
    1596              :         }
    1597              : 
    1598            8 :         text = lsp_token_string(token, "text");
    1599            8 :         if (!text || ZSTR_LEN(text) != 1) {
    1600            0 :                 return false;
    1601              :         }
    1602              : 
    1603            8 :         *open_index = next_index;
    1604            8 :         *open_offset = (size_t) lsp_token_long(token, "offset", 0);
    1605            8 :         *close_offset = 0;
    1606              : 
    1607            8 :         return true;
    1608              : }
    1609              : 
    1610            8 : static inline bool lsp_inlay_find_close_paren(zend_string *text, size_t open_offset, size_t *close_offset)
    1611              : {
    1612              :         const char *value;
    1613              :         uint32_t depth;
    1614              :         size_t i;
    1615              :         bool escaped;
    1616              :         char quote;
    1617              : 
    1618            8 :         value = ZSTR_VAL(text);
    1619            8 :         depth = 0;
    1620            8 :         quote = '\0';
    1621            8 :         escaped = false;
    1622           88 :         for (i = open_offset; i < ZSTR_LEN(text); i++) {
    1623           88 :                 if (quote != '\0') {
    1624            8 :                         if (escaped) {
    1625            0 :                                 escaped = false;
    1626            8 :                         } else if (value[i] == '\\') {
    1627            0 :                                 escaped = true;
    1628            8 :                         } else if (value[i] == quote) {
    1629            4 :                                 quote = '\0';
    1630              :                         }
    1631            8 :                         continue;
    1632              :                 }
    1633              : 
    1634           80 :                 if (value[i] == '\'' || value[i] == '"') {
    1635            4 :                         quote = value[i];
    1636            4 :                         continue;
    1637              :                 }
    1638              : 
    1639           76 :                 if (value[i] == '(') {
    1640            8 :                         depth++;
    1641           68 :                 } else if (value[i] == ')' && depth > 0) {
    1642            8 :                         depth--;
    1643            8 :                         if (depth == 0) {
    1644            8 :                                 *close_offset = i;
    1645              : 
    1646            8 :                                 return true;
    1647              :                         }
    1648              :                 }
    1649              :         }
    1650              : 
    1651            0 :         return false;
    1652              : }
    1653              : 
    1654           12 : static inline void lsp_inlay_add_param_map_entry(HashTable *param_map, zend_string *name, zval *params)
    1655              : {
    1656              :         zend_string *key;
    1657              : 
    1658           12 :         key = lsp_inlay_key(name);
    1659           12 :         zend_hash_update(param_map, key, params);
    1660           12 :         zend_string_release(key);
    1661           12 : }
    1662              : 
    1663           20 : static inline void lsp_inlay_collect_function_params(HashTable *tokens, uint32_t function_index, uint32_t name_index, HashTable *param_map)
    1664              : {
    1665              :         zend_string *name, *variable, *label;
    1666              :         zval params, *token;
    1667              :         uint32_t i, count;
    1668              :         size_t open_offset, close_offset;
    1669              :         bool saw_open;
    1670              : 
    1671           20 :         name = lsp_token_string(zend_hash_index_find(tokens, name_index), "text");
    1672           20 :         if (!name) {
    1673            8 :                 return;
    1674              :         }
    1675              : 
    1676           20 :         count = zend_hash_num_elements(tokens);
    1677           20 :         open_offset = 0;
    1678           20 :         close_offset = 0;
    1679           20 :         saw_open = false;
    1680           20 :         array_init(&params);
    1681              : 
    1682          120 :         for (i = function_index + 1; i < count; i++) {
    1683          120 :                 token = zend_hash_index_find(tokens, i);
    1684          120 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
    1685            0 :                         continue;
    1686              :                 }
    1687              : 
    1688          120 :                 if (!saw_open) {
    1689           60 :                         if (lsp_token_is_char(token, '(')) {
    1690           20 :                                 open_offset = (size_t) lsp_token_long(token, "offset", 0);
    1691           20 :                                 saw_open = true;
    1692              :                         }
    1693           60 :                         continue;
    1694              :                 }
    1695              : 
    1696           60 :                 if (lsp_token_is_char(token, ')')) {
    1697           20 :                         close_offset = (size_t) lsp_token_long(token, "offset", 0);
    1698           20 :                         break;
    1699              :                 }
    1700              : 
    1701           40 :                 if (!lsp_token_name_equals(token, "T_VARIABLE")) {
    1702           24 :                         continue;
    1703              :                 }
    1704              : 
    1705           16 :                 if ((size_t) lsp_token_long(token, "offset", 0) <= open_offset) {
    1706            0 :                         continue;
    1707              :                 }
    1708              : 
    1709           16 :                 variable = lsp_token_string(token, "text");
    1710           16 :                 if (!variable || ZSTR_LEN(variable) <= 1) {
    1711            0 :                         continue;
    1712              :                 }
    1713              : 
    1714           16 :                 label = strpprintf(0, "%.*s:", (int) (ZSTR_LEN(variable) - 1), ZSTR_VAL(variable) + 1);
    1715              : 
    1716           16 :                 add_next_index_str(&params, label);
    1717              :         }
    1718              : 
    1719           20 :         if (zend_hash_num_elements(Z_ARRVAL(params)) == 0 || close_offset <= open_offset) {
    1720            8 :                 zval_ptr_dtor(&params);
    1721              : 
    1722            8 :                 return;
    1723              :         }
    1724              : 
    1725           12 :         lsp_inlay_add_param_map_entry(param_map, name, &params);
    1726              : }
    1727              : 
    1728            8 : static inline void lsp_inlay_collect_param_map(lsp_document *document, HashTable *param_map)
    1729              : {
    1730              :         zval *tokens_zv, *token, *name_token;
    1731              :         HashTable *tokens;
    1732              :         uint32_t i, count, name_index;
    1733              : 
    1734            8 :         tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
    1735            8 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
    1736            0 :                 return;
    1737              :         }
    1738              : 
    1739            8 :         tokens = Z_ARRVAL_P(tokens_zv);
    1740            8 :         count = zend_hash_num_elements(tokens);
    1741          672 :         for (i = 0; i < count; i++) {
    1742          664 :                 token = zend_hash_index_find(tokens, i);
    1743          664 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_FUNCTION")) {
    1744          644 :                         continue;
    1745              :                 }
    1746              : 
    1747           20 :                 name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
    1748           20 :                 if (!name_token) {
    1749            0 :                         continue;
    1750              :                 }
    1751              : 
    1752           20 :                 lsp_inlay_collect_function_params(tokens, i, name_index, param_map);
    1753              :         }
    1754              : }
    1755              : 
    1756           12 : static inline void lsp_inlay_add_hint(zval *items, zend_string *text, size_t offset, zend_string *label)
    1757              : {
    1758              :         zval hint, range, position, *start;
    1759              : 
    1760           12 :         array_init(&hint);
    1761           12 :         lsp_range_from_offsets(text, offset, offset, &range);
    1762           12 :         start = zend_hash_str_find(Z_ARRVAL(range), "start", sizeof("start") - 1);
    1763           12 :         if (start) {
    1764           12 :                 ZVAL_COPY(&position, start);
    1765           12 :                 add_assoc_zval(&hint, "position", &position);
    1766              :         }
    1767              : 
    1768           12 :         add_assoc_str(&hint, "label", zend_string_copy(label));
    1769           12 :         add_assoc_long(&hint, "kind", 2);
    1770           12 :         add_assoc_bool(&hint, "paddingRight", true);
    1771           12 :         add_next_index_zval(items, &hint);
    1772              : 
    1773           12 :         zval_ptr_dtor(&range);
    1774           12 : }
    1775              : 
    1776            8 : static inline void lsp_inlay_add_call_hints(zval *items, zend_string *text, size_t open_offset, size_t close_offset, zval *params)
    1777              : {
    1778              :         const char *value;
    1779              :         zval *label;
    1780              :         size_t i, named_scan;
    1781              :         uint32_t depth, param_index;
    1782              :         bool escaped, need_hint;
    1783              :         char quote;
    1784              : 
    1785            8 :         value = ZSTR_VAL(text);
    1786            8 :         depth = 0;
    1787            8 :         param_index = 0;
    1788            8 :         quote = '\0';
    1789            8 :         escaped = false;
    1790            8 :         need_hint = true;
    1791           80 :         for (i = open_offset + 1; i < close_offset; i++) {
    1792           72 :                 if (quote != '\0') {
    1793            8 :                         if (escaped) {
    1794            0 :                                 escaped = false;
    1795            8 :                         } else if (value[i] == '\\') {
    1796            0 :                                 escaped = true;
    1797            8 :                         } else if (value[i] == quote) {
    1798            4 :                                 quote = '\0';
    1799              :                         }
    1800            8 :                         continue;
    1801              :                 }
    1802              : 
    1803              :                 /* The need_hint check must run before we decide to enter quote
    1804              :                  * tracking below: an argument that is itself a string literal
    1805              :                  * (e.g. `foo('x')`) has the opening quote as its very first,
    1806              :                  * non-whitespace character. Checking need_hint after the quote
    1807              :                  * branch would let that branch `continue` past this character
    1808              :                  * every time, so the hint for a string-literal argument would
    1809              :                  * never fire. */
    1810           64 :                 if (need_hint && !isspace((unsigned char) value[i])) {
    1811              :                         /* PHP 8 named arguments (`name: value`) already show the name;
    1812              :                          * a parameter hint would render it twice. */
    1813           12 :                         named_scan = i;
    1814           12 :                         while (named_scan < close_offset && lsp_doc_is_identifier_char(value[named_scan])) {
    1815            0 :                                 named_scan++;
    1816              :                         }
    1817           12 :                         if (named_scan > i && named_scan < close_offset && value[named_scan] == ':' &&
    1818            0 :                                 (named_scan + 1 >= close_offset || value[named_scan + 1] != ':')
    1819              :                         ) {
    1820            0 :                                 need_hint = false;
    1821            0 :                                 continue;
    1822              :                         }
    1823              : 
    1824           12 :                         label = zend_hash_index_find(Z_ARRVAL_P(params), param_index);
    1825           12 :                         if (label && Z_TYPE_P(label) == IS_STRING) {
    1826              :                                 /* Anchor at the argument itself, not at whitespace that may
    1827              :                                  * follow the opening parenthesis or comma. */
    1828           12 :                                 lsp_inlay_add_hint(items, text, i, Z_STR_P(label));
    1829              :                         }
    1830           12 :                         need_hint = false;
    1831              :                 }
    1832              : 
    1833           64 :                 if (value[i] == '\'' || value[i] == '"') {
    1834            4 :                         quote = value[i];
    1835            4 :                         continue;
    1836              :                 }
    1837              : 
    1838           60 :                 if (value[i] == '(' || value[i] == '[' || value[i] == '{') {
    1839            0 :                         depth++;
    1840           60 :                 } else if ((value[i] == ')' || value[i] == ']' || value[i] == '}') && depth > 0) {
    1841            0 :                         depth--;
    1842           60 :                 } else if (value[i] == ',' && depth == 0) {
    1843            4 :                         param_index++;
    1844            4 :                         need_hint = true;
    1845              :                 }
    1846              :         }
    1847            8 : }
    1848              : 
    1849            8 : static inline void lsp_inlay_add_document_hints(lsp_document *document, HashTable *param_map, zval *items)
    1850              : {
    1851              :         zend_string *name, *key;
    1852              :         zval *tokens_zv, *token, *params;
    1853              :         HashTable *tokens;
    1854              :         uint32_t i, count, open_index;
    1855              :         size_t open_offset, close_offset;
    1856              : 
    1857            8 :         tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
    1858            8 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
    1859            0 :                 return;
    1860              :         }
    1861              : 
    1862            8 :         tokens = Z_ARRVAL_P(tokens_zv);
    1863            8 :         count = zend_hash_num_elements(tokens);
    1864          672 :         for (i = 0; i < count; i++) {
    1865          664 :                 token = zend_hash_index_find(tokens, i);
    1866          664 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_STRING") || !lsp_inlay_token_is_call_name(tokens, i)) {
    1867          616 :                         continue;
    1868              :                 }
    1869              : 
    1870           48 :                 name = lsp_token_string(token, "text");
    1871           48 :                 if (!name) {
    1872            0 :                         continue;
    1873              :                 }
    1874              : 
    1875           48 :                 key = lsp_inlay_key(name);
    1876           48 :                 params = zend_hash_find(param_map, key);
    1877           48 :                 zend_string_release(key);
    1878           48 :                 if (!params || Z_TYPE_P(params) != IS_ARRAY || zend_hash_num_elements(Z_ARRVAL_P(params)) == 0) {
    1879           40 :                         continue;
    1880              :                 }
    1881              : 
    1882            8 :                 open_index = 0;
    1883            8 :                 open_offset = 0;
    1884            8 :                 close_offset = 0;
    1885            8 :                 if (!lsp_inlay_find_call_parens(tokens, i, &open_index, &open_offset, &close_offset) ||
    1886            8 :                         !lsp_inlay_find_close_paren(document->text, open_offset, &close_offset)
    1887              :                 ) {
    1888            0 :                         continue;
    1889              :                 }
    1890              : 
    1891              :                 (void) open_index;
    1892            8 :                 lsp_inlay_add_call_hints(items, document->text, open_offset, close_offset, params);
    1893              :         }
    1894              : }
    1895              : 
    1896            4 : extern void lsp_lsparrot_references(lsp_server *server, zval *return_value, lsp_document *document, zval *params)
    1897              : {
    1898              :         zend_long line, character;
    1899              :         zend_string *word;
    1900              :         zval *position;
    1901              :         size_t offset;
    1902              : 
    1903            4 :         array_init(return_value);
    1904            4 :         position = lsp_array_find(params, "position");
    1905            4 :         lsp_position_from_zval(position, &line, &character);
    1906            4 :         offset = lsp_offset_at(document->text, line, character);
    1907            4 :         word = lsp_word_at(document->text, offset);
    1908            4 :         if (ZSTR_LEN(word) == 0) {
    1909            0 :                 zend_string_release(word);
    1910            0 :                 return;
    1911              :         }
    1912              : 
    1913              :         /* Scope-aware in-document references: locals must not merge with a
    1914              :          * same-named property and vice versa. */
    1915            4 :         if (!lsp_refactor_add_scoped_matches(return_value, document->path, document, word, offset)) {
    1916            0 :                 lsp_refactor_add_token_locations(return_value, document->path, document->text, word);
    1917              :         }
    1918            4 :         if (!lsp_refactor_word_is_variable(word)) {
    1919            0 :                 lsp_refactor_add_project_reference_locations(server, return_value, document, word);
    1920              :         }
    1921              : 
    1922            4 :         zend_string_release(word);
    1923              : }
    1924              : 
    1925           32 : static inline void lsp_refactor_add_highlight_range(zval *items, zend_string *contents, size_t start, size_t end)
    1926              : {
    1927              :         zval item, range;
    1928              : 
    1929           32 :         array_init(&item);
    1930           32 :         lsp_range_from_offsets(contents, start, end, &range);
    1931           32 :         add_assoc_zval(&item, "range", &range);
    1932           32 :         add_assoc_long(&item, "kind", 1);
    1933           32 :         add_next_index_zval(items, &item);
    1934           32 : }
    1935              : 
    1936              : /* Emit either a DocumentHighlight (location_path == NULL) or a Location. */
    1937           40 : static inline void lsp_refactor_add_scoped_result(zval *items, zend_string *location_path, zend_string *contents, size_t start, size_t end)
    1938              : {
    1939              :         zval item;
    1940              : 
    1941           40 :         if (location_path) {
    1942            8 :                 lsp_refactor_location_from_offsets(location_path, contents, start, end, &item);
    1943            8 :                 add_next_index_zval(items, &item);
    1944              :         } else {
    1945           32 :                 lsp_refactor_add_highlight_range(items, contents, start, end);
    1946              :         }
    1947           40 : }
    1948              : 
    1949           20 : static inline zval *lsp_refactor_prev_significant_token(HashTable *tokens, uint32_t index)
    1950              : {
    1951              :         zval *token;
    1952              : 
    1953           28 :         while (index > 0) {
    1954           28 :                 index--;
    1955           28 :                 token = zend_hash_index_find(tokens, index);
    1956           28 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
    1957            0 :                         continue;
    1958              :                 }
    1959              : 
    1960           48 :                 if (lsp_token_name_equals(token, "T_WHITESPACE") ||
    1961           40 :                         lsp_token_name_equals(token, "T_COMMENT") ||
    1962           20 :                         lsp_token_name_equals(token, "T_DOC_COMMENT")
    1963              :                 ) {
    1964            8 :                         continue;
    1965              :                 }
    1966              : 
    1967           20 :                 return token;
    1968              :         }
    1969              : 
    1970            0 :         return NULL;
    1971              : }
    1972              : 
    1973           12 : static inline bool lsp_refactor_token_is_object_operator(zval *token)
    1974              : {
    1975           12 :         return lsp_token_name_equals(token, "T_OBJECT_OPERATOR") ||
    1976            0 :                 lsp_token_name_equals(token, "T_NULLSAFE_OBJECT_OPERATOR")
    1977              :         ;
    1978              : }
    1979              : 
    1980              : /* Highlight a property: its declaration(s) plus `->name` accesses. */
    1981            0 : static inline void lsp_refactor_add_property_highlights(zval *items, zend_string *location_path, lsp_document *document, HashTable *tokens, zend_string *member)
    1982              : {
    1983              :         zend_long class_depth;
    1984              :         zend_string *variable_name, *text;
    1985              :         zval *token, *previous;
    1986              :         uint32_t i, count;
    1987              :         size_t token_offset, class_start, body_start, body_end, promoted_param_start;
    1988              : 
    1989            0 :         variable_name = strpprintf(0, "$%s", ZSTR_VAL(member));
    1990            0 :         count = zend_hash_num_elements(tokens);
    1991            0 :         for (i = 0; i < count; i++) {
    1992            0 :                 token = zend_hash_index_find(tokens, i);
    1993            0 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
    1994            0 :                         continue;
    1995              :                 }
    1996              : 
    1997            0 :                 token_offset = (size_t) lsp_token_long(token, "offset", 0);
    1998              : 
    1999            0 :                 if (lsp_token_name_equals(token, "T_VARIABLE")) {
    2000            0 :                         text = lsp_token_string(token, "text");
    2001            0 :                         if (!text || !zend_string_equals(text, variable_name)) {
    2002            0 :                                 continue;
    2003              :                         }
    2004              : 
    2005            0 :                         if (!lsp_find_enclosing_class_header(document->text, token_offset, &class_start, &body_start, &body_end, &class_depth)) {
    2006            0 :                                 continue;
    2007              :                         }
    2008              : 
    2009            0 :                         if (lsp_token_is_property_declaration(tokens, i, document->text, class_depth) ||
    2010            0 :                                 lsp_token_is_promoted_property(tokens, i, document->text, class_depth, &promoted_param_start)
    2011              :                         ) {
    2012            0 :                                 lsp_refactor_add_scoped_result(items, location_path, document->text, token_offset, token_offset + ZSTR_LEN(text));
    2013              :                         }
    2014              : 
    2015            0 :                         continue;
    2016              :                 }
    2017              : 
    2018            0 :                 if (!lsp_token_name_equals(token, "T_STRING")) {
    2019            0 :                         continue;
    2020              :                 }
    2021              : 
    2022            0 :                 text = lsp_token_string(token, "text");
    2023            0 :                 if (!text || !zend_string_equals(text, member)) {
    2024            0 :                         continue;
    2025              :                 }
    2026              : 
    2027            0 :                 previous = lsp_refactor_prev_significant_token(tokens, i);
    2028            0 :                 if (previous && lsp_refactor_token_is_object_operator(previous)) {
    2029            0 :                         lsp_refactor_add_scoped_result(items, location_path, document->text, token_offset, token_offset + ZSTR_LEN(text));
    2030              :                 }
    2031              :         }
    2032              : 
    2033            0 :         zend_string_release(variable_name);
    2034            0 : }
    2035              : 
    2036              : /* Scope-aware highlights: a local `$name` must not merge with a same-named
    2037              :  * property, and a property must include its `->name` access sites. Returns
    2038              :  * false to fall back to the plain text matcher. */
    2039           20 : static inline bool lsp_refactor_add_scoped_matches(zval *items, zend_string *location_path, lsp_document *document, zend_string *word, size_t offset)
    2040              : {
    2041              :         zend_long class_depth, body_depth;
    2042              :         zend_string *text;
    2043              :         zval *tokens_zv, *token, *previous;
    2044              :         HashTable *tokens;
    2045              :         uint32_t i, count, cursor_index;
    2046              :         size_t token_offset, token_length, class_start, body_start, body_end, promoted_param_start,
    2047              :                 param_start, param_end, scope_body_start, scope_body_end;
    2048           20 :         bool is_variable, cursor_found = false, property_mode = false, has_scope;
    2049              : 
    2050           20 :         tokens_zv = Z_TYPE(document->lsparrot) == IS_ARRAY
    2051           20 :                 ? zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1)
    2052           20 :                 : NULL
    2053              :         ;
    2054           20 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
    2055            0 :                 return false;
    2056              :         }
    2057              : 
    2058           20 :         tokens = Z_ARRVAL_P(tokens_zv);
    2059           20 :         count = zend_hash_num_elements(tokens);
    2060           20 :         is_variable = ZSTR_LEN(word) > 0 && ZSTR_VAL(word)[0] == '$';
    2061              : 
    2062              :         /* Locate the token under the cursor and classify it. */
    2063           20 :         cursor_index = 0;
    2064          768 :         for (i = 0; i < count; i++) {
    2065          768 :                 token = zend_hash_index_find(tokens, i);
    2066          768 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
    2067            0 :                         continue;
    2068              :                 }
    2069              : 
    2070          768 :                 text = lsp_token_string(token, "text");
    2071          768 :                 if (!text || !zend_string_equals(text, word)) {
    2072          740 :                         continue;
    2073              :                 }
    2074              : 
    2075           28 :                 token_offset = (size_t) lsp_token_long(token, "offset", 0);
    2076           28 :                 token_length = ZSTR_LEN(text);
    2077           28 :                 if (offset < token_offset || offset > token_offset + token_length) {
    2078            8 :                         continue;
    2079              :                 }
    2080              : 
    2081           20 :                 if (is_variable && lsp_token_name_equals(token, "T_VARIABLE")) {
    2082           20 :                         cursor_found = true;
    2083           20 :                         cursor_index = i;
    2084           40 :                         if (lsp_find_enclosing_class_header(document->text, token_offset, &class_start, &body_start, &body_end, &class_depth) &&
    2085           40 :                                 (lsp_token_is_property_declaration(tokens, i, document->text, class_depth) ||
    2086           20 :                                         lsp_token_is_promoted_property(tokens, i, document->text, class_depth, &promoted_param_start))
    2087              :                         ) {
    2088            0 :                                 property_mode = true;
    2089              :                         }
    2090           20 :                         break;
    2091              :                 }
    2092              : 
    2093            0 :                 if (!is_variable && lsp_token_name_equals(token, "T_STRING")) {
    2094            0 :                         previous = lsp_refactor_prev_significant_token(tokens, i);
    2095            0 :                         if (previous && lsp_refactor_token_is_object_operator(previous)) {
    2096            0 :                                 cursor_found = true;
    2097            0 :                                 cursor_index = i;
    2098            0 :                                 property_mode = true;
    2099            0 :                                 break;
    2100              :                         }
    2101              :                 }
    2102              :         }
    2103              : 
    2104           20 :         if (!cursor_found) {
    2105            0 :                 return false;
    2106              :         }
    2107              : 
    2108              :         (void) cursor_index;
    2109              : 
    2110           20 :         if (property_mode) {
    2111            0 :                 zend_string *member = is_variable
    2112            0 :                         ? zend_string_init(ZSTR_VAL(word) + 1, ZSTR_LEN(word) - 1, 0)
    2113            0 :                         : zend_string_copy(word)
    2114              :                 ;
    2115            0 :                 lsp_refactor_add_property_highlights(items, location_path, document, tokens, member);
    2116            0 :                 zend_string_release(member);
    2117              : 
    2118            0 :                 return true;
    2119              :         }
    2120              : 
    2121              :         /* Local variable: restrict to the enclosing function scope and skip
    2122              :          * property declarations and `->name` member accesses. */
    2123           20 :         has_scope = lsp_find_function_scope_at(tokens, document->text, offset, &param_start, &param_end, &scope_body_start, &scope_body_end, &body_depth);
    2124         1408 :         for (i = 0; i < count; i++) {
    2125         1388 :                 token = zend_hash_index_find(tokens, i);
    2126         1388 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_VARIABLE")) {
    2127         1276 :                         continue;
    2128              :                 }
    2129              : 
    2130          112 :                 text = lsp_token_string(token, "text");
    2131          112 :                 if (!text || !zend_string_equals(text, word)) {
    2132           68 :                         continue;
    2133              :                 }
    2134              : 
    2135           44 :                 token_offset = (size_t) lsp_token_long(token, "offset", 0);
    2136           44 :                 if (has_scope &&
    2137           44 :                         (token_offset < param_start || token_offset > param_end) &&
    2138           44 :                         (token_offset < scope_body_start || token_offset > scope_body_end)
    2139              :                 ) {
    2140            4 :                         continue;
    2141              :                 }
    2142              : 
    2143           80 :                 if (lsp_find_enclosing_class_header(document->text, token_offset, &class_start, &body_start, &body_end, &class_depth) &&
    2144           80 :                         (lsp_token_is_property_declaration(tokens, i, document->text, class_depth) ||
    2145           40 :                                 lsp_token_is_promoted_property(tokens, i, document->text, class_depth, &promoted_param_start))
    2146              :                 ) {
    2147            0 :                         continue;
    2148              :                 }
    2149              : 
    2150           40 :                 lsp_refactor_add_scoped_result(items, location_path, document->text, token_offset, token_offset + ZSTR_LEN(text));
    2151              :         }
    2152              : 
    2153           20 :         return true;
    2154              : }
    2155              : 
    2156           12 : extern void lsp_lsparrot_document_highlight(lsp_server *server, zval *return_value, lsp_document *document, zval *position)
    2157              : {
    2158              :         zend_long line, character;
    2159              :         zend_string *word;
    2160              :         size_t offset;
    2161              : 
    2162              :         (void) server;
    2163           12 :         array_init(return_value);
    2164           12 :         lsp_position_from_zval(position, &line, &character);
    2165           12 :         offset = lsp_offset_at(document->text, line, character);
    2166           12 :         word = lsp_word_at(document->text, offset);
    2167           12 :         if (ZSTR_LEN(word) > 0 && !lsp_refactor_add_scoped_matches(return_value, NULL, document, word, offset)) {
    2168            0 :                 lsp_refactor_add_document_highlights(return_value, document->text, word);
    2169              :         }
    2170           12 :         zend_string_release(word);
    2171           12 : }
    2172              : 
    2173            4 : static inline void lsp_refactor_add_implementation_locations(lsp_server *server, zval *locations, zend_string *target)
    2174              : {
    2175            4 :         lsp_refactor_add_implementation_locations_ex(server, locations, target, NULL);
    2176            4 : }
    2177              : 
    2178              : /* True when the word ending the given bounds is a declared function/method
    2179              :  * name (immediately preceded by the `function` keyword). */
    2180            4 : static inline bool lsp_refactor_word_is_function_declaration(zend_string *text, size_t word_start)
    2181              : {
    2182            4 :         const char *value = ZSTR_VAL(text);
    2183            4 :         size_t i = word_start, kw_end, kw_start;
    2184              : 
    2185            8 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    2186            4 :                 i--;
    2187              :         }
    2188            4 :         if (i > 0 && value[i - 1] == '&') {
    2189            0 :                 i--;
    2190            0 :                 while (i > 0 && isspace((unsigned char) value[i - 1])) {
    2191            0 :                         i--;
    2192              :                 }
    2193              :         }
    2194              : 
    2195            4 :         kw_end = i;
    2196           40 :         while (i > 0 && lsp_doc_is_identifier_char(value[i - 1])) {
    2197           36 :                 i--;
    2198              :         }
    2199            4 :         kw_start = i;
    2200              : 
    2201            4 :         return kw_end - kw_start == sizeof("function") - 1 &&
    2202            0 :                 strncasecmp(value + kw_start, "function", sizeof("function") - 1) == 0
    2203              :         ;
    2204              : }
    2205              : 
    2206            4 : extern void lsp_lsparrot_implementation(lsp_server *server, zval *return_value, lsp_document *document, zval *position)
    2207              : {
    2208              :         zend_long line, character;
    2209              :         zend_string *word, *target;
    2210              :         size_t offset;
    2211              : 
    2212            4 :         array_init(return_value);
    2213            4 :         lsp_position_from_zval(position, &line, &character);
    2214            4 :         offset = lsp_offset_at(document->text, line, character);
    2215            4 :         word = lsp_word_at(document->text, offset);
    2216            4 :         if (ZSTR_LEN(word) == 0) {
    2217            0 :                 zend_string_release(word);
    2218            0 :                 return;
    2219              :         }
    2220              : 
    2221              :         /* On a method DECLARATION inside an interface/class, list each
    2222              :          * implementer's override of that method instead of the type itself. */
    2223              :         {
    2224            4 :                 zend_long enclosing_depth = 0;
    2225              :                 zend_string *enclosing;
    2226            4 :                 size_t word_start = offset > ZSTR_LEN(document->text) ? ZSTR_LEN(document->text) : offset,
    2227            4 :                         class_start = 0, body_start = 0, body_end = 0;
    2228              : 
    2229            8 :                 while (word_start > 0 && lsp_doc_is_identifier_char(ZSTR_VAL(document->text)[word_start - 1])) {
    2230            4 :                         word_start--;
    2231              :                 }
    2232              : 
    2233            4 :                 if (lsp_refactor_word_is_function_declaration(document->text, word_start) &&
    2234            0 :                         lsp_find_enclosing_class_header(document->text, offset, &class_start, &body_start, &body_end, &enclosing_depth)
    2235              :                 ) {
    2236            0 :                         enclosing = lsp_class_declared_name(document->text, class_start, body_start);
    2237            0 :                         if (enclosing) {
    2238            0 :                                 lsp_refactor_add_implementation_locations_ex(server, return_value, enclosing, word);
    2239            0 :                                 zend_string_release(enclosing);
    2240            0 :                                 zend_string_release(word);
    2241              : 
    2242            0 :                                 return;
    2243              :                         }
    2244              :                 }
    2245              :         }
    2246              : 
    2247            4 :         target = lsp_resolve_class_name_at(document->text, word, offset);
    2248            4 :         if (!target) {
    2249            0 :                 target = zend_string_copy(word);
    2250              :         }
    2251              : 
    2252            4 :         lsp_refactor_add_implementation_locations(server, return_value, target);
    2253            4 :         zend_string_release(target);
    2254            4 :         zend_string_release(word);
    2255              : }
    2256              : 
    2257           12 : extern void lsp_lsparrot_code_action(lsp_server *server, zval *return_value, lsp_document *document, zval *params)
    2258              : {
    2259              :         zend_string *word;
    2260              :         zval *range;
    2261              :         size_t offset, word_start, word_end;
    2262              : 
    2263           12 :         array_init(return_value);
    2264           12 :         if (lsp_refactor_context_only_has(params, "source.organizeImports")) {
    2265            8 :                 lsp_refactor_add_organize_imports_action(return_value, document);
    2266              :         }
    2267              : 
    2268           12 :         if (!lsp_refactor_context_only_has(params, "quickfix")) {
    2269            4 :                 return;
    2270              :         }
    2271              : 
    2272            8 :         lsp_add_analyzer_quick_fixes(return_value, document, params);
    2273              : 
    2274            8 :         range = lsp_array_find(params, "range");
    2275            8 :         if (!lsp_refactor_range_start_offset(document->text, range, &offset)) {
    2276            0 :                 return;
    2277              :         }
    2278              : 
    2279            8 :         if (!lsp_refactor_word_bounds(document->text, offset, &word_start, &word_end)) {
    2280            0 :                 return;
    2281              :         }
    2282              : 
    2283            8 :         word = zend_string_init(ZSTR_VAL(document->text) + word_start, word_end - word_start, 0);
    2284            8 :         lsp_refactor_add_import_actions(server, return_value, document, word);
    2285              : 
    2286            8 :         zend_string_release(word);
    2287              : }
    2288              : 
    2289              : /* Only identifier-bearing tokens are rename targets: keywords, string
    2290              :  * literal contents, and comments must return null from prepareRename. */
    2291            4 : static inline bool lsp_refactor_offset_is_renamable(lsp_document *document, size_t word_start)
    2292              : {
    2293              :         zval *tokens_zv, *token;
    2294              :         HashTable *tokens;
    2295              :         uint32_t i, count;
    2296              :         size_t token_offset, token_length;
    2297              :         zend_string *text;
    2298              : 
    2299            4 :         tokens_zv = Z_TYPE(document->lsparrot) == IS_ARRAY
    2300            4 :                 ? zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1)
    2301            4 :                 : NULL
    2302              :         ;
    2303            4 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
    2304            0 :                 return true;
    2305              :         }
    2306              : 
    2307            4 :         tokens = Z_ARRVAL_P(tokens_zv);
    2308            4 :         count = zend_hash_num_elements(tokens);
    2309          112 :         for (i = 0; i < count; i++) {
    2310          112 :                 token = zend_hash_index_find(tokens, i);
    2311          112 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
    2312            0 :                         continue;
    2313              :                 }
    2314              : 
    2315          112 :                 text = lsp_token_string(token, "text");
    2316          112 :                 if (!text) {
    2317            0 :                         continue;
    2318              :                 }
    2319              : 
    2320          112 :                 token_offset = (size_t) lsp_token_long(token, "offset", 0);
    2321          112 :                 token_length = ZSTR_LEN(text);
    2322          112 :                 if (word_start < token_offset || word_start >= token_offset + token_length) {
    2323          108 :                         continue;
    2324              :                 }
    2325              : 
    2326            4 :                 return lsp_token_name_equals(token, "T_VARIABLE") ||
    2327            0 :                         lsp_token_name_equals(token, "T_STRING") ||
    2328            0 :                         lsp_token_name_equals(token, "T_NAME_QUALIFIED") ||
    2329            4 :                         lsp_token_name_equals(token, "T_NAME_FULLY_QUALIFIED") ||
    2330            0 :                         lsp_token_name_equals(token, "T_NAME_RELATIVE")
    2331              :                 ;
    2332              :         }
    2333              : 
    2334            0 :         return true;
    2335              : }
    2336              : 
    2337            4 : extern void lsp_lsparrot_prepare_rename(lsp_server *server, zval *return_value, lsp_document *document, zval *position)
    2338              : {
    2339              :         zend_long line, character;
    2340              :         zend_string *word;
    2341              :         zval range;
    2342              :         size_t offset, word_start, word_end;
    2343              : 
    2344              :         (void) server;
    2345            4 :         lsp_position_from_zval(position, &line, &character);
    2346            4 :         offset = lsp_offset_at(document->text, line, character);
    2347            4 :         word = lsp_word_at(document->text, offset);
    2348            8 :         if (ZSTR_LEN(word) == 0 ||
    2349            4 :                 lsp_refactor_word_is_qualified(word) ||
    2350            4 :                 !lsp_refactor_word_bounds(document->text, offset, &word_start, &word_end) ||
    2351            4 :                 !lsp_refactor_offset_is_renamable(document, word_start)
    2352              :         ) {
    2353            0 :                 zend_string_release(word);
    2354            0 :                 ZVAL_NULL(return_value);
    2355            0 :                 return;
    2356              :         }
    2357              : 
    2358            4 :         lsp_range_from_offsets(document->text, word_start, word_end, &range);
    2359            4 :         ZVAL_COPY_VALUE(return_value, &range);
    2360            4 :         zend_string_release(word);
    2361              : }
    2362              : 
    2363              : /* Rename a variable using the same scoping rules as documentHighlight:
    2364              :  * collect the scoped ranges, then emit text edits for them. Returns false
    2365              :  * (leaving changes untouched) when scoping cannot classify the cursor. */
    2366            4 : static inline bool lsp_refactor_add_scoped_variable_rename_changes(zval *changes, lsp_document *document, zend_string *word, zend_string *new_text, size_t offset)
    2367              : {
    2368              :         zval matches, *match, *range, edits, edit, range_copy;
    2369            4 :         bool any = false;
    2370              : 
    2371            4 :         array_init(&matches);
    2372            4 :         if (!lsp_refactor_add_scoped_matches(&matches, NULL, document, word, offset)) {
    2373            0 :                 zval_ptr_dtor(&matches);
    2374              : 
    2375            0 :                 return false;
    2376              :         }
    2377              : 
    2378            4 :         array_init(&edits);
    2379           12 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL(matches), match) {
    2380            8 :                 range = lsp_array_find(match, "range");
    2381            8 :                 if (!range) {
    2382            0 :                         continue;
    2383              :                 }
    2384              : 
    2385            8 :                 array_init(&edit);
    2386            8 :                 ZVAL_COPY(&range_copy, range);
    2387            8 :                 add_assoc_zval(&edit, "range", &range_copy);
    2388            8 :                 add_assoc_str(&edit, "newText", zend_string_copy(new_text));
    2389            8 :                 add_next_index_zval(&edits, &edit);
    2390            8 :                 any = true;
    2391              :         } ZEND_HASH_FOREACH_END();
    2392            4 :         zval_ptr_dtor(&matches);
    2393              : 
    2394            4 :         if (!any) {
    2395            0 :                 zval_ptr_dtor(&edits);
    2396              : 
    2397            0 :                 return false;
    2398              :         }
    2399              : 
    2400            4 :         add_assoc_zval_ex(changes, ZSTR_VAL(document->uri), ZSTR_LEN(document->uri), &edits);
    2401              : 
    2402            4 :         return true;
    2403              : }
    2404              : 
    2405           12 : extern void lsp_lsparrot_rename(lsp_server *server, zval *return_value, lsp_document *document, zval *params)
    2406              : {
    2407              :         zend_long line, character;
    2408              :         zend_string *word, *new_name, *new_text;
    2409              :         zval *position, changes;
    2410              :         size_t offset;
    2411              :         bool variable;
    2412              : 
    2413           12 :         position = lsp_array_find(params, "position");
    2414           12 :         new_name = lsp_array_string(params, "newName");
    2415              : 
    2416           12 :         array_init(return_value);
    2417           12 :         array_init(&changes);
    2418              : 
    2419           12 :         if (!new_name || ZSTR_LEN(new_name) == 0) {
    2420            0 :                 add_assoc_zval(return_value, "changes", &changes);
    2421            0 :                 return;
    2422              :         }
    2423              : 
    2424           12 :         lsp_position_from_zval(position, &line, &character);
    2425           12 :         offset = lsp_offset_at(document->text, line, character);
    2426           12 :         word = lsp_word_at(document->text, offset);
    2427           12 :         if (ZSTR_LEN(word) == 0 || lsp_refactor_word_is_qualified(word)) {
    2428            0 :                 zend_string_release(word);
    2429            0 :                 add_assoc_zval(return_value, "changes", &changes);
    2430            0 :                 return;
    2431              :         }
    2432              : 
    2433           12 :         variable = lsp_refactor_word_is_variable(word);
    2434           12 :         new_text = lsp_refactor_rename_text(word, new_name);
    2435           12 :         if (variable && lsp_refactor_add_scoped_variable_rename_changes(&changes, document, word, new_text, offset)) {
    2436              :                 /* Local variable scoped rename: property declarations and other
    2437              :                  * functions' same-named locals stay untouched. */
    2438            8 :         } else if (!variable && lsp_refactor_add_method_rename_changes(server, &changes, document, offset, word, new_text)) {
    2439              :                 /* Type-directed method rename handled everything. */
    2440              :         } else {
    2441            4 :                 lsp_refactor_add_file_rename_changes(&changes, document->uri, document->text, word, new_text);
    2442            4 :                 if (!variable) {
    2443            4 :                         lsp_refactor_add_project_rename_changes(server, &changes, document, word, new_text);
    2444              :                 }
    2445              :         }
    2446              : 
    2447           12 :         zend_string_release(new_text);
    2448           12 :         zend_string_release(word);
    2449           12 :         add_assoc_zval(return_value, "changes", &changes);
    2450              : }
    2451              : 
    2452              : /* Resolves the class named directly after a `new` keyword token (e.g. the
    2453              :  * `Leaf` in `new Leaf(...)`). Only plain/qualified name tokens are handled;
    2454              :  * dynamic class expressions (`new $class(...)`, `new static(...)`) yield no
    2455              :  * text and therefore no hint, same as any other uninferrable expression. */
    2456            4 : static inline zend_string *lsp_inlay_new_class_text(HashTable *tokens, uint32_t new_index)
    2457              : {
    2458              :         zval *class_token;
    2459              :         zend_string *text;
    2460              :         uint32_t class_index;
    2461              : 
    2462            4 :         class_token = lsp_inlay_next_non_whitespace(tokens, new_index + 1, &class_index);
    2463            4 :         if (!class_token) {
    2464            0 :                 return NULL;
    2465              :         }
    2466              : 
    2467            4 :         if (!lsp_token_name_equals(class_token, "T_STRING") &&
    2468            0 :                 !lsp_token_name_equals(class_token, "T_NAME_QUALIFIED") &&
    2469            0 :                 !lsp_token_name_equals(class_token, "T_NAME_FULLY_QUALIFIED") &&
    2470            0 :                 !lsp_token_name_equals(class_token, "T_NAME_RELATIVE")
    2471              :         ) {
    2472            0 :                 return NULL;
    2473              :         }
    2474              : 
    2475            4 :         text = lsp_token_string(class_token, "text");
    2476            4 :         if (!text || ZSTR_LEN(text) == 0) {
    2477            0 :                 return NULL;
    2478              :         }
    2479              : 
    2480            4 :         return zend_string_copy(text);
    2481              : }
    2482              : 
    2483              : /* Resolves a raw class name token's text (as written in source) to a
    2484              :  * (possibly namespaced) FQCN, preferring an already-loaded engine class
    2485              :  * before falling back to import/namespace resolution against the document
    2486              :  * text — mirroring how the rest of the inference layer treats `new X`. */
    2487            8 : static inline zend_string *lsp_inlay_resolve_class_text(lsp_document *document, zend_string *class_text)
    2488              : {
    2489            8 :         if (zend_lookup_class(class_text)) {
    2490            0 :                 return zend_string_copy(class_text);
    2491              :         }
    2492              : 
    2493            8 :         return lsp_resolve_class_name(document->text, class_text);
    2494              : }
    2495              : 
    2496              : /* True if `offset` (measured from `body_start`) sits inside an unmatched
    2497              :  * `(...)` — i.e. a parameter list rather than a class-body statement. Used
    2498              :  * to keep the native property scan below from mistaking an ordinary
    2499              :  * (non-promoted) constructor parameter such as `__construct(Leaf $leaf)`
    2500              :  * for a `Leaf $leaf` property declaration; both have a type token directly
    2501              :  * before the `$name` and, since a parameter list precedes the method's own
    2502              :  * `{`, both sit at the same brace depth as the class body. */
    2503            4 : static inline bool lsp_inlay_within_parens(zend_string *text, size_t body_start, size_t offset)
    2504              : {
    2505            4 :         const char *value = ZSTR_VAL(text);
    2506            4 :         long depth = 0;
    2507              :         size_t i;
    2508              : 
    2509           76 :         for (i = body_start; i < offset && i < ZSTR_LEN(text); i++) {
    2510           72 :                 if (value[i] == '(') {
    2511            0 :                         depth++;
    2512           72 :                 } else if (value[i] == ')' && depth > 0) {
    2513            0 :                         depth--;
    2514              :                 }
    2515              :         }
    2516              : 
    2517            4 :         return depth != 0;
    2518              : }
    2519              : 
    2520              : /* Reimplements (locally, since the original is private to lsp_server_phpdoc.c)
    2521              :  * the "is there a visibility/var modifier earlier in this same statement"
    2522              :  * check used to confirm `$name` is being declared as a property rather than
    2523              :  * merely referenced. */
    2524            4 : static inline bool lsp_inlay_property_declaration_before(zend_string *text, size_t variable_offset)
    2525              : {
    2526              :         static const char *visibility[5] = {"public", "protected", "private", "var", "readonly"};
    2527              :         const char *value;
    2528              :         size_t declaration_start, scan, token_start, token_end;
    2529              :         uint32_t i;
    2530              : 
    2531            4 :         if (variable_offset > ZSTR_LEN(text)) {
    2532            0 :                 return false;
    2533              :         }
    2534              : 
    2535            4 :         value = ZSTR_VAL(text);
    2536            4 :         declaration_start = variable_offset;
    2537            4 :         while (declaration_start > 0 &&
    2538           76 :                 value[declaration_start - 1] != ';' &&
    2539          152 :                 value[declaration_start - 1] != '{' &&
    2540           72 :                 value[declaration_start - 1] != '}'
    2541              :         ) {
    2542           72 :                 declaration_start--;
    2543              :         }
    2544              : 
    2545            4 :         scan = declaration_start;
    2546           24 :         while (scan < variable_offset) {
    2547           24 :                 if (!lsp_doc_is_identifier_start(value[scan])) {
    2548           20 :                         scan++;
    2549           20 :                         continue;
    2550              :                 }
    2551              : 
    2552            4 :                 token_start = scan;
    2553            4 :                 scan++;
    2554           28 :                 while (scan < variable_offset && lsp_doc_is_identifier_char(value[scan])) {
    2555           24 :                         scan++;
    2556              :                 }
    2557            4 :                 token_end = scan;
    2558              : 
    2559           12 :                 for (i = 0; i < sizeof(visibility) / sizeof(visibility[0]); i++) {
    2560           12 :                         if (token_end - token_start == strlen(visibility[i]) &&
    2561            4 :                                 strncasecmp(value + token_start, visibility[i], token_end - token_start) == 0
    2562              :                         ) {
    2563            4 :                                 return true;
    2564              :                         }
    2565              :                 }
    2566              :         }
    2567              : 
    2568            0 :         return false;
    2569              : }
    2570              : 
    2571              : /* Finds the declared type of a plain, native typed property (no phpdoc,
    2572              :  * e.g. `private Leaf $leaf;`) by locating the enclosing class body and
    2573              :  * scanning it for a `$property` occurrence that reads as a property
    2574              :  * declaration: same brace depth as the class body, not inside a parameter
    2575              :  * list, with a visibility/var modifier earlier in the statement, and a
    2576              :  * bare type-name token directly before the `$`. */
    2577            4 : static inline zend_string *lsp_inlay_native_property_type(lsp_document *document, zend_string *property, size_t offset)
    2578              : {
    2579              :         const char *value;
    2580              :         zend_string *candidate;
    2581              :         size_t class_start, body_start, body_end, search, variable_end, type_start, type_end;
    2582              :         zend_long body_depth;
    2583              : 
    2584            4 :         if (!lsp_find_enclosing_class_header(document->text, offset, &class_start, &body_start, &body_end, &body_depth)) {
    2585            0 :                 return NULL;
    2586              :         }
    2587              : 
    2588            4 :         value = ZSTR_VAL(document->text);
    2589           76 :         for (search = body_start; search + ZSTR_LEN(property) < body_end; search++) {
    2590           76 :                 if (value[search] != '$') {
    2591           72 :                         continue;
    2592              :                 }
    2593              : 
    2594            4 :                 variable_end = search + 1 + ZSTR_LEN(property);
    2595            4 :                 if (variable_end > body_end || strncmp(value + search + 1, ZSTR_VAL(property), ZSTR_LEN(property)) != 0) {
    2596            0 :                         continue;
    2597              :                 }
    2598              : 
    2599            4 :                 if (variable_end < ZSTR_LEN(document->text) && lsp_doc_is_identifier_char(value[variable_end])) {
    2600            0 :                         continue;
    2601              :                 }
    2602              : 
    2603            4 :                 if (lsp_brace_depth_at(document->text, search) != body_depth || lsp_inlay_within_parens(document->text, body_start, search)) {
    2604            0 :                         continue;
    2605              :                 }
    2606              : 
    2607            4 :                 if (!lsp_inlay_property_declaration_before(document->text, search)) {
    2608            0 :                         continue;
    2609              :                 }
    2610              : 
    2611            4 :                 type_end = search;
    2612            8 :                 while (type_end > body_start && isspace((unsigned char) value[type_end - 1])) {
    2613            4 :                         type_end--;
    2614              :                 }
    2615              : 
    2616            4 :                 type_start = type_end;
    2617           20 :                 while (type_start > body_start && (lsp_doc_is_identifier_char(value[type_start - 1]) || value[type_start - 1] == '\\')) {
    2618           16 :                         type_start--;
    2619              :                 }
    2620              : 
    2621            4 :                 if (type_start == type_end) {
    2622            0 :                         continue;
    2623              :                 }
    2624              : 
    2625            4 :                 candidate = zend_string_init(value + type_start, type_end - type_start, 0);
    2626            4 :                 if (ZSTR_LEN(candidate) == 0) {
    2627            0 :                         zend_string_release(candidate);
    2628            0 :                         continue;
    2629              :                 }
    2630              : 
    2631            4 :                 return candidate;
    2632              :         }
    2633              : 
    2634            0 :         return NULL;
    2635              : }
    2636              : 
    2637              : /* Resolves the class of a bare `$this->property` read (no call, no
    2638              :  * subscript, no further chaining — the whole right-hand side of the
    2639              :  * assignment) via the declared/typed-property lookup used elsewhere for
    2640              :  * property inference. */
    2641            4 : static inline zend_string *lsp_inlay_this_property_class(lsp_document *document, HashTable *tokens, uint32_t this_index)
    2642              : {
    2643              :         zval *arrow_token, *prop_token, *terminator_token;
    2644              :         uint32_t arrow_index, prop_index, terminator_index;
    2645              :         zend_string *property, *type, *resolved;
    2646              :         size_t prop_end;
    2647              : 
    2648            4 :         arrow_token = lsp_inlay_next_non_whitespace(tokens, this_index + 1, &arrow_index);
    2649            4 :         if (!arrow_token || !lsp_token_name_equals(arrow_token, "T_OBJECT_OPERATOR")) {
    2650            0 :                 return NULL;
    2651              :         }
    2652              : 
    2653            4 :         prop_token = lsp_inlay_next_non_whitespace(tokens, arrow_index + 1, &prop_index);
    2654            4 :         if (!prop_token || !lsp_token_name_equals(prop_token, "T_STRING")) {
    2655            0 :                 return NULL;
    2656              :         }
    2657              : 
    2658            4 :         terminator_token = lsp_inlay_next_non_whitespace(tokens, prop_index + 1, &terminator_index);
    2659            4 :         if (!terminator_token || !lsp_token_is_char(terminator_token, ';')) {
    2660            0 :                 return NULL;
    2661              :         }
    2662              : 
    2663            4 :         property = lsp_token_string(prop_token, "text");
    2664            4 :         if (!property || ZSTR_LEN(property) == 0) {
    2665            0 :                 return NULL;
    2666              :         }
    2667              : 
    2668            4 :         prop_end = (size_t) lsp_token_long(prop_token, "offset", 0) + ZSTR_LEN(property);
    2669              : 
    2670              :         /* lsp_phpdoc_property_type_for_word only recognizes `@var`-documented
    2671              :          * properties; it returns NULL for plain native typed declarations
    2672              :          * (`private Leaf $leaf;`), which is the common case. Try it first
    2673              :          * (it also covers phpdoc-only properties this pass otherwise
    2674              :          * couldn't resolve), then fall back to a native-declaration scan. */
    2675            4 :         type = lsp_phpdoc_property_type_for_word(document->text, property, prop_end);
    2676            4 :         if (!type) {
    2677            4 :                 type = lsp_inlay_native_property_type(document, property, prop_end);
    2678              :         }
    2679              : 
    2680            4 :         if (!type) {
    2681            0 :                 return NULL;
    2682              :         }
    2683              : 
    2684            4 :         resolved = lsp_inlay_resolve_class_text(document, type);
    2685            4 :         zend_string_release(type);
    2686              : 
    2687            4 :         return resolved;
    2688              : }
    2689              : 
    2690              : /* Infers the class held by `$var` right after `$var = <rhs>;`. Only the two
    2691              :  * shapes PhpStorm-style hints care most about are handled: a fresh `new`
    2692              :  * instantiation and a bare `$this->property` read. Anything else (method
    2693              :  * call results, array access, literals, ...) intentionally yields NULL so no
    2694              :  * hint is produced — better no hint than a wrong one. */
    2695            8 : static inline zend_string *lsp_inlay_infer_assignment_class(lsp_document *document, HashTable *tokens, uint32_t equals_index)
    2696              : {
    2697              :         zval *rhs_token;
    2698              :         zend_string *variable_text;
    2699              :         uint32_t rhs_index;
    2700              : 
    2701            8 :         rhs_token = lsp_inlay_next_non_whitespace(tokens, equals_index + 1, &rhs_index);
    2702            8 :         if (!rhs_token) {
    2703            0 :                 return NULL;
    2704              :         }
    2705              : 
    2706            8 :         if (lsp_token_long(rhs_token, "id", -1) == T_NEW) {
    2707              :                 zend_string *class_text, *resolved;
    2708              : 
    2709            4 :                 class_text = lsp_inlay_new_class_text(tokens, rhs_index);
    2710            4 :                 if (!class_text) {
    2711            0 :                         return NULL;
    2712              :                 }
    2713              : 
    2714            4 :                 resolved = lsp_inlay_resolve_class_text(document, class_text);
    2715            4 :                 zend_string_release(class_text);
    2716              : 
    2717            4 :                 return resolved;
    2718              :         }
    2719              : 
    2720            4 :         if (lsp_token_name_equals(rhs_token, "T_VARIABLE")) {
    2721            4 :                 variable_text = lsp_token_string(rhs_token, "text");
    2722            4 :                 if (variable_text && zend_string_equals_literal(variable_text, "$this")) {
    2723            4 :                         return lsp_inlay_this_property_class(document, tokens, rhs_index);
    2724              :                 }
    2725              :         }
    2726              : 
    2727            0 :         return NULL;
    2728              : }
    2729              : 
    2730            8 : static inline void lsp_inlay_add_type_hint(zval *items, zend_string *text, size_t offset, zend_string *class_name)
    2731              : {
    2732              :         const char *short_name;
    2733              :         size_t short_length;
    2734              :         zend_string *label;
    2735              :         zval hint, range, position, *start;
    2736              : 
    2737            8 :         short_name = lsp_basename_from_fqcn(ZSTR_VAL(class_name), ZSTR_LEN(class_name), &short_length);
    2738            8 :         label = strpprintf(0, ": %.*s", (int) short_length, short_name);
    2739              : 
    2740            8 :         array_init(&hint);
    2741            8 :         lsp_range_from_offsets(text, offset, offset, &range);
    2742            8 :         start = zend_hash_str_find(Z_ARRVAL(range), "start", sizeof("start") - 1);
    2743            8 :         if (start) {
    2744            8 :                 ZVAL_COPY(&position, start);
    2745            8 :                 add_assoc_zval(&hint, "position", &position);
    2746              :         }
    2747              : 
    2748            8 :         add_assoc_str(&hint, "label", label);
    2749            8 :         add_assoc_long(&hint, "kind", 1);
    2750            8 :         add_assoc_bool(&hint, "paddingLeft", true);
    2751            8 :         add_next_index_zval(items, &hint);
    2752              : 
    2753            8 :         zval_ptr_dtor(&range);
    2754            8 : }
    2755              : 
    2756            8 : static inline bool lsp_inlay_range_lines(zval *params, zend_long *start_line, zend_long *end_line)
    2757              : {
    2758              :         zval *range, *start, *end;
    2759              : 
    2760            8 :         range = lsp_array_find(params, "range");
    2761            8 :         if (!range || Z_TYPE_P(range) != IS_ARRAY) {
    2762            0 :                 return false;
    2763              :         }
    2764              : 
    2765            8 :         start = lsp_array_find(range, "start");
    2766            8 :         end = lsp_array_find(range, "end");
    2767            8 :         if (!start || Z_TYPE_P(start) != IS_ARRAY || !end || Z_TYPE_P(end) != IS_ARRAY) {
    2768            0 :                 return false;
    2769              :         }
    2770              : 
    2771            8 :         *start_line = lsp_array_long(start, "line", 0);
    2772            8 :         *end_line = lsp_array_long(end, "line", 0);
    2773              : 
    2774            8 :         return true;
    2775              : }
    2776              : 
    2777              : /* Marks (line, variable) as seen, returning false if it was already present
    2778              :  * so callers can skip emitting a duplicate hint for the same variable on the
    2779              :  * same line (e.g. `$a = $b = new Leaf();`). */
    2780            8 : static inline bool lsp_inlay_mark_seen(HashTable *seen, zend_long line, zend_string *variable)
    2781              : {
    2782              :         zend_string *key;
    2783              :         zval marker;
    2784              :         bool inserted;
    2785              : 
    2786            8 :         key = strpprintf(0, "%ld:%s", (long) line, ZSTR_VAL(variable));
    2787            8 :         ZVAL_TRUE(&marker);
    2788            8 :         inserted = zend_hash_add(seen, key, &marker) != NULL;
    2789            8 :         zend_string_release(key);
    2790              : 
    2791            8 :         return inserted;
    2792              : }
    2793              : 
    2794            8 : static inline void lsp_inlay_add_variable_type_hints(lsp_document *document, zval *params, zval *items)
    2795              : {
    2796              :         zval *tokens_zv, *token, *equals_token;
    2797              :         HashTable *tokens, seen;
    2798              :         zend_string *variable, *class_name;
    2799              :         uint32_t i, count, equals_index;
    2800              :         zend_long start_line, end_line, token_line;
    2801              :         size_t hint_offset;
    2802              :         bool have_range;
    2803              :         uint32_t emitted;
    2804              : 
    2805            8 :         tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
    2806            8 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
    2807            0 :                 return;
    2808              :         }
    2809              : 
    2810            8 :         have_range = lsp_inlay_range_lines(params, &start_line, &end_line);
    2811            8 :         tokens = Z_ARRVAL_P(tokens_zv);
    2812            8 :         count = zend_hash_num_elements(tokens);
    2813            8 :         emitted = 0;
    2814            8 :         zend_hash_init(&seen, 16, NULL, ZVAL_PTR_DTOR, 0);
    2815              : 
    2816          672 :         for (i = 0; i < count && emitted < LSP_INLAY_TYPE_HINT_LIMIT; i++) {
    2817          664 :                 token = zend_hash_index_find(tokens, i);
    2818          664 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_VARIABLE")) {
    2819          608 :                         continue;
    2820              :                 }
    2821              : 
    2822           56 :                 variable = lsp_token_string(token, "text");
    2823           56 :                 if (!variable || ZSTR_LEN(variable) <= 1 || zend_string_equals_literal(variable, "$this")) {
    2824            8 :                         continue;
    2825              :                 }
    2826              : 
    2827           48 :                 token_line = lsp_token_long(token, "line", 1) - 1;
    2828           48 :                 if (have_range && (token_line < start_line || token_line > end_line)) {
    2829           20 :                         continue;
    2830              :                 }
    2831              : 
    2832           28 :                 equals_token = lsp_inlay_next_non_whitespace(tokens, i + 1, &equals_index);
    2833           28 :                 if (!equals_token || !lsp_token_is_char(equals_token, '=')) {
    2834           20 :                         continue;
    2835              :                 }
    2836              : 
    2837            8 :                 class_name = lsp_inlay_infer_assignment_class(document, tokens, equals_index);
    2838            8 :                 if (!class_name) {
    2839            0 :                         continue;
    2840              :                 }
    2841              : 
    2842            8 :                 if (!lsp_inlay_mark_seen(&seen, token_line, variable)) {
    2843            0 :                         zend_string_release(class_name);
    2844              : 
    2845            0 :                         continue;
    2846              :                 }
    2847              : 
    2848            8 :                 hint_offset = (size_t) lsp_token_long(token, "offset", 0) + ZSTR_LEN(variable);
    2849            8 :                 lsp_inlay_add_type_hint(items, document->text, hint_offset, class_name);
    2850            8 :                 zend_string_release(class_name);
    2851            8 :                 emitted++;
    2852              :         }
    2853              : 
    2854            8 :         zend_hash_destroy(&seen);
    2855              : }
    2856              : 
    2857            8 : extern void lsp_lsparrot_inlay_hint(lsp_server *server, zval *return_value, lsp_document *document, zval *params)
    2858              : {
    2859              :         HashTable param_map;
    2860              : 
    2861              :         (void) server;
    2862              : 
    2863            8 :         array_init(return_value);
    2864            8 :         zend_hash_init(&param_map, 32, NULL, ZVAL_PTR_DTOR, 0);
    2865            8 :         lsp_inlay_collect_param_map(document, &param_map);
    2866            8 :         lsp_inlay_add_document_hints(document, &param_map, return_value);
    2867            8 :         zend_hash_destroy(&param_map);
    2868              : 
    2869            8 :         lsp_inlay_add_variable_type_hints(document, params, return_value);
    2870            8 : }
        

Generated by: LCOV version 2.0-1