LCOV - code coverage report
Current view: top level - ext - lsp_server_hover.c (source / functions) Coverage Total Hit
Test: Extension code coverage Lines: 76.4 % 751 574
Test Date: 2026-07-13 00:58:38 Functions: 97.1 % 35 34

            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            4 : static inline zend_string *lsp_hover_generic_base_type(zend_string *type)
      19              : {
      20              :         const char *generic;
      21              :         size_t length;
      22              : 
      23            4 :         generic = memchr(ZSTR_VAL(type), '<', ZSTR_LEN(type));
      24            4 :         if (!generic) {
      25            4 :                 return NULL;
      26              :         }
      27              : 
      28            0 :         length = (size_t) (generic - ZSTR_VAL(type));
      29            0 :         while (length > 0 && isspace((unsigned char) ZSTR_VAL(type)[length - 1])) {
      30            0 :                 length--;
      31              :         }
      32              : 
      33            0 :         if (length == 0) {
      34            0 :                 return NULL;
      35              :         }
      36              : 
      37            0 :         return zend_string_init(ZSTR_VAL(type), length, 0);
      38              : }
      39              : 
      40          196 : static inline void lsp_hover_add_type_source(zend_string **types, smart_str *sources, uint32_t *group_count, zend_string *type, const char *source)
      41              : {
      42              :         uint32_t i;
      43              : 
      44          196 :         if (!type || ZSTR_LEN(type) == 0) {
      45            0 :                 return;
      46              :         }
      47              : 
      48          248 :         for (i = 0; i < *group_count; i++) {
      49           96 :                 if (!zend_string_equals(types[i], type)) {
      50           52 :                         continue;
      51              :                 }
      52              : 
      53           44 :                 if (sources[i].s && ZSTR_LEN(sources[i].s) > 0) {
      54           44 :                         smart_str_appendl(&sources[i], " / ", sizeof(" / ") - 1);
      55              :                 }
      56              : 
      57           44 :                 smart_str_appends(&sources[i], source);
      58              : 
      59           44 :                 return;
      60              :         }
      61              : 
      62          152 :         if (*group_count >= 4) {
      63            0 :                 return;
      64              :         }
      65              : 
      66          152 :         types[*group_count] = zend_string_copy(type);
      67          152 :         smart_str_appends(&sources[*group_count], source);
      68          152 :         (*group_count)++;
      69              : }
      70              : 
      71          112 : static inline void lsp_hover_release_type_groups(zend_string **types, smart_str *sources, uint32_t group_count)
      72              : {
      73              :         uint32_t i;
      74              : 
      75          264 :         for (i = 0; i < group_count; i++) {
      76          152 :                 if (types[i]) {
      77          152 :                         zend_string_release(types[i]);
      78              :                 }
      79              : 
      80          152 :                 smart_str_free(&sources[i]);
      81              :         }
      82          112 : }
      83              : 
      84          112 : static inline void lsp_hover_append_type_groups(smart_str *markdown, zend_string **types, smart_str *sources, uint32_t group_count)
      85              : {
      86              :         uint32_t i;
      87              : 
      88          264 :         for (i = 0; i < group_count; i++) {
      89          152 :                 if (i > 0) {
      90           40 :                         smart_str_appendl(markdown, "\n\n", sizeof("\n\n") - 1);
      91              :                 }
      92              : 
      93          152 :                 smart_str_appendc(markdown, '`');
      94          152 :                 smart_str_append(markdown, types[i]);
      95          152 :                 smart_str_appendl(markdown, "`\n\n", sizeof("`\n\n") - 1);
      96          152 :                 smart_str_0(&sources[i]);
      97          152 :                 smart_str_append(markdown, sources[i].s);
      98              :         }
      99          112 : }
     100              : 
     101          844 : static inline bool lsp_hover_is_word_char(char c)
     102              : {
     103          844 :         return lsp_doc_is_identifier_char(c) || c == '$' || c == '\\';
     104              : }
     105              : 
     106           96 : static inline bool lsp_hover_word_is_variable(zend_string *word)
     107              : {
     108           96 :         return word && ZSTR_LEN(word) > 1 && ZSTR_VAL(word)[0] == '$' && lsp_doc_is_identifier_start(ZSTR_VAL(word)[1]);
     109              : }
     110              : 
     111          116 : static inline bool lsp_hover_word_bounds(zend_string *text, zend_string *word, size_t offset, size_t *word_start, size_t *word_end)
     112              : {
     113              :         const char *value;
     114              :         size_t length, start, end;
     115              : 
     116          116 :         if (!text || !word || ZSTR_LEN(word) == 0) {
     117            0 :                 return false;
     118              :         }
     119              : 
     120          116 :         value = ZSTR_VAL(text);
     121          116 :         length = ZSTR_LEN(text);
     122          116 :         if (offset > length) {
     123            0 :                 offset = length;
     124              :         }
     125              : 
     126          116 :         start = offset;
     127          300 :         while (start > 0 && lsp_hover_is_word_char(value[start - 1])) {
     128          184 :                 start--;
     129              :         }
     130              : 
     131          116 :         end = offset;
     132          544 :         while (end < length && lsp_hover_is_word_char(value[end])) {
     133          428 :                 end++;
     134              :         }
     135              : 
     136          116 :         if (end <= start || end - start != ZSTR_LEN(word) || memcmp(value + start, ZSTR_VAL(word), ZSTR_LEN(word)) != 0) {
     137            0 :                 return false;
     138              :         }
     139              : 
     140          116 :         *word_start = start;
     141          116 :         *word_end = end;
     142              : 
     143          116 :         return true;
     144              : }
     145              : 
     146           60 : static inline zend_string *lsp_hover_assignment_rhs_expression_at(zend_string *text, size_t offset, size_t target_start, size_t target_end)
     147              : {
     148              :         const char *value;
     149              :         size_t length, line_start, line_end, i, rhs_start, rhs_end;
     150              : 
     151           60 :         if (!text) {
     152            0 :                 return NULL;
     153              :         }
     154              : 
     155           60 :         value = ZSTR_VAL(text);
     156           60 :         length = ZSTR_LEN(text);
     157           60 :         if (offset > length) {
     158            0 :                 offset = length;
     159              :         }
     160              : 
     161           60 :         line_start = offset;
     162          632 :         while (line_start > 0 && value[line_start - 1] != '\n' && value[line_start - 1] != '\r') {
     163          572 :                 line_start--;
     164              :         }
     165              : 
     166           60 :         line_end = offset;
     167          936 :         while (line_end < length && value[line_end] != '\n' && value[line_end] != '\r') {
     168          876 :                 line_end++;
     169              :         }
     170              : 
     171           60 :         if (target_start < line_start || target_end > line_end || target_end <= target_start) {
     172            0 :                 return NULL;
     173              :         }
     174              : 
     175           60 :         i = target_end;
     176           80 :         while (i < line_end && isspace((unsigned char) value[i])) {
     177           20 :                 i++;
     178              :         }
     179           60 :         if (i >= line_end || value[i] != '=') {
     180           40 :                 return NULL;
     181              :         }
     182           20 :         if ((i + 1 < line_end && (value[i + 1] == '=' || value[i + 1] == '>')) || (i > line_start && (value[i - 1] == '!' || value[i - 1] == '<' || value[i - 1] == '>'))) {
     183            0 :                 return NULL;
     184              :         }
     185              : 
     186           20 :         rhs_start = i + 1;
     187           40 :         while (rhs_start < line_end && isspace((unsigned char) value[rhs_start])) {
     188           20 :                 rhs_start++;
     189              :         }
     190              : 
     191           20 :         rhs_end = line_end;
     192           20 :         while (rhs_end > rhs_start && isspace((unsigned char) value[rhs_end - 1])) {
     193            0 :                 rhs_end--;
     194              :         }
     195           20 :         if (rhs_end > rhs_start && value[rhs_end - 1] == ';') {
     196           20 :                 rhs_end--;
     197           20 :                 while (rhs_end > rhs_start && isspace((unsigned char) value[rhs_end - 1])) {
     198            0 :                         rhs_end--;
     199              :                 }
     200              :         }
     201              : 
     202           20 :         if (rhs_end <= rhs_start) {
     203            0 :                 return NULL;
     204              :         }
     205              : 
     206           20 :         return zend_string_init(value + rhs_start, rhs_end - rhs_start, 0);
     207              : }
     208              : 
     209           56 : static inline zend_string *lsp_hover_assignment_rhs_expression(zend_string *text, zend_string *word, size_t offset)
     210              : {
     211              :         size_t word_start, word_end;
     212              : 
     213           56 :         if (!lsp_hover_word_bounds(text, word, offset, &word_start, &word_end)) {
     214            0 :                 return NULL;
     215              :         }
     216              : 
     217           56 :         return lsp_hover_assignment_rhs_expression_at(text, offset, word_start, word_end);
     218              : }
     219              : 
     220           60 : static inline zend_string *lsp_hover_member_access_expression(zend_string *text, zend_string *word, size_t offset, size_t *expression_start, size_t *expression_end)
     221              : {
     222              :         const char *value;
     223              :         size_t word_start, word_end, i, receiver_end, start, depth;
     224              : 
     225           60 :         if (!lsp_hover_word_bounds(text, word, offset, &word_start, &word_end)) {
     226            0 :                 return NULL;
     227              :         }
     228              : 
     229           60 :         value = ZSTR_VAL(text);
     230           60 :         i = word_start;
     231          532 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
     232          472 :                 i--;
     233              :         }
     234              : 
     235           60 :         if (i >= 3 && value[i - 3] == '?' && value[i - 2] == '-' && value[i - 1] == '>') {
     236            0 :                 receiver_end = i - 3;
     237           60 :         } else if (i >= 2 && value[i - 2] == '-' && value[i - 1] == '>') {
     238            4 :                 receiver_end = i - 2;
     239              :         } else {
     240           56 :                 return NULL;
     241              :         }
     242              : 
     243            4 :         start = receiver_end;
     244            4 :         depth = 0;
     245           56 :         for (i = receiver_end; i > 0; i--) {
     246           56 :                 if (value[i - 1] == ')' || value[i - 1] == ']' || value[i - 1] == '}') {
     247            0 :                         depth++;
     248            0 :                         continue;
     249              :                 }
     250              : 
     251           56 :                 if (value[i - 1] == '(' || value[i - 1] == '[' || value[i - 1] == '{') {
     252            0 :                         if (depth > 0) {
     253            0 :                                 depth--;
     254            0 :                                 continue;
     255              :                         }
     256              : 
     257            0 :                         break;
     258              :                 }
     259              : 
     260           56 :                 if (depth == 0 && (value[i - 1] == '\n' || value[i - 1] == '\r' || value[i - 1] == ';' || value[i - 1] == '=' || value[i - 1] == ',')) {
     261              :                         break;
     262              :                 }
     263              :         }
     264              : 
     265            4 :         start = i;
     266           36 :         while (start < receiver_end && isspace((unsigned char) value[start])) {
     267           32 :                 start++;
     268              :         }
     269              : 
     270            4 :         if (receiver_end >= start + sizeof("return") - 1 &&
     271            0 :                 memcmp(value + start, "return", sizeof("return") - 1) == 0 &&
     272            0 :                 !lsp_doc_is_identifier_char(value[start + sizeof("return") - 1])
     273              :         ) {
     274            0 :                 start += sizeof("return") - 1;
     275            0 :                 while (start < receiver_end && isspace((unsigned char) value[start])) {
     276            0 :                         start++;
     277              :                 }
     278              :         }
     279              : 
     280            4 :         if (word_end <= start) {
     281            0 :                 return NULL;
     282              :         }
     283              : 
     284            4 :         *expression_start = start;
     285            4 :         *expression_end = word_end;
     286              : 
     287            4 :         return zend_string_init(value + start, word_end - start, 0);
     288              : }
     289              : 
     290           60 : static inline zend_string *lsp_hover_analyzer_expression(zend_string *text, zend_string *word, size_t offset)
     291              : {
     292              :         zend_string *member_expression, *rhs_expression;
     293              :         size_t expression_start, expression_end;
     294              : 
     295           60 :         member_expression = lsp_hover_member_access_expression(text, word, offset, &expression_start, &expression_end);
     296           60 :         if (member_expression) {
     297            4 :                 rhs_expression = lsp_hover_assignment_rhs_expression_at(text, offset, expression_start, expression_end);
     298            4 :                 if (rhs_expression) {
     299            0 :                         zend_string_release(member_expression);
     300              : 
     301            0 :                         return rhs_expression;
     302              :                 }
     303              : 
     304            4 :                 return member_expression;
     305              :         }
     306              : 
     307           56 :         rhs_expression = lsp_hover_assignment_rhs_expression(text, word, offset);
     308           56 :         if (rhs_expression) {
     309           20 :                 return rhs_expression;
     310              :         }
     311              : 
     312           36 :         if (lsp_hover_word_is_variable(word)) {
     313           36 :                 return zend_string_copy(word);
     314              :         }
     315              : 
     316            0 :         return NULL;
     317              : }
     318              : 
     319          112 : static inline zend_string *lsp_hover_type_markdown(lsp_server *server, lsp_document *document, zval *position, zend_string *word, size_t offset, zend_string *primary_type, zend_string *analyzer_expression)
     320              : {
     321              :         zend_string *types[4], *psalm_type, *array_shape_analyzer_type, *phpstan_type, *phpstan_query_type, *phpstan_query_expression, *psalm_query_type, *psalm_ls_query_type, *markdown;
     322          112 :         smart_str sources[4], buffer = {0};
     323              :         uint32_t group_count;
     324              : 
     325          112 :         memset(types, 0, sizeof(types));
     326          112 :         memset(sources, 0, sizeof(sources));
     327          112 :         group_count = 0;
     328          112 :         psalm_type = NULL;
     329          112 :         array_shape_analyzer_type = NULL;
     330          112 :         phpstan_type = NULL;
     331          112 :         phpstan_query_type = NULL;
     332          112 :         phpstan_query_expression = NULL;
     333          112 :         psalm_query_type = NULL;
     334          112 :         psalm_ls_query_type = NULL;
     335              : 
     336          112 :         lsp_hover_add_type_source(types, sources, &group_count, primary_type, "LSParrot Engine");
     337              : 
     338          112 :         if (server->phpstan_enabled || server->psalm_enabled || server->psalm_ls_enabled) {
     339           64 :                 array_shape_analyzer_type = lsp_infer_variable_phpdoc_type(document, word, offset);
     340           64 :                 if (array_shape_analyzer_type && !lsp_phpdoc_type_has_array_shape(array_shape_analyzer_type)) {
     341           32 :                         zend_string_release(array_shape_analyzer_type);
     342           32 :                         array_shape_analyzer_type = NULL;
     343              :                 }
     344              :         }
     345              : 
     346          112 :         if (server->phpstan_enabled) {
     347           60 :                 phpstan_query_expression = analyzer_expression ? zend_string_copy(analyzer_expression) : zend_string_copy(word);
     348           60 :                 phpstan_query_type = lsp_phpstan_type_for_expression(server, document, phpstan_query_expression, offset);
     349           60 :                 zend_string_release(phpstan_query_expression);
     350           60 :                 if (lsp_type_is_unhelpful(phpstan_query_type)) {
     351           32 :                         if (phpstan_query_type) {
     352            0 :                                 zend_string_release(phpstan_query_type);
     353              :                         }
     354           32 :                         phpstan_query_type = NULL;
     355              :                 }
     356           60 :                 phpstan_type = phpstan_query_type ? phpstan_query_type : (array_shape_analyzer_type ? array_shape_analyzer_type : primary_type);
     357           60 :                 lsp_hover_add_type_source(types, sources, &group_count, phpstan_type, "PHPStan");
     358              :         }
     359              : 
     360          112 :         if (server->psalm_enabled || server->psalm_ls_enabled) {
     361           24 :                 if (array_shape_analyzer_type) {
     362            4 :                         psalm_type = zend_string_copy(array_shape_analyzer_type);
     363              :                 } else {
     364           20 :                         psalm_type = lsp_infer_variable_declared_type(server, document, word, offset);
     365              :                 }
     366           24 :                 if (!psalm_type && !array_shape_analyzer_type) {
     367            4 :                         psalm_type = lsp_hover_generic_base_type(primary_type);
     368              :                 }
     369              :         }
     370              : 
     371          112 :         if (server->psalm_enabled) {
     372           20 :                 psalm_query_type = lsp_psalm_type_for_expression(server, document, word, offset);
     373           20 :                 if (lsp_type_is_unhelpful(psalm_query_type)) {
     374           12 :                         if (psalm_query_type) {
     375            0 :                                 zend_string_release(psalm_query_type);
     376              :                         }
     377           12 :                         psalm_query_type = NULL;
     378              :                 }
     379           20 :                 lsp_hover_add_type_source(types, sources, &group_count, psalm_query_type ? psalm_query_type : (psalm_type ? psalm_type : primary_type), "Psalm");
     380              :         }
     381              : 
     382          112 :         if (server->psalm_ls_enabled) {
     383            4 :                 psalm_ls_query_type = lsp_psalm_ls_type_for_position(server, document, position, offset, word);
     384            4 :                 if (lsp_type_is_unhelpful(psalm_ls_query_type)) {
     385            0 :                         if (psalm_ls_query_type) {
     386            0 :                                 zend_string_release(psalm_ls_query_type);
     387              :                         }
     388            0 :                         psalm_ls_query_type = NULL;
     389              :                 }
     390            4 :                 lsp_hover_add_type_source(types, sources, &group_count, psalm_ls_query_type ? psalm_ls_query_type : (psalm_type ? psalm_type : primary_type), "Psalm-LS");
     391              :         }
     392              : 
     393          112 :         lsp_hover_append_type_groups(&buffer, types, sources, group_count);
     394          112 :         smart_str_0(&buffer);
     395          112 :         markdown = buffer.s ? buffer.s : zend_string_init("", 0, 0);
     396          112 :         lsp_hover_release_type_groups(types, sources, group_count);
     397              : 
     398          112 :         if (psalm_type) {
     399           20 :                 zend_string_release(psalm_type);
     400              :         }
     401              : 
     402          112 :         if (array_shape_analyzer_type) {
     403            4 :                 zend_string_release(array_shape_analyzer_type);
     404              :         }
     405              : 
     406          112 :         if (phpstan_query_type) {
     407           28 :                 zend_string_release(phpstan_query_type);
     408              :         }
     409              : 
     410          112 :         if (psalm_query_type) {
     411            8 :                 zend_string_release(psalm_query_type);
     412              :         }
     413              : 
     414          112 :         if (psalm_ls_query_type) {
     415            4 :                 zend_string_release(psalm_ls_query_type);
     416              :         }
     417              : 
     418          112 :         return markdown;
     419              : }
     420              : 
     421           12 : static inline zend_string *lsp_hover_zval_type(zval *value)
     422              : {
     423           12 :         if (!value) {
     424            0 :                 return NULL;
     425              :         }
     426              : 
     427           12 :         switch (Z_TYPE_P(value)) {
     428            0 :                 case IS_NULL:
     429            0 :                         return zend_string_init("null", sizeof("null") - 1, 0);
     430            4 :                 case IS_FALSE:
     431              :                 case IS_TRUE:
     432            4 :                         return zend_string_init("bool", sizeof("bool") - 1, 0);
     433            0 :                 case IS_LONG:
     434            0 :                         return zend_string_init("int", sizeof("int") - 1, 0);
     435            0 :                 case IS_DOUBLE:
     436            0 :                         return zend_string_init("float", sizeof("float") - 1, 0);
     437            8 :                 case IS_STRING:
     438            8 :                         return zend_string_init("string", sizeof("string") - 1, 0);
     439            0 :                 case IS_ARRAY:
     440            0 :                         return zend_string_init("array", sizeof("array") - 1, 0);
     441            0 :                 case IS_OBJECT:
     442            0 :                         if (Z_OBJCE_P(value) && Z_OBJCE_P(value)->name) {
     443            0 :                                 return zend_string_copy(Z_OBJCE_P(value)->name);
     444              :                         }
     445              : 
     446            0 :                         return zend_string_init("object", sizeof("object") - 1, 0);
     447            0 :                 case IS_RESOURCE:
     448            0 :                         return zend_string_init("resource", sizeof("resource") - 1, 0);
     449            0 :                 default:
     450            0 :                         return zend_string_init("mixed", sizeof("mixed") - 1, 0);
     451              :         }
     452              : }
     453              : 
     454          124 : static inline zend_string *lsp_hover_normalized_global_name(zend_string *word)
     455              : {
     456          124 :         if (ZSTR_LEN(word) > 0 && ZSTR_VAL(word)[0] == '\\') {
     457           36 :                 return zend_string_init(ZSTR_VAL(word) + 1, ZSTR_LEN(word) - 1, 0);
     458              :         }
     459              : 
     460           88 :         return zend_string_copy(word);
     461              : }
     462              : 
     463           60 : static inline zend_string *lsp_hover_builtin_constant_type(zend_string *word)
     464              : {
     465              :         zend_string *name;
     466              :         zval *value;
     467              : 
     468           60 :         if (ZSTR_LEN(word) == 0) {
     469            0 :                 return NULL;
     470              :         }
     471              : 
     472           60 :         name = lsp_hover_normalized_global_name(word);
     473           60 :         value = zend_get_constant(name);
     474           60 :         zend_string_release(name);
     475           60 :         if (!value) {
     476           48 :                 return NULL;
     477              :         }
     478              : 
     479           12 :         return lsp_hover_zval_type(value);
     480              : }
     481              : 
     482            8 : static inline zend_string *lsp_hover_literal_token_type(zval *token)
     483              : {
     484              :         zend_string *text;
     485              : 
     486            8 :         if (!token || Z_TYPE_P(token) != IS_ARRAY) {
     487            0 :                 return NULL;
     488              :         }
     489              : 
     490            8 :         if (lsp_token_name_equals(token, "T_LNUMBER")) {
     491            8 :                 return zend_string_init("int", sizeof("int") - 1, 0);
     492              :         }
     493              : 
     494            0 :         if (lsp_token_name_equals(token, "T_DNUMBER")) {
     495            0 :                 return zend_string_init("float", sizeof("float") - 1, 0);
     496              :         }
     497              : 
     498            0 :         if (lsp_token_name_equals(token, "T_CONSTANT_ENCAPSED_STRING")) {
     499            0 :                 return zend_string_init("string", sizeof("string") - 1, 0);
     500              :         }
     501              : 
     502            0 :         if (lsp_token_is_char(token, '[')) {
     503            0 :                 return zend_string_init("array", sizeof("array") - 1, 0);
     504              :         }
     505              : 
     506            0 :         text = lsp_token_string(token, "text");
     507            0 :         if (!text) {
     508            0 :                 return NULL;
     509              :         }
     510              : 
     511            0 :         if (zend_string_equals_literal_ci(text, "true") || zend_string_equals_literal_ci(text, "false")) {
     512            0 :                 return zend_string_init("bool", sizeof("bool") - 1, 0);
     513              :         }
     514              : 
     515            0 :         if (zend_string_equals_literal_ci(text, "null")) {
     516            0 :                 return zend_string_init("null", sizeof("null") - 1, 0);
     517              :         }
     518              : 
     519            0 :         if (zend_string_equals_literal_ci(text, "array")) {
     520            0 :                 return zend_string_init("array", sizeof("array") - 1, 0);
     521              :         }
     522              : 
     523            0 :         return NULL;
     524              : }
     525              : 
     526           48 : static inline zend_string *lsp_hover_current_document_constant_type(lsp_document *document, zend_string *word)
     527              : {
     528              :         zend_string *label, *name;
     529              :         zval *tokens_zv, *token, *value_token;
     530              :         HashTable *tokens;
     531              :         uint32_t i, j, count;
     532              :         size_t token_offset;
     533              :         bool found_equals;
     534              : 
     535           48 :         tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
     536           48 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
     537            0 :                 return NULL;
     538              :         }
     539              : 
     540           48 :         tokens = Z_ARRVAL_P(tokens_zv);
     541           48 :         count = zend_hash_num_elements(tokens);
     542           48 :         name = lsp_hover_normalized_global_name(word);
     543         2988 :         for (i = 0; i < count; i++) {
     544         2948 :                 token = zend_hash_index_find(tokens, i);
     545         2948 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_CONST")) {
     546         2932 :                         continue;
     547              :                 }
     548              : 
     549           16 :                 token_offset = (size_t) lsp_token_long(token, "offset", 0);
     550           16 :                 if (lsp_offset_is_inside_class_body(document->text, token_offset)) {
     551            0 :                         continue;
     552              :                 }
     553              : 
     554           16 :                 label = lsp_next_string_token(tokens, i + 1);
     555           16 :                 if (!label || !zend_string_equals(label, name)) {
     556            8 :                         continue;
     557              :                 }
     558              : 
     559            8 :                 found_equals = false;
     560           48 :                 for (j = i + 1; j < count; j++) {
     561           48 :                         value_token = zend_hash_index_find(tokens, j);
     562           48 :                         if (!value_token || Z_TYPE_P(value_token) != IS_ARRAY) {
     563            0 :                                 continue;
     564              :                         }
     565              : 
     566           48 :                         if (lsp_token_is_char(value_token, ';') || lsp_token_is_char(value_token, ',')) {
     567              :                                 break;
     568              :                         }
     569              : 
     570           48 :                         if (!found_equals) {
     571           32 :                                 if (lsp_token_is_char(value_token, '=')) {
     572            8 :                                         found_equals = true;
     573              :                                 }
     574              : 
     575           32 :                                 continue;
     576              :                         }
     577              : 
     578           16 :                         if (lsp_token_name_equals(value_token, "T_WHITESPACE")) {
     579            8 :                                 continue;
     580              :                         }
     581              : 
     582            8 :                         zend_string_release(name);
     583              : 
     584            8 :                         return lsp_hover_literal_token_type(value_token);
     585              :                 }
     586              :         }
     587              : 
     588           40 :         zend_string_release(name);
     589              : 
     590           40 :         return NULL;
     591              : }
     592              : 
     593           60 : static inline zend_string *lsp_hover_constant_type(lsp_document *document, zend_string *word)
     594              : {
     595              :         zend_string *type;
     596              : 
     597           60 :         type = lsp_hover_builtin_constant_type(word);
     598           60 :         if (type) {
     599           12 :                 return type;
     600              :         }
     601              : 
     602           48 :         return lsp_hover_current_document_constant_type(document, word);
     603              : }
     604              : 
     605           20 : static inline zend_string *lsp_hover_constant_markdown(zend_string *word, zend_string *type)
     606              : {
     607           20 :         return strpprintf(0, "`%s`\n\nLSParrot Engine constant %s", ZSTR_VAL(type), ZSTR_VAL(word));
     608              : }
     609              : 
     610           16 : static inline zend_string *lsp_hover_builtin_symbol_markdown(zend_string *word)
     611              : {
     612              :         zend_string *name, *lookup, *markdown;
     613              :         zend_function *function;
     614              :         zend_class_entry *ce;
     615              : 
     616           16 :         if (ZSTR_LEN(word) == 0) {
     617            0 :                 return NULL;
     618              :         }
     619              : 
     620           16 :         name = lsp_hover_normalized_global_name(word);
     621           16 :         if (ZSTR_LEN(name) == 0) {
     622            0 :                 zend_string_release(name);
     623              : 
     624            0 :                 return NULL;
     625              :         }
     626              : 
     627           16 :         lookup = zend_string_tolower(name);
     628           16 :         function = CG(function_table) ? zend_hash_find_ptr(CG(function_table), lookup) : NULL;
     629           16 :         zend_string_release(lookup);
     630           16 :         if (function && function->common.function_name) {
     631            4 :                 markdown = strpprintf(0, "`function %s(...)`\n\nLSParrot Engine", ZSTR_VAL(function->common.function_name));
     632            4 :                 zend_string_release(name);
     633              : 
     634            4 :                 return markdown;
     635              :         }
     636              : 
     637           12 :         ce = zend_lookup_class(name);
     638           12 :         if (ce && ce->name) {
     639            4 :                 markdown = strpprintf(0, "`%s%s`\n\nLSParrot Engine", lsp_builtin_class_detail_prefix(ce), ZSTR_VAL(ce->name));
     640            4 :                 zend_string_release(name);
     641              : 
     642            4 :                 return markdown;
     643              :         }
     644              : 
     645            8 :         zend_string_release(name);
     646              : 
     647            8 :         return NULL;
     648              : }
     649              : 
     650          176 : static inline bool lsp_hover_offset_in_string_literal(lsp_document *document, size_t offset)
     651              : {
     652              :         zend_long id;
     653              :         zval *tokens_zv, *token;
     654              :         HashTable *tokens;
     655              :         uint32_t i, count;
     656              :         size_t token_offset, token_length;
     657              : 
     658          176 :         tokens_zv = Z_TYPE(document->lsparrot) == IS_ARRAY
     659          176 :                 ? zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1)
     660          176 :                 : NULL
     661              :         ;
     662          176 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
     663            0 :                 return false;
     664              :         }
     665              : 
     666          176 :         tokens = Z_ARRVAL_P(tokens_zv);
     667          176 :         count = zend_hash_num_elements(tokens);
     668         7416 :         for (i = 0; i < count; i++) {
     669         7412 :                 token = zend_hash_index_find(tokens, i);
     670         7412 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
     671            0 :                         continue;
     672              :                 }
     673              : 
     674         7412 :                 token_offset = (size_t) lsp_token_long(token, "offset", 0);
     675         7412 :                 token_length = (size_t) lsp_token_long(token, "length", 0);
     676         7412 :                 if (offset < token_offset || offset >= token_offset + token_length) {
     677         7240 :                         continue;
     678              :                 }
     679              : 
     680          172 :                 id = lsp_token_long(token, "id", 0);
     681              : 
     682          168 :                 return id == T_CONSTANT_ENCAPSED_STRING ||
     683          168 :                         id == T_ENCAPSED_AND_WHITESPACE ||
     684          340 :                         id == T_START_HEREDOC ||
     685              :                         id == T_END_HEREDOC
     686              :                 ;
     687              :         }
     688              : 
     689            4 :         return false;
     690              : }
     691              : 
     692              : /* Hover for a property declaration ("private string $secret = ...;"):
     693              :  * surface the declared type instead of the generic "variable" detail. */
     694           20 : static inline zend_string *lsp_hover_property_declaration_markdown(lsp_document *document, zend_string *word, size_t offset)
     695              : {
     696           20 :         zend_long body_depth = 0;
     697              :         zend_string *text_value, *detail, *markdown;
     698              :         zval *tokens_zv, *token;
     699              :         HashTable *tokens;
     700              :         uint32_t i, count;
     701           20 :         size_t class_start = 0, body_start = 0, body_end = 0, token_offset, token_length;
     702              : 
     703           20 :         if (ZSTR_LEN(word) == 0 || ZSTR_VAL(word)[0] != '$') {
     704           16 :                 return NULL;
     705              :         }
     706              : 
     707            4 :         if (!lsp_find_enclosing_class_header(document->text, offset, &class_start, &body_start, &body_end, &body_depth)) {
     708            0 :                 return NULL;
     709              :         }
     710              :         (void) class_start;
     711              : 
     712            4 :         tokens_zv = Z_TYPE(document->lsparrot) == IS_ARRAY
     713            4 :                 ? zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1)
     714            4 :                 : NULL
     715              :         ;
     716            4 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
     717            0 :                 return NULL;
     718              :         }
     719              : 
     720            4 :         tokens = Z_ARRVAL_P(tokens_zv);
     721            4 :         count = zend_hash_num_elements(tokens);
     722           68 :         for (i = 0; i < count; i++) {
     723           68 :                 token = zend_hash_index_find(tokens, i);
     724           68 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_VARIABLE")) {
     725           64 :                         continue;
     726              :                 }
     727              : 
     728            4 :                 token_offset = (size_t) lsp_token_long(token, "offset", 0);
     729            4 :                 token_length = (size_t) lsp_token_long(token, "length", 0);
     730            4 :                 if (offset < token_offset || offset > token_offset + token_length) {
     731            0 :                         continue;
     732              :                 }
     733              : 
     734            4 :                 text_value = lsp_token_string(token, "text");
     735            4 :                 if (!text_value || !zend_string_equals(text_value, word)) {
     736            0 :                         return NULL;
     737              :                 }
     738              : 
     739            4 :                 if (!lsp_token_is_property_declaration(tokens, i, document->text, body_depth)) {
     740            0 :                         return NULL;
     741              :                 }
     742              : 
     743            4 :                 detail = lsp_property_completion_detail(document->text, token, false);
     744            4 :                 if (!detail) {
     745            0 :                         return NULL;
     746              :                 }
     747              : 
     748            4 :                 markdown = strpprintf(0, "`%s`\n\nLSParrot Engine", ZSTR_VAL(detail));
     749            4 :                 zend_string_release(detail);
     750              : 
     751            4 :                 return markdown;
     752              :         }
     753              : 
     754            0 :         return NULL;
     755              : }
     756              : 
     757              : /* Look up a member declared by the class enclosing the given offset,
     758              :  * including private members (which the member cache excludes). */
     759            4 : static inline zend_string *lsp_hover_own_member_detail(lsp_document *document, zend_string *member, size_t offset)
     760              : {
     761            4 :         zend_long body_depth = 0;
     762              :         zend_string *text_value, *property_name, *detail;
     763              :         zval *tokens_zv, *token, *name_token;
     764              :         HashTable *tokens;
     765              :         uint32_t i, count, name_index;
     766            4 :         size_t class_start = 0, body_start = 0, body_end = 0;
     767              : 
     768            4 :         if (!lsp_find_enclosing_class_header(document->text, offset, &class_start, &body_start, &body_end, &body_depth)) {
     769            0 :                 return NULL;
     770              :         }
     771              : 
     772            4 :         tokens_zv = Z_TYPE(document->lsparrot) == IS_ARRAY
     773            4 :                 ? zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1)
     774            4 :                 : NULL
     775              :         ;
     776            4 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
     777            0 :                 return NULL;
     778              :         }
     779              : 
     780            4 :         property_name = strpprintf(0, "$%s", ZSTR_VAL(member));
     781            4 :         tokens = Z_ARRVAL_P(tokens_zv);
     782            4 :         count = zend_hash_num_elements(tokens);
     783            4 :         detail = NULL;
     784              : 
     785           72 :         for (i = 0; i < count && !detail; i++) {
     786           68 :                 token = zend_hash_index_find(tokens, i);
     787           68 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_in_bounds(token, body_start, body_end)) {
     788           44 :                         continue;
     789              :                 }
     790              : 
     791           24 :                 if (lsp_token_name_equals(token, "T_VARIABLE")) {
     792            4 :                         text_value = lsp_token_string(token, "text");
     793            8 :                         if (text_value && zend_string_equals(text_value, property_name) &&
     794            8 :                                 lsp_token_at_depth(document->text, token, body_depth) &&
     795            4 :                                 lsp_token_is_property_declaration(tokens, i, document->text, body_depth)
     796              :                         ) {
     797            4 :                                 detail = lsp_property_completion_detail(document->text, token, false);
     798              :                         }
     799              : 
     800            4 :                         continue;
     801              :                 }
     802              : 
     803           20 :                 if (!lsp_token_name_equals(token, "T_FUNCTION") || !lsp_token_at_depth(document->text, token, body_depth)) {
     804           20 :                         continue;
     805              :                 }
     806              : 
     807            0 :                 name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
     808            0 :                 text_value = lsp_token_string(name_token, "text");
     809            0 :                 if (text_value && zend_string_equals(text_value, member)) {
     810            0 :                         detail = lsp_function_signature_detail(document->text, name_token, tokens, name_index, body_depth, NULL);
     811              :                 }
     812              :         }
     813              : 
     814            4 :         zend_string_release(property_name);
     815              : 
     816            4 :         return detail;
     817              : }
     818              : 
     819              : /* Hover for "$receiver->member" / "$this->member": resolve the receiver
     820              :  * class and surface the member declaration (type or signature) from the
     821              :  * class member cache. */
     822           36 : static inline zend_string *lsp_hover_member_access_markdown(lsp_server *server, lsp_document *document, zend_string *word, size_t offset)
     823              : {
     824           36 :         const char *value = ZSTR_VAL(document->text);
     825           36 :         zend_long body_depth = 0;
     826              :         zend_string *class_name, *member_prefix, *detail, *markdown;
     827           36 :         size_t word_start, arrow_end, receiver_end, class_start = 0, body_start = 0, body_end = 0;
     828              : 
     829           36 :         if (ZSTR_LEN(word) == 0 || ZSTR_VAL(word)[0] == '$' || offset > ZSTR_LEN(document->text)) {
     830            4 :                 return NULL;
     831              :         }
     832              : 
     833           32 :         word_start = offset;
     834          148 :         while (word_start > 0 && lsp_doc_is_identifier_char(value[word_start - 1])) {
     835          116 :                 word_start--;
     836              :         }
     837              : 
     838           32 :         arrow_end = word_start;
     839           36 :         while (arrow_end > 0 && isspace((unsigned char) value[arrow_end - 1])) {
     840            4 :                 arrow_end--;
     841              :         }
     842              : 
     843           32 :         if (arrow_end < 2 || value[arrow_end - 1] != '>' || value[arrow_end - 2] != '-') {
     844           16 :                 return NULL;
     845              :         }
     846              : 
     847           16 :         class_name = NULL;
     848              :         /* The completion-oriented context helpers expect the offset at the END
     849              :          * of the typed prefix; hover positions land mid-word, so realign. */
     850           16 :         if (lsp_member_access_class_context(server, document, word_start + ZSTR_LEN(word), word, &class_name, &member_prefix)) {
     851            4 :                 zend_string_release(member_prefix);
     852              :         } else {
     853              :                 /* "$this->" receivers resolve to the enclosing class. */
     854           12 :                 if (!lsp_find_enclosing_class_header(document->text, offset, &class_start, &body_start, &body_end, &body_depth)) {
     855            0 :                         return NULL;
     856              :                 }
     857              : 
     858           12 :                 class_name = lsp_class_declared_name(document->text, class_start, body_start);
     859              :         }
     860              : 
     861           16 :         if (!class_name) {
     862            0 :                 return NULL;
     863              :         }
     864              : 
     865              :         /* Search the ancestor chain and traits, not only the receiver class. */
     866           16 :         detail = lsp_inherited_member_detail(server, class_name, word);
     867           16 :         zend_string_release(class_name);
     868              : 
     869           16 :         if (!detail) {
     870              :                 /* The member cache only holds public/protected members. "$this->"
     871              :                  * receivers may legally access private members, so scan the
     872              :                  * enclosing class declaration directly. */
     873            4 :                 receiver_end = arrow_end >= 2 ? arrow_end - 2 : 0;
     874            4 :                 if (receiver_end >= sizeof("$this") - 1 &&
     875            4 :                         memcmp(value + receiver_end - (sizeof("$this") - 1), "$this", sizeof("$this") - 1) == 0
     876              :                 ) {
     877            4 :                         detail = lsp_hover_own_member_detail(document, word, offset);
     878              :                 }
     879              :         }
     880              : 
     881           16 :         if (!detail) {
     882            0 :                 return NULL;
     883              :         }
     884              : 
     885           16 :         markdown = strpprintf(0, "`%s`\n\nLSParrot Engine", ZSTR_VAL(detail));
     886           16 :         zend_string_release(detail);
     887              : 
     888           16 :         return markdown;
     889              : }
     890              : 
     891              : /* ------------------------------------------------------------------------
     892              :  * Rich symbol hover: resolve the hovered word through the project symbol
     893              :  * index and render "FQCN: declaration header" (e.g. "Foo\\Bar: abstract
     894              :  * class Bar implements Baz"), reading the real declaration from the
     895              :  * defining file. Covers class-likes, functions, and constants, including
     896              :  * use sites in other files.
     897              :  * ------------------------------------------------------------------------ */
     898              : 
     899            4 : static inline zend_string *lsp_hover_collapse_whitespace(const char *value, size_t length)
     900              : {
     901            4 :         smart_str collapsed = {0};
     902              :         size_t i;
     903            4 :         bool in_space = false;
     904              : 
     905            4 :         while (length > 0 && isspace((unsigned char) value[0])) {
     906            0 :                 value++;
     907            0 :                 length--;
     908              :         }
     909              : 
     910            4 :         while (length > 0 && isspace((unsigned char) value[length - 1])) {
     911            0 :                 length--;
     912              :         }
     913              : 
     914          208 :         for (i = 0; i < length; i++) {
     915          204 :                 if (isspace((unsigned char) value[i])) {
     916           16 :                         in_space = true;
     917           16 :                         continue;
     918              :                 }
     919              : 
     920          188 :                 if (in_space && collapsed.s) {
     921           16 :                         smart_str_appendc(&collapsed, ' ');
     922              :                 }
     923          188 :                 in_space = false;
     924          188 :                 smart_str_appendc(&collapsed, value[i]);
     925              :         }
     926              : 
     927            4 :         smart_str_0(&collapsed);
     928              : 
     929            4 :         return collapsed.s ? collapsed.s : zend_string_init("", 0, 0);
     930              : }
     931              : 
     932              : /* Extend a class-like header start backwards over declaration modifiers
     933              :  * (abstract / final / readonly) so they appear in the hover. */
     934            4 : static inline size_t lsp_hover_header_start_with_modifiers(zend_string *text, size_t class_start)
     935              : {
     936              :         static const char *modifiers[] = { "abstract", "final", "readonly" };
     937            4 :         const char *value = ZSTR_VAL(text);
     938            4 :         size_t start = class_start, word_start, word_length, i;
     939              :         bool matched;
     940              : 
     941              :         for (;;) {
     942            8 :                 word_start = start;
     943           20 :                 while (word_start > 0 && isspace((unsigned char) value[word_start - 1])) {
     944           12 :                         word_start--;
     945              :                 }
     946              : 
     947            8 :                 word_length = 0;
     948           40 :                 while (word_start > word_length && lsp_doc_is_identifier_char(value[word_start - word_length - 1])) {
     949           32 :                         word_length++;
     950              :                 }
     951              : 
     952            8 :                 if (word_length == 0) {
     953            4 :                         return start;
     954              :                 }
     955              : 
     956            4 :                 matched = false;
     957            4 :                 for (i = 0; i < sizeof(modifiers) / sizeof(modifiers[0]); i++) {
     958            4 :                         if (word_length == strlen(modifiers[i]) &&
     959            4 :                                 strncasecmp(value + word_start - word_length, modifiers[i], word_length) == 0
     960              :                         ) {
     961            4 :                                 matched = true;
     962            4 :                                 break;
     963              :                         }
     964              :                 }
     965              : 
     966            4 :                 if (!matched) {
     967            0 :                         return start;
     968              :                 }
     969              : 
     970            4 :                 start = word_start - word_length;
     971              :         }
     972              : }
     973              : 
     974            8 : static inline zend_string *lsp_hover_symbol_file_contents(lsp_server *server, zend_string *path, bool *owns_contents)
     975              : {
     976              :         lsp_document *document;
     977              : 
     978            8 :         document = lsp_document_for_path(server, path);
     979            8 :         if (document) {
     980            0 :                 *owns_contents = false;
     981              : 
     982            0 :                 return document->text;
     983              :         }
     984              : 
     985            8 :         *owns_contents = true;
     986              : 
     987            8 :         return lsp_read_file(path);
     988              : }
     989              : 
     990            4 : static inline zend_string *lsp_hover_class_declaration_header(zend_string *contents, zend_string *fqcn)
     991              : {
     992            4 :         zend_long body_depth = 0;
     993              :         zend_string *header;
     994            4 :         size_t class_start = 0, body_start = 0, body_end = 0, header_start, header_end;
     995              : 
     996              :         /* lsp_find_class_header_for_name compares against the RESOLVED declared
     997              :          * name, so it expects the fully qualified class name. */
     998            4 :         if (!lsp_find_class_header_for_name(contents, fqcn, &class_start, &body_start, &body_end, &body_depth)) {
     999            0 :                 return NULL;
    1000              :         }
    1001              : 
    1002            4 :         header_start = lsp_hover_header_start_with_modifiers(contents, class_start);
    1003            4 :         header_end = body_start;
    1004           12 :         while (header_end > header_start && (ZSTR_VAL(contents)[header_end - 1] == '{' || isspace((unsigned char) ZSTR_VAL(contents)[header_end - 1]))) {
    1005            8 :                 header_end--;
    1006              :         }
    1007              : 
    1008            4 :         if (header_end <= header_start) {
    1009            0 :                 return NULL;
    1010              :         }
    1011              : 
    1012            4 :         header = lsp_hover_collapse_whitespace(ZSTR_VAL(contents) + header_start, header_end - header_start);
    1013            4 :         if (ZSTR_LEN(header) == 0) {
    1014            0 :                 zend_string_release(header);
    1015              : 
    1016            0 :                 return NULL;
    1017              :         }
    1018              : 
    1019            4 :         return header;
    1020              : }
    1021              : 
    1022            4 : static inline zend_string *lsp_hover_function_declaration_header(zend_string *contents, const char *short_name, size_t short_length)
    1023              : {
    1024              :         zend_string *label, *detail;
    1025              :         zval tokens_zv, *token, *name_token;
    1026              :         HashTable *tokens;
    1027              :         uint32_t i, count, name_index;
    1028              : 
    1029            4 :         ZVAL_UNDEF(&tokens_zv);
    1030            4 :         lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
    1031            4 :         if (Z_TYPE(tokens_zv) != IS_ARRAY) {
    1032            0 :                 if (!Z_ISUNDEF(tokens_zv)) {
    1033            0 :                         zval_ptr_dtor(&tokens_zv);
    1034              :                 }
    1035              : 
    1036            0 :                 return NULL;
    1037              :         }
    1038              : 
    1039            4 :         detail = NULL;
    1040            4 :         tokens = Z_ARRVAL(tokens_zv);
    1041            4 :         count = zend_hash_num_elements(tokens);
    1042           28 :         for (i = 0; i < count; i++) {
    1043           28 :                 token = zend_hash_index_find(tokens, i);
    1044           28 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_FUNCTION")) {
    1045           24 :                         continue;
    1046              :                 }
    1047              : 
    1048            4 :                 name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
    1049            4 :                 label = lsp_token_string(name_token, "text");
    1050            4 :                 if (!label || ZSTR_LEN(label) != short_length || strncasecmp(ZSTR_VAL(label), short_name, short_length) != 0) {
    1051            0 :                         continue;
    1052              :                 }
    1053              : 
    1054            4 :                 detail = lsp_function_signature_detail(contents, name_token, tokens, name_index, 0, NULL);
    1055            4 :                 if (detail) {
    1056            4 :                         label = strpprintf(0, "function %s", ZSTR_VAL(detail));
    1057            4 :                         zend_string_release(detail);
    1058            4 :                         detail = label;
    1059              :                 }
    1060            4 :                 break;
    1061              :         }
    1062              : 
    1063            4 :         zval_ptr_dtor(&tokens_zv);
    1064              : 
    1065            4 :         return detail;
    1066              : }
    1067              : 
    1068            0 : static inline const char *lsp_hover_symbol_kind_word(char kind)
    1069              : {
    1070            0 :         switch (kind) {
    1071            0 :                 case LSP_SYMBOL_INTERFACE:
    1072            0 :                         return "interface";
    1073            0 :                 case LSP_SYMBOL_TRAIT:
    1074            0 :                         return "trait";
    1075            0 :                 case LSP_SYMBOL_ENUM:
    1076            0 :                         return "enum";
    1077            0 :                 case LSP_SYMBOL_FUNCTION:
    1078            0 :                         return "function";
    1079            0 :                 case LSP_SYMBOL_CONSTANT:
    1080            0 :                         return "const";
    1081              :         }
    1082              : 
    1083            0 :         return "class";
    1084              : }
    1085              : 
    1086           12 : static inline zend_string *lsp_hover_symbol_markdown_for(lsp_server *server, zend_string *fqcn, char expected_kind)
    1087              : {
    1088              :         const char *path_value, *short_name, *stored_value;
    1089              :         zend_string *path, *contents, *header, *markdown, *stored;
    1090           12 :         size_t path_length = 0, short_length;
    1091           12 :         char found_kind = '\0';
    1092           12 :         bool owns_contents = false;
    1093              : 
    1094           12 :         stored_value = NULL;
    1095           12 :         path_value = lsp_symbol_index_lookup(&server->symbol_index, expected_kind, fqcn, &found_kind, &stored_value, &path_length);
    1096           12 :         if (!path_value || path_length == 0) {
    1097            4 :                 return NULL;
    1098              :         }
    1099              : 
    1100              :         /* Display the symbol with the casing it was declared with, regardless of
    1101              :          * how the hovered reference happens to be spelled (lookups are
    1102              :          * case-insensitive). */
    1103            8 :         stored = stored_value ? zend_string_init(stored_value, strlen(stored_value), 0) : zend_string_copy(fqcn);
    1104            8 :         short_name = lsp_basename_from_fqcn(ZSTR_VAL(stored), ZSTR_LEN(stored), &short_length);
    1105            8 :         path = zend_string_init(path_value, path_length, 0);
    1106            8 :         contents = lsp_hover_symbol_file_contents(server, path, &owns_contents);
    1107            8 :         header = NULL;
    1108              : 
    1109            8 :         if (contents != zend_empty_string) {
    1110            8 :                 if (found_kind == LSP_SYMBOL_FUNCTION) {
    1111            4 :                         header = lsp_hover_function_declaration_header(contents, short_name, short_length);
    1112            4 :                 } else if (found_kind != LSP_SYMBOL_CONSTANT) {
    1113            4 :                         header = lsp_hover_class_declaration_header(contents, stored);
    1114              :                 }
    1115              :         }
    1116              : 
    1117            8 :         if (!header) {
    1118            0 :                 header = strpprintf(0, "%s %.*s", lsp_hover_symbol_kind_word(found_kind), (int) short_length, short_name);
    1119              :         }
    1120              : 
    1121            8 :         markdown = strpprintf(0, "`%s: %s`\n\nLSParrot Engine", ZSTR_VAL(stored), ZSTR_VAL(header));
    1122              : 
    1123            8 :         zend_string_release(header);
    1124            8 :         if (owns_contents && contents != zend_empty_string) {
    1125            8 :                 zend_string_release(contents);
    1126              :         }
    1127            8 :         zend_string_release(path);
    1128            8 :         zend_string_release(stored);
    1129              : 
    1130            8 :         return markdown;
    1131              : }
    1132              : 
    1133            8 : static inline zend_string *lsp_hover_project_symbol_markdown(lsp_server *server, lsp_document *document, zend_string *word, size_t offset)
    1134              : {
    1135              :         zend_string *resolved, *markdown;
    1136              : 
    1137            8 :         if (ZSTR_LEN(word) == 0 || ZSTR_VAL(word)[0] == '$' || !lsp_doc_is_identifier_start(ZSTR_VAL(word)[ZSTR_VAL(word)[0] == '\\' ? 1 : 0])) {
    1138            0 :                 return NULL;
    1139              :         }
    1140              : 
    1141            8 :         lsp_index_join_worker(server);
    1142              : 
    1143            8 :         resolved = lsp_resolve_class_name_at(document->text, word, offset);
    1144              : 
    1145            8 :         markdown = NULL;
    1146            8 :         if (resolved) {
    1147            8 :                 markdown = lsp_hover_symbol_markdown_for(server, resolved, LSP_SYMBOL_CLASS);
    1148            8 :                 if (!markdown) {
    1149            4 :                         markdown = lsp_hover_symbol_markdown_for(server, resolved, LSP_SYMBOL_FUNCTION);
    1150              :                 }
    1151            8 :                 if (!markdown) {
    1152            0 :                         markdown = lsp_hover_symbol_markdown_for(server, resolved, LSP_SYMBOL_CONSTANT);
    1153              :                 }
    1154            8 :                 zend_string_release(resolved);
    1155              :         }
    1156              : 
    1157            8 :         if (!markdown) {
    1158              :                 /* Unqualified functions and constants fall back to the global
    1159              :                  * namespace in PHP; probe the raw word as well. */
    1160            0 :                 markdown = lsp_hover_symbol_markdown_for(server, word, LSP_SYMBOL_FUNCTION);
    1161              :         }
    1162            8 :         if (!markdown) {
    1163            0 :                 markdown = lsp_hover_symbol_markdown_for(server, word, LSP_SYMBOL_CONSTANT);
    1164              :         }
    1165              : 
    1166            8 :         return markdown;
    1167              : }
    1168              : 
    1169              : /* Hover on the declaration site of an enum case (`case Red = 'r';`). */
    1170           20 : static inline zend_string *lsp_hover_enum_case_declaration_markdown(lsp_server *server, lsp_document *document, zend_string *word, size_t offset)
    1171              : {
    1172           20 :         const char *value = ZSTR_VAL(document->text);
    1173           20 :         zend_long body_depth = 0;
    1174              :         zend_string *enclosing, *detail, *markdown;
    1175           20 :         size_t word_start, kw_end, kw_start, class_start = 0, body_start = 0, body_end = 0;
    1176              : 
    1177           20 :         if (ZSTR_LEN(word) == 0 || offset > ZSTR_LEN(document->text)) {
    1178            0 :                 return NULL;
    1179              :         }
    1180              : 
    1181           20 :         word_start = offset;
    1182          104 :         while (word_start > 0 && lsp_doc_is_identifier_char(value[word_start - 1])) {
    1183           84 :                 word_start--;
    1184              :         }
    1185              : 
    1186           20 :         kw_end = word_start;
    1187           24 :         while (kw_end > 0 && isspace((unsigned char) value[kw_end - 1])) {
    1188            4 :                 kw_end--;
    1189              :         }
    1190           20 :         kw_start = kw_end;
    1191           44 :         while (kw_start > 0 && lsp_doc_is_identifier_char(value[kw_start - 1])) {
    1192           24 :                 kw_start--;
    1193              :         }
    1194           20 :         if (kw_end - kw_start != sizeof("case") - 1 || strncasecmp(value + kw_start, "case", sizeof("case") - 1) != 0) {
    1195           20 :                 return NULL;
    1196              :         }
    1197              : 
    1198            0 :         if (!lsp_find_enclosing_class_header(document->text, offset, &class_start, &body_start, &body_end, &body_depth)) {
    1199            0 :                 return NULL;
    1200              :         }
    1201              : 
    1202            0 :         enclosing = lsp_class_declared_name(document->text, class_start, body_start);
    1203            0 :         if (!enclosing) {
    1204            0 :                 return NULL;
    1205              :         }
    1206              : 
    1207            0 :         detail = lsp_inherited_member_detail(server, enclosing, word);
    1208            0 :         zend_string_release(enclosing);
    1209            0 :         if (!detail) {
    1210            0 :                 return NULL;
    1211              :         }
    1212              : 
    1213            0 :         markdown = strpprintf(0, "`%s`\n\nLSParrot Engine", ZSTR_VAL(detail));
    1214            0 :         zend_string_release(detail);
    1215              : 
    1216            0 :         return markdown;
    1217              : }
    1218              : 
    1219              : /* Hover for "Receiver::word" (class constants, enum cases, static members,
    1220              :  * parent::/self::/static:: calls): resolve the receiver class and surface the
    1221              :  * member declaration from the inheritance-aware member cache. */
    1222           40 : static inline zend_string *lsp_hover_static_member_markdown(lsp_server *server, lsp_document *document, zend_string *word, size_t offset)
    1223              : {
    1224              :         zend_string *class_name, *lookup, *detail, *markdown;
    1225              :         bool public_only;
    1226              : 
    1227           40 :         if (ZSTR_LEN(word) == 0) {
    1228            0 :                 return NULL;
    1229              :         }
    1230              : 
    1231           40 :         if (!lsp_static_member_receiver_class(document, offset, word, &class_name, &public_only)) {
    1232           36 :                 return NULL;
    1233              :         }
    1234              : 
    1235              :         /* Static property hovers arrive as "$name"; the cache stores "name". */
    1236            0 :         lookup = ZSTR_VAL(word)[0] == '$' && ZSTR_LEN(word) > 1
    1237            0 :                 ? zend_string_init(ZSTR_VAL(word) + 1, ZSTR_LEN(word) - 1, 0)
    1238            4 :                 : zend_string_copy(word)
    1239              :         ;
    1240            4 :         detail = lsp_inherited_member_detail(server, class_name, lookup);
    1241            4 :         zend_string_release(lookup);
    1242            4 :         zend_string_release(class_name);
    1243              : 
    1244            4 :         if (!detail) {
    1245            0 :                 return NULL;
    1246              :         }
    1247              : 
    1248            4 :         markdown = strpprintf(0, "`%s`\n\nLSParrot Engine", ZSTR_VAL(detail));
    1249            4 :         zend_string_release(detail);
    1250              : 
    1251            4 :         return markdown;
    1252              : }
    1253              : 
    1254          176 : extern void lsp_lsparrot_hover(lsp_server *server, zval *return_value, lsp_document *document, zval *position)
    1255              : {
    1256              :         zend_long line, character;
    1257              :         zend_string *word, *detail, *markdown, *inferred_type, *constant_type, *analyzer_expression;
    1258              :         zval contents;
    1259              :         size_t offset;
    1260              : 
    1261          176 :         lsp_position_from_zval(position, &line, &character);
    1262          176 :         offset = lsp_offset_at(document->text, line, character);
    1263              : 
    1264              :         /* No symbol hover inside string literals: words in literal content must
    1265              :          * not resolve as class or function references. */
    1266          176 :         if (lsp_hover_offset_in_string_literal(document, offset)) {
    1267            4 :                 ZVAL_NULL(return_value);
    1268              : 
    1269          176 :                 return;
    1270              :         }
    1271              : 
    1272          172 :         word = lsp_word_at(document->text, offset);
    1273          172 :         analyzer_expression = NULL;
    1274          172 :         if (server->phpstan_enabled) {
    1275           60 :                 analyzer_expression = lsp_hover_analyzer_expression(document->text, word, offset);
    1276              :         }
    1277              : 
    1278          172 :         inferred_type = lsp_infer_variable_type(server, document, word, offset);
    1279          172 :         if (inferred_type) {
    1280           88 :                 array_init(return_value);
    1281           88 :                 array_init(&contents);
    1282           88 :                 markdown = lsp_hover_type_markdown(server, document, position, word, offset, inferred_type, analyzer_expression);
    1283           88 :                 add_assoc_string(&contents, "kind", "markdown");
    1284           88 :                 add_assoc_str(&contents, "value", markdown);
    1285           88 :                 add_assoc_zval(return_value, "contents", &contents);
    1286           88 :                 zend_string_release(inferred_type);
    1287           88 :                 if (analyzer_expression) {
    1288           36 :                         zend_string_release(analyzer_expression);
    1289              :                 }
    1290           88 :                 zend_string_release(word);
    1291              : 
    1292           88 :                 return;
    1293              :         }
    1294              : 
    1295           84 :         if ((analyzer_expression || lsp_hover_word_is_variable(word)) && (server->phpstan_enabled || server->psalm_enabled || server->psalm_ls_enabled)) {
    1296           24 :                 inferred_type = zend_string_init("mixed", sizeof("mixed") - 1, 0);
    1297           24 :                 array_init(return_value);
    1298           24 :                 array_init(&contents);
    1299           24 :                 markdown = lsp_hover_type_markdown(server, document, position, word, offset, inferred_type, analyzer_expression);
    1300           24 :                 add_assoc_string(&contents, "kind", "markdown");
    1301           24 :                 add_assoc_str(&contents, "value", markdown);
    1302           24 :                 add_assoc_zval(return_value, "contents", &contents);
    1303           24 :                 zend_string_release(inferred_type);
    1304           24 :                 if (analyzer_expression) {
    1305           24 :                         zend_string_release(analyzer_expression);
    1306              :                 }
    1307           24 :                 zend_string_release(word);
    1308              : 
    1309           24 :                 return;
    1310              :         }
    1311              : 
    1312           60 :         constant_type = lsp_hover_constant_type(document, word);
    1313           60 :         if (constant_type) {
    1314           20 :                 array_init(return_value);
    1315           20 :                 array_init(&contents);
    1316           20 :                 markdown = lsp_hover_constant_markdown(word, constant_type);
    1317           20 :                 add_assoc_string(&contents, "kind", "markdown");
    1318           20 :                 add_assoc_str(&contents, "value", markdown);
    1319           20 :                 add_assoc_zval(return_value, "contents", &contents);
    1320           20 :                 zend_string_release(constant_type);
    1321           20 :                 if (analyzer_expression) {
    1322            0 :                         zend_string_release(analyzer_expression);
    1323              :                 }
    1324           20 :                 zend_string_release(word);
    1325              : 
    1326           20 :                 return;
    1327              :         }
    1328              : 
    1329              :         /* `use Foo\Bar;` / `use function Foo\bar;` statement names hover as
    1330              :          * the imported symbol. */
    1331           40 :         markdown = NULL;
    1332              :         {
    1333           40 :                 zend_string *import_name = lsp_use_statement_class_name_at(document->text, offset);
    1334              : 
    1335           40 :                 if (import_name) {
    1336            0 :                         lsp_index_join_worker(server);
    1337            0 :                         markdown = lsp_hover_symbol_markdown_for(server, import_name, LSP_SYMBOL_CLASS);
    1338            0 :                         if (!markdown) {
    1339            0 :                                 markdown = lsp_hover_symbol_markdown_for(server, import_name, LSP_SYMBOL_FUNCTION);
    1340              :                         }
    1341            0 :                         if (!markdown) {
    1342            0 :                                 markdown = lsp_hover_symbol_markdown_for(server, import_name, LSP_SYMBOL_CONSTANT);
    1343              :                         }
    1344            0 :                         zend_string_release(import_name);
    1345              :                 }
    1346              :         }
    1347              : 
    1348              :         /* Bare call to a function imported via `use function`. */
    1349           40 :         if (!markdown) {
    1350           40 :                 zend_string *imported_function = lsp_document_import_fqcn_for_bound_name(document, LSP_SYMBOL_FUNCTION, word);
    1351              : 
    1352           40 :                 if (imported_function) {
    1353            0 :                         lsp_index_join_worker(server);
    1354            0 :                         markdown = lsp_hover_symbol_markdown_for(server, imported_function, LSP_SYMBOL_FUNCTION);
    1355            0 :                         zend_string_release(imported_function);
    1356              :                 }
    1357              :         }
    1358              : 
    1359              :         /* Member/static hovers first: a property or constant named like a PHP
    1360              :          * builtin (Widget::MAX, $w->count) must not surface the builtin. */
    1361           40 :         if (!markdown) {
    1362           40 :                 markdown = lsp_hover_static_member_markdown(server, document, word, offset);
    1363              :         }
    1364           40 :         if (!markdown) {
    1365           36 :                 markdown = lsp_hover_member_access_markdown(server, document, word, offset);
    1366              :         }
    1367           40 :         if (!markdown) {
    1368           20 :                 markdown = lsp_hover_enum_case_declaration_markdown(server, document, word, offset);
    1369              :         }
    1370           40 :         if (!markdown) {
    1371           20 :                 markdown = lsp_hover_property_declaration_markdown(document, word, offset);
    1372              :         }
    1373           40 :         if (!markdown) {
    1374           16 :                 markdown = lsp_hover_builtin_symbol_markdown(word);
    1375              :         }
    1376           40 :         if (!markdown) {
    1377            8 :                 markdown = lsp_hover_project_symbol_markdown(server, document, word, offset);
    1378              :         }
    1379           40 :         if (markdown) {
    1380           40 :                 array_init(return_value);
    1381           40 :                 array_init(&contents);
    1382           40 :                 add_assoc_string(&contents, "kind", "markdown");
    1383           40 :                 add_assoc_str(&contents, "value", markdown);
    1384           40 :                 add_assoc_zval(return_value, "contents", &contents);
    1385           40 :                 if (analyzer_expression) {
    1386            0 :                         zend_string_release(analyzer_expression);
    1387              :                 }
    1388           40 :                 zend_string_release(word);
    1389              : 
    1390           40 :                 return;
    1391              :         }
    1392              : 
    1393            0 :         if (ZSTR_LEN(word) == 0 || !lsp_find_symbol(&document->lsparrot, word, NULL, NULL, &detail)) {
    1394            0 :                 if (analyzer_expression) {
    1395            0 :                         zend_string_release(analyzer_expression);
    1396              :                 }
    1397            0 :                 zend_string_release(word);
    1398            0 :                 ZVAL_NULL(return_value);
    1399              : 
    1400            0 :                 return;
    1401              :         }
    1402              : 
    1403            0 :         array_init(return_value);
    1404            0 :         array_init(&contents);
    1405            0 :         markdown = strpprintf(0, "`%s`", ZSTR_VAL(detail));
    1406            0 :         add_assoc_string(&contents, "kind", "markdown");
    1407            0 :         add_assoc_str(&contents, "value", markdown);
    1408            0 :         add_assoc_zval(return_value, "contents", &contents);
    1409            0 :         zend_string_release(detail);
    1410            0 :         if (analyzer_expression) {
    1411            0 :                 zend_string_release(analyzer_expression);
    1412              :         }
    1413            0 :         zend_string_release(word);
    1414              : }
        

Generated by: LCOV version 2.0-1