LCOV - code coverage report
Current view: top level - ext - lsp_server_inference.c (source / functions) Coverage Total Hit
Test: Extension code coverage Lines: 74.9 % 1975 1479
Test Date: 2026-07-13 00:58:38 Functions: 96.9 % 96 93

            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              : static inline zend_string *lsp_infer_property_fetch_assignment_class(lsp_server *server, lsp_document *document, zend_string *receiver, size_t offset);
      17              : 
      18         1224 : extern bool lsp_text_is_word_boundary(zend_string *text, size_t offset)
      19              : {
      20         1224 :         const char *value = ZSTR_VAL(text);
      21              : 
      22         1224 :         return offset >= ZSTR_LEN(text) || !lsp_doc_is_identifier_char(value[offset]);
      23              : }
      24              : 
      25              : /* lsp_brace_depth_at used to rescan the document from offset zero on every
      26              :  * call, and features call it once per token — O(tokens x file size) per
      27              :  * request. Cache the brace events per text (keyed by string identity, the
      28              :  * same scheme as the token cache) and answer with a binary search. */
      29              : typedef struct _lsp_brace_event {
      30              :         uint32_t position;
      31              :         int32_t depth_after;
      32              : } lsp_brace_event;
      33              : 
      34              : typedef struct _lsp_brace_cache_entry {
      35              :         zend_string *text;
      36              :         lsp_brace_event *events;
      37              :         uint32_t count;
      38              : } lsp_brace_cache_entry;
      39              : 
      40              : #define LSP_BRACE_CACHE_SIZE 8
      41              : 
      42              : static lsp_brace_cache_entry lsp_brace_cache[LSP_BRACE_CACHE_SIZE];
      43              : static uint32_t lsp_brace_cache_next = 0;
      44              : 
      45        20908 : static inline lsp_brace_cache_entry *lsp_brace_cache_entry_for(zend_string *text)
      46              : {
      47              :         lsp_brace_cache_entry *slot;
      48        20908 :         const char *value = ZSTR_VAL(text);
      49              :         lsp_brace_event *events;
      50        20908 :         int32_t depth = 0;
      51        20908 :         size_t i, length = ZSTR_LEN(text);
      52        20908 :         uint32_t count = 0, capacity;
      53              : 
      54        47960 :         for (i = 0; i < LSP_BRACE_CACHE_SIZE; i++) {
      55        47264 :                 if (lsp_brace_cache[i].text == text) {
      56        20212 :                         return &lsp_brace_cache[i];
      57              :                 }
      58              :         }
      59              : 
      60          696 :         capacity = 64;
      61          696 :         events = safe_emalloc(capacity, sizeof(lsp_brace_event), 0);
      62       151692 :         for (i = 0; i < length; i++) {
      63       150996 :                 if (value[i] != '{' && value[i] != '}') {
      64       147452 :                         continue;
      65              :                 }
      66              : 
      67         3544 :                 if (value[i] == '{') {
      68         1772 :                         depth++;
      69         1772 :                 } else if (depth > 0) {
      70         1772 :                         depth--;
      71              :                 } else {
      72              :                         /* Stray closer at depth zero: no depth change to record. */
      73            0 :                         continue;
      74              :                 }
      75              : 
      76         3544 :                 if (count == capacity) {
      77            0 :                         capacity *= 2;
      78            0 :                         events = safe_erealloc(events, capacity, sizeof(lsp_brace_event), 0);
      79              :                 }
      80              : 
      81         3544 :                 events[count].position = (uint32_t) i;
      82         3544 :                 events[count].depth_after = depth;
      83         3544 :                 count++;
      84              :         }
      85              : 
      86          696 :         slot = &lsp_brace_cache[lsp_brace_cache_next];
      87          696 :         lsp_brace_cache_next = (lsp_brace_cache_next + 1) % LSP_BRACE_CACHE_SIZE;
      88              : 
      89          696 :         if (slot->text) {
      90           20 :                 zend_string_release(slot->text);
      91           20 :                 efree(slot->events);
      92              :         }
      93              : 
      94          696 :         slot->text = zend_string_copy(text);
      95          696 :         slot->events = events;
      96          696 :         slot->count = count;
      97              : 
      98          696 :         return slot;
      99              : }
     100              : 
     101          548 : extern void lsp_brace_cache_clear(void)
     102              : {
     103              :         uint32_t i;
     104              : 
     105         4932 :         for (i = 0; i < LSP_BRACE_CACHE_SIZE; i++) {
     106         4384 :                 if (lsp_brace_cache[i].text) {
     107          676 :                         zend_string_release(lsp_brace_cache[i].text);
     108          676 :                         efree(lsp_brace_cache[i].events);
     109          676 :                         lsp_brace_cache[i].text = NULL;
     110          676 :                         lsp_brace_cache[i].events = NULL;
     111          676 :                         lsp_brace_cache[i].count = 0;
     112              :                 }
     113              :         }
     114              : 
     115          548 :         lsp_brace_cache_next = 0;
     116          548 : }
     117              : 
     118        20908 : extern zend_long lsp_brace_depth_at(zend_string *text, size_t offset)
     119              : {
     120        20908 :         const lsp_brace_cache_entry *entry = lsp_brace_cache_entry_for(text);
     121        20908 :         uint32_t lo = 0, hi = entry->count, mid;
     122        20908 :         size_t length = offset > ZSTR_LEN(text) ? ZSTR_LEN(text) : offset;
     123              : 
     124              :         /* Find the number of events strictly before `length`; the depth after
     125              :          * the last such event is the depth at the offset. */
     126        86560 :         while (lo < hi) {
     127        65652 :                 mid = lo + (hi - lo) / 2;
     128        65652 :                 if ((size_t) entry->events[mid].position < length) {
     129        18028 :                         lo = mid + 1;
     130              :                 } else {
     131        47624 :                         hi = mid;
     132              :                 }
     133              :         }
     134              : 
     135        20908 :         return lo == 0 ? 0 : (zend_long) entry->events[lo - 1].depth_after;
     136              : }
     137              : 
     138         1932 : extern bool lsp_find_matching_brace(zend_string *text, size_t open_offset, size_t *close_offset)
     139              : {
     140         1932 :         const char *value = ZSTR_VAL(text);
     141         1932 :         zend_long depth = 0;
     142              :         size_t i;
     143              : 
     144       210796 :         for (i = open_offset; i < ZSTR_LEN(text); i++) {
     145       210796 :                 if (value[i] == '{') {
     146         3472 :                         depth++;
     147       207324 :                 } else if (value[i] == '}') {
     148         3472 :                         depth--;
     149         3472 :                         if (depth == 0) {
     150         1932 :                                 *close_offset = i;
     151              : 
     152         1932 :                                 return true;
     153              :                         }
     154              :                 }
     155              :         }
     156              : 
     157            0 :         *close_offset = ZSTR_LEN(text);
     158              : 
     159            0 :         return true;
     160              : }
     161              : 
     162        61420 : extern bool lsp_token_is_char(zval *token, char value)
     163              : {
     164        61420 :         zend_string *text = lsp_token_string(token, "text");
     165              : 
     166        61420 :         return lsp_token_name_equals(token, "CHAR") && text && ZSTR_LEN(text) == 1 && ZSTR_VAL(text)[0] == value;
     167              : }
     168              : 
     169         1328 : static inline bool lsp_token_is_visibility(zval *token)
     170              : {
     171         2588 :         return lsp_token_name_equals(token, "T_PUBLIC") ||
     172         2588 :                 lsp_token_name_equals(token, "T_PROTECTED") ||
     173         1244 :                 lsp_token_name_equals(token, "T_PRIVATE")
     174              :         ;
     175              : }
     176              : 
     177        22252 : extern bool lsp_token_in_bounds(zval *token, size_t start, size_t end)
     178              : {
     179        22252 :         size_t offset = (size_t) lsp_token_long(token, "offset", 0);
     180              : 
     181        22252 :         return offset >= start && offset < end;
     182              : }
     183              : 
     184        18816 : extern bool lsp_token_at_depth(zend_string *text, zval *token, zend_long depth)
     185              : {
     186        18816 :         return lsp_brace_depth_at(text, (size_t) lsp_token_long(token, "offset", 0)) == depth;
     187              : }
     188              : 
     189          312 : extern bool lsp_token_is_property_declaration(HashTable *tokens, uint32_t index, zend_string *text, zend_long body_depth)
     190              : {
     191              :         zend_long i;
     192              :         zval *token;
     193          312 :         bool saw_visibility = false, saw_open_paren = false;
     194              : 
     195         3616 :         for (i = (zend_long) index - 1; i >= 0; i--) {
     196         3536 :                 token = zend_hash_index_find(tokens, (zend_ulong) i);
     197         3536 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
     198            0 :                         continue;
     199              :                 }
     200              : 
     201         3536 :                 if (!lsp_token_at_depth(text, token, body_depth)) {
     202         1976 :                         continue;
     203              :                 }
     204              : 
     205         1560 :                 if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '{') || lsp_token_is_char(token, '}')) {
     206              :                         break;
     207              :                 }
     208              : 
     209         1448 :                 if (lsp_token_is_char(token, '(')) {
     210          120 :                         saw_open_paren = true;
     211          120 :                         break;
     212              :                 }
     213              : 
     214         1328 :                 if (lsp_token_is_visibility(token)) {
     215          148 :                         saw_visibility = true;
     216              :                 }
     217              :         }
     218              : 
     219          312 :         return saw_visibility && !saw_open_paren;
     220              : }
     221              : 
     222           44 : static inline zend_string *lsp_method_declared_return_type(zend_string *text, zval *function_token)
     223              : {
     224           44 :         const char *value = ZSTR_VAL(text), *end = value + ZSTR_LEN(text), *p, *start;
     225           44 :         zend_long depth = 0;
     226           44 :         size_t offset = (size_t) lsp_token_long(function_token, "offset", 0);
     227              : 
     228           44 :         if (offset >= ZSTR_LEN(text)) {
     229            0 :                 return NULL;
     230              :         }
     231              : 
     232           44 :         p = value + offset;
     233         1008 :         while (p < end && *p != '(') {
     234          964 :                 p++;
     235              :         }
     236              : 
     237           44 :         if (p >= end) {
     238            0 :                 return NULL;
     239              :         }
     240              : 
     241          184 :         for (; p < end; p++) {
     242          184 :                 if (*p == '(') {
     243           44 :                         depth++;
     244          140 :                 } else if (*p == ')' && --depth == 0) {
     245           44 :                         p++;
     246           44 :                         break;
     247              :                 }
     248              :         }
     249              : 
     250           44 :         while (p < end && isspace((unsigned char) *p)) {
     251            0 :                 p++;
     252              :         }
     253              : 
     254           44 :         if (p >= end || *p != ':') {
     255            0 :                 return NULL;
     256              :         }
     257              : 
     258           44 :         p++;
     259           88 :         while (p < end && isspace((unsigned char) *p)) {
     260           44 :                 p++;
     261              :         }
     262              : 
     263           44 :         start = p;
     264          396 :         while (p < end && (lsp_doc_is_identifier_char(*p) || *p == '\\' || *p == '?' || *p == '|' || *p == '&')) {
     265          352 :                 p++;
     266              :         }
     267              : 
     268           44 :         if (p == start) {
     269            0 :                 return NULL;
     270              :         }
     271              : 
     272           44 :         return zend_string_init(start, p - start, 0);
     273              : }
     274              : 
     275           52 : static inline bool lsp_type_segment_equals_name(const char *start, const char *end, zend_string *name)
     276              : {
     277           52 :         while (start < end && (*start == '\\' || *start == '?')) {
     278            0 :                 start++;
     279              :         }
     280              : 
     281           52 :         while (end > start && isspace((unsigned char) end[-1])) {
     282            0 :                 end--;
     283              :         }
     284              : 
     285           52 :         return (size_t) (end - start) == ZSTR_LEN(name) && strncasecmp(start, ZSTR_VAL(name), ZSTR_LEN(name)) == 0;
     286              : }
     287              : 
     288           52 : static inline bool lsp_type_contains_name(zend_string *type, zend_string *name)
     289              : {
     290           52 :         const char *value = ZSTR_VAL(type), *end = value + ZSTR_LEN(type), *segment_start, *p;
     291              : 
     292           52 :         segment_start = value;
     293          104 :         for (p = value; p < end; p++) {
     294           52 :                 if (lsp_doc_is_identifier_char(*p) || *p == '\\' || *p == '?' || *p == '-') {
     295           52 :                         continue;
     296              :                 }
     297              : 
     298            0 :                 if (lsp_type_segment_equals_name(segment_start, p, name)) {
     299            0 :                         return true;
     300              :                 }
     301              : 
     302            0 :                 segment_start = p + 1;
     303              :         }
     304              : 
     305           52 :         return lsp_type_segment_equals_name(segment_start, end, name);
     306              : }
     307              : 
     308          224 : static inline const char *lsp_next_template_tag(const char *cursor, const char *end, size_t *tag_length)
     309              : {
     310          224 :         const char *tags[] = {"@template", "@phpstan-template", "@psalm-template"};
     311              :         const char *match, *best;
     312              :         size_t i, length;
     313              : 
     314          224 :         best = NULL;
     315          224 :         *tag_length = 0;
     316          896 :         for (i = 0; i < sizeof(tags) / sizeof(tags[0]); i++) {
     317          672 :                 match = strstr(cursor, tags[i]);
     318          672 :                 if (!match || match >= end) {
     319          620 :                         continue;
     320              :                 }
     321              : 
     322           52 :                 if (!best || match < best) {
     323           52 :                         best = match;
     324           52 :                         length = strlen(tags[i]);
     325           52 :                         *tag_length = length;
     326              :                 }
     327              :         }
     328              : 
     329          224 :         return best;
     330              : }
     331              : 
     332          140 : static inline bool lsp_method_phpdoc_type_uses_template(zend_string *comment, zend_string *type)
     333              : {
     334              :         const char *cursor, *end, *p, *start;
     335              :         zend_string *template_name;
     336              :         size_t tag_length;
     337              :         bool found;
     338              : 
     339          140 :         cursor = ZSTR_VAL(comment);
     340          140 :         end = cursor + ZSTR_LEN(comment);
     341          140 :         while ((cursor = lsp_next_template_tag(cursor, end, &tag_length)) != NULL && cursor < end) {
     342            0 :                 p = cursor + tag_length;
     343            0 :                 if (p < end && *p == '-') {
     344            0 :                         p++;
     345            0 :                         while (p < end && (islower((unsigned char) *p) || *p == '-')) {
     346            0 :                                 p++;
     347              :                         }
     348              :                 }
     349              : 
     350            0 :                 while (p < end && isspace((unsigned char) *p)) {
     351            0 :                         p++;
     352              :                 }
     353              : 
     354            0 :                 if (p >= end || !lsp_doc_is_identifier_start(*p)) {
     355            0 :                         cursor = p;
     356            0 :                         continue;
     357              :                 }
     358              : 
     359            0 :                 start = p++;
     360            0 :                 while (p < end && lsp_doc_is_identifier_char(*p)) {
     361            0 :                         p++;
     362              :                 }
     363              : 
     364            0 :                 template_name = zend_string_init(start, p - start, 0);
     365            0 :                 found = lsp_type_contains_name(type, template_name);
     366            0 :                 zend_string_release(template_name);
     367            0 :                 if (found) {
     368            0 :                         return true;
     369              :                 }
     370              : 
     371            0 :                 cursor = p;
     372              :         }
     373              : 
     374          140 :         return false;
     375              : }
     376              : 
     377          176 : static inline zend_string *lsp_method_phpdoc_return_type(zend_string *text, HashTable *tokens, uint32_t function_index)
     378              : {
     379              :         zend_long i;
     380              :         zend_string *comment, *type;
     381              :         zval *token;
     382              :         size_t function_offset;
     383              : 
     384          792 :         for (i = (zend_long) function_index - 1; i >= 0; i--) {
     385          792 :                 token = zend_hash_index_find(tokens, (zend_ulong) i);
     386          792 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
     387            0 :                         continue;
     388              :                 }
     389              : 
     390          792 :                 if (lsp_token_name_equals(token, "T_DOC_COMMENT")) {
     391          140 :                         comment = lsp_token_string(token, "text");
     392          140 :                         type = comment ? lsp_phpdoc_return_type_from_comment(comment) : NULL;
     393          140 :                         if (comment && type && lsp_method_phpdoc_type_uses_template(comment, type)) {
     394            0 :                                 zend_string_release(type);
     395              : 
     396            0 :                                 return zend_string_init("mixed", sizeof("mixed") - 1, 0);
     397              :                         }
     398              : 
     399          140 :                         return type;
     400              :                 }
     401              : 
     402          652 :                 if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '{') || lsp_token_is_char(token, '}')) {
     403              :                         break;
     404              :                 }
     405              :         }
     406              : 
     407           36 :         token = zend_hash_index_find(tokens, function_index);
     408           36 :         function_offset = token && Z_TYPE_P(token) == IS_ARRAY ? (size_t) lsp_token_long(token, "offset", 0) : 0;
     409              : 
     410           36 :         return lsp_phpdoc_attribute_shape_type_before(text, function_offset, true);
     411              : }
     412              : 
     413           28 : static inline zend_string *lsp_method_phpdoc_return_type_raw(zend_string *text, HashTable *tokens, uint32_t function_index)
     414              : {
     415              :         zend_long i;
     416              :         zend_string *comment, *type;
     417              :         zval *token;
     418              :         size_t function_offset;
     419              : 
     420          112 :         for (i = (zend_long) function_index - 1; i >= 0; i--) {
     421          112 :                 token = zend_hash_index_find(tokens, (zend_ulong) i);
     422          112 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
     423            0 :                         continue;
     424              :                 }
     425              : 
     426          112 :                 if (lsp_token_name_equals(token, "T_DOC_COMMENT")) {
     427           28 :                         comment = lsp_token_string(token, "text");
     428           28 :                         type = comment ? lsp_phpdoc_return_type_from_comment_raw(comment) : NULL;
     429              : 
     430           28 :                         return type;
     431              :                 }
     432              : 
     433           84 :                 if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '{') || lsp_token_is_char(token, '}')) {
     434              :                         break;
     435              :                 }
     436              :         }
     437              : 
     438            0 :         token = zend_hash_index_find(tokens, function_index);
     439            0 :         function_offset = token && Z_TYPE_P(token) == IS_ARRAY ? (size_t) lsp_token_long(token, "offset", 0) : 0;
     440              : 
     441            0 :         return lsp_phpdoc_attribute_shape_type_before(text, function_offset, false);
     442              : }
     443              : 
     444          112 : static inline zend_string *lsp_find_method_return_type(lsp_document *document, zend_string *method_name)
     445              : {
     446              :         zend_string *label, *type;
     447              :         zval *tokens_zv, *token;
     448              :         HashTable *tokens;
     449              :         uint32_t i, count;
     450              : 
     451          112 :         tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
     452          112 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
     453            0 :                 return NULL;
     454              :         }
     455              : 
     456          112 :         tokens = Z_ARRVAL_P(tokens_zv);
     457          112 :         count = zend_hash_num_elements(tokens);
     458              : 
     459         3568 :         for (i = 0; i < count; i++) {
     460         3544 :                 token = zend_hash_index_find(tokens, i);
     461         3544 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_FUNCTION")) {
     462         3432 :                         continue;
     463              :                 }
     464              : 
     465          112 :                 label = lsp_next_string_token(tokens, i + 1);
     466          112 :                 if (!label || !zend_string_equals(label, method_name)) {
     467           24 :                         continue;
     468              :                 }
     469              : 
     470           88 :                 type = lsp_method_phpdoc_return_type(document->text, tokens, i);
     471           88 :                 if (type) {
     472           68 :                         return type;
     473              :                 }
     474              : 
     475           20 :                 return lsp_method_declared_return_type(document->text, token);
     476              :         }
     477              : 
     478           24 :         return NULL;
     479              : }
     480              : 
     481           52 : static inline zend_string *lsp_find_method_phpdoc_return_type(lsp_document *document, zend_string *method_name)
     482              : {
     483              :         zend_string *label, *type;
     484              :         zval *tokens_zv, *token;
     485              :         HashTable *tokens;
     486              :         uint32_t i, count;
     487              : 
     488           52 :         tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
     489           52 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
     490            0 :                 return NULL;
     491              :         }
     492              : 
     493           52 :         tokens = Z_ARRVAL_P(tokens_zv);
     494           52 :         count = zend_hash_num_elements(tokens);
     495              : 
     496         1824 :         for (i = 0; i < count; i++) {
     497         1800 :                 token = zend_hash_index_find(tokens, i);
     498         1800 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_FUNCTION")) {
     499         1748 :                         continue;
     500              :                 }
     501              : 
     502           52 :                 label = lsp_next_string_token(tokens, i + 1);
     503           52 :                 if (!label || !zend_string_equals(label, method_name)) {
     504           24 :                         continue;
     505              :                 }
     506              : 
     507           28 :                 type = lsp_method_phpdoc_return_type_raw(document->text, tokens, i);
     508           28 :                 if (type) {
     509           28 :                         return type;
     510              :                 }
     511              : 
     512            0 :                 return NULL;
     513              :         }
     514              : 
     515           24 :         return NULL;
     516              : }
     517              : 
     518           12 : static inline zend_string *lsp_find_method_declared_return_type(lsp_document *document, zend_string *method_name)
     519              : {
     520              :         zend_string *label;
     521              :         zval *tokens_zv, *token;
     522              :         HashTable *tokens;
     523              :         uint32_t i, count;
     524              : 
     525           12 :         tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
     526           12 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
     527            0 :                 return NULL;
     528              :         }
     529              : 
     530           12 :         tokens = Z_ARRVAL_P(tokens_zv);
     531           12 :         count = zend_hash_num_elements(tokens);
     532          300 :         for (i = 0; i < count; i++) {
     533          300 :                 token = zend_hash_index_find(tokens, i);
     534          300 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_FUNCTION")) {
     535          288 :                         continue;
     536              :                 }
     537              : 
     538           12 :                 label = lsp_next_string_token(tokens, i + 1);
     539           12 :                 if (!label || !zend_string_equals(label, method_name)) {
     540            0 :                         continue;
     541              :                 }
     542              : 
     543           12 :                 return lsp_method_declared_return_type(document->text, token);
     544              :         }
     545              : 
     546            0 :         return NULL;
     547              : }
     548              : 
     549          112 : static inline zend_string *lsp_find_method_return_type_in_text(zend_string *text, zend_string *method_name)
     550              : {
     551              :         zend_string *label, *type;
     552              :         zval tokens_zv, *token;
     553              :         HashTable *tokens;
     554              :         uint32_t i, count;
     555              : 
     556          112 :         ZVAL_UNDEF(&tokens_zv);
     557          112 :         lsp_lsparrot_tokens_to_zval(&tokens_zv, text);
     558          112 :         if (Z_TYPE(tokens_zv) != IS_ARRAY) {
     559            0 :                 if (!Z_ISUNDEF(tokens_zv)) {
     560            0 :                         zval_ptr_dtor(&tokens_zv);
     561              :                 }
     562              : 
     563            0 :                 return NULL;
     564              :         }
     565              : 
     566          112 :         tokens = Z_ARRVAL(tokens_zv);
     567          112 :         count = zend_hash_num_elements(tokens);
     568              : 
     569         3576 :         for (i = 0; i < count; i++) {
     570         3552 :                 token = zend_hash_index_find(tokens, i);
     571         3552 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_FUNCTION")) {
     572         3432 :                         continue;
     573              :                 }
     574              : 
     575          120 :                 label = lsp_next_string_token(tokens, i + 1);
     576          120 :                 if (!label || !zend_string_equals(label, method_name)) {
     577           32 :                         continue;
     578              :                 }
     579              : 
     580           88 :                 type = lsp_method_phpdoc_return_type(text, tokens, i);
     581           88 :                 if (!type) {
     582           12 :                         type = lsp_method_declared_return_type(text, token);
     583              :                 }
     584              : 
     585           88 :                 zval_ptr_dtor(&tokens_zv);
     586              : 
     587           88 :                 return type;
     588              :         }
     589              : 
     590           24 :         zval_ptr_dtor(&tokens_zv);
     591              : 
     592           24 :         return NULL;
     593              : }
     594              : 
     595           52 : static inline const char *lsp_next_param_tag(const char *cursor, const char *end, size_t *tag_length)
     596              : {
     597           52 :         const char *tags[] = {"@param", "@phpstan-param", "@psalm-param"};
     598              :         const char *match, *best;
     599              :         size_t i, length;
     600              : 
     601           52 :         best = NULL;
     602           52 :         *tag_length = 0;
     603          208 :         for (i = 0; i < sizeof(tags) / sizeof(tags[0]); i++) {
     604          156 :                 match = strstr(cursor, tags[i]);
     605          156 :                 if (!match || match >= end) {
     606          104 :                         continue;
     607              :                 }
     608              : 
     609           52 :                 if (!best || match < best) {
     610           52 :                         best = match;
     611           52 :                         length = strlen(tags[i]);
     612           52 :                         *tag_length = length;
     613              :                 }
     614              :         }
     615              : 
     616           52 :         return best;
     617              : }
     618              : 
     619           52 : static inline bool lsp_type_text_is_exact_template(zend_string *type, zend_string *template_name)
     620              : {
     621              :         const char *start, *end;
     622              : 
     623           52 :         start = ZSTR_VAL(type);
     624           52 :         end = start + ZSTR_LEN(type);
     625           52 :         while (start < end && isspace((unsigned char) *start)) {
     626            0 :                 start++;
     627              :         }
     628           52 :         while (end > start && isspace((unsigned char) end[-1])) {
     629            0 :                 end--;
     630              :         }
     631           52 :         while (start < end && (*start == '?' || *start == '(')) {
     632            0 :                 start++;
     633            0 :                 while (start < end && isspace((unsigned char) *start)) {
     634            0 :                         start++;
     635              :                 }
     636              :         }
     637           52 :         while (end > start && end[-1] == ')') {
     638            0 :                 end--;
     639            0 :                 while (end > start && isspace((unsigned char) end[-1])) {
     640            0 :                         end--;
     641              :                 }
     642              :         }
     643              : 
     644          104 :         return (size_t) (end - start) == ZSTR_LEN(template_name) &&
     645           52 :                 strncasecmp(start, ZSTR_VAL(template_name), ZSTR_LEN(template_name)) == 0
     646              :         ;
     647              : }
     648              : 
     649           84 : static inline zend_string *lsp_phpdoc_return_template_from_comment(zend_string *comment)
     650              : {
     651              :         const char *cursor, *end, *p, *start;
     652              :         zend_string *return_type, *template_name;
     653              :         size_t tag_length;
     654              :         bool matches;
     655              : 
     656           84 :         return_type = lsp_phpdoc_return_type_from_comment_raw(comment);
     657           84 :         if (!return_type) {
     658            0 :                 return NULL;
     659              :         }
     660              : 
     661           84 :         cursor = ZSTR_VAL(comment);
     662           84 :         end = cursor + ZSTR_LEN(comment);
     663           84 :         while ((cursor = lsp_next_template_tag(cursor, end, &tag_length)) != NULL && cursor < end) {
     664           52 :                 p = cursor + tag_length;
     665           52 :                 if (p < end && *p == '-') {
     666            0 :                         p++;
     667            0 :                         while (p < end && (islower((unsigned char) *p) || *p == '-')) {
     668            0 :                                 p++;
     669              :                         }
     670              :                 }
     671              : 
     672          104 :                 while (p < end && isspace((unsigned char) *p)) {
     673           52 :                         p++;
     674              :                 }
     675              : 
     676           52 :                 if (p >= end || !lsp_doc_is_identifier_start(*p)) {
     677            0 :                         cursor = p;
     678            0 :                         continue;
     679              :                 }
     680              : 
     681           52 :                 start = p++;
     682           52 :                 while (p < end && lsp_doc_is_identifier_char(*p)) {
     683            0 :                         p++;
     684              :                 }
     685              : 
     686           52 :                 template_name = zend_string_init(start, p - start, 0);
     687           52 :                 matches = lsp_type_text_is_exact_template(return_type, template_name);
     688           52 :                 if (matches) {
     689           52 :                         zend_string_release(return_type);
     690              : 
     691           52 :                         return template_name;
     692              :                 }
     693            0 :                 zend_string_release(template_name);
     694            0 :                 cursor = p;
     695              :         }
     696              : 
     697           32 :         zend_string_release(return_type);
     698              : 
     699           32 :         return NULL;
     700              : }
     701              : 
     702           52 : static inline bool lsp_phpdoc_param_index_for_template(zend_string *comment, zend_string *template_name, uint32_t *param_index)
     703              : {
     704              :         const char *cursor, *end, *p, *type_start, *type_end;
     705              :         zend_string *type;
     706              :         size_t tag_length;
     707              :         uint32_t index;
     708              :         bool found_variable, found;
     709              : 
     710           52 :         cursor = ZSTR_VAL(comment);
     711           52 :         end = cursor + ZSTR_LEN(comment);
     712           52 :         index = 0;
     713           52 :         while ((cursor = lsp_next_param_tag(cursor, end, &tag_length)) != NULL && cursor < end) {
     714           52 :                 p = cursor + tag_length;
     715          104 :                 while (p < end && isspace((unsigned char) *p)) {
     716           52 :                         p++;
     717              :                 }
     718              : 
     719           52 :                 type_start = p;
     720           52 :                 found_variable = false;
     721          156 :                 while (p < end && *p != '\n' && *p != '\r') {
     722          156 :                         if (*p == '$' && p + 1 < end && lsp_doc_is_identifier_start(p[1])) {
     723           52 :                                 type_end = p;
     724          104 :                                 while (type_end > type_start && isspace((unsigned char) type_end[-1])) {
     725           52 :                                         type_end--;
     726              :                                 }
     727           52 :                                 found_variable = true;
     728           52 :                                 break;
     729              :                         }
     730          104 :                         p++;
     731              :                 }
     732              : 
     733           52 :                 if (found_variable && type_end > type_start) {
     734           52 :                         type = zend_string_init(type_start, type_end - type_start, 0);
     735           52 :                         found = lsp_type_contains_name(type, template_name);
     736           52 :                         zend_string_release(type);
     737           52 :                         if (found) {
     738           52 :                                 *param_index = index;
     739              : 
     740           52 :                                 return true;
     741              :                         }
     742            0 :                         index++;
     743              :                 }
     744              : 
     745            0 :                 cursor = p;
     746              :         }
     747              : 
     748            0 :         return false;
     749              : }
     750              : 
     751          132 : static inline zend_string *lsp_find_method_doc_comment(lsp_document *document, zend_string *method_name)
     752              : {
     753              :         zend_string *label, *comment;
     754              :         zval tokens_zv, *token;
     755              :         HashTable *tokens;
     756              :         uint32_t i, count;
     757              :         zend_long j;
     758              : 
     759          132 :         ZVAL_UNDEF(&tokens_zv);
     760          132 :         lsp_lsparrot_tokens_to_zval(&tokens_zv, document->text);
     761          132 :         if (Z_TYPE(tokens_zv) != IS_ARRAY) {
     762            0 :                 if (!Z_ISUNDEF(tokens_zv)) {
     763            0 :                         zval_ptr_dtor(&tokens_zv);
     764              :                 }
     765              : 
     766            0 :                 return NULL;
     767              :         }
     768              : 
     769          132 :         tokens = Z_ARRVAL(tokens_zv);
     770          132 :         count = zend_hash_num_elements(tokens);
     771         3808 :         for (i = 0; i < count; i++) {
     772         3784 :                 token = zend_hash_index_find(tokens, i);
     773         3784 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_name_equals(token, "T_FUNCTION")) {
     774         3652 :                         continue;
     775              :                 }
     776              : 
     777          132 :                 label = lsp_next_string_token(tokens, i + 1);
     778          132 :                 if (!label || !zend_string_equals(label, method_name)) {
     779           24 :                         continue;
     780              :                 }
     781              : 
     782          512 :                 for (j = (zend_long) i - 1; j >= 0; j--) {
     783          512 :                         token = zend_hash_index_find(tokens, (zend_ulong) j);
     784          512 :                         if (!token || Z_TYPE_P(token) != IS_ARRAY) {
     785            0 :                                 continue;
     786              :                         }
     787          512 :                         if (lsp_token_name_equals(token, "T_DOC_COMMENT")) {
     788           84 :                                 comment = lsp_token_string(token, "text");
     789           84 :                                 if (comment) {
     790           84 :                                         comment = zend_string_copy(comment);
     791              :                                 }
     792           84 :                                 zval_ptr_dtor(&tokens_zv);
     793              : 
     794           84 :                                 return comment;
     795              :                         }
     796          428 :                         if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '{') || lsp_token_is_char(token, '}')) {
     797              :                                 break;
     798              :                         }
     799              :                 }
     800              : 
     801           24 :                 zval_ptr_dtor(&tokens_zv);
     802              : 
     803           24 :                 return NULL;
     804              :         }
     805              : 
     806           24 :         zval_ptr_dtor(&tokens_zv);
     807              : 
     808           24 :         return NULL;
     809              : }
     810              : 
     811           52 : static inline zend_string *lsp_method_call_argument_variable_at(zend_string *text, size_t open_paren_offset, uint32_t target_index)
     812              : {
     813              :         const char *value;
     814              :         size_t length, p, arg_start, arg_end;
     815              :         uint32_t index;
     816              :         int32_t paren_depth, bracket_depth, brace_depth;
     817              : 
     818           52 :         value = ZSTR_VAL(text);
     819           52 :         length = ZSTR_LEN(text);
     820           52 :         if (open_paren_offset >= length || value[open_paren_offset] != '(') {
     821            0 :                 return NULL;
     822              :         }
     823              : 
     824           52 :         p = open_paren_offset + 1;
     825           52 :         arg_start = p;
     826           52 :         index = 0;
     827           52 :         paren_depth = 0;
     828           52 :         bracket_depth = 0;
     829           52 :         brace_depth = 0;
     830          320 :         while (p < length) {
     831          320 :                 if (value[p] == '(') {
     832            0 :                         paren_depth++;
     833          320 :                 } else if (value[p] == ')' && paren_depth > 0) {
     834            0 :                         paren_depth--;
     835          320 :                 } else if (value[p] == '[') {
     836            0 :                         bracket_depth++;
     837          320 :                 } else if (value[p] == ']' && bracket_depth > 0) {
     838            0 :                         bracket_depth--;
     839          320 :                 } else if (value[p] == '{') {
     840            0 :                         brace_depth++;
     841          320 :                 } else if (value[p] == '}' && brace_depth > 0) {
     842            0 :                         brace_depth--;
     843          320 :                 } else if (paren_depth == 0 && bracket_depth == 0 && brace_depth == 0 && (value[p] == ',' || value[p] == ')')) {
     844           52 :                         if (index == target_index) {
     845           52 :                                 arg_end = p;
     846           52 :                                 while (arg_start < arg_end && isspace((unsigned char) value[arg_start])) {
     847            0 :                                         arg_start++;
     848              :                                 }
     849           52 :                                 while (arg_end > arg_start && isspace((unsigned char) value[arg_end - 1])) {
     850            0 :                                         arg_end--;
     851              :                                 }
     852           52 :                                 if (arg_end > arg_start + 1 && value[arg_start] == '$' && lsp_doc_is_identifier_start(value[arg_start + 1])) {
     853           52 :                                         p = arg_start + 2;
     854          216 :                                         while (p < arg_end && lsp_doc_is_identifier_char(value[p])) {
     855          164 :                                                 p++;
     856              :                                         }
     857           52 :                                         if (p == arg_end) {
     858           52 :                                                 return zend_string_init(value + arg_start, arg_end - arg_start, 0);
     859              :                                         }
     860              :                                 }
     861              : 
     862            0 :                                 return NULL;
     863              :                         }
     864            0 :                         if (value[p] == ')') {
     865            0 :                                 return NULL;
     866              :                         }
     867            0 :                         index++;
     868            0 :                         arg_start = p + 1;
     869              :                 }
     870          268 :                 p++;
     871              :         }
     872              : 
     873            0 :         return NULL;
     874              : }
     875              : 
     876          132 : static inline zend_string *lsp_resolve_template_method_call_assignment_type(lsp_server *server, lsp_document *document, zend_string *method_name, size_t open_paren_offset, size_t offset)
     877              : {
     878              :         zend_string *comment, *template_name, *argument, *type;
     879              :         uint32_t param_index;
     880              : 
     881          132 :         comment = lsp_find_method_doc_comment(document, method_name);
     882          132 :         if (!comment) {
     883           48 :                 return NULL;
     884              :         }
     885              : 
     886           84 :         template_name = lsp_phpdoc_return_template_from_comment(comment);
     887           84 :         if (!template_name) {
     888           32 :                 zend_string_release(comment);
     889              : 
     890           32 :                 return NULL;
     891              :         }
     892              : 
     893           52 :         if (!lsp_phpdoc_param_index_for_template(comment, template_name, &param_index)) {
     894            0 :                 zend_string_release(comment);
     895            0 :                 zend_string_release(template_name);
     896              : 
     897            0 :                 return NULL;
     898              :         }
     899           52 :         zend_string_release(comment);
     900              : 
     901           52 :         argument = lsp_method_call_argument_variable_at(document->text, open_paren_offset, param_index);
     902           52 :         if (!argument) {
     903            0 :                 zend_string_release(template_name);
     904              : 
     905            0 :                 return NULL;
     906              :         }
     907              : 
     908           52 :         type = lsp_infer_variable_type(server, document, argument, offset);
     909           52 :         zend_string_release(argument);
     910           52 :         zend_string_release(template_name);
     911           52 :         if (lsp_type_is_unhelpful(type)) {
     912            0 :                 if (type) {
     913            0 :                         zend_string_release(type);
     914              :                 }
     915              : 
     916            0 :                 return NULL;
     917              :         }
     918              : 
     919           52 :         return type;
     920              : }
     921              : 
     922          476 : static inline zend_string *lsp_infer_method_call_assignment_type(lsp_server *server, lsp_document *document, zend_string *receiver, size_t offset)
     923              : {
     924          476 :         const char *value = ZSTR_VAL(document->text), *end = value + lsp_current_statement_scan_limit(document->text, offset),
     925          476 :                 *p = value, *match, *q, *method_start, *method_end;
     926          476 :         zend_string *method_name, *return_type = NULL;
     927              : 
     928         2256 :         while (p < end) {
     929         2252 :                 match = strstr(p, ZSTR_VAL(receiver));
     930         2252 :                 if (!match || match >= end) {
     931              :                         break;
     932              :                 }
     933              : 
     934         1780 :                 q = match + ZSTR_LEN(receiver);
     935         2824 :                 while (q < end && isspace((unsigned char) *q)) {
     936         1044 :                         q++;
     937              :                 }
     938              : 
     939         1780 :                 if (q >= end || *q != '=') {
     940         1520 :                         p = match + 1;
     941         1520 :                         continue;
     942              :                 }
     943              : 
     944          260 :                 q++;
     945          520 :                 while (q < end && isspace((unsigned char) *q)) {
     946          260 :                         q++;
     947              :                 }
     948              : 
     949          260 :                 if (q + sizeof("$this") - 1 >= end || memcmp(q, "$this", sizeof("$this") - 1) != 0) {
     950          116 :                         p = match + 1;
     951          116 :                         continue;
     952              :                 }
     953              : 
     954          144 :                 q += sizeof("$this") - 1;
     955          144 :                 while (q < end && isspace((unsigned char) *q)) {
     956            0 :                         q++;
     957              :                 }
     958              : 
     959          144 :                 if (q + 2 >= end || q[0] != '-' || q[1] != '>') {
     960            0 :                         p = match + 1;
     961            0 :                         continue;
     962              :                 }
     963              : 
     964          144 :                 q += 2;
     965          144 :                 while (q < end && isspace((unsigned char) *q)) {
     966            0 :                         q++;
     967              :                 }
     968              : 
     969          144 :                 if (q >= end || !lsp_doc_is_identifier_start(*q)) {
     970            0 :                         p = match + 1;
     971            0 :                         continue;
     972              :                 }
     973              : 
     974          144 :                 method_start = q++;
     975         1700 :                 while (q < end && lsp_doc_is_identifier_char(*q)) {
     976         1556 :                         q++;
     977              :                 }
     978              : 
     979          144 :                 method_end = q;
     980          144 :                 while (q < end && isspace((unsigned char) *q)) {
     981            0 :                         q++;
     982              :                 }
     983              : 
     984          144 :                 if (q >= end || *q != '(') {
     985           12 :                         p = match + 1;
     986           12 :                         continue;
     987              :                 }
     988              : 
     989          132 :                 method_name = zend_string_init(method_start, method_end - method_start, 0);
     990              : 
     991          132 :                 if (return_type) {
     992            0 :                         zend_string_release(return_type);
     993              :                 }
     994              : 
     995          132 :                 return_type = lsp_resolve_template_method_call_assignment_type(server, document, method_name, (size_t) (q - value), (size_t) (match - value));
     996          132 :                 if (!return_type) {
     997           80 :                         return_type = lsp_find_method_return_type(document, method_name);
     998              :                 }
     999              : 
    1000          132 :                 zend_string_release(method_name);
    1001              : 
    1002          132 :                 p = q + 1;
    1003              :         }
    1004              : 
    1005          476 :         return return_type;
    1006              : }
    1007              : 
    1008           56 : static inline zend_string *lsp_infer_method_call_assignment_phpdoc_type(lsp_document *document, zend_string *receiver, size_t offset)
    1009              : {
    1010           56 :         const char *value = ZSTR_VAL(document->text), *end = value + lsp_current_statement_scan_limit(document->text, offset),
    1011           56 :                 *p = value, *match, *q, *method_start, *method_end;
    1012           56 :         zend_string *method_name, *return_type = NULL;
    1013              : 
    1014          984 :         while (p < end) {
    1015          980 :                 match = strstr(p, ZSTR_VAL(receiver));
    1016          980 :                 if (!match || match >= end) {
    1017              :                         break;
    1018              :                 }
    1019              : 
    1020          928 :                 q = match + ZSTR_LEN(receiver);
    1021         1768 :                 while (q < end && isspace((unsigned char) *q)) {
    1022          840 :                         q++;
    1023              :                 }
    1024              : 
    1025          928 :                 if (q >= end || *q != '=') {
    1026          860 :                         p = match + 1;
    1027          860 :                         continue;
    1028              :                 }
    1029              : 
    1030           68 :                 q++;
    1031          136 :                 while (q < end && isspace((unsigned char) *q)) {
    1032           68 :                         q++;
    1033              :                 }
    1034              : 
    1035           68 :                 if (q + sizeof("$this") - 1 >= end || memcmp(q, "$this", sizeof("$this") - 1) != 0) {
    1036           16 :                         p = match + 1;
    1037           16 :                         continue;
    1038              :                 }
    1039              : 
    1040           52 :                 q += sizeof("$this") - 1;
    1041           52 :                 while (q < end && isspace((unsigned char) *q)) {
    1042            0 :                         q++;
    1043              :                 }
    1044              : 
    1045           52 :                 if (q + 2 >= end || q[0] != '-' || q[1] != '>') {
    1046            0 :                         p = match + 1;
    1047            0 :                         continue;
    1048              :                 }
    1049              : 
    1050           52 :                 q += 2;
    1051           52 :                 while (q < end && isspace((unsigned char) *q)) {
    1052            0 :                         q++;
    1053              :                 }
    1054              : 
    1055           52 :                 if (q >= end || !lsp_doc_is_identifier_start(*q)) {
    1056            0 :                         p = match + 1;
    1057            0 :                         continue;
    1058              :                 }
    1059              : 
    1060           52 :                 method_start = q++;
    1061          548 :                 while (q < end && lsp_doc_is_identifier_char(*q)) {
    1062          496 :                         q++;
    1063              :                 }
    1064              : 
    1065           52 :                 method_end = q;
    1066           52 :                 while (q < end && isspace((unsigned char) *q)) {
    1067            0 :                         q++;
    1068              :                 }
    1069              : 
    1070           52 :                 if (q >= end || *q != '(') {
    1071            0 :                         p = match + 1;
    1072            0 :                         continue;
    1073              :                 }
    1074              : 
    1075           52 :                 method_name = zend_string_init(method_start, method_end - method_start, 0);
    1076              : 
    1077           52 :                 if (return_type) {
    1078            0 :                         zend_string_release(return_type);
    1079              :                 }
    1080              : 
    1081           52 :                 return_type = lsp_find_method_phpdoc_return_type(document, method_name);
    1082              : 
    1083           52 :                 zend_string_release(method_name);
    1084              : 
    1085           52 :                 p = q + 1;
    1086              :         }
    1087              : 
    1088           56 :         return return_type;
    1089              : }
    1090              : 
    1091           20 : static inline zend_string *lsp_infer_method_call_assignment_declared_type(lsp_document *document, zend_string *receiver, size_t offset)
    1092              : {
    1093           20 :         const char *value = ZSTR_VAL(document->text), *end = value + lsp_current_statement_scan_limit(document->text, offset),
    1094           20 :                 *p = value, *match, *q, *method_start, *method_end;
    1095           20 :         zend_string *method_name, *return_type = NULL;
    1096              : 
    1097          876 :         while (p < end) {
    1098          872 :                 match = strstr(p, ZSTR_VAL(receiver));
    1099          872 :                 if (!match || match >= end) {
    1100              :                         break;
    1101              :                 }
    1102              : 
    1103          856 :                 q = match + ZSTR_LEN(receiver);
    1104         1664 :                 while (q < end && isspace((unsigned char) *q)) {
    1105          808 :                         q++;
    1106              :                 }
    1107              : 
    1108          856 :                 if (q >= end || *q != '=') {
    1109          824 :                         p = match + 1;
    1110          824 :                         continue;
    1111              :                 }
    1112              : 
    1113           32 :                 q++;
    1114           64 :                 while (q < end && isspace((unsigned char) *q)) {
    1115           32 :                         q++;
    1116              :                 }
    1117              : 
    1118           32 :                 if (q + sizeof("$this") - 1 >= end || memcmp(q, "$this", sizeof("$this") - 1) != 0) {
    1119           20 :                         p = match + 1;
    1120           20 :                         continue;
    1121              :                 }
    1122              : 
    1123           12 :                 q += sizeof("$this") - 1;
    1124           12 :                 while (q < end && isspace((unsigned char) *q)) {
    1125            0 :                         q++;
    1126              :                 }
    1127              : 
    1128           12 :                 if (q + 2 >= end || q[0] != '-' || q[1] != '>') {
    1129            0 :                         p = match + 1;
    1130            0 :                         continue;
    1131              :                 }
    1132              : 
    1133           12 :                 q += 2;
    1134           12 :                 while (q < end && isspace((unsigned char) *q)) {
    1135            0 :                         q++;
    1136              :                 }
    1137              : 
    1138           12 :                 if (q >= end || !lsp_doc_is_identifier_start(*q)) {
    1139            0 :                         p = match + 1;
    1140            0 :                         continue;
    1141              :                 }
    1142              : 
    1143           12 :                 method_start = q++;
    1144          156 :                 while (q < end && lsp_doc_is_identifier_char(*q)) {
    1145          144 :                         q++;
    1146              :                 }
    1147              : 
    1148           12 :                 method_end = q;
    1149           12 :                 while (q < end && isspace((unsigned char) *q)) {
    1150            0 :                         q++;
    1151              :                 }
    1152              : 
    1153           12 :                 if (q >= end || *q != '(') {
    1154            0 :                         p = match + 1;
    1155            0 :                         continue;
    1156              :                 }
    1157              : 
    1158           12 :                 method_name = zend_string_init(method_start, method_end - method_start, 0);
    1159              : 
    1160           12 :                 if (return_type) {
    1161            0 :                         zend_string_release(return_type);
    1162              :                 }
    1163              : 
    1164           12 :                 return_type = lsp_find_method_declared_return_type(document, method_name);
    1165              : 
    1166           12 :                 zend_string_release(method_name);
    1167              : 
    1168           12 :                 p = q + 1;
    1169              :         }
    1170              : 
    1171           20 :         return return_type;
    1172              : }
    1173              : 
    1174          160 : static inline zend_string *lsp_infer_method_call_assignment_class(lsp_server *server, lsp_document *document, zend_string *receiver, size_t offset)
    1175              : {
    1176              :         zend_string *return_type, *class_name;
    1177              : 
    1178          160 :         return_type = lsp_infer_method_call_assignment_type(server, document, receiver, offset);
    1179              : 
    1180          160 :         if (!return_type) {
    1181          128 :                 return NULL;
    1182              :         }
    1183              : 
    1184              :         /* Qualified return types still need import/namespace resolution: a
    1185              :          * leading backslash must be stripped for index lookups and a relative
    1186              :          * name such as Sub\Widget resolves against the declaring file. */
    1187           32 :         class_name = lsp_resolve_class_name(document->text, return_type);
    1188           32 :         zend_string_release(return_type);
    1189              : 
    1190           32 :         return class_name;
    1191              : }
    1192              : 
    1193           80 : extern bool lsp_find_first_class_bounds(zend_string *text, size_t *body_start, size_t *body_end, zend_long *body_depth)
    1194              : {
    1195           80 :         const char *keywords[4] = {"class", "interface", "trait", "enum"}, *value = ZSTR_VAL(text);
    1196              :         size_t i, p, close_offset, keyword_index, keyword_length;
    1197              : 
    1198         5560 :         for (i = 0; i < ZSTR_LEN(text); i++) {
    1199        27480 :                 for (keyword_index = 0; keyword_index < sizeof(keywords) / sizeof(keywords[0]); keyword_index++) {
    1200        22000 :                         keyword_length = strlen(keywords[keyword_index]);
    1201        22000 :                         if (i + keyword_length >= ZSTR_LEN(text) || strncasecmp(value + i, keywords[keyword_index], keyword_length) != 0) {
    1202        21912 :                                 continue;
    1203              :                         }
    1204              : 
    1205           88 :                         if (i > 0 && (lsp_doc_is_identifier_char(value[i - 1]) || value[i - 1] == '$')) {
    1206            8 :                                 continue;
    1207              :                         }
    1208              : 
    1209           80 :                         if (!lsp_text_is_word_boundary(text, i + keyword_length)) {
    1210            0 :                                 continue;
    1211              :                         }
    1212              : 
    1213           80 :                         p = i + keyword_length;
    1214         2068 :                         while (p < ZSTR_LEN(text) && value[p] != '{') {
    1215         1988 :                                 p++;
    1216              :                         }
    1217              : 
    1218           80 :                         if (p >= ZSTR_LEN(text) || !lsp_find_matching_brace(text, p, &close_offset)) {
    1219            0 :                                 return false;
    1220              :                         }
    1221              : 
    1222           80 :                         *body_start = p + 1;
    1223           80 :                         *body_end = close_offset;
    1224           80 :                         *body_depth = lsp_brace_depth_at(text, p + 1);
    1225              : 
    1226           80 :                         return true;
    1227              :                 }
    1228              :         }
    1229              : 
    1230            0 :         return false;
    1231              : }
    1232              : 
    1233            0 : extern bool lsp_method_is_public(HashTable *tokens, uint32_t index, zend_string *text, zend_long body_depth)
    1234              : {
    1235              :         zend_long i;
    1236              :         zval *token;
    1237            0 :         bool saw_visibility = false, public_visibility = true;
    1238              : 
    1239            0 :         for (i = (zend_long) index - 1; i >= 0; i--) {
    1240            0 :                 token = zend_hash_index_find(tokens, (zend_ulong) i);
    1241            0 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
    1242            0 :                         continue;
    1243              :                 }
    1244              : 
    1245            0 :                 if (!lsp_token_at_depth(text, token, body_depth)) {
    1246            0 :                         continue;
    1247              :                 }
    1248              : 
    1249            0 :                 if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '{') || lsp_token_is_char(token, '}')) {
    1250              :                         break;
    1251              :                 }
    1252              : 
    1253            0 :                 if (lsp_token_name_equals(token, "T_PRIVATE") || lsp_token_name_equals(token, "T_PROTECTED")) {
    1254            0 :                         saw_visibility = true;
    1255            0 :                         public_visibility = false;
    1256            0 :                 } else if (lsp_token_name_equals(token, "T_PUBLIC")) {
    1257            0 :                         saw_visibility = true;
    1258            0 :                         public_visibility = true;
    1259              :                 }
    1260              :         }
    1261              : 
    1262            0 :         return !saw_visibility || public_visibility;
    1263              : }
    1264              : 
    1265              : 
    1266           80 : static inline zend_string *lsp_inference_class_name_after_keyword(zend_string *text, size_t header_start, size_t header_end, const char *keyword)
    1267              : {
    1268              :         const char *value, *name_start, *name_end;
    1269              :         zend_string *raw, *resolved;
    1270              :         size_t p, keyword_end;
    1271              : 
    1272           80 :         value = ZSTR_VAL(text);
    1273         5972 :         for (p = header_start; p < header_end; p++) {
    1274         5972 :                 if (!lsp_keyword_at_slice(value, p, header_end, keyword, &keyword_end)) {
    1275         5892 :                         continue;
    1276              :                 }
    1277              : 
    1278           80 :                 p = keyword_end;
    1279          160 :                 while (p < header_end && isspace((unsigned char) value[p])) {
    1280           80 :                         p++;
    1281              :                 }
    1282              : 
    1283           80 :                 name_start = value + p;
    1284          936 :                 while (p < header_end && (lsp_doc_is_identifier_char(value[p]) || value[p] == '\\')) {
    1285          856 :                         p++;
    1286              :                 }
    1287              : 
    1288           80 :                 name_end = value + p;
    1289           80 :                 if (name_end <= name_start) {
    1290            0 :                         return NULL;
    1291              :                 }
    1292              : 
    1293           80 :                 raw = zend_string_init(name_start, name_end - name_start, 0);
    1294           80 :                 resolved = lsp_resolve_class_name(text, raw);
    1295           80 :                 zend_string_release(raw);
    1296              : 
    1297           80 :                 return resolved;
    1298              :         }
    1299              : 
    1300            0 :         return NULL;
    1301              : }
    1302              : 
    1303           60 : static inline zend_string *lsp_inference_declared_class_name(zend_string *text)
    1304              : {
    1305           60 :         const char *keywords[4] = {"class", "interface", "trait", "enum"};
    1306              :         zend_long body_depth;
    1307              :         zend_string *resolved;
    1308              :         size_t body_start, body_end, i;
    1309              : 
    1310           60 :         body_start = 0;
    1311           60 :         body_end = 0;
    1312           60 :         body_depth = 0;
    1313              : 
    1314           60 :         if (!lsp_find_first_class_bounds(text, &body_start, &body_end, &body_depth)) {
    1315            0 :                 return NULL;
    1316              :         }
    1317              : 
    1318           60 :         for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
    1319           60 :                 resolved = lsp_inference_class_name_after_keyword(text, 0, body_start > 0 ? body_start - 1 : body_start, keywords[i]);
    1320           60 :                 if (resolved) {
    1321           60 :                         return resolved;
    1322              :                 }
    1323              :         }
    1324              : 
    1325            0 :         return NULL;
    1326              : }
    1327              : 
    1328           20 : static inline zend_string *lsp_inference_class_extends_name(zend_string *text)
    1329              : {
    1330              :         zend_long body_depth;
    1331              :         size_t body_start, body_end;
    1332              : 
    1333           20 :         body_start = 0;
    1334           20 :         body_end = 0;
    1335           20 :         body_depth = 0;
    1336              : 
    1337           20 :         if (!lsp_find_first_class_bounds(text, &body_start, &body_end, &body_depth)) {
    1338            0 :                 return NULL;
    1339              :         }
    1340              : 
    1341           20 :         return lsp_inference_class_name_after_keyword(text, 0, body_start > 0 ? body_start - 1 : body_start, "extends");
    1342              : }
    1343              : 
    1344           44 : static inline bool lsp_type_segment_is_fluent_this(const char *start, const char *end)
    1345              : {
    1346           44 :         while (start < end && isspace((unsigned char) *start)) {
    1347            0 :                 start++;
    1348              :         }
    1349              : 
    1350           44 :         while (end > start && isspace((unsigned char) end[-1])) {
    1351            0 :                 end--;
    1352              :         }
    1353              : 
    1354           44 :         while (start < end && (*start == '?' || *start == '\\')) {
    1355            0 :                 start++;
    1356              :         }
    1357              : 
    1358              :         /* `self` resolves to the declaring class; for fluent-chain purposes the
    1359              :          * container class is the best approximation, same as static/$this. */
    1360            8 :         return ((size_t) (end - start) == sizeof("static") - 1 && strncasecmp(start, "static", sizeof("static") - 1) == 0) ||
    1361           88 :                 ((size_t) (end - start) == sizeof("self") - 1 && strncasecmp(start, "self", sizeof("self") - 1) == 0) ||
    1362           36 :                 ((size_t) (end - start) == sizeof("$this") - 1 && strncmp(start, "$this", sizeof("$this") - 1) == 0)
    1363              :         ;
    1364              : }
    1365              : 
    1366           40 : static inline bool lsp_type_is_fluent_this(zend_string *type)
    1367              : {
    1368              :         const char *value, *end, *segment_start, *p;
    1369           40 :         uint32_t depth = 0;
    1370              : 
    1371           40 :         value = ZSTR_VAL(type);
    1372           40 :         end = value + ZSTR_LEN(type);
    1373           40 :         segment_start = value;
    1374              : 
    1375          420 :         for (p = value; p < end; p++) {
    1376          380 :                 if (*p == '<' || *p == '(') {
    1377           12 :                         depth++;
    1378          368 :                 } else if ((*p == '>' || *p == ')') && depth > 0) {
    1379           12 :                         depth--;
    1380          356 :                 } else if ((*p == '|' || *p == '&') && depth == 0) {
    1381            4 :                         if (lsp_type_segment_is_fluent_this(segment_start, p)) {
    1382            0 :                                 return true;
    1383              :                         }
    1384            4 :                         segment_start = p + 1;
    1385              :                 }
    1386              :         }
    1387              : 
    1388           40 :         return lsp_type_segment_is_fluent_this(segment_start, end);
    1389              : }
    1390              : 
    1391          144 : static inline zend_string *lsp_project_class_contents(lsp_server *server, zend_string *class_name)
    1392              : {
    1393              :         zend_string *path, *contents;
    1394              : 
    1395          144 :         path = lsp_find_project_symbol_path(server, LSP_SYMBOL_CLASS, class_name);
    1396          144 :         if (!path) {
    1397            0 :                 return NULL;
    1398              :         }
    1399              : 
    1400          144 :         contents = lsp_read_file(path);
    1401              : 
    1402          144 :         zend_string_release(path);
    1403              : 
    1404          144 :         if (contents == zend_empty_string) {
    1405            0 :                 return NULL;
    1406              :         }
    1407              : 
    1408          144 :         return contents;
    1409              : }
    1410              : 
    1411          112 : static inline zend_string *lsp_project_method_return_type_in_class(lsp_server *server, zend_string *class_name, zend_string *method_name, zend_string **class_text, zend_string **origin_class)
    1412              : {
    1413              :         zend_string *contents, *return_type;
    1414              : 
    1415          112 :         *class_text = NULL;
    1416              : 
    1417          112 :         if (origin_class) {
    1418          112 :                 *origin_class = NULL;
    1419              :         }
    1420              : 
    1421          112 :         contents = lsp_project_class_contents(server, class_name);
    1422          112 :         if (!contents) {
    1423            0 :                 return NULL;
    1424              :         }
    1425              : 
    1426          112 :         return_type = lsp_find_method_return_type_in_text(contents, method_name);
    1427          112 :         if (!return_type) {
    1428           24 :                 zend_string_release(contents);
    1429           24 :                 return NULL;
    1430              :         }
    1431              : 
    1432           88 :         *class_text = contents;
    1433           88 :         if (origin_class) {
    1434           88 :                 *origin_class = zend_string_copy(class_name);
    1435              :         }
    1436              : 
    1437           88 :         return return_type;
    1438              : }
    1439              : 
    1440          112 : static inline zend_string *lsp_project_method_return_type_recursive(lsp_server *server, zend_string *class_name, zend_string *method_name, zend_string **class_text, zend_string **origin_class, HashTable *visited, uint32_t depth)
    1441              : {
    1442          112 :         zend_string *contents, *parent_class, *return_type = NULL;
    1443              :         zval traits, *trait_zv;
    1444              : 
    1445          112 :         *class_text = NULL;
    1446              : 
    1447          112 :         if (origin_class) {
    1448          112 :                 *origin_class = NULL;
    1449              :         }
    1450              : 
    1451          112 :         if (depth > 64 || zend_hash_exists(visited, class_name)) {
    1452            0 :                 return NULL;
    1453              :         }
    1454              : 
    1455          112 :         zend_hash_add_empty_element(visited, class_name);
    1456          112 :         return_type = lsp_project_method_return_type_in_class(server, class_name, method_name, class_text, origin_class);
    1457          112 :         if (return_type) {
    1458           88 :                 return return_type;
    1459              :         }
    1460              : 
    1461           24 :         contents = lsp_project_class_contents(server, class_name);
    1462           24 :         if (!contents) {
    1463            0 :                 return NULL;
    1464              :         }
    1465              : 
    1466           24 :         lsp_collect_class_trait_names_for(contents, class_name, &traits);
    1467              : 
    1468           24 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL(traits), trait_zv) {
    1469            8 :                 if (Z_TYPE_P(trait_zv) != IS_STRING) {
    1470            0 :                         continue;
    1471              :                 }
    1472              : 
    1473            8 :                 return_type = lsp_project_method_return_type_recursive(server, Z_STR_P(trait_zv), method_name, class_text, origin_class, visited, depth + 1);
    1474            8 :                 if (return_type) {
    1475            8 :                         break;
    1476              :                 }
    1477              :         } ZEND_HASH_FOREACH_END();
    1478              : 
    1479           24 :         zval_ptr_dtor(&traits);
    1480              : 
    1481           24 :         if (return_type) {
    1482            8 :                 zend_string_release(contents);
    1483              : 
    1484            8 :                 return return_type;
    1485              :         }
    1486              : 
    1487           16 :         parent_class = lsp_inference_class_extends_name(contents);
    1488              : 
    1489           16 :         zend_string_release(contents);
    1490              : 
    1491           16 :         if (!parent_class) {
    1492            0 :                 return NULL;
    1493              :         }
    1494              : 
    1495           16 :         return_type = lsp_project_method_return_type_recursive(server, parent_class, method_name, class_text, origin_class, visited, depth + 1);
    1496           16 :         zend_string_release(parent_class);
    1497              : 
    1498           16 :         return return_type;
    1499              : }
    1500              : 
    1501           88 : static inline zend_string *lsp_project_method_return_type(lsp_server *server, zend_string *class_name, zend_string *method_name, zend_string **class_text, zend_string **origin_class)
    1502              : {
    1503              :         zend_string *return_type;
    1504              :         HashTable visited;
    1505              : 
    1506           88 :         zend_hash_init(&visited, 8, NULL, NULL, 0);
    1507           88 :         return_type = lsp_project_method_return_type_recursive(server, class_name, method_name, class_text, origin_class, &visited, 0);
    1508           88 :         zend_hash_destroy(&visited);
    1509              : 
    1510           88 :         return return_type;
    1511              : }
    1512              : 
    1513           72 : static inline bool lsp_type_segment_equals_template(const char *start, const char *end, zend_string *template_name)
    1514              : {
    1515           72 :         while (start < end && isspace((unsigned char) *start)) {
    1516            0 :                 start++;
    1517              :         }
    1518              : 
    1519           72 :         while (end > start && isspace((unsigned char) end[-1])) {
    1520            0 :                 end--;
    1521              :         }
    1522              : 
    1523          128 :         return (size_t) (end - start) == ZSTR_LEN(template_name) &&
    1524           56 :                 strncasecmp(start, ZSTR_VAL(template_name), ZSTR_LEN(template_name)) == 0
    1525              :         ;
    1526              : }
    1527              : 
    1528           68 : static inline bool lsp_type_contains_template(zend_string *type, zend_string *template_name)
    1529              : {
    1530           68 :         const char *value = ZSTR_VAL(type), *end = value + ZSTR_LEN(type), *segment_start, *p;
    1531           68 :         uint32_t depth = 0;
    1532              : 
    1533           68 :         segment_start = value;
    1534          472 :         for (p = value; p < end; p++) {
    1535          408 :                 if (*p == '<' || *p == '(') {
    1536            0 :                         depth++;
    1537          408 :                 } else if ((*p == '>' || *p == ')') && depth > 0) {
    1538            0 :                         depth--;
    1539          408 :                 } else if (*p == '|' && depth == 0) {
    1540            8 :                         if (lsp_type_segment_equals_template(segment_start, p, template_name)) {
    1541            4 :                                 return true;
    1542              :                         }
    1543            4 :                         segment_start = p + 1;
    1544              :                 }
    1545              :         }
    1546              : 
    1547           64 :         return lsp_type_segment_equals_template(segment_start, end, template_name);
    1548              : }
    1549              : 
    1550           52 : static inline zend_string *lsp_resolve_generic_argument_at(lsp_document *document, zend_string *container_type, uint32_t target_index)
    1551              : {
    1552              :         zend_string *argument, *resolved;
    1553              : 
    1554           52 :         argument = lsp_type_generic_argument(container_type, target_index);
    1555           52 :         if (!argument && target_index > 0) {
    1556            0 :                 argument = lsp_type_generic_argument(container_type, 0);
    1557              :         }
    1558              : 
    1559           52 :         if (!argument) {
    1560            4 :                 return NULL;
    1561              :         }
    1562              : 
    1563           48 :         resolved = lsp_resolve_class_name(document->text, argument);
    1564           48 :         zend_string_release(argument);
    1565              : 
    1566           48 :         return resolved;
    1567              : }
    1568              : 
    1569            4 : static inline zend_string *lsp_resolve_trait_use_template_type(lsp_server *server, zend_string *container_class, zend_string *trait_name, uint32_t target_index)
    1570              : {
    1571              :         zend_string *current, *contents, *parent_class, *resolved;
    1572              :         HashTable visited;
    1573              : 
    1574            4 :         zend_hash_init(&visited, 8, NULL, NULL, 0);
    1575            4 :         current = zend_string_copy(container_class);
    1576            8 :         while (current) {
    1577            8 :                 if (zend_hash_exists(&visited, current)) {
    1578            0 :                         break;
    1579              :                 }
    1580              : 
    1581            8 :                 zend_hash_add_empty_element(&visited, current);
    1582              : 
    1583            8 :                 contents = lsp_project_class_contents(server, current);
    1584            8 :                 if (!contents) {
    1585            0 :                         break;
    1586              :                 }
    1587              : 
    1588            8 :                 resolved = lsp_phpdoc_trait_use_generic_argument(contents, trait_name, target_index);
    1589            8 :                 if (resolved) {
    1590            4 :                         zend_string_release(contents);
    1591            4 :                         zend_string_release(current);
    1592            4 :                         zend_hash_destroy(&visited);
    1593              : 
    1594            4 :                         return resolved;
    1595              :                 }
    1596              : 
    1597            4 :                 parent_class = lsp_inference_class_extends_name(contents);
    1598            4 :                 zend_string_release(contents);
    1599            4 :                 zend_string_release(current);
    1600            4 :                 current = parent_class;
    1601              :         }
    1602              : 
    1603            0 :         if (current) {
    1604            0 :                 zend_string_release(current);
    1605              :         }
    1606              : 
    1607            0 :         zend_hash_destroy(&visited);
    1608              : 
    1609            0 :         return NULL;
    1610              : }
    1611              : 
    1612           68 : static inline zend_string *lsp_resolve_project_template_type(lsp_server *server, lsp_document *document, zend_string *class_text, zend_string *container_class, zend_string *container_type, zend_string *origin_class, zend_string *type)
    1613              : {
    1614              :         zend_string *template_name, *resolved;
    1615              :         uint32_t index;
    1616              : 
    1617           68 :         for (index = 0; ; index++) {
    1618           84 :                 template_name = lsp_phpdoc_template_type_at(class_text, index);
    1619           84 :                 if (!template_name) {
    1620           16 :                         break;
    1621              :                 }
    1622              : 
    1623           68 :                 if (lsp_type_contains_template(type, template_name)) {
    1624           52 :                         resolved = lsp_resolve_generic_argument_at(document, container_type, index);
    1625           52 :                         if (!resolved && origin_class) {
    1626            4 :                                 resolved = lsp_resolve_trait_use_template_type(server, container_class, origin_class, index);
    1627              :                         }
    1628              : 
    1629           52 :                         zend_string_release(template_name);
    1630              : 
    1631           52 :                         if (resolved) {
    1632           52 :                                 return resolved;
    1633              :                         }
    1634              :                 } else {
    1635           16 :                         zend_string_release(template_name);
    1636              :                 }
    1637              :         }
    1638              : 
    1639           16 :         return NULL;
    1640              : }
    1641              : 
    1642            0 : static inline zend_string *lsp_resolve_this_method_return_class(lsp_server *server, lsp_document *document, zend_string *container_class, zend_string *method_name)
    1643              : {
    1644              :         zend_string *class_text, *return_type, *origin_class, *resolved;
    1645              : 
    1646            0 :         return_type = lsp_project_method_return_type(server, container_class, method_name, &class_text, &origin_class);
    1647            0 :         if (!return_type) {
    1648            0 :                 return NULL;
    1649              :         }
    1650              : 
    1651            0 :         if (lsp_type_is_fluent_this(return_type)) {
    1652            0 :                 zend_string_release(return_type);
    1653            0 :                 zend_string_release(class_text);
    1654              : 
    1655            0 :                 if (origin_class) {
    1656            0 :                         zend_string_release(origin_class);
    1657              :                 }
    1658              : 
    1659            0 :                 return zend_string_copy(container_class);
    1660              :         }
    1661              : 
    1662            0 :         resolved = lsp_resolve_project_template_type(server, document, class_text, container_class, container_class, origin_class, return_type);
    1663            0 :         if (!resolved) {
    1664            0 :                 resolved = lsp_resolve_class_name(class_text, return_type);
    1665              :         }
    1666              : 
    1667            0 :         zend_string_release(return_type);
    1668            0 :         zend_string_release(class_text);
    1669              : 
    1670            0 :         if (origin_class) {
    1671            0 :                 zend_string_release(origin_class);
    1672              :         }
    1673              : 
    1674            0 :         return resolved;
    1675              : }
    1676              : 
    1677           40 : static inline zend_string *lsp_resolve_project_method_return_class(lsp_server *server, lsp_document *document, zend_string *container_class, zend_string *container_type, zend_string *method_name)
    1678              : {
    1679              :         zend_string *class_text, *return_type, *origin_class, *resolved;
    1680              : 
    1681           40 :         return_type = lsp_project_method_return_type(server, container_class, method_name, &class_text, &origin_class);
    1682           40 :         if (!return_type) {
    1683            0 :                 return NULL;
    1684              :         }
    1685              : 
    1686           40 :         if (lsp_type_is_fluent_this(return_type)) {
    1687           16 :                 zend_string_release(return_type);
    1688           16 :                 zend_string_release(class_text);
    1689              : 
    1690           16 :                 if (origin_class) {
    1691           16 :                         zend_string_release(origin_class);
    1692              :                 }
    1693              : 
    1694           16 :                 return zend_string_copy(container_class);
    1695              :         }
    1696              : 
    1697           24 :         resolved = lsp_resolve_project_template_type(server, document, class_text, container_class, container_type, origin_class, return_type);
    1698           24 :         if (!resolved) {
    1699           16 :                 resolved = lsp_resolve_class_name(class_text, return_type);
    1700              :         }
    1701              : 
    1702           24 :         zend_string_release(return_type);
    1703           24 :         zend_string_release(class_text);
    1704              : 
    1705           24 :         if (origin_class) {
    1706           24 :                 zend_string_release(origin_class);
    1707              :         }
    1708              : 
    1709           24 :         return resolved;
    1710              : }
    1711              : 
    1712           44 : static inline zend_string *lsp_resolve_project_method_array_element_class(lsp_server *server, lsp_document *document, zend_string *container_class, zend_string *container_type, zend_string *method_name)
    1713              : {
    1714              :         zend_string *class_text, *return_type, *element_type, *origin_class, *resolved;
    1715              : 
    1716           44 :         return_type = lsp_project_method_return_type(server, container_class, method_name, &class_text, &origin_class);
    1717           44 :         if (!return_type) {
    1718            0 :                 return NULL;
    1719              :         }
    1720              : 
    1721           44 :         element_type = lsp_type_array_element_type(return_type);
    1722           44 :         zend_string_release(return_type);
    1723           44 :         if (!element_type) {
    1724            0 :                 zend_string_release(class_text);
    1725            0 :                 if (origin_class) {
    1726            0 :                         zend_string_release(origin_class);
    1727              :                 }
    1728              : 
    1729            0 :                 return NULL;
    1730              :         }
    1731              : 
    1732           44 :         resolved = lsp_resolve_project_template_type(server, document, class_text, container_class, container_type, origin_class, element_type);
    1733           44 :         if (!resolved) {
    1734            0 :                 resolved = lsp_resolve_class_name(class_text, element_type);
    1735              :         }
    1736              : 
    1737           44 :         zend_string_release(element_type);
    1738           44 :         zend_string_release(class_text);
    1739              : 
    1740           44 :         if (origin_class) {
    1741           44 :                 zend_string_release(origin_class);
    1742              :         }
    1743              : 
    1744           44 :         return resolved;
    1745              : }
    1746              : 
    1747          104 : static inline bool lsp_variable_ending_at(zend_string *text, size_t offset, zend_string **variable)
    1748              : {
    1749          104 :         const char *value = ZSTR_VAL(text);
    1750              :         size_t i, variable_start, variable_end;
    1751              : 
    1752          104 :         *variable = NULL;
    1753          104 :         if (offset > ZSTR_LEN(text)) {
    1754            0 :                 offset = ZSTR_LEN(text);
    1755              :         }
    1756              : 
    1757          104 :         i = offset;
    1758          104 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    1759            0 :                 i--;
    1760              :         }
    1761              : 
    1762          104 :         variable_end = i;
    1763              : 
    1764          364 :         while (i > 0 && lsp_doc_is_identifier_char(value[i - 1])) {
    1765          260 :                 i--;
    1766              :         }
    1767              : 
    1768          104 :         if (i == 0 || value[i - 1] != '$' || variable_end == i) {
    1769           44 :                 return false;
    1770              :         }
    1771              : 
    1772           60 :         variable_start = i - 1;
    1773           60 :         *variable = zend_string_init(value + variable_start, variable_end - variable_start, 0);
    1774              : 
    1775           60 :         return true;
    1776              : }
    1777              : 
    1778           24 : static inline zend_string *lsp_infer_variable_container_type(lsp_server *server, lsp_document *document, zend_string *variable, size_t offset)
    1779              : {
    1780              :         zend_string *type;
    1781              : 
    1782           24 :         type = lsp_phpdoc_type_for_word(document->text, variable);
    1783           24 :         if (type) {
    1784           16 :                 return type;
    1785              :         }
    1786              : 
    1787            8 :         type = lsp_phpdoc_property_type_for_word(document->text, variable, offset);
    1788            8 :         if (type) {
    1789            0 :                 return type;
    1790              :         }
    1791              : 
    1792            8 :         type = lsp_infer_method_call_assignment_type(server, document, variable, offset);
    1793            8 :         if (type) {
    1794            0 :                 return type;
    1795              :         }
    1796              : 
    1797              :         /* Declared parameter types and `new X` assignments are how most chain
    1798              :          * receivers outside $this get their type. */
    1799            8 :         type = lsp_parameter_declared_type_for_variable(document, variable, offset);
    1800            8 :         if (type) {
    1801            4 :                 return type;
    1802              :         }
    1803              : 
    1804            4 :         return lsp_infer_new_assignment_class(document->text, variable, lsp_current_statement_scan_limit(document->text, offset));
    1805              : }
    1806              : 
    1807           12 : static inline zend_string *lsp_resolve_variable_method_return_class(lsp_server *server, lsp_document *document, zend_string *variable, zend_string *method_name, size_t offset)
    1808              : {
    1809              :         zend_string *container_type, *container_class, *resolved;
    1810              : 
    1811           12 :         container_type = lsp_infer_variable_container_type(server, document, variable, offset);
    1812           12 :         if (!container_type) {
    1813            0 :                 return NULL;
    1814              :         }
    1815              : 
    1816           12 :         container_class = lsp_resolve_class_name(document->text, container_type);
    1817           12 :         if (!container_class) {
    1818            0 :                 zend_string_release(container_type);
    1819              : 
    1820            0 :                 return NULL;
    1821              :         }
    1822              : 
    1823           12 :         resolved = lsp_resolve_project_method_return_class(server, document, container_class, container_type, method_name);
    1824              : 
    1825           12 :         zend_string_release(container_class);
    1826           12 :         zend_string_release(container_type);
    1827              : 
    1828           12 :         return resolved;
    1829              : }
    1830              : 
    1831           12 : static inline zend_string *lsp_resolve_variable_method_array_element_class(lsp_server *server, lsp_document *document, zend_string *variable, zend_string *method_name, size_t offset)
    1832              : {
    1833              :         zend_string *container_type, *container_class, *resolved;
    1834              : 
    1835           12 :         container_type = lsp_infer_variable_container_type(server, document, variable, offset);
    1836           12 :         if (!container_type) {
    1837            0 :                 return NULL;
    1838              :         }
    1839              : 
    1840           12 :         container_class = lsp_resolve_class_name(document->text, container_type);
    1841           12 :         if (!container_class) {
    1842            0 :                 zend_string_release(container_type);
    1843              : 
    1844            0 :                 return NULL;
    1845              :         }
    1846              : 
    1847           12 :         resolved = lsp_resolve_project_method_array_element_class(server, document, container_class, container_type, method_name);
    1848              : 
    1849           12 :         zend_string_release(container_class);
    1850           12 :         zend_string_release(container_type);
    1851              : 
    1852           12 :         return resolved;
    1853              : }
    1854              : 
    1855          516 : static inline zend_string *lsp_infer_method_array_access_assignment_class(lsp_server *server, lsp_document *document, zend_string *receiver, size_t offset)
    1856              : {
    1857          516 :         const char *value = ZSTR_VAL(document->text), *end = value + lsp_current_statement_scan_limit(document->text, offset), *p = value, *match, *q, *statement_end, *scan;
    1858              :         zend_string *container_method, *container_variable, *chain_method, *container_return_type, *container_class, *element_class;
    1859              :         size_t close_bracket, open_bracket, chain_receiver_end;
    1860              : 
    1861         3136 :         while (p < end) {
    1862         3128 :                 match = strstr(p, ZSTR_VAL(receiver));
    1863         3128 :                 if (!match || match >= end) {
    1864              :                         break;
    1865              :                 }
    1866              : 
    1867         2648 :                 q = match + ZSTR_LEN(receiver);
    1868         4524 :                 while (q < end && isspace((unsigned char) *q)) {
    1869         1876 :                         q++;
    1870              :                 }
    1871              : 
    1872         2648 :                 if (q >= end || *q != '=') {
    1873         2332 :                         p = match + 1;
    1874         2332 :                         continue;
    1875              :                 }
    1876              : 
    1877          316 :                 q++;
    1878          632 :                 while (q < end && isspace((unsigned char) *q)) {
    1879          316 :                         q++;
    1880              :                 }
    1881              : 
    1882          316 :                 statement_end = q;
    1883         5964 :                 while (statement_end < end && *statement_end != ';' && *statement_end != '{' && *statement_end != '}') {
    1884         5648 :                         statement_end++;
    1885              :                 }
    1886              : 
    1887          316 :                 close_bracket = (size_t) -1;
    1888         5156 :                 for (scan = statement_end; scan > q; scan--) {
    1889         4868 :                         if (scan[-1] == ']') {
    1890           28 :                                 close_bracket = (size_t) ((scan - 1) - value);
    1891           28 :                                 break;
    1892              :                         }
    1893              :                 }
    1894              : 
    1895          316 :                 if (close_bracket == (size_t) -1 || !lsp_find_matching_open_bracket(document->text, close_bracket, &open_bracket)) {
    1896          288 :                         p = match + 1;
    1897          288 :                         continue;
    1898              :                 }
    1899              : 
    1900           28 :                 if (!lsp_parse_method_call_before_offset(document->text, open_bracket, &chain_method, &chain_receiver_end)) {
    1901            0 :                         p = match + 1;
    1902            0 :                         continue;
    1903              :                 }
    1904              : 
    1905           28 :                 element_class = NULL;
    1906              : 
    1907           28 :                 if (lsp_parse_this_method_call_ending_at(document->text, chain_receiver_end, &container_method)) {
    1908           20 :                         container_return_type = lsp_find_method_return_type(document, container_method);
    1909           20 :                         zend_string_release(container_method);
    1910           20 :                         if (container_return_type) {
    1911           20 :                                 container_class = lsp_resolve_class_name(document->text, container_return_type);
    1912           20 :                                 if (container_class) {
    1913           20 :                                         element_class = lsp_resolve_project_method_array_element_class(server, document, container_class, container_return_type, chain_method);
    1914           20 :                                         zend_string_release(container_class);
    1915              :                                 }
    1916              : 
    1917           20 :                                 zend_string_release(container_return_type);
    1918              :                         }
    1919            8 :                 } else if (lsp_variable_ending_at(document->text, chain_receiver_end, &container_variable)) {
    1920            8 :                         element_class = lsp_resolve_variable_method_array_element_class(server, document, container_variable, chain_method, (size_t) (match - value));
    1921            8 :                         zend_string_release(container_variable);
    1922              :                 }
    1923              : 
    1924           28 :                 zend_string_release(chain_method);
    1925              : 
    1926           28 :                 if (element_class) {
    1927           28 :                         return element_class;
    1928              :                 }
    1929              : 
    1930            0 :                 p = match + 1;
    1931              :         }
    1932              : 
    1933          488 :         return NULL;
    1934              : }
    1935              : 
    1936              : /* `$x = $recv->method(...)`: resolve $recv's class and the method's return
    1937              :  * class. The $this-> variant is handled by the dedicated scanner; this covers
    1938              :  * every other variable receiver. */
    1939          228 : static inline zend_string *lsp_infer_external_call_assignment_class(lsp_server *server, lsp_document *document, zend_string *variable, size_t offset)
    1940              : {
    1941          228 :         const char *value = ZSTR_VAL(document->text), *end, *match, *q, *recv_start, *method_start, *p;
    1942              :         zend_string *receiver, *method_name, *resolved;
    1943              : 
    1944          228 :         end = value + lsp_current_statement_scan_limit(document->text, offset);
    1945          228 :         p = value;
    1946          564 :         while (p < end) {
    1947          564 :                 match = strstr(p, ZSTR_VAL(variable));
    1948          564 :                 if (!match || match >= end) {
    1949              :                         break;
    1950              :                 }
    1951              : 
    1952          336 :                 p = match + 1;
    1953          336 :                 if (match > value && (lsp_doc_is_identifier_char(match[-1]) || match[-1] == '$')) {
    1954            8 :                         continue;
    1955              :                 }
    1956              : 
    1957          328 :                 q = match + ZSTR_LEN(variable);
    1958          328 :                 if (q < end && lsp_doc_is_identifier_char(*q)) {
    1959           24 :                         continue;
    1960              :                 }
    1961              : 
    1962          348 :                 while (q < end && isspace((unsigned char) *q)) {
    1963           44 :                         q++;
    1964              :                 }
    1965          304 :                 if (q >= end || *q != '=' || (q + 1 < end && (q[1] == '=' || q[1] == '>'))) {
    1966          272 :                         continue;
    1967              :                 }
    1968              : 
    1969           32 :                 q++;
    1970           64 :                 while (q < end && isspace((unsigned char) *q)) {
    1971           32 :                         q++;
    1972              :                 }
    1973           32 :                 if (q >= end || *q != '$') {
    1974            8 :                         continue;
    1975              :                 }
    1976              : 
    1977           24 :                 recv_start = q;
    1978           24 :                 q++;
    1979          120 :                 while (q < end && lsp_doc_is_identifier_char(*q)) {
    1980           96 :                         q++;
    1981              :                 }
    1982              : 
    1983           24 :                 receiver = zend_string_init(recv_start, (size_t) (q - recv_start), 0);
    1984           24 :                 if (zend_string_equals_literal(receiver, "$this")) {
    1985           24 :                         zend_string_release(receiver);
    1986           24 :                         continue;
    1987              :                 }
    1988              : 
    1989            0 :                 while (q < end && isspace((unsigned char) *q)) {
    1990            0 :                         q++;
    1991              :                 }
    1992            0 :                 if (q + 1 >= end || q[0] != '-' || q[1] != '>') {
    1993            0 :                         zend_string_release(receiver);
    1994            0 :                         continue;
    1995              :                 }
    1996              : 
    1997            0 :                 q += 2;
    1998            0 :                 while (q < end && isspace((unsigned char) *q)) {
    1999            0 :                         q++;
    2000              :                 }
    2001            0 :                 method_start = q;
    2002            0 :                 while (q < end && lsp_doc_is_identifier_char(*q)) {
    2003            0 :                         q++;
    2004              :                 }
    2005            0 :                 if (q == method_start || q >= end || *q != '(') {
    2006            0 :                         zend_string_release(receiver);
    2007            0 :                         continue;
    2008              :                 }
    2009              : 
    2010            0 :                 method_name = zend_string_init(method_start, (size_t) (q - method_start), 0);
    2011            0 :                 resolved = lsp_resolve_variable_method_return_class(server, document, receiver, method_name, offset);
    2012            0 :                 zend_string_release(method_name);
    2013            0 :                 zend_string_release(receiver);
    2014              : 
    2015            0 :                 if (resolved) {
    2016            0 :                         return resolved;
    2017              :                 }
    2018              :         }
    2019              : 
    2020          228 :         return NULL;
    2021              : }
    2022              : 
    2023              : /* `foreach ($container as $item)` / `as $key => $item`: infer $item's class
    2024              :  * from the container's array-ish type (`Foo[]`, `array<Foo>`, ...). */
    2025          228 : static inline zend_string *lsp_infer_foreach_element_class(lsp_server *server, lsp_document *document, zend_string *variable, size_t offset)
    2026              : {
    2027          228 :         const char *value = ZSTR_VAL(document->text);
    2028              :         zend_string *container, *container_type, *element_type, *resolved;
    2029              :         size_t limit, p, open, close, as_pos, bound_start, bound_end, container_end, depth, i;
    2030              : 
    2031          228 :         limit = offset > ZSTR_LEN(document->text) ? ZSTR_LEN(document->text) : offset;
    2032          228 :         resolved = NULL;
    2033              : 
    2034        49240 :         for (p = 0; p + sizeof("foreach") - 1 < limit; p++) {
    2035        49012 :                 if (strncasecmp(value + p, "foreach", sizeof("foreach") - 1) != 0 ||
    2036           16 :                         (p > 0 && lsp_doc_is_identifier_char(value[p - 1])) ||
    2037            8 :                         lsp_doc_is_identifier_char(value[p + sizeof("foreach") - 1])
    2038              :                 ) {
    2039        49004 :                         continue;
    2040              :                 }
    2041              : 
    2042            8 :                 open = p + sizeof("foreach") - 1;
    2043           16 :                 while (open < limit && isspace((unsigned char) value[open])) {
    2044            8 :                         open++;
    2045              :                 }
    2046            8 :                 if (open >= limit || value[open] != '(') {
    2047            0 :                         continue;
    2048              :                 }
    2049              : 
    2050            8 :                 depth = 1;
    2051            8 :                 as_pos = 0;
    2052            8 :                 close = 0;
    2053          128 :                 for (i = open + 1; i < ZSTR_LEN(document->text); i++) {
    2054          128 :                         if (value[i] == '(' || value[i] == '[') {
    2055            0 :                                 depth++;
    2056          128 :                         } else if (value[i] == ')' || value[i] == ']') {
    2057            8 :                                 depth--;
    2058            8 :                                 if (depth == 0) {
    2059            8 :                                         close = i;
    2060            8 :                                         break;
    2061              :                                 }
    2062          120 :                         } else if (depth == 1 &&
    2063          120 :                                 (value[i] == 'a' || value[i] == 'A') &&
    2064            8 :                                 i + 1 < ZSTR_LEN(document->text) &&
    2065            8 :                                 (value[i + 1] == 's' || value[i + 1] == 'S') &&
    2066            8 :                                 !lsp_doc_is_identifier_char(value[i - 1]) &&
    2067            8 :                                 (i + 2 >= ZSTR_LEN(document->text) || !lsp_doc_is_identifier_char(value[i + 2]))
    2068              :                         ) {
    2069            8 :                                 as_pos = i;
    2070              :                         }
    2071              :                 }
    2072              : 
    2073              :                 /* The hovered/completed use site must be past the foreach header. */
    2074            8 :                 if (!close || !as_pos || limit <= close) {
    2075            0 :                         continue;
    2076              :                 }
    2077              : 
    2078              :                 /* Bound variable: the LAST $ident before ')' (covers `$k => $v`). */
    2079            8 :                 bound_end = close;
    2080            8 :                 while (bound_end > as_pos && isspace((unsigned char) value[bound_end - 1])) {
    2081            0 :                         bound_end--;
    2082              :                 }
    2083            8 :                 bound_start = bound_end;
    2084           48 :                 while (bound_start > as_pos && (lsp_doc_is_identifier_char(value[bound_start - 1]) || value[bound_start - 1] == '$')) {
    2085           40 :                         bound_start--;
    2086              :                 }
    2087            8 :                 if (bound_end <= bound_start ||
    2088            8 :                         value[bound_start] != '$' ||
    2089            8 :                         bound_end - bound_start != ZSTR_LEN(variable) ||
    2090            8 :                         memcmp(value + bound_start, ZSTR_VAL(variable), ZSTR_LEN(variable)) != 0
    2091              :                 ) {
    2092            0 :                         continue;
    2093              :                 }
    2094              : 
    2095            8 :                 container_end = as_pos;
    2096           16 :                 while (container_end > open + 1 && isspace((unsigned char) value[container_end - 1])) {
    2097            8 :                         container_end--;
    2098              :                 }
    2099            8 :                 i = open + 1;
    2100            8 :                 while (i < container_end && isspace((unsigned char) value[i])) {
    2101            0 :                         i++;
    2102              :                 }
    2103            8 :                 if (i >= container_end || value[i] != '$') {
    2104            0 :                         continue;
    2105              :                 }
    2106              : 
    2107            8 :                 container = zend_string_init(value + i, container_end - i, 0);
    2108            8 :                 container_type = lsp_infer_variable_phpdoc_type(document, container, p);
    2109            8 :                 if (!container_type) {
    2110            0 :                         container_type = lsp_parameter_declared_type_for_variable(document, container, p);
    2111              :                 }
    2112            8 :                 zend_string_release(container);
    2113            8 :                 if (!container_type) {
    2114            0 :                         continue;
    2115              :                 }
    2116              : 
    2117            8 :                 element_type = lsp_type_array_element_type(container_type);
    2118            8 :                 zend_string_release(container_type);
    2119            8 :                 if (!element_type) {
    2120            0 :                         continue;
    2121              :                 }
    2122              : 
    2123            8 :                 if (resolved) {
    2124            0 :                         zend_string_release(resolved);
    2125              :                 }
    2126            8 :                 resolved = lsp_resolve_class_name(document->text, element_type);
    2127            8 :                 zend_string_release(element_type);
    2128              :         }
    2129              : 
    2130          228 :         return resolved;
    2131              : }
    2132              : 
    2133          196 : extern zend_string *lsp_infer_receiver_class(lsp_server *server, lsp_document *document, zend_string *receiver, size_t offset)
    2134              : {
    2135              :         zend_string *type, *class_name, *resolved;
    2136              : 
    2137          196 :         type = lsp_phpdoc_type_for_word(document->text, receiver);
    2138          196 :         if (type) {
    2139            8 :                 class_name = lsp_resolve_class_name_at(document->text, type, offset);
    2140            8 :                 zend_string_release(type);
    2141              : 
    2142            8 :                 if (class_name) {
    2143            4 :                         return class_name;
    2144              :                 }
    2145              :         }
    2146              : 
    2147          192 :         type = lsp_phpdoc_property_type_for_word(document->text, receiver, offset);
    2148          192 :         if (type) {
    2149            0 :                 class_name = lsp_resolve_class_name_at(document->text, type, offset);
    2150            0 :                 zend_string_release(type);
    2151              : 
    2152            0 :                 if (class_name) {
    2153            0 :                         return class_name;
    2154              :                 }
    2155              :         }
    2156              : 
    2157          192 :         class_name = lsp_infer_new_assignment_class(document->text, receiver, offset);
    2158          192 :         if (class_name) {
    2159              :                 /* Resolve imports and the current namespace before consulting
    2160              :                  * loaded classes, so `use App\DateTime; new DateTime()` does not
    2161              :                  * get shadowed by the builtin sharing the short name. */
    2162           20 :                 resolved = lsp_resolve_class_name_at(document->text, class_name, offset);
    2163           20 :                 if (resolved) {
    2164           20 :                         zend_string_release(class_name);
    2165              : 
    2166           20 :                         return resolved;
    2167              :                 }
    2168              : 
    2169            0 :                 if (zend_lookup_class(class_name)) {
    2170            0 :                         return class_name;
    2171              :                 }
    2172              : 
    2173            0 :                 zend_string_release(class_name);
    2174              :         }
    2175              : 
    2176          172 :         class_name = lsp_infer_method_array_access_assignment_class(server, document, receiver, offset);
    2177          172 :         if (class_name) {
    2178           12 :                 return class_name;
    2179              :         }
    2180              : 
    2181          160 :         class_name = lsp_infer_method_call_assignment_class(server, document, receiver, offset);
    2182          160 :         if (class_name) {
    2183           32 :                 return class_name;
    2184              :         }
    2185              : 
    2186          128 :         class_name = lsp_infer_property_fetch_assignment_class(server, document, receiver, offset);
    2187          128 :         if (class_name) {
    2188            8 :                 return class_name;
    2189              :         }
    2190              : 
    2191          120 :         type = lsp_parameter_declared_type_for_variable(document, receiver, offset);
    2192          120 :         if (type) {
    2193           36 :                 class_name = lsp_resolve_class_name_at(document->text, type, offset);
    2194           36 :                 zend_string_release(type);
    2195              : 
    2196           36 :                 if (class_name) {
    2197           36 :                         return class_name;
    2198              :                 }
    2199              :         }
    2200              : 
    2201           84 :         class_name = lsp_infer_external_call_assignment_class(server, document, receiver, offset);
    2202           84 :         if (class_name) {
    2203            0 :                 return class_name;
    2204              :         }
    2205              : 
    2206           84 :         return lsp_infer_foreach_element_class(server, document, receiver, offset);
    2207              : }
    2208              : 
    2209           12 : static inline bool lsp_static_method_call_before_offset(lsp_document *document, size_t offset, zend_string **method_name, zend_string **class_name)
    2210              : {
    2211           12 :         const char *value = ZSTR_VAL(document->text);
    2212              :         zend_string *raw, *resolved;
    2213              :         size_t i, close_offset, open_offset, method_start, method_end, class_start, class_end;
    2214              : 
    2215           12 :         *method_name = NULL;
    2216           12 :         *class_name = NULL;
    2217              : 
    2218           12 :         if (offset > ZSTR_LEN(document->text)) {
    2219            0 :                 offset = ZSTR_LEN(document->text);
    2220              :         }
    2221              : 
    2222           12 :         i = offset;
    2223           12 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    2224            0 :                 i--;
    2225              :         }
    2226              : 
    2227           12 :         if (i == 0 || value[i - 1] != ')') {
    2228            8 :                 return false;
    2229              :         }
    2230              : 
    2231            4 :         close_offset = i - 1;
    2232            4 :         if (!lsp_find_matching_open_paren(document->text, close_offset, &open_offset)) {
    2233            0 :                 return false;
    2234              :         }
    2235              : 
    2236            4 :         i = open_offset;
    2237            4 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    2238            0 :                 i--;
    2239              :         }
    2240              : 
    2241            4 :         method_end = i;
    2242           32 :         while (i > 0 && lsp_doc_is_identifier_char(value[i - 1])) {
    2243           28 :                 i--;
    2244              :         }
    2245              : 
    2246            4 :         method_start = i;
    2247            4 :         if (method_start == method_end) {
    2248            0 :                 return false;
    2249              :         }
    2250              : 
    2251            4 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    2252            0 :                 i--;
    2253              :         }
    2254              : 
    2255            4 :         if (i < 2 || value[i - 2] != ':' || value[i - 1] != ':') {
    2256            0 :                 return false;
    2257              :         }
    2258              : 
    2259            4 :         i -= 2;
    2260              : 
    2261            4 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    2262            0 :                 i--;
    2263              :         }
    2264              : 
    2265            4 :         class_end = i;
    2266           20 :         while (i > 0 && (lsp_doc_is_identifier_char(value[i - 1]) || value[i - 1] == '\\')) {
    2267           16 :                 i--;
    2268              :         }
    2269              : 
    2270            4 :         class_start = i;
    2271            4 :         if (class_start == class_end) {
    2272            0 :                 return false;
    2273              :         }
    2274              : 
    2275            4 :         raw = zend_string_init(value + class_start, class_end - class_start, 0);
    2276            4 :         if ((ZSTR_LEN(raw) == sizeof("self") - 1 && strncasecmp(ZSTR_VAL(raw), "self", ZSTR_LEN(raw)) == 0) ||
    2277            4 :                 (ZSTR_LEN(raw) == sizeof("static") - 1 && strncasecmp(ZSTR_VAL(raw), "static", ZSTR_LEN(raw)) == 0)
    2278              :         ) {
    2279            0 :                 zend_string_release(raw);
    2280            0 :                 resolved = lsp_inference_declared_class_name(document->text);
    2281              :         } else {
    2282            4 :                 resolved = lsp_resolve_class_name(document->text, raw);
    2283            4 :                 zend_string_release(raw);
    2284              :         }
    2285              : 
    2286            4 :         if (!resolved) {
    2287            0 :                 return false;
    2288              :         }
    2289              : 
    2290            4 :         *class_name = resolved;
    2291            4 :         *method_name = zend_string_init(value + method_start, method_end - method_start, 0);
    2292              : 
    2293            4 :         return true;
    2294              : }
    2295              : 
    2296           24 : static inline zend_string *lsp_array_access_literal_key(zend_string *text, size_t open_bracket, size_t close_bracket)
    2297              : {
    2298              :         const char *value;
    2299              :         size_t start, end;
    2300              :         char quote;
    2301              : 
    2302           24 :         if (open_bracket >= close_bracket || close_bracket > ZSTR_LEN(text)) {
    2303            0 :                 return NULL;
    2304              :         }
    2305              : 
    2306           24 :         value = ZSTR_VAL(text);
    2307           24 :         start = open_bracket + 1;
    2308           24 :         end = close_bracket;
    2309           24 :         while (start < end && isspace((unsigned char) value[start])) {
    2310            0 :                 start++;
    2311              :         }
    2312              : 
    2313           24 :         while (end > start && isspace((unsigned char) value[end - 1])) {
    2314            0 :                 end--;
    2315              :         }
    2316              : 
    2317           24 :         if (start >= end) {
    2318            0 :                 return NULL;
    2319              :         }
    2320              : 
    2321           24 :         if (value[start] == '\'' || value[start] == '"') {
    2322            8 :                 quote = value[start++];
    2323            8 :                 if (end <= start || value[end - 1] != quote) {
    2324            0 :                         return NULL;
    2325              :                 }
    2326            8 :                 end--;
    2327              :         }
    2328              : 
    2329           24 :         if (start >= end) {
    2330            0 :                 return NULL;
    2331              :         }
    2332              : 
    2333           24 :         return zend_string_init(value + start, end - start, 0);
    2334              : }
    2335              : 
    2336           12 : static inline zend_string *lsp_resolve_array_shape_key_class(lsp_document *document, zend_string *container_type, zend_string *key)
    2337              : {
    2338              :         zend_string *key_type, *class_name;
    2339              : 
    2340           12 :         key_type = lsp_phpdoc_array_shape_key_type(container_type, key);
    2341           12 :         if (!key_type) {
    2342            4 :                 return NULL;
    2343              :         }
    2344              : 
    2345            8 :         class_name = lsp_resolve_class_name(document->text, key_type);
    2346            8 :         zend_string_release(key_type);
    2347              : 
    2348            8 :         return class_name;
    2349              : }
    2350              : 
    2351           16 : static inline zend_string *lsp_project_method_return_type_for_receiver(lsp_server *server, lsp_document *document, size_t receiver_end, zend_string *method_name, size_t context_offset)
    2352              : {
    2353              :         zend_string *variable, *receiver_class, *class_text, *origin_class, *return_type;
    2354              : 
    2355           16 :         class_text = NULL;
    2356           16 :         origin_class = NULL;
    2357           16 :         if (!lsp_variable_ending_at(document->text, receiver_end, &variable)) {
    2358           12 :                 return NULL;
    2359              :         }
    2360              : 
    2361            4 :         if (zend_string_equals_literal(variable, "$this")) {
    2362            0 :                 receiver_class = lsp_inference_declared_class_name(document->text);
    2363              :         } else {
    2364            4 :                 receiver_class = lsp_infer_receiver_class(server, document, variable, context_offset);
    2365              :         }
    2366              : 
    2367            4 :         zend_string_release(variable);
    2368            4 :         if (!receiver_class) {
    2369            0 :                 return NULL;
    2370              :         }
    2371              : 
    2372            4 :         return_type = lsp_project_method_return_type(server, receiver_class, method_name, &class_text, &origin_class);
    2373            4 :         zend_string_release(receiver_class);
    2374            4 :         if (class_text) {
    2375            4 :                 zend_string_release(class_text);
    2376              :         }
    2377              : 
    2378            4 :         if (origin_class) {
    2379            4 :                 zend_string_release(origin_class);
    2380              :         }
    2381              : 
    2382            4 :         return return_type;
    2383              : }
    2384              : 
    2385           24 : static inline zend_string *lsp_resolve_array_access_expression_class(lsp_server *server, lsp_document *document, size_t close_bracket, size_t context_offset)
    2386              : {
    2387              :         zend_string *key, *variable, *container_type, *method_name, *class_name;
    2388              :         size_t open_bracket, receiver_end;
    2389              : 
    2390           24 :         if (!lsp_find_matching_open_bracket(document->text, close_bracket, &open_bracket)) {
    2391            0 :                 return NULL;
    2392              :         }
    2393              : 
    2394           24 :         key = lsp_array_access_literal_key(document->text, open_bracket, close_bracket);
    2395           24 :         if (!key) {
    2396            0 :                 return NULL;
    2397              :         }
    2398              : 
    2399           24 :         container_type = NULL;
    2400           24 :         if (lsp_variable_ending_at(document->text, open_bracket, &variable)) {
    2401            8 :                 container_type = lsp_infer_variable_type(server, document, variable, context_offset);
    2402            8 :                 zend_string_release(variable);
    2403           16 :         } else if (lsp_parse_method_call_before_offset(document->text, open_bracket, &method_name, &receiver_end)) {
    2404           16 :                 container_type = lsp_project_method_return_type_for_receiver(server, document, receiver_end, method_name, context_offset);
    2405           16 :                 zend_string_release(method_name);
    2406              :         }
    2407              : 
    2408           24 :         if (!container_type) {
    2409           12 :                 zend_string_release(key);
    2410              : 
    2411           12 :                 return NULL;
    2412              :         }
    2413              : 
    2414           12 :         class_name = lsp_resolve_array_shape_key_class(document, container_type, key);
    2415           12 :         zend_string_release(container_type);
    2416           12 :         zend_string_release(key);
    2417              : 
    2418           12 :         return class_name;
    2419              : }
    2420              : 
    2421           32 : static inline bool lsp_inference_modifier_at(const char *value, size_t start, size_t end, const char *modifier, size_t *next)
    2422              : {
    2423              :         size_t length;
    2424              : 
    2425           32 :         length = strlen(modifier);
    2426           32 :         if (start + length > end || strncasecmp(value + start, modifier, length) != 0) {
    2427           28 :                 return false;
    2428              :         }
    2429              : 
    2430            4 :         if (start + length < end && lsp_doc_is_identifier_char(value[start + length])) {
    2431            0 :                 return false;
    2432              :         }
    2433              : 
    2434            4 :         *next = start + length;
    2435              : 
    2436            4 :         return true;
    2437              : }
    2438              : 
    2439            4 : static inline zend_string *lsp_inference_property_type_before_variable_fallback(zend_string *text, size_t variable_offset)
    2440              : {
    2441            4 :         const char *modifiers[7] = {"public", "protected", "private", "readonly", "static", "final", "var"}, *value = ZSTR_VAL(text);
    2442              :         size_t declaration_start, type_start, type_end, modifier_index, next, scan;
    2443              :         bool stripped;
    2444              : 
    2445            4 :         if (variable_offset > ZSTR_LEN(text)) {
    2446            0 :                 return NULL;
    2447              :         }
    2448              : 
    2449            4 :         declaration_start = variable_offset;
    2450            4 :         while (declaration_start > 0 &&
    2451           88 :                 value[declaration_start - 1] != ';' &&
    2452          176 :                 value[declaration_start - 1] != '{' &&
    2453           84 :                 value[declaration_start - 1] != '}'
    2454              :         ) {
    2455           84 :                 declaration_start--;
    2456              :         }
    2457              : 
    2458            4 :         type_start = declaration_start;
    2459            4 :         type_end = variable_offset;
    2460           24 :         while (type_start < type_end && isspace((unsigned char) value[type_start])) {
    2461           20 :                 type_start++;
    2462              :         }
    2463              : 
    2464            8 :         while (type_end > type_start && isspace((unsigned char) value[type_end - 1])) {
    2465            4 :                 type_end--;
    2466              :         }
    2467              : 
    2468           60 :         for (scan = type_start; scan + 1 < type_end; scan++) {
    2469           56 :                 if (value[scan] == '*' && value[scan + 1] == '/') {
    2470            0 :                         type_start = scan + 2;
    2471              :                 }
    2472              :         }
    2473              : 
    2474            4 :         while (type_start < type_end && isspace((unsigned char) value[type_start])) {
    2475            0 :                 type_start++;
    2476              :         }
    2477              : 
    2478              :         do {
    2479            8 :                 stripped = false;
    2480           36 :                 for (modifier_index = 0; modifier_index < sizeof(modifiers) / sizeof(modifiers[0]); modifier_index++) {
    2481           32 :                         if (!lsp_inference_modifier_at(value, type_start, type_end, modifiers[modifier_index], &next)) {
    2482           28 :                                 continue;
    2483              :                         }
    2484              : 
    2485            4 :                         type_start = next;
    2486            8 :                         while (type_start < type_end && isspace((unsigned char) value[type_start])) {
    2487            4 :                                 type_start++;
    2488              :                         }
    2489              : 
    2490            4 :                         stripped = true;
    2491            4 :                         break;
    2492              :                 }
    2493            8 :         } while (stripped);
    2494              : 
    2495            4 :         if (type_end <= type_start || (type_end - type_start == 1 && value[type_start] == '?')) {
    2496            0 :                 return NULL;
    2497              :         }
    2498              : 
    2499            4 :         return zend_string_init(value + type_start, type_end - type_start, 0);
    2500              : }
    2501              : 
    2502           12 : static inline void lsp_inference_append_ast_name(smart_str *buffer, zend_ast *ast)
    2503              : {
    2504              :         zend_string *name;
    2505              : 
    2506           12 :         name = lsp_ast_string_value(ast);
    2507           12 :         if (!name) {
    2508            0 :                 return;
    2509              :         }
    2510              : 
    2511           12 :         if (ast->attr == ZEND_NAME_FQ) {
    2512            0 :                 smart_str_appendc(buffer, '\\');
    2513           12 :         } else if (ast->attr == ZEND_NAME_RELATIVE) {
    2514            0 :                 smart_str_appends(buffer, "namespace\\");
    2515              :         }
    2516              : 
    2517           12 :         smart_str_append(buffer, name);
    2518              : }
    2519              : 
    2520           12 : static inline void lsp_inference_append_ast_type(smart_str *buffer, zend_ast *type_ast)
    2521              : {
    2522              :         zend_ast_list *list;
    2523              :         uint32_t i;
    2524              :         char separator;
    2525              :         bool parenthesize;
    2526              : 
    2527           12 :         if (!type_ast) {
    2528            0 :                 return;
    2529              :         }
    2530              : 
    2531           12 :         if (type_ast->kind == ZEND_AST_TYPE_UNION || type_ast->kind == ZEND_AST_TYPE_INTERSECTION) {
    2532            0 :                 list = zend_ast_get_list(type_ast);
    2533            0 :                 separator = type_ast->kind == ZEND_AST_TYPE_UNION ? '|' : '&';
    2534            0 :                 for (i = 0; i < list->children; i++) {
    2535            0 :                         if (i > 0) {
    2536            0 :                                 smart_str_appendc(buffer, separator);
    2537              :                         }
    2538              : 
    2539            0 :                         parenthesize = type_ast->kind == ZEND_AST_TYPE_UNION &&
    2540            0 :                                 list->child[i] &&
    2541            0 :                                 list->child[i]->kind == ZEND_AST_TYPE_INTERSECTION
    2542              :                         ;
    2543            0 :                         if (parenthesize) {
    2544            0 :                                 smart_str_appendc(buffer, '(');
    2545              :                         }
    2546              : 
    2547            0 :                         lsp_inference_append_ast_type(buffer, list->child[i]);
    2548              : 
    2549            0 :                         if (parenthesize) {
    2550            0 :                                 smart_str_appendc(buffer, ')');
    2551              :                         }
    2552              :                 }
    2553              : 
    2554            0 :                 return;
    2555              :         }
    2556              : 
    2557           12 :         if ((type_ast->attr & ZEND_TYPE_NULLABLE) != 0) {
    2558            0 :                 smart_str_appendc(buffer, '?');
    2559              :         }
    2560              : 
    2561           12 :         if (type_ast->kind == ZEND_AST_TYPE) {
    2562            0 :                 switch (type_ast->attr & ~ZEND_TYPE_NULLABLE) {
    2563            0 :                         case IS_ARRAY:
    2564            0 :                                 smart_str_appends(buffer, "array");
    2565            0 :                                 return;
    2566            0 :                         case IS_CALLABLE:
    2567            0 :                                 smart_str_appends(buffer, "callable");
    2568            0 :                                 return;
    2569            0 :                         case IS_STATIC:
    2570            0 :                                 smart_str_appends(buffer, "static");
    2571            0 :                                 return;
    2572            0 :                         case IS_MIXED:
    2573            0 :                                 smart_str_appends(buffer, "mixed");
    2574            0 :                                 return;
    2575            0 :                         default:
    2576            0 :                                 return;
    2577              :                 }
    2578              :         }
    2579              : 
    2580           12 :         lsp_inference_append_ast_name(buffer, type_ast);
    2581              : }
    2582              : 
    2583           12 : static inline zend_string *lsp_inference_ast_type_string(zend_ast *type_ast)
    2584              : {
    2585           12 :         smart_str buffer = {0};
    2586              : 
    2587           12 :         if (!type_ast) {
    2588            0 :                 return NULL;
    2589              :         }
    2590              : 
    2591           12 :         lsp_inference_append_ast_type(&buffer, type_ast);
    2592           12 :         smart_str_0(&buffer);
    2593              : 
    2594           12 :         if (!buffer.s || ZSTR_LEN(buffer.s) == 0) {
    2595            0 :                 if (buffer.s) {
    2596            0 :                         zend_string_release(buffer.s);
    2597              :                 }
    2598              : 
    2599            0 :                 return NULL;
    2600              :         }
    2601              : 
    2602           12 :         return buffer.s;
    2603              : }
    2604              : 
    2605           12 : static inline bool lsp_inference_ast_property_name_matches(zend_ast *property_ast, zend_string *property_name)
    2606              : {
    2607              :         zend_string *name;
    2608              : 
    2609           12 :         if (!property_ast || property_ast->kind != ZEND_AST_PROP_ELEM) {
    2610            0 :                 return false;
    2611              :         }
    2612              : 
    2613           12 :         name = lsp_ast_string_value(property_ast->child[0]);
    2614           12 :         if (!name) {
    2615            0 :                 return false;
    2616              :         }
    2617              : 
    2618           24 :         return ZSTR_LEN(name) == ZSTR_LEN(property_name) &&
    2619           12 :                 strncmp(ZSTR_VAL(name), ZSTR_VAL(property_name), ZSTR_LEN(name)) == 0
    2620              :         ;
    2621              : }
    2622              : 
    2623           12 : static inline zend_string *lsp_resolve_ast_property_group_declared_class(zend_string *contents, zend_ast *prop_group, zend_string *property_name)
    2624              : {
    2625              :         zend_ast_list *properties;
    2626              :         zend_ast *type_ast, *property_ast;
    2627              :         zend_string *type, *class_name;
    2628              :         uint32_t i;
    2629              : 
    2630           12 :         if (!prop_group || prop_group->kind != ZEND_AST_PROP_GROUP || !prop_group->child[1] || !zend_ast_is_list(prop_group->child[1])) {
    2631            0 :                 return NULL;
    2632              :         }
    2633              : 
    2634           12 :         type_ast = prop_group->child[0];
    2635           12 :         if (!type_ast) {
    2636            0 :                 return NULL;
    2637              :         }
    2638              : 
    2639           12 :         properties = zend_ast_get_list(prop_group->child[1]);
    2640           12 :         for (i = 0; i < properties->children; i++) {
    2641           12 :                 property_ast = properties->child[i];
    2642           12 :                 if (!lsp_inference_ast_property_name_matches(property_ast, property_name)) {
    2643            0 :                         continue;
    2644              :                 }
    2645              : 
    2646           12 :                 type = lsp_inference_ast_type_string(type_ast);
    2647           12 :                 if (!type) {
    2648            0 :                         return NULL;
    2649              :                 }
    2650              : 
    2651           12 :                 class_name = lsp_resolve_class_name(contents, type);
    2652           12 :                 zend_string_release(type);
    2653              : 
    2654           12 :                 return class_name;
    2655              :         }
    2656              : 
    2657            0 :         return NULL;
    2658              : }
    2659              : 
    2660            0 : static inline zend_string *lsp_resolve_ast_promoted_property_declared_class(zend_string *contents, zend_ast_decl *method, zend_string *property_name)
    2661              : {
    2662              :         zend_ast_list *params;
    2663              :         zend_ast *param;
    2664              :         zend_string *name, *type, *class_name;
    2665              :         uint32_t i;
    2666              : 
    2667            0 :         if (!method->name ||
    2668            0 :                 !zend_string_equals_literal_ci(method->name, "__construct") ||
    2669            0 :                 !method->child[0] ||
    2670            0 :                 !zend_ast_is_list(method->child[0])
    2671              :         ) {
    2672            0 :                 return NULL;
    2673              :         }
    2674              : 
    2675            0 :         params = zend_ast_get_list(method->child[0]);
    2676            0 :         for (i = 0; i < params->children; i++) {
    2677            0 :                 param = params->child[i];
    2678            0 :                 if (!param || param->kind != ZEND_AST_PARAM || !param->child[0]) {
    2679            0 :                         continue;
    2680              :                 }
    2681              : 
    2682              :                 /* Only params carrying a visibility flag promote to properties. */
    2683            0 :                 if ((param->attr & (ZEND_ACC_PUBLIC | ZEND_ACC_PROTECTED | ZEND_ACC_PRIVATE)) == 0) {
    2684            0 :                         continue;
    2685              :                 }
    2686              : 
    2687            0 :                 name = lsp_ast_string_value(param->child[1]);
    2688            0 :                 if (!name ||
    2689            0 :                         ZSTR_LEN(name) != ZSTR_LEN(property_name) ||
    2690            0 :                         strncmp(ZSTR_VAL(name), ZSTR_VAL(property_name), ZSTR_LEN(name)) != 0
    2691              :                 ) {
    2692            0 :                         continue;
    2693              :                 }
    2694              : 
    2695            0 :                 type = lsp_inference_ast_type_string(param->child[0]);
    2696            0 :                 if (!type) {
    2697            0 :                         return NULL;
    2698              :                 }
    2699              : 
    2700            0 :                 class_name = lsp_resolve_class_name(contents, type);
    2701            0 :                 zend_string_release(type);
    2702              : 
    2703            0 :                 return class_name;
    2704              :         }
    2705              : 
    2706            0 :         return NULL;
    2707              : }
    2708              : 
    2709           12 : static inline zend_string *lsp_resolve_ast_declared_class_name(zend_string *contents, zend_string *raw_name)
    2710              : {
    2711              :         zend_string *namespace_name, *declared;
    2712              : 
    2713           12 :         namespace_name = lsp_document_namespace(contents);
    2714           12 :         if (namespace_name != zend_empty_string) {
    2715           12 :                 declared = strpprintf(0, "%s\\%s", ZSTR_VAL(namespace_name), ZSTR_VAL(raw_name));
    2716           12 :                 zend_string_release(namespace_name);
    2717              : 
    2718           12 :                 return declared;
    2719              :         }
    2720              : 
    2721            0 :         return zend_string_copy(raw_name);
    2722              : }
    2723              : 
    2724           48 : static inline zend_string *lsp_resolve_ast_class_property_declared_class(zend_string *contents, zend_ast *class_ast, zend_string *receiver_class, zend_string *property_name)
    2725              : {
    2726              :         zend_ast_decl *decl;
    2727              :         zend_ast_list *statements;
    2728              :         zend_ast *statement;
    2729              :         zend_string *raw_name, *declared, *class_name;
    2730              :         uint32_t i;
    2731              : 
    2732           48 :         if (!class_ast || class_ast->kind != ZEND_AST_CLASS) {
    2733           36 :                 return NULL;
    2734              :         }
    2735              : 
    2736           12 :         decl = (zend_ast_decl *) class_ast;
    2737           12 :         raw_name = decl->name ? zend_string_copy(decl->name) : NULL;
    2738           12 :         if (!raw_name) {
    2739            0 :                 return NULL;
    2740              :         }
    2741              : 
    2742           12 :         declared = lsp_resolve_ast_declared_class_name(contents, raw_name);
    2743           12 :         zend_string_release(raw_name);
    2744           12 :         if (ZSTR_LEN(declared) != ZSTR_LEN(receiver_class) ||
    2745           12 :                 strncasecmp(ZSTR_VAL(declared), ZSTR_VAL(receiver_class), ZSTR_LEN(declared)) != 0
    2746              :         ) {
    2747            0 :                 zend_string_release(declared);
    2748              : 
    2749            0 :                 return NULL;
    2750              :         }
    2751              : 
    2752           12 :         zend_string_release(declared);
    2753           12 :         if (!decl->child[2] || !zend_ast_is_list(decl->child[2])) {
    2754            0 :                 return NULL;
    2755              :         }
    2756              : 
    2757           12 :         statements = zend_ast_get_list(decl->child[2]);
    2758           12 :         for (i = 0; i < statements->children; i++) {
    2759           12 :                 statement = statements->child[i];
    2760           12 :                 if (!statement || statement->kind != ZEND_AST_PROP_GROUP) {
    2761            0 :                         continue;
    2762              :                 }
    2763              : 
    2764           12 :                 class_name = lsp_resolve_ast_property_group_declared_class(contents, statement, property_name);
    2765           12 :                 if (class_name) {
    2766           12 :                         return class_name;
    2767              :                 }
    2768              :         }
    2769              : 
    2770              :         /* Constructor-promoted properties (`__construct(private Repo $repo)`)
    2771              :          * declare no PROP_GROUP; their type lives on the constructor param. */
    2772            0 :         for (i = 0; i < statements->children; i++) {
    2773            0 :                 statement = statements->child[i];
    2774            0 :                 if (!statement || statement->kind != ZEND_AST_METHOD) {
    2775            0 :                         continue;
    2776              :                 }
    2777              : 
    2778            0 :                 class_name = lsp_resolve_ast_promoted_property_declared_class(contents, (zend_ast_decl *) statement, property_name);
    2779            0 :                 if (class_name) {
    2780            0 :                         return class_name;
    2781              :                 }
    2782              :         }
    2783              : 
    2784            0 :         return NULL;
    2785              : }
    2786              : 
    2787           60 : static inline zend_string *lsp_resolve_property_declared_class_in_ast(zend_string *contents, zend_ast *ast, zend_string *receiver_class, zend_string *property_name)
    2788              : {
    2789              :         zend_ast_list *list;
    2790              :         zend_string *class_name;
    2791              :         uint32_t i, count;
    2792              : 
    2793           60 :         if (!ast) {
    2794           12 :                 return NULL;
    2795              :         }
    2796              : 
    2797           48 :         class_name = lsp_resolve_ast_class_property_declared_class(contents, ast, receiver_class, property_name);
    2798           48 :         if (class_name) {
    2799           12 :                 return class_name;
    2800              :         }
    2801              : 
    2802           36 :         if (zend_ast_is_list(ast)) {
    2803           12 :                 list = zend_ast_get_list(ast);
    2804           24 :                 for (i = 0; i < list->children; i++) {
    2805           24 :                         class_name = lsp_resolve_property_declared_class_in_ast(contents, list->child[i], receiver_class, property_name);
    2806           24 :                         if (class_name) {
    2807           12 :                                 return class_name;
    2808              :                         }
    2809              :                 }
    2810              : 
    2811            0 :                 return NULL;
    2812              :         }
    2813              : 
    2814           24 :         if (zend_ast_is_special(ast) || php_ver_abstract.ast_is_opaque_node(ast->kind)) {
    2815           12 :                 return NULL;
    2816              :         }
    2817              : 
    2818           12 :         count = zend_ast_get_num_children(ast);
    2819           36 :         for (i = 0; i < count; i++) {
    2820           24 :                 class_name = lsp_resolve_property_declared_class_in_ast(contents, ast->child[i], receiver_class, property_name);
    2821           24 :                 if (class_name) {
    2822            0 :                         return class_name;
    2823              :                 }
    2824              :         }
    2825              : 
    2826           12 :         return NULL;
    2827              : }
    2828              : 
    2829            8 : static inline zend_string *lsp_resolve_property_declared_class_in_tokens(zend_string *contents, zend_string *receiver_class, zend_string *property_name)
    2830              : {
    2831            8 :         zend_long body_depth = 0;
    2832              :         zend_string *variable, *type, *class_name;
    2833              :         zval tokens_zv, *token;
    2834              :         HashTable *tokens;
    2835              :         uint32_t i, count;
    2836            8 :         size_t class_start = 0, body_start = 0, body_end = 0, token_offset, promoted_param_start;
    2837              :         bool property_matches, promoted;
    2838              : 
    2839            8 :         ZVAL_UNDEF(&tokens_zv);
    2840            8 :         if (!lsp_find_class_header_for_name(contents, receiver_class, &class_start, &body_start, &body_end, &body_depth)) {
    2841            0 :                 return NULL;
    2842              :         }
    2843              : 
    2844            8 :         lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
    2845            8 :         if (Z_TYPE(tokens_zv) != IS_ARRAY) {
    2846            0 :                 return NULL;
    2847              :         }
    2848              : 
    2849            8 :         tokens = Z_ARRVAL(tokens_zv);
    2850            8 :         count = zend_hash_num_elements(tokens);
    2851          276 :         for (i = 0; i < count; i++) {
    2852          276 :                 token = zend_hash_index_find(tokens, i);
    2853          552 :                 if (!token ||
    2854          276 :                         Z_TYPE_P(token) != IS_ARRAY ||
    2855          276 :                         !lsp_token_in_bounds(token, body_start, body_end) ||
    2856          100 :                         !lsp_token_at_depth(contents, token, body_depth) ||
    2857          100 :                         !lsp_token_name_equals(token, "T_VARIABLE")
    2858              :                 ) {
    2859          268 :                         continue;
    2860              :                 }
    2861              : 
    2862            8 :                 promoted = false;
    2863            8 :                 promoted_param_start = 0;
    2864            8 :                 if (!lsp_token_is_property_declaration(tokens, i, contents, body_depth)) {
    2865              :                         /* Constructor-promoted properties declare their type on the
    2866              :                          * __construct parameter. */
    2867            4 :                         promoted = lsp_token_is_promoted_property(tokens, i, contents, body_depth, &promoted_param_start);
    2868            4 :                         if (!promoted) {
    2869            0 :                                 continue;
    2870              :                         }
    2871              :                 }
    2872              : 
    2873            8 :                 variable = lsp_token_string(token, "text");
    2874           16 :                 property_matches = variable &&
    2875            8 :                         ZSTR_LEN(variable) == ZSTR_LEN(property_name) + 1 &&
    2876           24 :                         ZSTR_VAL(variable)[0] == '$' &&
    2877            8 :                         strncmp(ZSTR_VAL(variable) + 1, ZSTR_VAL(property_name), ZSTR_LEN(property_name)) == 0
    2878              :                 ;
    2879            8 :                 if (!property_matches) {
    2880            0 :                         continue;
    2881              :                 }
    2882              : 
    2883            8 :                 token_offset = (size_t) lsp_token_long(token, "offset", 0);
    2884            8 :                 type = promoted
    2885            4 :                         ? lsp_parameter_declared_type_before_variable(contents, token_offset, promoted_param_start)
    2886            8 :                         : lsp_inference_property_type_before_variable_fallback(contents, token_offset)
    2887              :                 ;
    2888            8 :                 if (!type) {
    2889            0 :                         zval_ptr_dtor(&tokens_zv);
    2890              : 
    2891            0 :                         return NULL;
    2892              :                 }
    2893              : 
    2894            8 :                 class_name = lsp_resolve_class_name(contents, type);
    2895            8 :                 zend_string_release(type);
    2896            8 :                 zval_ptr_dtor(&tokens_zv);
    2897              : 
    2898            8 :                 return class_name;
    2899              :         }
    2900              : 
    2901            0 :         zval_ptr_dtor(&tokens_zv);
    2902              : 
    2903            0 :         return NULL;
    2904              : }
    2905              : 
    2906           20 : static inline zend_string *lsp_resolve_property_declared_class_in_text(zend_string *contents, zend_string *receiver_class, zend_string *property_name)
    2907              : {
    2908              :         zend_arena *ast_arena;
    2909              :         zend_ast *ast;
    2910              :         zend_string *class_name;
    2911              : 
    2912           20 :         ast = lsp_compile_string_to_ast_silent(contents, ZSTR_EMPTY_ALLOC(), &ast_arena);
    2913           20 :         if (ast) {
    2914           12 :                 class_name = lsp_resolve_property_declared_class_in_ast(contents, ast, receiver_class, property_name);
    2915           12 :                 lsp_compiled_ast_destroy(ast, ast_arena);
    2916              : 
    2917           12 :                 return class_name;
    2918              :         }
    2919              : 
    2920            8 :         return lsp_resolve_property_declared_class_in_tokens(contents, receiver_class, property_name);
    2921              : }
    2922              : 
    2923           20 : static inline zend_string *lsp_resolve_project_property_declared_class(lsp_server *server, lsp_document *document, zend_string *receiver_class, zend_string *property_name)
    2924              : {
    2925              :         lsp_document *class_document;
    2926              :         zend_string *current_class, *path, *contents, *class_name;
    2927              :         bool owns_contents;
    2928              : 
    2929           20 :         current_class = lsp_inference_declared_class_name(document->text);
    2930           20 :         if (current_class) {
    2931           20 :                 if (zend_string_equals(current_class, receiver_class)) {
    2932           20 :                         class_name = lsp_resolve_property_declared_class_in_text(document->text, receiver_class, property_name);
    2933           20 :                         zend_string_release(current_class);
    2934           20 :                         if (class_name) {
    2935           20 :                                 return class_name;
    2936              :                         }
    2937              :                 } else {
    2938            0 :                         zend_string_release(current_class);
    2939              :                 }
    2940              :         }
    2941              : 
    2942            0 :         path = lsp_find_project_symbol_path(server, LSP_SYMBOL_CLASS, receiver_class);
    2943            0 :         if (!path) {
    2944            0 :                 return NULL;
    2945              :         }
    2946              : 
    2947            0 :         class_document = lsp_document_for_path(server, path);
    2948            0 :         contents = class_document ? zend_string_copy(class_document->text) : lsp_read_file(path);
    2949            0 :         zend_string_release(path);
    2950            0 :         owns_contents = class_document || contents != zend_empty_string;
    2951            0 :         if (!owns_contents) {
    2952            0 :                 return NULL;
    2953              :         }
    2954              : 
    2955            0 :         class_name = lsp_resolve_property_declared_class_in_text(contents, receiver_class, property_name);
    2956            0 :         zend_string_release(contents);
    2957              : 
    2958            0 :         return class_name;
    2959              : }
    2960              : 
    2961              : /* `$x = $this->prop;` / `$x = $other->prop;` — assignment from a bare
    2962              :  * property fetch. The property's declared (or promoted/constructor) type
    2963              :  * gives $x its class, matching what PhpStorm resolves instantly. Method-call
    2964              :  * assignments take the earlier lsp_infer_method_call_assignment_class path;
    2965              :  * this one requires the fetch NOT to be a call. */
    2966          360 : static inline zend_string *lsp_infer_property_fetch_assignment_class(lsp_server *server, lsp_document *document, zend_string *receiver, size_t offset)
    2967              : {
    2968          360 :         const char *value = ZSTR_VAL(document->text), *end = value + lsp_current_statement_scan_limit(document->text, offset),
    2969          360 :                 *p = value, *match, *q, *recv_start, *recv_end, *prop_start, *prop_end;
    2970          360 :         zend_string *recv_variable, *receiver_class, *property_name, *class_name = NULL;
    2971              : 
    2972         1772 :         while (p < end) {
    2973         1768 :                 match = strstr(p, ZSTR_VAL(receiver));
    2974         1768 :                 if (!match || match >= end) {
    2975              :                         break;
    2976              :                 }
    2977              : 
    2978         1412 :                 q = match + ZSTR_LEN(receiver);
    2979         1412 :                 if (lsp_doc_is_identifier_char(*q)) {
    2980          500 :                         p = match + 1;
    2981          500 :                         continue;
    2982              :                 }
    2983              : 
    2984         1844 :                 while (q < end && isspace((unsigned char) *q)) {
    2985          932 :                         q++;
    2986              :                 }
    2987              : 
    2988          912 :                 if (q >= end || *q != '=' || (q + 1 < end && q[1] == '=')) {
    2989          764 :                         p = match + 1;
    2990          764 :                         continue;
    2991              :                 }
    2992              : 
    2993          148 :                 q++;
    2994          296 :                 while (q < end && isspace((unsigned char) *q)) {
    2995          148 :                         q++;
    2996              :                 }
    2997              : 
    2998          148 :                 if (q >= end || *q != '$') {
    2999          104 :                         p = match + 1;
    3000          104 :                         continue;
    3001              :                 }
    3002              : 
    3003           44 :                 recv_start = q++;
    3004          220 :                 while (q < end && lsp_doc_is_identifier_char(*q)) {
    3005          176 :                         q++;
    3006              :                 }
    3007           44 :                 recv_end = q;
    3008              : 
    3009           44 :                 if (q + 2 >= end || q[0] != '-' || q[1] != '>') {
    3010            8 :                         p = match + 1;
    3011            8 :                         continue;
    3012              :                 }
    3013              : 
    3014           36 :                 q += 2;
    3015           36 :                 if (q >= end || !lsp_doc_is_identifier_start(*q)) {
    3016            0 :                         p = match + 1;
    3017            0 :                         continue;
    3018              :                 }
    3019              : 
    3020           36 :                 prop_start = q++;
    3021          216 :                 while (q < end && lsp_doc_is_identifier_char(*q)) {
    3022          180 :                         q++;
    3023              :                 }
    3024           36 :                 prop_end = q;
    3025              : 
    3026           36 :                 while (q < end && isspace((unsigned char) *q)) {
    3027            0 :                         q++;
    3028              :                 }
    3029              : 
    3030              :                 /* A '(' means a method call; another '->' means a chain — both are
    3031              :                  * handled by other strategies. Only a terminated bare fetch counts. */
    3032           36 :                 if (q >= end || *q != ';') {
    3033           24 :                         p = match + 1;
    3034           24 :                         continue;
    3035              :                 }
    3036              : 
    3037           12 :                 recv_variable = zend_string_init(recv_start, recv_end - recv_start, 0);
    3038           12 :                 if (zend_string_equals_literal(recv_variable, "$this")) {
    3039           12 :                         receiver_class = lsp_inference_declared_class_name(document->text);
    3040              :                 } else {
    3041              :                         /* Scan limit shrinks to this assignment, so recursion always
    3042              :                          * terminates even for self-referential chains. */
    3043            0 :                         receiver_class = lsp_infer_receiver_class(server, document, recv_variable, (size_t) (match - value));
    3044              :                 }
    3045           12 :                 zend_string_release(recv_variable);
    3046              : 
    3047           12 :                 if (receiver_class) {
    3048           12 :                         property_name = zend_string_init(prop_start, prop_end - prop_start, 0);
    3049           12 :                         if (class_name) {
    3050            0 :                                 zend_string_release(class_name);
    3051              :                         }
    3052           12 :                         class_name = lsp_resolve_project_property_declared_class(server, document, receiver_class, property_name);
    3053           12 :                         zend_string_release(property_name);
    3054           12 :                         zend_string_release(receiver_class);
    3055              :                 }
    3056              : 
    3057           12 :                 p = match + 1;
    3058              :         }
    3059              : 
    3060          360 :         return class_name;
    3061              : }
    3062              : 
    3063            4 : static inline zend_string *lsp_resolve_ast_variable_class(lsp_server *server, lsp_document *document, zend_ast *ast, size_t context_offset)
    3064              : {
    3065              :         zend_string *name, *variable, *class_name;
    3066              : 
    3067            4 :         if (!ast || ast->kind != ZEND_AST_VAR) {
    3068            0 :                 return NULL;
    3069              :         }
    3070              : 
    3071            4 :         name = lsp_ast_string_value(ast->child[0]);
    3072            4 :         if (!name) {
    3073            0 :                 return NULL;
    3074              :         }
    3075              : 
    3076            4 :         if (zend_string_equals_literal(name, "this")) {
    3077            4 :                 return lsp_inference_declared_class_name(document->text);
    3078              :         }
    3079              : 
    3080            0 :         variable = strpprintf(0, "$%s", ZSTR_VAL(name));
    3081            0 :         class_name = lsp_infer_receiver_class(server, document, variable, context_offset);
    3082            0 :         zend_string_release(variable);
    3083              : 
    3084            0 :         return class_name;
    3085              : }
    3086              : 
    3087            8 : static inline zend_string *lsp_resolve_expression_ast_class(lsp_server *server, lsp_document *document, zend_ast *ast, size_t context_offset, uint32_t depth)
    3088              : {
    3089              :         zend_string *receiver_class, *property_name, *method_name, *resolved, *raw_class;
    3090              : 
    3091            8 :         if (!ast || depth > 32) {
    3092            0 :                 return NULL;
    3093              :         }
    3094              : 
    3095            8 :         if (ast->kind == ZEND_AST_VAR) {
    3096            4 :                 return lsp_resolve_ast_variable_class(server, document, ast, context_offset);
    3097              :         }
    3098              : 
    3099            4 :         if (ast->kind == ZEND_AST_PROP || ast->kind == ZEND_AST_NULLSAFE_PROP) {
    3100            4 :                 property_name = lsp_ast_string_value(ast->child[1]);
    3101            4 :                 if (!property_name) {
    3102            0 :                         return NULL;
    3103              :                 }
    3104              : 
    3105            4 :                 receiver_class = lsp_resolve_expression_ast_class(server, document, ast->child[0], context_offset, depth + 1);
    3106            4 :                 if (!receiver_class) {
    3107            0 :                         return NULL;
    3108              :                 }
    3109              : 
    3110            4 :                 resolved = lsp_resolve_project_property_declared_class(server, document, receiver_class, property_name);
    3111            4 :                 zend_string_release(receiver_class);
    3112              : 
    3113            4 :                 return resolved;
    3114              :         }
    3115              : 
    3116            0 :         if (ast->kind == ZEND_AST_METHOD_CALL || ast->kind == ZEND_AST_NULLSAFE_METHOD_CALL) {
    3117            0 :                 method_name = lsp_ast_string_value(ast->child[1]);
    3118            0 :                 if (!method_name) {
    3119            0 :                         return NULL;
    3120              :                 }
    3121              : 
    3122            0 :                 receiver_class = lsp_resolve_expression_ast_class(server, document, ast->child[0], context_offset, depth + 1);
    3123            0 :                 if (!receiver_class) {
    3124            0 :                         return NULL;
    3125              :                 }
    3126              : 
    3127            0 :                 resolved = lsp_resolve_project_method_return_class(server, document, receiver_class, receiver_class, method_name);
    3128            0 :                 zend_string_release(receiver_class);
    3129              : 
    3130            0 :                 return resolved;
    3131              :         }
    3132              : 
    3133            0 :         if (ast->kind == ZEND_AST_STATIC_CALL) {
    3134            0 :                 method_name = lsp_ast_string_value(ast->child[1]);
    3135            0 :                 raw_class = lsp_ast_string_value(ast->child[0]);
    3136            0 :                 if (!method_name || !raw_class) {
    3137            0 :                         return NULL;
    3138              :                 }
    3139              : 
    3140            0 :                 if (zend_string_equals_literal_ci(raw_class, "self") || zend_string_equals_literal_ci(raw_class, "static")) {
    3141            0 :                         receiver_class = lsp_inference_declared_class_name(document->text);
    3142              :                 } else {
    3143            0 :                         receiver_class = lsp_resolve_class_name(document->text, raw_class);
    3144              :                 }
    3145              : 
    3146            0 :                 if (!receiver_class) {
    3147            0 :                         return NULL;
    3148              :                 }
    3149              : 
    3150            0 :                 resolved = lsp_resolve_project_method_return_class(server, document, receiver_class, receiver_class, method_name);
    3151            0 :                 zend_string_release(receiver_class);
    3152              : 
    3153            0 :                 return resolved;
    3154              :         }
    3155              : 
    3156            0 :         if (ast->kind == ZEND_AST_ASSIGN || ast->kind == ZEND_AST_ASSIGN_REF || ast->kind == ZEND_AST_ASSIGN_OP) {
    3157            0 :                 return lsp_resolve_expression_ast_class(server, document, ast->child[1], context_offset, depth + 1);
    3158              :         }
    3159              : 
    3160            0 :         if (ast->kind == ZEND_AST_DIM) {
    3161            0 :                 return NULL;
    3162              :         }
    3163              : 
    3164            0 :         return NULL;
    3165              : }
    3166              : 
    3167            8 : static inline zend_ast *lsp_inference_return_expression_from_ast(zend_ast *ast)
    3168              : {
    3169              :         zend_ast_list *list;
    3170              :         uint32_t i;
    3171              : 
    3172            8 :         if (!ast) {
    3173            0 :                 return NULL;
    3174              :         }
    3175              : 
    3176            8 :         if (ast->kind == ZEND_AST_RETURN) {
    3177            4 :                 return ast->child[0];
    3178              :         }
    3179              : 
    3180            4 :         if (zend_ast_is_list(ast)) {
    3181            4 :                 list = zend_ast_get_list(ast);
    3182            4 :                 for (i = 0; i < list->children; i++) {
    3183            4 :                         ast = lsp_inference_return_expression_from_ast(list->child[i]);
    3184            4 :                         if (ast) {
    3185            4 :                                 return ast;
    3186              :                         }
    3187              :                 }
    3188              : 
    3189            0 :                 return NULL;
    3190              :         }
    3191              : 
    3192            0 :         return NULL;
    3193              : }
    3194              : 
    3195            8 : static inline size_t lsp_inference_expression_start_before(zend_string *text, size_t expression_end)
    3196              : {
    3197            8 :         const char *value = ZSTR_VAL(text);
    3198              :         size_t start;
    3199              : 
    3200            8 :         start = expression_end > ZSTR_LEN(text) ? ZSTR_LEN(text) : expression_end;
    3201            8 :         while (start > 0 &&
    3202          204 :                 value[start - 1] != ';' &&
    3203          204 :                 value[start - 1] != '{' &&
    3204          408 :                 value[start - 1] != '}' &&
    3205          204 :                 value[start - 1] != '\n'
    3206              :         ) {
    3207          196 :                 start--;
    3208              :         }
    3209              : 
    3210           72 :         while (start < expression_end && isspace((unsigned char) value[start])) {
    3211           64 :                 start++;
    3212              :         }
    3213              : 
    3214            8 :         return start;
    3215              : }
    3216              : 
    3217            8 : static inline zend_string *lsp_resolve_expression_slice_ast_class(lsp_server *server, lsp_document *document, size_t expression_end, size_t context_offset)
    3218              : {
    3219              :         const char *value;
    3220              :         zend_arena *ast_arena;
    3221              :         zend_ast *ast, *expr;
    3222              :         zend_string *snippet, *class_name;
    3223              :         size_t expression_start;
    3224              : 
    3225            8 :         if (expression_end > ZSTR_LEN(document->text)) {
    3226            0 :                 expression_end = ZSTR_LEN(document->text);
    3227              :         }
    3228              : 
    3229            8 :         expression_start = lsp_inference_expression_start_before(document->text, expression_end);
    3230            8 :         if (expression_start >= expression_end) {
    3231            0 :                 return NULL;
    3232              :         }
    3233              : 
    3234            8 :         value = ZSTR_VAL(document->text);
    3235            8 :         snippet = strpprintf(0, "<?php return %.*s;", (int) (expression_end - expression_start), value + expression_start);
    3236            8 :         ast = lsp_compile_string_to_ast_silent(snippet, ZSTR_EMPTY_ALLOC(), &ast_arena);
    3237            8 :         zend_string_release(snippet);
    3238            8 :         if (!ast) {
    3239            4 :                 return NULL;
    3240              :         }
    3241              : 
    3242            4 :         expr = lsp_inference_return_expression_from_ast(ast);
    3243            4 :         class_name = lsp_resolve_expression_ast_class(server, document, expr, context_offset, 0);
    3244            4 :         lsp_compiled_ast_destroy(ast, ast_arena);
    3245              : 
    3246            4 :         return class_name;
    3247              : }
    3248              : 
    3249            4 : static inline bool lsp_property_access_ending_at_fallback(zend_string *text, size_t expression_end, zend_string **property_name, size_t *receiver_end)
    3250              : {
    3251            4 :         const char *value = ZSTR_VAL(text);
    3252              :         size_t i, property_start, property_end;
    3253              : 
    3254            4 :         *property_name = NULL;
    3255            4 :         *receiver_end = 0;
    3256            4 :         if (expression_end > ZSTR_LEN(text)) {
    3257            0 :                 expression_end = ZSTR_LEN(text);
    3258              :         }
    3259              : 
    3260            4 :         i = expression_end;
    3261            4 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    3262            0 :                 i--;
    3263              :         }
    3264              : 
    3265            4 :         property_end = i;
    3266           20 :         while (i > 0 && lsp_doc_is_identifier_char(value[i - 1])) {
    3267           16 :                 i--;
    3268              :         }
    3269              : 
    3270            4 :         property_start = i;
    3271            4 :         if (property_start == property_end) {
    3272            0 :                 return false;
    3273              :         }
    3274              : 
    3275            4 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    3276            0 :                 i--;
    3277              :         }
    3278              : 
    3279            4 :         if (i >= 3 && value[i - 3] == '?' && value[i - 2] == '-' && value[i - 1] == '>') {
    3280            0 :                 *receiver_end = i - 3;
    3281            4 :         } else if (i >= 2 && value[i - 2] == '-' && value[i - 1] == '>') {
    3282            4 :                 *receiver_end = i - 2;
    3283              :         } else {
    3284            0 :                 return false;
    3285              :         }
    3286              : 
    3287            4 :         *property_name = zend_string_init(value + property_start, property_end - property_start, 0);
    3288              : 
    3289            4 :         return true;
    3290              : }
    3291              : 
    3292            4 : static inline zend_string *lsp_resolve_property_access_expression_class_fallback(lsp_server *server, lsp_document *document, size_t expression_end, size_t context_offset)
    3293              : {
    3294              :         zend_string *property_name, *variable, *receiver_class, *resolved;
    3295              :         size_t receiver_end;
    3296              : 
    3297            4 :         if (!lsp_property_access_ending_at_fallback(document->text, expression_end, &property_name, &receiver_end)) {
    3298            0 :                 return NULL;
    3299              :         }
    3300              : 
    3301            4 :         if (!lsp_variable_ending_at(document->text, receiver_end, &variable)) {
    3302            0 :                 zend_string_release(property_name);
    3303              : 
    3304            0 :                 return NULL;
    3305              :         }
    3306              : 
    3307            4 :         if (zend_string_equals_literal(variable, "$this")) {
    3308            4 :                 receiver_class = lsp_inference_declared_class_name(document->text);
    3309              :         } else {
    3310            0 :                 receiver_class = lsp_infer_receiver_class(server, document, variable, context_offset);
    3311              :         }
    3312              : 
    3313            4 :         zend_string_release(variable);
    3314            4 :         if (!receiver_class) {
    3315            0 :                 zend_string_release(property_name);
    3316              : 
    3317            0 :                 return NULL;
    3318              :         }
    3319              : 
    3320            4 :         resolved = lsp_resolve_project_property_declared_class(server, document, receiver_class, property_name);
    3321            4 :         zend_string_release(receiver_class);
    3322            4 :         zend_string_release(property_name);
    3323              : 
    3324            4 :         return resolved;
    3325              : }
    3326              : 
    3327            8 : static inline zend_string *lsp_resolve_property_access_expression_class(lsp_server *server, lsp_document *document, size_t expression_end, size_t context_offset)
    3328              : {
    3329              :         zend_string *resolved;
    3330              : 
    3331            8 :         resolved = lsp_resolve_expression_slice_ast_class(server, document, expression_end, context_offset);
    3332            8 :         if (resolved) {
    3333            4 :                 return resolved;
    3334              :         }
    3335              : 
    3336            4 :         return lsp_resolve_property_access_expression_class_fallback(server, document, expression_end, context_offset);
    3337              : }
    3338              : 
    3339           72 : static inline zend_string *lsp_resolve_expression_class_ending_at(lsp_server *server, lsp_document *document, size_t expression_end, size_t context_offset, uint32_t depth)
    3340              : {
    3341              :         const char *value;
    3342              :         zend_string *method_name, *receiver_class, *resolved, *variable;
    3343              :         size_t i, receiver_end;
    3344              : 
    3345           72 :         if (depth > 32) {
    3346            0 :                 return NULL;
    3347              :         }
    3348              : 
    3349           72 :         value = ZSTR_VAL(document->text);
    3350           72 :         if (expression_end > ZSTR_LEN(document->text)) {
    3351            0 :                 expression_end = ZSTR_LEN(document->text);
    3352              :         }
    3353              : 
    3354           72 :         i = expression_end;
    3355           72 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    3356            0 :                 i--;
    3357              :         }
    3358              : 
    3359           72 :         if (i > 0 && value[i - 1] == ']') {
    3360           24 :                 resolved = lsp_resolve_array_access_expression_class(server, document, i - 1, context_offset);
    3361           24 :                 if (resolved) {
    3362            8 :                         return resolved;
    3363              :                 }
    3364              : 
    3365           16 :                 return NULL;
    3366              :         }
    3367              : 
    3368           48 :         if (lsp_parse_method_call_before_offset(document->text, i, &method_name, &receiver_end)) {
    3369           36 :                 if (lsp_variable_ending_at(document->text, receiver_end, &variable)) {
    3370           32 :                         if (zend_string_equals_literal(variable, "$this")) {
    3371           20 :                                 receiver_class = lsp_inference_declared_class_name(document->text);
    3372           20 :                                 resolved = receiver_class ? lsp_resolve_project_method_return_class(server, document, receiver_class, receiver_class, method_name) : NULL;
    3373           20 :                                 if (receiver_class) {
    3374           20 :                                         zend_string_release(receiver_class);
    3375              :                                 }
    3376              :                         } else {
    3377           12 :                                 resolved = lsp_resolve_variable_method_return_class(server, document, variable, method_name, context_offset);
    3378              :                         }
    3379              : 
    3380           32 :                         zend_string_release(variable);
    3381           32 :                         zend_string_release(method_name);
    3382              : 
    3383           32 :                         if (resolved) {
    3384           32 :                                 return resolved;
    3385              :                         }
    3386              : 
    3387            0 :                         return NULL;
    3388              :                 }
    3389              : 
    3390            4 :                 receiver_class = lsp_resolve_expression_class_ending_at(server, document, receiver_end, context_offset, depth + 1);
    3391            4 :                 if (!receiver_class) {
    3392            0 :                         zend_string_release(method_name);
    3393              : 
    3394            0 :                         return NULL;
    3395              :                 }
    3396              : 
    3397            4 :                 resolved = lsp_resolve_project_method_return_class(server, document, receiver_class, receiver_class, method_name);
    3398            4 :                 zend_string_release(receiver_class);
    3399            4 :                 zend_string_release(method_name);
    3400              : 
    3401            4 :                 return resolved;
    3402              :         }
    3403              : 
    3404           12 :         if (lsp_static_method_call_before_offset(document, i, &method_name, &receiver_class)) {
    3405            4 :                 resolved = lsp_resolve_project_method_return_class(server, document, receiver_class, receiver_class, method_name);
    3406            4 :                 zend_string_release(receiver_class);
    3407            4 :                 zend_string_release(method_name);
    3408              : 
    3409            4 :                 return resolved;
    3410              :         }
    3411              : 
    3412            8 :         resolved = lsp_resolve_property_access_expression_class(server, document, i, context_offset);
    3413            8 :         if (resolved) {
    3414            8 :                 return resolved;
    3415              :         }
    3416              : 
    3417            0 :         if (lsp_variable_ending_at(document->text, i, &variable)) {
    3418            0 :                 if (zend_string_equals_literal(variable, "$this")) {
    3419            0 :                         zend_string_release(variable);
    3420              : 
    3421            0 :                         return lsp_inference_declared_class_name(document->text);
    3422              :                 }
    3423              : 
    3424            0 :                 resolved = lsp_infer_receiver_class(server, document, variable, context_offset);
    3425            0 :                 zend_string_release(variable);
    3426              : 
    3427            0 :                 return resolved;
    3428              :         }
    3429              : 
    3430            0 :         return NULL;
    3431              : }
    3432              : 
    3433          108 : static inline bool lsp_method_chain_member_context(lsp_server *server, lsp_document *document, size_t offset, zend_string *prefix, zend_string **class_name, zend_string **member_prefix)
    3434              : {
    3435              :         size_t receiver_end;
    3436              : 
    3437          108 :         *class_name = NULL;
    3438          108 :         *member_prefix = NULL;
    3439          108 :         if (!lsp_member_access_arrow(document->text, offset, prefix, NULL, &receiver_end)) {
    3440           40 :                 return false;
    3441              :         }
    3442              : 
    3443           68 :         *class_name = lsp_resolve_expression_class_ending_at(server, document, receiver_end, offset, 0);
    3444           68 :         if (!*class_name) {
    3445           16 :                 return false;
    3446              :         }
    3447              : 
    3448           52 :         *member_prefix = zend_string_copy(prefix);
    3449              : 
    3450           52 :         return true;
    3451              : }
    3452              : 
    3453          432 : extern zend_string *lsp_infer_variable_type(lsp_server *server, lsp_document *document, zend_string *variable, size_t offset)
    3454              : {
    3455              :         zend_string *type;
    3456              : 
    3457          432 :         type = lsp_parameter_declared_type_for_variable(document, variable, offset);
    3458          432 :         if (type) {
    3459           64 :                 return type;
    3460              :         }
    3461              : 
    3462          368 :         type = lsp_phpdoc_type_for_word(document->text, variable);
    3463          368 :         if (type) {
    3464           28 :                 return type;
    3465              :         }
    3466              : 
    3467          340 :         type = lsp_phpdoc_property_type_for_word(document->text, variable, offset);
    3468          340 :         if (type) {
    3469           16 :                 return type;
    3470              :         }
    3471              : 
    3472          324 :         type = lsp_infer_method_array_access_assignment_class(server, document, variable, offset);
    3473          324 :         if (type) {
    3474           16 :                 return type;
    3475              :         }
    3476              : 
    3477          308 :         type = lsp_infer_method_call_assignment_type(server, document, variable, offset);
    3478          308 :         if (type) {
    3479           76 :                 return type;
    3480              :         }
    3481              : 
    3482          232 :         type = lsp_infer_property_fetch_assignment_class(server, document, variable, offset);
    3483          232 :         if (type) {
    3484            4 :                 return type;
    3485              :         }
    3486              : 
    3487          228 :         type = lsp_infer_new_assignment_class(document->text, variable, lsp_current_statement_scan_limit(document->text, offset));
    3488          228 :         if (type) {
    3489           84 :                 return type;
    3490              :         }
    3491              : 
    3492          144 :         type = lsp_infer_external_call_assignment_class(server, document, variable, offset);
    3493          144 :         if (type) {
    3494            0 :                 return type;
    3495              :         }
    3496              : 
    3497          144 :         return lsp_infer_foreach_element_class(server, document, variable, offset);
    3498              : }
    3499              : 
    3500           72 : extern zend_string *lsp_infer_variable_phpdoc_type(lsp_document *document, zend_string *variable, size_t offset)
    3501              : {
    3502              :         zend_string *type;
    3503              : 
    3504           72 :         type = lsp_phpdoc_type_for_word_raw(document->text, variable);
    3505           72 :         if (type) {
    3506           12 :                 return type;
    3507              :         }
    3508              : 
    3509           60 :         type = lsp_phpdoc_property_type_for_word_raw(document->text, variable, offset);
    3510           60 :         if (type) {
    3511            4 :                 return type;
    3512              :         }
    3513              : 
    3514           56 :         return lsp_infer_method_call_assignment_phpdoc_type(document, variable, offset);
    3515              : }
    3516              : 
    3517           20 : extern zend_string *lsp_infer_variable_declared_type(lsp_server *server, lsp_document *document, zend_string *variable, size_t offset)
    3518              : {
    3519              :         zend_string *type;
    3520              : 
    3521           20 :         type = lsp_infer_method_array_access_assignment_class(server, document, variable, offset);
    3522           20 :         if (type) {
    3523            0 :                 return type;
    3524              :         }
    3525              : 
    3526           20 :         type = lsp_infer_method_call_assignment_declared_type(document, variable, offset);
    3527           20 :         if (type) {
    3528           12 :                 return type;
    3529              :         }
    3530              : 
    3531            8 :         type = lsp_infer_new_assignment_class(document->text, variable, lsp_current_statement_scan_limit(document->text, offset));
    3532            8 :         if (type) {
    3533            4 :                 return type;
    3534              :         }
    3535              : 
    3536            4 :         return lsp_parameter_declared_type_for_variable(document, variable, offset);
    3537              : }
    3538              : 
    3539           52 : static inline bool lsp_this_method_array_access_member_context(lsp_server *server, lsp_document *document, size_t offset, zend_string *prefix, zend_string **class_name, zend_string **member_prefix)
    3540              : {
    3541           52 :         const char *value = ZSTR_VAL(document->text);
    3542              :         zend_string *container_method, *chain_method, *container_return_type, *container_class;
    3543              :         size_t receiver_end, i, close_bracket, open_bracket, chain_receiver_end;
    3544              : 
    3545           52 :         *class_name = NULL;
    3546           52 :         *member_prefix = NULL;
    3547              : 
    3548           52 :         if (!lsp_member_access_arrow(document->text, offset, prefix, NULL, &receiver_end)) {
    3549           40 :                 return false;
    3550              :         }
    3551              : 
    3552           12 :         i = receiver_end;
    3553           12 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    3554            0 :                 i--;
    3555              :         }
    3556              : 
    3557           12 :         if (i == 0 || value[i - 1] != ']') {
    3558            0 :                 return false;
    3559              :         }
    3560              : 
    3561           12 :         close_bracket = i - 1;
    3562           12 :         if (!lsp_find_matching_open_bracket(document->text, close_bracket, &open_bracket)) {
    3563            0 :                 return false;
    3564              :         }
    3565              : 
    3566           12 :         if (!lsp_parse_method_call_before_offset(document->text, open_bracket, &chain_method, &chain_receiver_end)) {
    3567            0 :                 return false;
    3568              :         }
    3569              : 
    3570           12 :         if (!lsp_parse_this_method_call_ending_at(document->text, chain_receiver_end, &container_method)) {
    3571            0 :                 zend_string_release(chain_method);
    3572              : 
    3573            0 :                 return false;
    3574              :         }
    3575              : 
    3576           12 :         container_return_type = lsp_find_method_return_type(document, container_method);
    3577           12 :         zend_string_release(container_method);
    3578           12 :         if (!container_return_type) {
    3579            0 :                 zend_string_release(chain_method);
    3580              : 
    3581            0 :                 return false;
    3582              :         }
    3583              : 
    3584           12 :         container_class = lsp_resolve_class_name(document->text, container_return_type);
    3585           12 :         if (!container_class) {
    3586            0 :                 zend_string_release(container_return_type);
    3587            0 :                 zend_string_release(chain_method);
    3588              : 
    3589            0 :                 return false;
    3590              :         }
    3591              : 
    3592           12 :         *class_name = lsp_resolve_project_method_array_element_class(server, document, container_class, container_return_type, chain_method);
    3593              : 
    3594           12 :         zend_string_release(container_class);
    3595           12 :         zend_string_release(container_return_type);
    3596           12 :         zend_string_release(chain_method);
    3597              : 
    3598           12 :         if (!*class_name) {
    3599            0 :                 return false;
    3600              :         }
    3601              : 
    3602           12 :         *member_prefix = zend_string_copy(prefix);
    3603              : 
    3604           12 :         return true;
    3605              : }
    3606              : 
    3607           56 : static inline bool lsp_variable_method_call_member_context(lsp_server *server, lsp_document *document, size_t offset, zend_string *prefix, zend_string **class_name, zend_string **member_prefix)
    3608              : {
    3609              :         zend_string *method_name, *variable;
    3610              :         size_t receiver_end, call_receiver_end;
    3611              : 
    3612           56 :         *class_name = NULL;
    3613           56 :         *member_prefix = NULL;
    3614              : 
    3615           56 :         if (!lsp_member_access_arrow(document->text, offset, prefix, NULL, &receiver_end)) {
    3616           40 :                 return false;
    3617              :         }
    3618              : 
    3619           16 :         if (!lsp_parse_method_call_before_offset(document->text, receiver_end, &method_name, &call_receiver_end)) {
    3620           16 :                 return false;
    3621              :         }
    3622              : 
    3623            0 :         if (!lsp_variable_ending_at(document->text, call_receiver_end, &variable)) {
    3624            0 :                 zend_string_release(method_name);
    3625              : 
    3626            0 :                 return false;
    3627              :         }
    3628              : 
    3629            0 :         *class_name = lsp_resolve_variable_method_return_class(server, document, variable, method_name, offset);
    3630              : 
    3631            0 :         zend_string_release(variable);
    3632            0 :         zend_string_release(method_name);
    3633              : 
    3634            0 :         if (!*class_name) {
    3635            0 :                 return false;
    3636              :         }
    3637              : 
    3638            0 :         *member_prefix = zend_string_copy(prefix);
    3639              : 
    3640            0 :         return true;
    3641              : }
    3642              : 
    3643           56 : static inline bool lsp_variable_method_array_access_member_context(lsp_server *server, lsp_document *document, size_t offset, zend_string *prefix, zend_string **class_name, zend_string **member_prefix)
    3644              : {
    3645           56 :         const char *value = ZSTR_VAL(document->text);
    3646              :         zend_string *chain_method, *variable;
    3647              :         size_t receiver_end, i, close_bracket, open_bracket, chain_receiver_end;
    3648              : 
    3649           56 :         *class_name = NULL;
    3650           56 :         *member_prefix = NULL;
    3651              : 
    3652           56 :         if (!lsp_member_access_arrow(document->text, offset, prefix, NULL, &receiver_end)) {
    3653           40 :                 return false;
    3654              :         }
    3655              : 
    3656           16 :         i = receiver_end;
    3657           16 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    3658            0 :                 i--;
    3659              :         }
    3660              : 
    3661           16 :         if (i == 0 || value[i - 1] != ']') {
    3662            0 :                 return false;
    3663              :         }
    3664              : 
    3665           16 :         close_bracket = i - 1;
    3666           16 :         if (!lsp_find_matching_open_bracket(document->text, close_bracket, &open_bracket)) {
    3667            0 :                 return false;
    3668              :         }
    3669              : 
    3670           16 :         if (!lsp_parse_method_call_before_offset(document->text, open_bracket, &chain_method, &chain_receiver_end)) {
    3671            0 :                 return false;
    3672              :         }
    3673              : 
    3674           16 :         if (!lsp_variable_ending_at(document->text, chain_receiver_end, &variable)) {
    3675           12 :                 zend_string_release(chain_method);
    3676              : 
    3677           12 :                 return false;
    3678              :         }
    3679              : 
    3680            4 :         *class_name = lsp_resolve_variable_method_array_element_class(server, document, variable, chain_method, offset);
    3681              : 
    3682            4 :         zend_string_release(variable);
    3683            4 :         zend_string_release(chain_method);
    3684              : 
    3685            4 :         if (!*class_name) {
    3686            0 :                 return false;
    3687              :         }
    3688              : 
    3689            4 :         *member_prefix = zend_string_copy(prefix);
    3690              : 
    3691            4 :         return true;
    3692              : }
    3693              : 
    3694          296 : extern bool lsp_member_access_class_context(lsp_server *server, lsp_document *document, size_t offset, zend_string *prefix, zend_string **class_name, zend_string **member_prefix)
    3695              : {
    3696              :         zend_string *receiver, *method_name, *current_class;
    3697              : 
    3698          296 :         *class_name = NULL;
    3699          296 :         *member_prefix = NULL;
    3700              : 
    3701          296 :         if (lsp_member_access_context(document->text, offset, prefix, &receiver, member_prefix)) {
    3702          188 :                 *class_name = lsp_infer_receiver_class(server, document, receiver, offset);
    3703              : 
    3704          188 :                 zend_string_release(receiver);
    3705              : 
    3706          188 :                 if (*class_name) {
    3707          108 :                         return true;
    3708              :                 }
    3709              : 
    3710           80 :                 zend_string_release(*member_prefix);
    3711           80 :                 *member_prefix = NULL;
    3712              : 
    3713           80 :                 return false;
    3714              :         }
    3715              : 
    3716          108 :         if (lsp_method_chain_member_context(server, document, offset, prefix, class_name, member_prefix)) {
    3717           52 :                 return true;
    3718              :         }
    3719              : 
    3720           56 :         if (lsp_variable_method_call_member_context(server, document, offset, prefix, class_name, member_prefix)) {
    3721            0 :                 return true;
    3722              :         }
    3723              : 
    3724           56 :         if (lsp_this_method_call_member_access_context(document->text, offset, prefix, &method_name, member_prefix)) {
    3725            0 :                 current_class = lsp_inference_declared_class_name(document->text);
    3726              : 
    3727            0 :                 *class_name = current_class ? lsp_resolve_this_method_return_class(server, document, current_class, method_name) : NULL;
    3728            0 :                 if (current_class) {
    3729            0 :                         zend_string_release(current_class);
    3730              :                 }
    3731              : 
    3732            0 :                 zend_string_release(method_name);
    3733              : 
    3734            0 :                 if (*class_name) {
    3735            0 :                         return true;
    3736              :                 }
    3737              : 
    3738            0 :                 zend_string_release(*member_prefix);
    3739            0 :                 *member_prefix = NULL;
    3740              : 
    3741            0 :                 return false;
    3742              :         }
    3743              : 
    3744           56 :         if (lsp_variable_method_array_access_member_context(server, document, offset, prefix, class_name, member_prefix)) {
    3745            4 :                 return true;
    3746              :         }
    3747              : 
    3748           52 :         if (lsp_this_method_array_access_member_context(server, document, offset, prefix, class_name, member_prefix)) {
    3749           12 :                 return true;
    3750              :         }
    3751              : 
    3752           40 :         return false;
    3753              : }
        

Generated by: LCOV version 2.0-1