LCOV - code coverage report
Current view: top level - ext - lsp_server_phpdoc.c (source / functions) Coverage Total Hit
Test: Extension code coverage Lines: 79.5 % 1927 1532
Test Date: 2026-07-13 00:58:38 Functions: 100.0 % 110 110

            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        10924 : static inline bool lsp_phpdoc_token_equals_literal_ci(const char *token, size_t token_length, const char *literal)
      17              : {
      18              :         size_t literal_length;
      19              : 
      20        10924 :         literal_length = strlen(literal);
      21              : 
      22        10924 :         return token_length == literal_length && strncasecmp(token, literal, token_length) == 0;
      23              : }
      24              : 
      25         1208 : static inline bool lsp_phpdoc_token_is_lsparrot_int_alias(const char *token, size_t token_length)
      26              : {
      27         2332 :         return lsp_phpdoc_token_equals_literal_ci(token, token_length, "positive-int") ||
      28         2248 :                 lsp_phpdoc_token_equals_literal_ci(token, token_length, "negative-int") ||
      29         3456 :                 lsp_phpdoc_token_equals_literal_ci(token, token_length, "non-positive-int") ||
      30         1124 :                 lsp_phpdoc_token_equals_literal_ci(token, token_length, "non-negative-int")
      31              :         ;
      32              : }
      33              : 
      34         1124 : static inline bool lsp_phpdoc_token_is_lsparrot_string_alias(const char *token, size_t token_length)
      35              : {
      36         2168 :         return lsp_phpdoc_token_equals_literal_ci(token, token_length, "non-empty-string") ||
      37         2088 :                 lsp_phpdoc_token_equals_literal_ci(token, token_length, "literal-string") ||
      38         2088 :                 lsp_phpdoc_token_equals_literal_ci(token, token_length, "lowercase-string") ||
      39         3212 :                 lsp_phpdoc_token_equals_literal_ci(token, token_length, "numeric-string") ||
      40         1044 :                 lsp_phpdoc_token_equals_literal_ci(token, token_length, "class-string")
      41              :         ;
      42              : }
      43              : 
      44         1044 : static inline bool lsp_phpdoc_token_is_lsparrot_array_alias(const char *token, size_t token_length)
      45              : {
      46         1044 :         return lsp_phpdoc_token_equals_literal_ci(token, token_length, "non-empty-array");
      47              : }
      48              : 
      49            8 : static inline const char *lsp_phpdoc_angle_expression_end(const char *open, const char *end)
      50              : {
      51              :         const char *cursor;
      52              :         uint32_t depth;
      53              :         char quote;
      54              :         bool escaped;
      55              : 
      56            8 :         cursor = open;
      57            8 :         depth = 0;
      58            8 :         quote = '\0';
      59            8 :         escaped = false;
      60              : 
      61           64 :         while (cursor < end) {
      62           64 :                 if (quote != '\0') {
      63            0 :                         if (escaped) {
      64            0 :                                 escaped = false;
      65            0 :                         } else if (*cursor == '\\') {
      66            0 :                                 escaped = true;
      67            0 :                         } else if (*cursor == quote) {
      68            0 :                                 quote = '\0';
      69              :                         }
      70            0 :                         cursor++;
      71            0 :                         continue;
      72              :                 }
      73              : 
      74           64 :                 if (*cursor == '\'' || *cursor == '"') {
      75            0 :                         quote = *cursor;
      76            0 :                         cursor++;
      77            0 :                         continue;
      78              :                 }
      79              : 
      80           64 :                 if (*cursor == '<') {
      81            8 :                         depth++;
      82           56 :                 } else if (*cursor == '>' && depth > 0 && --depth == 0) {
      83            8 :                         return cursor + 1;
      84              :                 }
      85              : 
      86           56 :                 cursor++;
      87              :         }
      88              : 
      89            0 :         return cursor;
      90              : }
      91              : 
      92         2236 : static inline bool lsp_phpdoc_token_starts_at_boundary(const char *value, const char *cursor)
      93              : {
      94         2236 :         return cursor == value || (!lsp_doc_is_identifier_char(cursor[-1]) && cursor[-1] != '-');
      95              : }
      96              : 
      97          536 : static inline zend_string *lsp_phpdoc_normalize_lsparrot_type(zend_string *type)
      98              : {
      99              :         const char *value, *cursor, *end, *token_start, *token_end, *finish;
     100          536 :         smart_str buffer = {0};
     101              :         bool changed;
     102              : 
     103          536 :         value = ZSTR_VAL(type);
     104          536 :         cursor = value;
     105          536 :         end = value + ZSTR_LEN(type);
     106          536 :         changed = false;
     107              : 
     108         3256 :         while (cursor < end) {
     109         2720 :                 if (cursor + sizeof("int<") - 1 <= end && lsp_phpdoc_token_starts_at_boundary(value, cursor) &&
     110         1564 :                         strncasecmp(cursor, "int<", sizeof("int<") - 1) == 0
     111              :                 ) {
     112            8 :                         finish = lsp_phpdoc_angle_expression_end(cursor + sizeof("int") - 1, end);
     113            8 :                         smart_str_appendl(&buffer, "int", sizeof("int") - 1);
     114            8 :                         cursor = finish;
     115            8 :                         changed = true;
     116            8 :                         continue;
     117              :                 }
     118              : 
     119         2712 :                 if (isalpha((unsigned char) *cursor) || *cursor == '_' || *cursor == '\\') {
     120         1208 :                         token_start = cursor;
     121         1208 :                         cursor++;
     122         9856 :                         while (cursor < end && (lsp_doc_is_identifier_char(*cursor) || *cursor == '-' || *cursor == '\\')) {
     123         8648 :                                 cursor++;
     124              :                         }
     125         1208 :                         token_end = cursor;
     126              : 
     127         1208 :                         if (lsp_phpdoc_token_is_lsparrot_int_alias(token_start, token_end - token_start)) {
     128           84 :                                 smart_str_appendl(&buffer, "int", sizeof("int") - 1);
     129           84 :                                 changed = true;
     130         1124 :                         } else if (lsp_phpdoc_token_is_lsparrot_string_alias(token_start, token_end - token_start)) {
     131           80 :                                 smart_str_appendl(&buffer, "string", sizeof("string") - 1);
     132           80 :                                 changed = true;
     133         1044 :                         } else if (lsp_phpdoc_token_is_lsparrot_array_alias(token_start, token_end - token_start)) {
     134            0 :                                 smart_str_appendl(&buffer, "array", sizeof("array") - 1);
     135            0 :                                 changed = true;
     136              :                         } else {
     137         1044 :                                 smart_str_appendl(&buffer, token_start, token_end - token_start);
     138              :                         }
     139         1208 :                         continue;
     140              :                 }
     141              : 
     142         1504 :                 smart_str_appendc(&buffer, *cursor);
     143         1504 :                 cursor++;
     144              :         }
     145              : 
     146          536 :         if (!changed) {
     147          448 :                 smart_str_free(&buffer);
     148              : 
     149          448 :                 return zend_string_copy(type);
     150              :         }
     151              : 
     152           88 :         smart_str_0(&buffer);
     153              : 
     154           88 :         return buffer.s ? buffer.s : zend_string_init("", 0, 0);
     155              : }
     156              : 
     157          716 : static inline zend_string *lsp_phpdoc_type_slice(const char *start, const char *end, bool normalize)
     158              : {
     159              :         zend_string *raw, *normalized;
     160              : 
     161          716 :         raw = zend_string_init(start, end - start, 0);
     162          716 :         if (!normalize) {
     163          180 :                 return raw;
     164              :         }
     165              : 
     166          536 :         normalized = lsp_phpdoc_normalize_lsparrot_type(raw);
     167          536 :         zend_string_release(raw);
     168              : 
     169          536 :         return normalized;
     170              : }
     171              : 
     172          164 : static inline bool lsp_phpdoc_context_at(zend_string *text, size_t offset)
     173              : {
     174              :         const char *value;
     175              :         size_t i, comment_start;
     176              :         bool found_start;
     177              : 
     178          164 :         if (offset > ZSTR_LEN(text)) {
     179            0 :                 offset = ZSTR_LEN(text);
     180              :         }
     181              : 
     182          164 :         value = ZSTR_VAL(text);
     183          164 :         i = offset;
     184          164 :         found_start = false;
     185          164 :         comment_start = 0;
     186        13960 :         while (i > 0) {
     187        13844 :                 if (i >= 2 && value[i - 2] == '*' && value[i - 1] == '/') {
     188           28 :                         return false;
     189              :                 }
     190              : 
     191        13816 :                 if (i >= 2 && value[i - 2] == '/' && value[i - 1] == '*') {
     192           20 :                         comment_start = i - 2;
     193           20 :                         found_start = true;
     194           20 :                         break;
     195              :                 }
     196              : 
     197        13796 :                 i--;
     198              :         }
     199              : 
     200          136 :         return found_start && comment_start + 2 < ZSTR_LEN(text) && value[comment_start + 2] == '*';
     201              : }
     202              : 
     203          216 : static inline bool lsp_phpdoc_completion_char(char c)
     204              : {
     205          216 :         return lsp_doc_is_identifier_char(c) || c == '-' || c == '\\' || c == '@';
     206              : }
     207              : 
     208           20 : static inline zend_string *lsp_phpdoc_completion_prefix_at(zend_string *text, size_t offset, size_t *start_offset)
     209              : {
     210              :         const char *value;
     211              :         size_t start;
     212              : 
     213           20 :         if (offset > ZSTR_LEN(text)) {
     214            0 :                 offset = ZSTR_LEN(text);
     215              :         }
     216              : 
     217           20 :         value = ZSTR_VAL(text);
     218           20 :         start = offset;
     219          216 :         while (start > 0 && lsp_phpdoc_completion_char(value[start - 1])) {
     220          196 :                 start--;
     221              :         }
     222              : 
     223           20 :         *start_offset = start;
     224              : 
     225           20 :         return zend_string_init(value + start, offset - start, 0);
     226              : }
     227              : 
     228         2800 : static inline bool lsp_phpdoc_matches_completion_prefix(const char *label, zend_string *prefix)
     229              : {
     230              :         const char *prefix_value;
     231              :         size_t label_length, prefix_length;
     232              : 
     233         2800 :         prefix_value = ZSTR_VAL(prefix);
     234         2800 :         prefix_length = ZSTR_LEN(prefix);
     235         2800 :         label_length = strlen(label);
     236         2800 :         if (prefix_length == 0) {
     237            0 :                 return true;
     238              :         }
     239              : 
     240         2800 :         if (prefix_length <= label_length && strncasecmp(label, prefix_value, prefix_length) == 0) {
     241           24 :                 return true;
     242              :         }
     243              : 
     244         2776 :         if (prefix_length > 0 && prefix_value[0] == '@') {
     245         1664 :                 return prefix_length - 1 <= label_length && strncasecmp(label, prefix_value + 1, prefix_length - 1) == 0;
     246              :         }
     247              : 
     248         1112 :         return false;
     249              : }
     250              : 
     251           24 : static inline void lsp_phpdoc_add_completion_edit(zval *items, zend_string *text, size_t start_offset, size_t end_offset, const char *label, zend_long kind, const char *detail)
     252              : {
     253              :         zval item, data, text_edit, range;
     254              : 
     255           24 :         array_init(&item);
     256           24 :         add_assoc_string(&item, "label", label);
     257           24 :         add_assoc_long(&item, "kind", kind);
     258           24 :         add_assoc_string(&item, "detail", detail);
     259           24 :         add_assoc_string(&item, "filterText", label);
     260           24 :         array_init(&text_edit);
     261           24 :         lsp_range_from_offsets(text, start_offset, end_offset, &range);
     262           24 :         add_assoc_zval(&text_edit, "range", &range);
     263           24 :         add_assoc_string(&text_edit, "newText", label);
     264           24 :         add_assoc_zval(&item, "textEdit", &text_edit);
     265           24 :         array_init(&data);
     266           24 :         add_assoc_string(&data, "source", "lsparrot");
     267           24 :         add_assoc_zval(&item, "data", &data);
     268           24 :         add_next_index_zval(items, &item);
     269           24 : }
     270              : 
     271         2800 : static inline void lsp_phpdoc_add_completion_edit_if_matches(zval *items, zend_string *text, size_t start_offset, size_t end_offset, zend_string *prefix, const char *label, zend_long kind, const char *detail)
     272              : {
     273         2800 :         if (!lsp_phpdoc_matches_completion_prefix(label, prefix)) {
     274         2776 :                 return;
     275              :         }
     276              : 
     277           24 :         lsp_phpdoc_add_completion_edit(items, text, start_offset, end_offset, label, kind, detail);
     278              : }
     279              : 
     280           20 : static inline void lsp_phpdoc_add_tag_completion_edits(zval *items, zend_string *text, size_t start_offset, size_t end_offset, zend_string *prefix)
     281              : {
     282              :         static const char *tags[] = {
     283              :                 "@api", "@deprecated", "@extends", "@implements", "@method", "@mixin", "@param", "@param-out",
     284              :                 "@property", "@property-read", "@property-write", "@return", "@self-out", "@template",
     285              :                 "@template-covariant", "@template-contravariant", "@throws", "@use", "@var",
     286              :                 "@phpstan-assert", "@phpstan-assert-if-false", "@phpstan-assert-if-true", "@phpstan-consistent-constructor",
     287              :                 "@phpstan-extends", "@phpstan-implements", "@phpstan-import-type", "@phpstan-method", "@phpstan-param",
     288              :                 "@phpstan-param-out", "@phpstan-property", "@phpstan-property-read", "@phpstan-property-write",
     289              :                 "@phpstan-pure", "@phpstan-require-extends", "@phpstan-require-implements", "@phpstan-return",
     290              :                 "@phpstan-self-out", "@phpstan-template", "@phpstan-template-covariant", "@phpstan-this-out",
     291              :                 "@phpstan-throws", "@phpstan-type", "@phpstan-use", "@phpstan-var",
     292              :                 "@psalm-api", "@psalm-assert", "@psalm-assert-if-false", "@psalm-assert-if-true",
     293              :                 "@psalm-check-type", "@psalm-check-type-exact", "@psalm-consistent-constructor",
     294              :                 "@psalm-consistent-templates", "@psalm-external-mutation-free", "@psalm-immutable",
     295              :                 "@psalm-import-type", "@psalm-implements", "@psalm-inheritors", "@psalm-internal",
     296              :                 "@psalm-method", "@psalm-mutation-free", "@psalm-param", "@psalm-param-out",
     297              :                 "@psalm-property", "@psalm-property-read", "@psalm-property-write", "@psalm-pure",
     298              :                 "@psalm-readonly", "@psalm-readonly-allow-private-mutation", "@psalm-require-extends",
     299              :                 "@psalm-require-implements", "@psalm-return", "@psalm-seal-methods", "@psalm-seal-properties",
     300              :                 "@psalm-suppress", "@psalm-taint-escape", "@psalm-taint-sink", "@psalm-taint-source",
     301              :                 "@psalm-template", "@psalm-template-covariant", "@psalm-this-out", "@psalm-trace",
     302              :                 "@psalm-type", "@psalm-var", "@psalm-yield"
     303              :         };
     304              :         size_t i;
     305              : 
     306         1700 :         for (i = 0; i < sizeof(tags) / sizeof(tags[0]); i++) {
     307         1680 :                 lsp_phpdoc_add_completion_edit_if_matches(items, text, start_offset, end_offset, prefix, tags[i], 14, "PHPDoc annotation");
     308              :         }
     309           20 : }
     310              : 
     311           20 : static inline void lsp_phpdoc_add_type_completion_edits(zval *items, zend_string *text, size_t start_offset, size_t end_offset, zend_string *prefix)
     312              : {
     313              :         static const char *types[] = {
     314              :                 "array", "array-key", "bool", "callable", "callable-string", "class-string",
     315              :                 "class-string-map", "closed-resource", "conditional", "decimal-int-string",
     316              :                 "empty", "enum-string", "false", "float", "int", "interface-string", "iterable",
     317              :                 "key-of", "list", "literal-string", "lowercase-string", "mixed", "negative-int",
     318              :                 "never", "non-decimal-int-string", "non-empty-array", "non-empty-list",
     319              :                 "non-empty-lowercase-string", "non-empty-string", "non-empty-uppercase-string",
     320              :                 "non-falsy-string", "non-negative-int", "non-positive-int", "null", "numeric",
     321              :                 "numeric-string", "object", "parent", "positive-int", "private-properties-of",
     322              :                 "protected-properties-of", "public-properties-of", "pure-callable", "pure-Closure",
     323              :                 "resource", "scalar", "self", "static", "string", "template-type", "trait-string",
     324              :                 "true", "truthy-string", "uppercase-string", "value-of", "void"
     325              :         };
     326              :         size_t i;
     327              : 
     328         1140 :         for (i = 0; i < sizeof(types) / sizeof(types[0]); i++) {
     329         1120 :                 lsp_phpdoc_add_completion_edit_if_matches(items, text, start_offset, end_offset, prefix, types[i], 25, "PHPDoc type");
     330              :         }
     331           20 : }
     332              : 
     333          164 : extern void lsp_phpdoc_add_annotation_completions(zval *items, zend_string *text, size_t offset)
     334              : {
     335              :         size_t start_offset;
     336              :         zend_string *prefix;
     337              : 
     338          164 :         if (!lsp_phpdoc_context_at(text, offset)) {
     339          144 :                 return;
     340              :         }
     341              : 
     342           20 :         prefix = lsp_phpdoc_completion_prefix_at(text, offset, &start_offset);
     343           20 :         lsp_phpdoc_add_tag_completion_edits(items, text, start_offset, offset, prefix);
     344           20 :         lsp_phpdoc_add_type_completion_edits(items, text, start_offset, offset, prefix);
     345           20 :         zend_string_release(prefix);
     346              : }
     347              : 
     348         1312 : static inline bool lsp_doc_find_next_type_ex(const char **cursor, const char *end, zend_string **variable, zend_string **type, bool normalize)
     349              : {
     350         1312 :         const char *var_tag = NULL, *param_tag = NULL, *tag, *p,
     351              :                 *type_start, *type_end, *var_start, *var_end
     352              :         ;
     353              : 
     354         1312 :         *variable = NULL;
     355         1312 :         *type = NULL;
     356         1312 :         var_tag = strstr(*cursor, "@var");
     357         1312 :         param_tag = strstr(*cursor, "@param");
     358              : 
     359         1312 :         if (var_tag && var_tag < end && (!param_tag || var_tag < param_tag)) {
     360          216 :                 tag = var_tag + sizeof("@var") - 1;
     361         1096 :         } else if (param_tag && param_tag < end) {
     362          208 :                 tag = param_tag + sizeof("@param") - 1;
     363              :         } else {
     364          888 :                 *cursor = end;
     365              : 
     366          888 :                 return false;
     367              :         }
     368              : 
     369          424 :         p = tag;
     370          848 :         while (p < end && isspace((unsigned char) *p)) {
     371          424 :                 p++;
     372              :         }
     373              : 
     374          424 :         type_start = p;
     375         8524 :         while (p < end && *p != '\n' && *p != '\r') {
     376         8468 :                 if (*p == '$' && p + 1 < end && lsp_doc_is_identifier_start(p[1])) {
     377          368 :                         var_start = p;
     378          368 :                         type_end = p;
     379          736 :                         while (type_end > type_start && isspace((unsigned char) type_end[-1])) {
     380          368 :                                 type_end--;
     381              :                         }
     382              : 
     383          368 :                         var_end = p + 2;
     384         2332 :                         while (var_end < end && lsp_doc_is_identifier_char(*var_end)) {
     385         1964 :                                 var_end++;
     386              :                         }
     387              : 
     388          368 :                         if (type_end > type_start) {
     389          368 :                                 *variable = zend_string_init(var_start, var_end - var_start, 0);
     390          368 :                                 *type = lsp_phpdoc_type_slice(type_start, type_end, normalize);
     391          368 :                                 *cursor = var_end;
     392              : 
     393          368 :                                 return true;
     394              :                         }
     395              :                 }
     396              : 
     397         8100 :                 p++;
     398              :         }
     399              : 
     400           56 :         *cursor = p;
     401              : 
     402           56 :         return false;
     403              : }
     404              : 
     405          276 : static inline bool lsp_doc_find_next_type(const char **cursor, const char *end, zend_string **variable, zend_string **type)
     406              : {
     407          276 :         return lsp_doc_find_next_type_ex(cursor, end, variable, type, true);
     408              : }
     409              : 
     410         1188 : static inline zend_string *lsp_type_class_name(zend_string *type)
     411              : {
     412         1188 :         const char *p = ZSTR_VAL(type), *end = p + ZSTR_LEN(type), *start;
     413              : 
     414         1196 :         while (p < end && isspace((unsigned char) *p)) {
     415            8 :                 p++;
     416              :         }
     417              : 
     418         1188 :         while (p < end && (*p == '?' || *p == '(')) {
     419            0 :                 p++;
     420              :         }
     421              : 
     422         1188 :         while (p < end && isspace((unsigned char) *p)) {
     423            0 :                 p++;
     424              :         }
     425              : 
     426         1188 :         if (p < end && *p == '\\') {
     427            8 :                 p++;
     428              :         }
     429              : 
     430         1188 :         start = p;
     431        10620 :         while (p < end && (lsp_doc_is_identifier_char(*p) || *p == '\\')) {
     432         9432 :                 p++;
     433              :         }
     434              : 
     435         1188 :         if (p == start) {
     436            0 :                 return NULL;
     437              :         }
     438              : 
     439         1188 :         return zend_string_init(start, p - start, 0);
     440              : }
     441              : 
     442          112 : static inline zend_string *lsp_string_trim_slice(const char *start, const char *end)
     443              : {
     444          128 :         while (start < end && isspace((unsigned char) *start)) {
     445           16 :                 start++;
     446              :         }
     447              : 
     448          112 :         while (end > start && isspace((unsigned char) end[-1])) {
     449            0 :                 end--;
     450              :         }
     451              : 
     452          112 :         return zend_string_init(start, end - start, 0);
     453              : }
     454              : 
     455           32 : static inline bool lsp_phpdoc_whitespace_continues_type(const char *type_start, const char *cursor, const char *end)
     456              : {
     457              :         const char *previous, *next;
     458              : 
     459           32 :         previous = cursor;
     460           32 :         while (previous > type_start && isspace((unsigned char) previous[-1])) {
     461            0 :                 previous--;
     462              :         }
     463              : 
     464           32 :         next = cursor;
     465           64 :         while (next < end && isspace((unsigned char) *next) && *next != '\n' && *next != '\r') {
     466           32 :                 next++;
     467              :         }
     468              : 
     469           32 :         if (next < end && (*next == '|' || *next == '&')) {
     470            0 :                 return true;
     471              :         }
     472              : 
     473           32 :         return previous > type_start && (previous[-1] == '|' || previous[-1] == '&');
     474              : }
     475              : 
     476          116 : static inline bool lsp_phpdoc_tag_at(const char *cursor, const char *end, const char *tag)
     477              : {
     478              :         size_t tag_length;
     479              : 
     480          116 :         tag_length = strlen(tag);
     481          116 :         if (cursor + tag_length > end || memcmp(cursor, tag, tag_length) != 0) {
     482            0 :                 return false;
     483              :         }
     484              : 
     485          232 :         return cursor + tag_length == end ||
     486          116 :                 isspace((unsigned char) cursor[tag_length]) ||
     487            0 :                 cursor[tag_length] == '*'
     488              :         ;
     489              : }
     490              : 
     491          320 : static inline const char *lsp_phpdoc_type_expression_end(const char *type_start, const char *end)
     492              : {
     493              :         const char *cursor;
     494              :         uint32_t brace_depth, angle_depth, paren_depth, bracket_depth;
     495              :         char quote;
     496              :         bool escaped;
     497              : 
     498          320 :         cursor = type_start;
     499          320 :         brace_depth = 0;
     500          320 :         angle_depth = 0;
     501          320 :         paren_depth = 0;
     502          320 :         bracket_depth = 0;
     503          320 :         quote = '\0';
     504          320 :         escaped = false;
     505              : 
     506         7540 :         while (cursor < end) {
     507         7540 :                 if (quote != '\0') {
     508            0 :                         if (escaped) {
     509            0 :                                 escaped = false;
     510            0 :                         } else if (*cursor == '\\') {
     511            0 :                                 escaped = true;
     512            0 :                         } else if (*cursor == quote) {
     513            0 :                                 quote = '\0';
     514              :                         }
     515            0 :                         cursor++;
     516            0 :                         continue;
     517              :                 }
     518              : 
     519         7540 :                 if (*cursor == '\'' || *cursor == '"') {
     520            0 :                         quote = *cursor;
     521            0 :                         cursor++;
     522            0 :                         continue;
     523              :                 }
     524              : 
     525         7540 :                 if (*cursor == '\n' || *cursor == '\r') {
     526              :                         break;
     527              :                 }
     528              : 
     529         7252 :                 if (isspace((unsigned char) *cursor) && brace_depth == 0 && angle_depth == 0 && paren_depth == 0 && bracket_depth == 0) {
     530           32 :                         if (!lsp_phpdoc_whitespace_continues_type(type_start, cursor, end)) {
     531           32 :                                 break;
     532              :                         }
     533         7220 :                 } else if (*cursor == '{') {
     534           92 :                         brace_depth++;
     535         7128 :                 } else if (*cursor == '}' && brace_depth > 0) {
     536           92 :                         brace_depth--;
     537         7036 :                 } else if (*cursor == '<') {
     538          176 :                         angle_depth++;
     539         6860 :                 } else if (*cursor == '>' && angle_depth > 0) {
     540          176 :                         angle_depth--;
     541         6684 :                 } else if (*cursor == '(') {
     542            0 :                         paren_depth++;
     543         6684 :                 } else if (*cursor == ')' && paren_depth > 0) {
     544            0 :                         paren_depth--;
     545         6684 :                 } else if (*cursor == '[') {
     546            0 :                         bracket_depth++;
     547         6684 :                 } else if (*cursor == ']' && bracket_depth > 0) {
     548            0 :                         bracket_depth--;
     549              :                 }
     550              : 
     551         7220 :                 cursor++;
     552              :         }
     553              : 
     554          320 :         return cursor;
     555              : }
     556              : 
     557          144 : static inline zend_string *lsp_phpdoc_local_type_alias(zend_string *text, zend_string *alias, bool normalize)
     558              : {
     559          144 :         const char *tags[2] = {"@phpstan-type", "@psalm-type"};
     560              :         const char *value, *end, *cursor, *match, *p, *name_start, *name_end, *type_start, *type_end;
     561              :         uint32_t i;
     562              : 
     563          144 :         if (!text || !alias || ZSTR_LEN(alias) == 0 || ZSTR_VAL(alias)[0] == '$' || memchr(ZSTR_VAL(alias), '\\', ZSTR_LEN(alias))) {
     564            0 :                 return NULL;
     565              :         }
     566              : 
     567          144 :         value = ZSTR_VAL(text);
     568          144 :         end = value + ZSTR_LEN(text);
     569          376 :         for (i = 0; i < sizeof(tags) / sizeof(tags[0]); i++) {
     570          260 :                 cursor = value;
     571          308 :                 while ((match = strstr(cursor, tags[i])) != NULL && match < end) {
     572           76 :                         if (!lsp_phpdoc_tag_at(match, end, tags[i])) {
     573            0 :                                 cursor = match + strlen(tags[i]);
     574            0 :                                 continue;
     575              :                         }
     576              : 
     577           76 :                         p = match + strlen(tags[i]);
     578          152 :                         while (p < end && isspace((unsigned char) *p)) {
     579           76 :                                 p++;
     580              :                         }
     581              : 
     582           76 :                         name_start = p;
     583         1040 :                         while (p < end && lsp_doc_is_identifier_char(*p)) {
     584          964 :                                 p++;
     585              :                         }
     586           76 :                         name_end = p;
     587           76 :                         if ((size_t) (name_end - name_start) != ZSTR_LEN(alias) || strncasecmp(name_start, ZSTR_VAL(alias), ZSTR_LEN(alias)) != 0) {
     588           48 :                                 cursor = p;
     589           48 :                                 continue;
     590              :                         }
     591              : 
     592           56 :                         while (p < end && isspace((unsigned char) *p)) {
     593           28 :                                 p++;
     594              :                         }
     595           28 :                         if (p < end && *p == '=') {
     596            0 :                                 p++;
     597            0 :                                 while (p < end && isspace((unsigned char) *p)) {
     598            0 :                                         p++;
     599              :                                 }
     600              :                         }
     601              : 
     602           28 :                         type_start = p;
     603           28 :                         type_end = lsp_phpdoc_type_expression_end(type_start, end);
     604           28 :                         while (type_end > type_start && isspace((unsigned char) type_end[-1])) {
     605            0 :                                 type_end--;
     606              :                         }
     607           28 :                         if (type_end > type_start) {
     608           28 :                                 return lsp_phpdoc_type_slice(type_start, type_end, normalize);
     609              :                         }
     610              : 
     611            0 :                         cursor = p;
     612              :                 }
     613              :         }
     614              : 
     615          116 :         return NULL;
     616              : }
     617              : 
     618          116 : static inline zend_string *lsp_phpdoc_import_type_alias(zend_string *text, zend_string *alias, bool normalize)
     619              : {
     620          116 :         const char *tags[2] = {"@phpstan-import-type", "@psalm-import-type"};
     621              :         const char *value, *end, *cursor, *match, *p, *import_name_start, *import_name_end, *local_name_start, *local_name_end;
     622              :         zend_string *import_name, *resolved;
     623              :         uint32_t i;
     624              :         bool has_local_name, matches;
     625              : 
     626          116 :         if (!text || !alias || ZSTR_LEN(alias) == 0 || ZSTR_VAL(alias)[0] == '$' || memchr(ZSTR_VAL(alias), '\\', ZSTR_LEN(alias))) {
     627            0 :                 return NULL;
     628              :         }
     629              : 
     630          116 :         value = ZSTR_VAL(text);
     631          116 :         end = value + ZSTR_LEN(text);
     632          324 :         for (i = 0; i < sizeof(tags) / sizeof(tags[0]); i++) {
     633          220 :                 cursor = value;
     634          228 :                 while ((match = strstr(cursor, tags[i])) != NULL && match < end) {
     635           20 :                         if (!lsp_phpdoc_tag_at(match, end, tags[i])) {
     636            0 :                                 cursor = match + strlen(tags[i]);
     637            0 :                                 continue;
     638              :                         }
     639              : 
     640           20 :                         p = match + strlen(tags[i]);
     641           40 :                         while (p < end && isspace((unsigned char) *p)) {
     642           20 :                                 p++;
     643              :                         }
     644              : 
     645           20 :                         import_name_start = p;
     646          320 :                         while (p < end && lsp_doc_is_identifier_char(*p)) {
     647          300 :                                 p++;
     648              :                         }
     649           20 :                         import_name_end = p;
     650           40 :                         while (p < end && isspace((unsigned char) *p)) {
     651           20 :                                 p++;
     652              :                         }
     653           20 :                         if (p + sizeof("from") - 1 > end || strncasecmp(p, "from", sizeof("from") - 1) != 0 || (p + sizeof("from") - 1 < end && lsp_doc_is_identifier_char(p[sizeof("from") - 1]))) {
     654            0 :                                 cursor = p;
     655            0 :                                 continue;
     656              :                         }
     657              : 
     658           20 :                         p += sizeof("from") - 1;
     659           40 :                         while (p < end && isspace((unsigned char) *p)) {
     660           20 :                                 p++;
     661              :                         }
     662          220 :                         while (p < end && (lsp_doc_is_identifier_char(*p) || *p == '\\')) {
     663          200 :                                 p++;
     664              :                         }
     665           40 :                         while (p < end && isspace((unsigned char) *p)) {
     666           20 :                                 p++;
     667              :                         }
     668              : 
     669           20 :                         local_name_start = NULL;
     670           20 :                         local_name_end = NULL;
     671           20 :                         has_local_name = false;
     672           20 :                         if (p + sizeof("as") - 1 <= end && strncasecmp(p, "as", sizeof("as") - 1) == 0 && (p + sizeof("as") - 1 == end || !lsp_doc_is_identifier_char(p[sizeof("as") - 1]))) {
     673           20 :                                 p += sizeof("as") - 1;
     674           40 :                                 while (p < end && isspace((unsigned char) *p)) {
     675           20 :                                         p++;
     676              :                                 }
     677           20 :                                 local_name_start = p;
     678          320 :                                 while (p < end && lsp_doc_is_identifier_char(*p)) {
     679          300 :                                         p++;
     680              :                                 }
     681           20 :                                 local_name_end = p;
     682           20 :                                 has_local_name = local_name_end > local_name_start;
     683              :                         }
     684              : 
     685           20 :                         matches = has_local_name
     686           20 :                                 ? (ZSTR_LEN(alias) == (size_t) (local_name_end - local_name_start) && strncasecmp(local_name_start, ZSTR_VAL(alias), ZSTR_LEN(alias)) == 0)
     687            0 :                                 : (ZSTR_LEN(alias) == (size_t) (import_name_end - import_name_start) && strncasecmp(import_name_start, ZSTR_VAL(alias), ZSTR_LEN(alias)) == 0)
     688           40 :                         ;
     689           20 :                         if (!matches || import_name_end <= import_name_start) {
     690            8 :                                 cursor = p;
     691            8 :                                 continue;
     692              :                         }
     693              : 
     694           12 :                         import_name = zend_string_init(import_name_start, import_name_end - import_name_start, 0);
     695           12 :                         resolved = lsp_phpdoc_local_type_alias(text, import_name, normalize);
     696           12 :                         zend_string_release(import_name);
     697           12 :                         if (resolved) {
     698           12 :                                 return resolved;
     699              :                         }
     700              : 
     701            0 :                         cursor = p;
     702              :                 }
     703              :         }
     704              : 
     705          104 :         return NULL;
     706              : }
     707              : 
     708          132 : static inline zend_string *lsp_phpdoc_type_alias(zend_string *text, zend_string *alias, bool normalize)
     709              : {
     710              :         zend_string *resolved;
     711              : 
     712          132 :         resolved = lsp_phpdoc_local_type_alias(text, alias, normalize);
     713          132 :         if (resolved) {
     714           16 :                 return resolved;
     715              :         }
     716              : 
     717          116 :         return lsp_phpdoc_import_type_alias(text, alias, normalize);
     718              : }
     719              : 
     720           20 : static inline zend_string *lsp_phpdoc_var_type_from_comment_ex(zend_string *comment, bool normalize)
     721              : {
     722           20 :         const char *tags[3] = {"@var", "@phpstan-var", "@psalm-var"};
     723              :         const char *value, *end, *cursor, *match, *best, *p, *start, *finish;
     724              :         size_t tag_length, best_length;
     725              :         uint32_t i;
     726              : 
     727           20 :         value = ZSTR_VAL(comment);
     728           20 :         end = value + ZSTR_LEN(comment);
     729           20 :         best = NULL;
     730           20 :         best_length = 0;
     731              : 
     732           80 :         for (i = 0; i < sizeof(tags) / sizeof(tags[0]); i++) {
     733           60 :                 tag_length = strlen(tags[i]);
     734           60 :                 cursor = value;
     735           60 :                 while ((match = strstr(cursor, tags[i])) != NULL && match < end) {
     736           20 :                         if (lsp_phpdoc_tag_at(match, end, tags[i]) && (!best || match < best)) {
     737           20 :                                 best = match;
     738           20 :                                 best_length = tag_length;
     739           20 :                                 break;
     740              :                         }
     741              : 
     742            0 :                         cursor = match + tag_length;
     743              :                 }
     744              :         }
     745              : 
     746           20 :         if (!best) {
     747            0 :                 return NULL;
     748              :         }
     749              : 
     750           20 :         p = best + best_length;
     751           40 :         while (p < end && isspace((unsigned char) *p)) {
     752           20 :                 p++;
     753              :         }
     754              : 
     755           20 :         start = p;
     756           20 :         finish = lsp_phpdoc_type_expression_end(start, end);
     757           20 :         while (finish > start && isspace((unsigned char) finish[-1])) {
     758            0 :                 finish--;
     759              :         }
     760              : 
     761           20 :         if (finish - start >= 2 && finish[-2] == '*' && finish[-1] == '/') {
     762            0 :                 finish -= 2;
     763            0 :                 while (finish > start && isspace((unsigned char) finish[-1])) {
     764            0 :                         finish--;
     765              :                 }
     766              :         }
     767              : 
     768           20 :         if (finish <= start) {
     769            0 :                 return NULL;
     770              :         }
     771              : 
     772           20 :         return lsp_phpdoc_type_slice(start, finish, normalize);
     773              : }
     774              : 
     775           40 : static inline zend_string *lsp_phpdoc_property_var_type_before(zend_string *text, size_t variable_offset, bool normalize)
     776              : {
     777              :         const char *value;
     778              :         zend_string *comment, *type;
     779              :         size_t declaration_start, scan, doc_end, doc_start, base;
     780              :         bool found_doc_start;
     781              : 
     782           40 :         if (variable_offset > ZSTR_LEN(text)) {
     783            0 :                 return NULL;
     784              :         }
     785              : 
     786           40 :         value = ZSTR_VAL(text);
     787           40 :         declaration_start = variable_offset;
     788           40 :         while (declaration_start > 0 &&
     789         1632 :                 value[declaration_start - 1] != ';' &&
     790         3260 :                 value[declaration_start - 1] != '{' &&
     791         1592 :                 value[declaration_start - 1] != '}'
     792              :         ) {
     793         1592 :                 declaration_start--;
     794              :         }
     795              : 
     796           40 :         doc_end = 0;
     797         1592 :         for (scan = declaration_start; scan + 1 < variable_offset; scan++) {
     798         1552 :                 if (value[scan] == '*' && value[scan + 1] == '/') {
     799           20 :                         doc_end = scan + 2;
     800              :                 }
     801              :         }
     802              : 
     803           40 :         if (doc_end == 0 || doc_end < 3) {
     804           20 :                 return NULL;
     805              :         }
     806              : 
     807           20 :         doc_start = 0;
     808           20 :         base = declaration_start + 2;
     809           20 :         found_doc_start = false;
     810          600 :         for (scan = doc_end - 2; scan >= base; scan--) {
     811          600 :                 if (value[scan - 2] == '/' && value[scan - 1] == '*' && value[scan] == '*') {
     812           20 :                         doc_start = scan - 2;
     813           20 :                         found_doc_start = true;
     814           20 :                         break;
     815              :                 }
     816              : 
     817          580 :                 if (scan == base) {
     818            0 :                         break;
     819              :                 }
     820              :         }
     821              : 
     822           20 :         if (!found_doc_start || doc_end <= doc_start) {
     823            0 :                 return NULL;
     824              :         }
     825              : 
     826           20 :         comment = zend_string_init(value + doc_start, doc_end - doc_start, 0);
     827           20 :         type = lsp_phpdoc_var_type_from_comment_ex(comment, normalize);
     828           20 :         zend_string_release(comment);
     829              : 
     830           20 :         return type;
     831              : }
     832              : 
     833         4764 : static inline bool lsp_phpdoc_word_char(char c)
     834              : {
     835         4764 :         return lsp_doc_is_identifier_char(c) || c == '$' || c == '\\';
     836              : }
     837              : 
     838          748 : static inline bool lsp_phpdoc_word_bounds(zend_string *text, zend_string *word, size_t offset, size_t *word_start, size_t *word_end)
     839              : {
     840              :         const char *value;
     841              :         size_t length, start, end;
     842              : 
     843          748 :         if (!text || !word || ZSTR_LEN(word) == 0) {
     844            0 :                 return false;
     845              :         }
     846              : 
     847          748 :         value = ZSTR_VAL(text);
     848          748 :         length = ZSTR_LEN(text);
     849          748 :         if (offset > length) {
     850            0 :                 offset = length;
     851              :         }
     852              : 
     853          748 :         start = offset;
     854         2488 :         while (start > 0 && lsp_phpdoc_word_char(value[start - 1])) {
     855         1740 :                 start--;
     856              :         }
     857              : 
     858          748 :         end = offset;
     859         2276 :         while (end < length && lsp_phpdoc_word_char(value[end])) {
     860         1528 :                 end++;
     861              :         }
     862              : 
     863          748 :         if (end <= start || end - start != ZSTR_LEN(word) || memcmp(value + start, ZSTR_VAL(word), ZSTR_LEN(word)) != 0) {
     864          552 :                 return false;
     865              :         }
     866              : 
     867          196 :         *word_start = start;
     868          196 :         *word_end = end;
     869              : 
     870          196 :         return true;
     871              : }
     872              : 
     873           72 : static inline bool lsp_phpdoc_this_member_access_at(zend_string *text, zend_string *word, size_t offset)
     874              : {
     875              :         const char *value;
     876              :         size_t word_start, word_end, i, receiver_end, receiver_start;
     877              : 
     878           72 :         if (!word || ZSTR_LEN(word) == 0 || ZSTR_VAL(word)[0] == '$') {
     879            0 :                 return false;
     880              :         }
     881              : 
     882           72 :         if (!lsp_phpdoc_word_bounds(text, word, offset, &word_start, &word_end)) {
     883            0 :                 return false;
     884              :         }
     885              : 
     886           72 :         value = ZSTR_VAL(text);
     887           72 :         i = word_start;
     888          104 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
     889           32 :                 i--;
     890              :         }
     891              : 
     892           72 :         if (i >= 3 && value[i - 3] == '?' && value[i - 2] == '-' && value[i - 1] == '>') {
     893            0 :                 receiver_end = i - 3;
     894           72 :         } else if (i >= 2 && value[i - 2] == '-' && value[i - 1] == '>') {
     895           32 :                 receiver_end = i - 2;
     896              :         } else {
     897           40 :                 return false;
     898              :         }
     899              : 
     900           32 :         i = receiver_end;
     901           32 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
     902            0 :                 i--;
     903              :         }
     904           32 :         receiver_end = i;
     905              : 
     906          188 :         while (i > 0 && (lsp_doc_is_identifier_char(value[i - 1]) || value[i - 1] == '$' || value[i - 1] == '\\')) {
     907          156 :                 i--;
     908              :         }
     909           32 :         receiver_start = i;
     910              : 
     911           60 :         return receiver_end - receiver_start == sizeof("$this") - 1 &&
     912           28 :                 memcmp(value + receiver_start, "$this", sizeof("$this") - 1) == 0
     913              :         ;
     914              : }
     915              : 
     916           40 : static inline bool lsp_phpdoc_property_declaration_before_variable(zend_string *text, size_t variable_offset)
     917              : {
     918           40 :         const char *visibility[4] = {"public", "protected", "private", "var"};
     919              :         const char *value;
     920              :         size_t declaration_start, scan, token_start, token_end;
     921              :         uint32_t i;
     922              : 
     923           40 :         if (variable_offset > ZSTR_LEN(text)) {
     924            0 :                 return false;
     925              :         }
     926              : 
     927           40 :         value = ZSTR_VAL(text);
     928           40 :         declaration_start = variable_offset;
     929           40 :         while (declaration_start > 0 &&
     930         1632 :                 value[declaration_start - 1] != ';' &&
     931         3260 :                 value[declaration_start - 1] != '{' &&
     932         1592 :                 value[declaration_start - 1] != '}'
     933              :         ) {
     934         1592 :                 declaration_start--;
     935              :         }
     936              : 
     937           40 :         scan = declaration_start;
     938          344 :         while (scan < variable_offset) {
     939          344 :                 if (!lsp_doc_is_identifier_start(value[scan])) {
     940          304 :                         scan++;
     941          304 :                         continue;
     942              :                 }
     943              : 
     944           40 :                 token_start = scan;
     945           40 :                 scan++;
     946          192 :                 while (scan < variable_offset && lsp_doc_is_identifier_char(value[scan])) {
     947          152 :                         scan++;
     948              :                 }
     949           40 :                 token_end = scan;
     950              : 
     951          124 :                 for (i = 0; i < sizeof(visibility) / sizeof(visibility[0]); i++) {
     952          124 :                         if (token_end - token_start == strlen(visibility[i]) &&
     953           40 :                                 strncasecmp(value + token_start, visibility[i], token_end - token_start) == 0
     954              :                         ) {
     955           40 :                                 return true;
     956              :                         }
     957              :                 }
     958              :         }
     959              : 
     960            0 :         return false;
     961              : }
     962              : 
     963          756 : static inline zend_string *lsp_phpdoc_property_type_for_word_ex(zend_string *text, zend_string *word, size_t offset, bool normalize)
     964              : {
     965              :         const char *value, *target;
     966              :         zend_string *type, *resolved;
     967              :         size_t class_start, body_start, body_end, search, target_length, variable_end, word_start, word_end;
     968              :         zend_long body_depth;
     969              :         bool has_dollar, require_same_offset;
     970              : 
     971          756 :         if (!text || !word || ZSTR_LEN(word) == 0) {
     972            8 :                 return NULL;
     973              :         }
     974              : 
     975          748 :         value = ZSTR_VAL(text);
     976          748 :         has_dollar = ZSTR_VAL(word)[0] == '$';
     977          748 :         require_same_offset = has_dollar;
     978          748 :         word_start = 0;
     979          748 :         word_end = 0;
     980          748 :         target = ZSTR_VAL(word) + (has_dollar ? 1 : 0);
     981          748 :         target_length = ZSTR_LEN(word) - (has_dollar ? 1 : 0);
     982          748 :         if (target_length == 0) {
     983            0 :                 return NULL;
     984              :         }
     985              : 
     986          748 :         if (has_dollar) {
     987          676 :                 if (!lsp_phpdoc_word_bounds(text, word, offset, &word_start, &word_end)) {
     988          552 :                         return NULL;
     989              :                 }
     990           72 :         } else if (!lsp_phpdoc_this_member_access_at(text, word, offset)) {
     991           44 :                 return NULL;
     992              :         }
     993              : 
     994          152 :         if (!lsp_find_enclosing_class_header(text, offset, &class_start, &body_start, &body_end, &body_depth)) {
     995            0 :                 return NULL;
     996              :         }
     997              : 
     998        28300 :         for (search = body_start; search + target_length < body_end; search++) {
     999        28168 :                 if (value[search] != '$') {
    1000        27520 :                         continue;
    1001              :                 }
    1002              : 
    1003          648 :                 variable_end = search + 1 + target_length;
    1004          648 :                 if (variable_end > body_end || strncmp(value + search + 1, target, target_length) != 0) {
    1005          284 :                         continue;
    1006              :                 }
    1007              : 
    1008          364 :                 if (variable_end < ZSTR_LEN(text) && lsp_doc_is_identifier_char(value[variable_end])) {
    1009           72 :                         continue;
    1010              :                 }
    1011              : 
    1012          292 :                 if (lsp_brace_depth_at(text, search) != body_depth) {
    1013          252 :                         continue;
    1014              :                 }
    1015              : 
    1016           40 :                 if (require_same_offset && (word_start != search || word_end != variable_end)) {
    1017            0 :                         continue;
    1018              :                 }
    1019              : 
    1020           40 :                 if (!lsp_phpdoc_property_declaration_before_variable(text, search)) {
    1021            0 :                         continue;
    1022              :                 }
    1023              : 
    1024           40 :                 type = lsp_phpdoc_property_var_type_before(text, search, normalize);
    1025           40 :                 if (type) {
    1026           20 :                         resolved = lsp_phpdoc_type_alias(text, type, normalize);
    1027           20 :                         if (resolved) {
    1028            0 :                                 zend_string_release(type);
    1029            0 :                                 return resolved;
    1030              :                         }
    1031           20 :                         return type;
    1032              :                 }
    1033              :         }
    1034              : 
    1035          132 :         return NULL;
    1036              : }
    1037              : 
    1038          188 : static inline zend_string *lsp_phpdoc_shape_from_type_named(zend_string *type, const char *shape_name)
    1039              : {
    1040              :         const char *value, *end, *cursor, *open;
    1041              :         size_t shape_name_length;
    1042              :         uint32_t depth;
    1043              :         char quote;
    1044              :         bool escaped;
    1045              : 
    1046          188 :         value = ZSTR_VAL(type);
    1047          188 :         end = value + ZSTR_LEN(type);
    1048          188 :         open = NULL;
    1049          188 :         shape_name_length = strlen(shape_name);
    1050              : 
    1051         1160 :         for (cursor = value; cursor + shape_name_length <= end; cursor++) {
    1052         1028 :                 if (strncasecmp(cursor, shape_name, shape_name_length) != 0) {
    1053          972 :                         continue;
    1054              :                 }
    1055              : 
    1056           56 :                 if (cursor > value && lsp_doc_is_identifier_char(cursor[-1])) {
    1057            0 :                         continue;
    1058              :                 }
    1059           56 :                 if (cursor + shape_name_length < end && lsp_doc_is_identifier_char(cursor[shape_name_length])) {
    1060            0 :                         continue;
    1061              :                 }
    1062              : 
    1063           56 :                 open = cursor + shape_name_length;
    1064           56 :                 while (open < end && isspace((unsigned char) *open)) {
    1065            0 :                         open++;
    1066              :                 }
    1067              : 
    1068           56 :                 if (open < end && *open == '{') {
    1069           56 :                         break;
    1070              :                 }
    1071              : 
    1072            0 :                 open = NULL;
    1073              :         }
    1074              : 
    1075          188 :         if (!open || open >= end || *open != '{') {
    1076          132 :                 return NULL;
    1077              :         }
    1078              : 
    1079           56 :         depth = 1;
    1080           56 :         quote = '\0';
    1081           56 :         escaped = false;
    1082         1820 :         for (cursor = open + 1; cursor < end; cursor++) {
    1083         1820 :                 if (quote != '\0') {
    1084           84 :                         if (escaped) {
    1085            0 :                                 escaped = false;
    1086           84 :                         } else if (*cursor == '\\') {
    1087            0 :                                 escaped = true;
    1088           84 :                         } else if (*cursor == quote) {
    1089            8 :                                 quote = '\0';
    1090              :                         }
    1091           84 :                         continue;
    1092              :                 }
    1093              : 
    1094         1736 :                 if (*cursor == '\'' || *cursor == '"') {
    1095            8 :                         quote = *cursor;
    1096            8 :                         continue;
    1097              :                 }
    1098              : 
    1099         1728 :                 if (*cursor == '{') {
    1100            4 :                         depth++;
    1101         1724 :                 } else if (*cursor == '}' && --depth == 0) {
    1102           56 :                         return zend_string_init(open + 1, cursor - open - 1, 0);
    1103              :                 }
    1104              :         }
    1105              : 
    1106            0 :         return NULL;
    1107              : }
    1108              : 
    1109           88 : static inline zend_string *lsp_phpdoc_array_shape_from_type(zend_string *type)
    1110              : {
    1111           88 :         return lsp_phpdoc_shape_from_type_named(type, "array");
    1112              : }
    1113              : 
    1114          100 : static inline zend_string *lsp_phpdoc_object_shape_from_type(zend_string *type)
    1115              : {
    1116          100 :         return lsp_phpdoc_shape_from_type_named(type, "object");
    1117              : }
    1118              : 
    1119          104 : static inline const char *lsp_phpdoc_shape_value_end(const char *value_start, const char *end)
    1120              : {
    1121              :         const char *cursor;
    1122              :         uint32_t brace_depth, angle_depth, paren_depth, bracket_depth;
    1123              :         char quote;
    1124              :         bool escaped;
    1125              : 
    1126          104 :         cursor = value_start;
    1127          104 :         brace_depth = 0;
    1128          104 :         angle_depth = 0;
    1129          104 :         paren_depth = 0;
    1130          104 :         bracket_depth = 0;
    1131          104 :         quote = '\0';
    1132          104 :         escaped = false;
    1133              : 
    1134          716 :         while (cursor < end) {
    1135          664 :                 if (quote != '\0') {
    1136            0 :                         if (escaped) {
    1137            0 :                                 escaped = false;
    1138            0 :                         } else if (*cursor == '\\') {
    1139            0 :                                 escaped = true;
    1140            0 :                         } else if (*cursor == quote) {
    1141            0 :                                 quote = '\0';
    1142              :                         }
    1143            0 :                         cursor++;
    1144            0 :                         continue;
    1145              :                 }
    1146              : 
    1147          664 :                 if (*cursor == '\'' || *cursor == '"') {
    1148            0 :                         quote = *cursor;
    1149            0 :                         cursor++;
    1150            0 :                         continue;
    1151              :                 }
    1152              : 
    1153          664 :                 if (*cursor == ',' && brace_depth == 0 && angle_depth == 0 && paren_depth == 0 && bracket_depth == 0) {
    1154           52 :                         break;
    1155              :                 }
    1156              : 
    1157          612 :                 if (*cursor == '{') {
    1158            0 :                         brace_depth++;
    1159          612 :                 } else if (*cursor == '}' && brace_depth > 0) {
    1160            0 :                         brace_depth--;
    1161          612 :                 } else if (*cursor == '<') {
    1162            0 :                         angle_depth++;
    1163          612 :                 } else if (*cursor == '>' && angle_depth > 0) {
    1164            0 :                         angle_depth--;
    1165          612 :                 } else if (*cursor == '(') {
    1166            0 :                         paren_depth++;
    1167          612 :                 } else if (*cursor == ')' && paren_depth > 0) {
    1168            0 :                         paren_depth--;
    1169          612 :                 } else if (*cursor == '[') {
    1170            0 :                         bracket_depth++;
    1171          612 :                 } else if (*cursor == ']' && bracket_depth > 0) {
    1172            0 :                         bracket_depth--;
    1173              :                 }
    1174              : 
    1175          612 :                 cursor++;
    1176              :         }
    1177              : 
    1178          104 :         return cursor;
    1179              : }
    1180              : 
    1181          164 : static inline bool lsp_phpdoc_type_wrapper_argument(zend_string *type, const char *wrapper, zend_string **argument)
    1182              : {
    1183              :         const char *value, *end, *open, *close;
    1184              :         size_t wrapper_length;
    1185              : 
    1186          164 :         *argument = NULL;
    1187          164 :         value = ZSTR_VAL(type);
    1188          164 :         end = value + ZSTR_LEN(type);
    1189          164 :         wrapper_length = strlen(wrapper);
    1190          164 :         while (value < end && isspace((unsigned char) *value)) {
    1191            0 :                 value++;
    1192              :         }
    1193          164 :         while (end > value && isspace((unsigned char) end[-1])) {
    1194            0 :                 end--;
    1195              :         }
    1196              : 
    1197          164 :         if ((size_t) (end - value) <= wrapper_length + 2 || strncasecmp(value, wrapper, wrapper_length) != 0) {
    1198          156 :                 return false;
    1199              :         }
    1200              : 
    1201            8 :         open = value + wrapper_length;
    1202            8 :         while (open < end && isspace((unsigned char) *open)) {
    1203            0 :                 open++;
    1204              :         }
    1205            8 :         if (open >= end || *open != '<') {
    1206            0 :                 return false;
    1207              :         }
    1208              : 
    1209            8 :         close = end - 1;
    1210            8 :         while (close > open && isspace((unsigned char) *close)) {
    1211            0 :                 close--;
    1212              :         }
    1213            8 :         if (close <= open || *close != '>') {
    1214            0 :                 return false;
    1215              :         }
    1216              : 
    1217            8 :         *argument = lsp_string_trim_slice(open + 1, close);
    1218              : 
    1219            8 :         return *argument != NULL && ZSTR_LEN(*argument) > 0;
    1220              : }
    1221              : 
    1222           16 : static inline void lsp_phpdoc_append_shape_key_union_part(smart_str *text, const char *key_start, const char *key_end)
    1223              : {
    1224              :         bool needs_quotes;
    1225              :         const char *p;
    1226              : 
    1227           16 :         if (key_end <= key_start) {
    1228            0 :                 return;
    1229              :         }
    1230              : 
    1231           16 :         needs_quotes = false;
    1232          100 :         for (p = key_start; p < key_end; p++) {
    1233           84 :                 if (!lsp_doc_is_identifier_char(*p)) {
    1234            0 :                         needs_quotes = true;
    1235            0 :                         break;
    1236              :                 }
    1237              :         }
    1238              : 
    1239           16 :         if (!needs_quotes) {
    1240           16 :                 smart_str_appendl(text, key_start, key_end - key_start);
    1241           16 :                 return;
    1242              :         }
    1243              : 
    1244            0 :         smart_str_appendc(text, '\'');
    1245            0 :         for (p = key_start; p < key_end; p++) {
    1246            0 :                 if (*p == '\'' || *p == '\\') {
    1247            0 :                         smart_str_appendc(text, '\\');
    1248              :                 }
    1249            0 :                 smart_str_appendc(text, *p);
    1250              :         }
    1251            0 :         smart_str_appendc(text, '\'');
    1252              : }
    1253              : 
    1254            8 : static inline zend_string *lsp_phpdoc_shape_key_or_value_union(zend_string *shape, bool values, bool normalize)
    1255              : {
    1256              :         const char *p, *end, *key_start, *key_end, *value_start, *value_end;
    1257              :         zend_string *part;
    1258            8 :         smart_str union_text = {0};
    1259              :         char quote;
    1260              :         bool appended;
    1261              : 
    1262            8 :         p = ZSTR_VAL(shape);
    1263            8 :         end = p + ZSTR_LEN(shape);
    1264            8 :         appended = false;
    1265           24 :         while (p < end) {
    1266           24 :                 while (p < end && (isspace((unsigned char) *p) || *p == ',')) {
    1267            8 :                         p++;
    1268              :                 }
    1269           16 :                 if (p >= end) {
    1270            0 :                         break;
    1271              :                 }
    1272              : 
    1273           16 :                 if (*p == '\'' || *p == '"') {
    1274            0 :                         quote = *p++;
    1275            0 :                         key_start = p;
    1276            0 :                         while (p < end && *p != quote) {
    1277            0 :                                 p++;
    1278              :                         }
    1279            0 :                         key_end = p;
    1280            0 :                         if (p < end) {
    1281            0 :                                 p++;
    1282              :                         }
    1283              :                 } else {
    1284           16 :                         key_start = p;
    1285           96 :                         while (p < end && !isspace((unsigned char) *p) && *p != ':' && *p != ',' && *p != '?') {
    1286           80 :                                 p++;
    1287              :                         }
    1288           16 :                         key_end = p;
    1289              :                 }
    1290              : 
    1291           16 :                 while (p < end && isspace((unsigned char) *p)) {
    1292            0 :                         p++;
    1293              :                 }
    1294           16 :                 if (p < end && *p == '?') {
    1295            0 :                         p++;
    1296            0 :                         while (p < end && isspace((unsigned char) *p)) {
    1297            0 :                                 p++;
    1298              :                         }
    1299              :                 }
    1300           16 :                 if (p >= end || *p != ':') {
    1301            0 :                         p = lsp_phpdoc_shape_value_end(p, end);
    1302            0 :                         if (p < end && *p == ',') {
    1303            0 :                                 p++;
    1304              :                         }
    1305            0 :                         continue;
    1306              :                 }
    1307              : 
    1308           16 :                 p++;
    1309           32 :                 while (p < end && isspace((unsigned char) *p)) {
    1310           16 :                         p++;
    1311              :                 }
    1312           16 :                 value_start = p;
    1313           16 :                 value_end = lsp_phpdoc_shape_value_end(value_start, end);
    1314           16 :                 if (key_end > key_start) {
    1315           16 :                         if (appended) {
    1316            8 :                                 smart_str_appendc(&union_text, '|');
    1317              :                         }
    1318           16 :                         if (values) {
    1319            8 :                                 part = lsp_phpdoc_type_slice(value_start, value_end, normalize);
    1320            8 :                                 smart_str_append(&union_text, part);
    1321            8 :                                 zend_string_release(part);
    1322              :                         } else {
    1323            8 :                                 lsp_phpdoc_append_shape_key_union_part(&union_text, key_start, key_end);
    1324              :                         }
    1325           16 :                         appended = true;
    1326              :                 }
    1327              : 
    1328           16 :                 p = value_end;
    1329           16 :                 if (p < end && *p == ',') {
    1330            8 :                         p++;
    1331              :                 }
    1332              :         }
    1333              : 
    1334            8 :         if (!appended) {
    1335            0 :                 return NULL;
    1336              :         }
    1337              : 
    1338            8 :         smart_str_0(&union_text);
    1339              : 
    1340            8 :         return union_text.s;
    1341              : }
    1342              : 
    1343           84 : static inline zend_string *lsp_phpdoc_utility_alias_type(zend_string *text, zend_string *type, bool normalize)
    1344              : {
    1345              :         zend_string *argument, *resolved_argument, *shape, *result;
    1346              :         bool want_values;
    1347              : 
    1348           84 :         argument = NULL;
    1349           84 :         want_values = false;
    1350           84 :         if (!lsp_phpdoc_type_wrapper_argument(type, "key-of", &argument)) {
    1351           80 :                 want_values = true;
    1352           80 :                 if (!lsp_phpdoc_type_wrapper_argument(type, "value-of", &argument)) {
    1353           76 :                         return NULL;
    1354              :                 }
    1355              :         }
    1356              : 
    1357            8 :         resolved_argument = lsp_phpdoc_type_alias(text, argument, normalize);
    1358            8 :         shape = NULL;
    1359            8 :         if (resolved_argument) {
    1360            8 :                 shape = lsp_phpdoc_array_shape_from_type(resolved_argument);
    1361            8 :                 if (!shape) {
    1362            0 :                         shape = lsp_phpdoc_object_shape_from_type(resolved_argument);
    1363              :                 }
    1364            8 :                 zend_string_release(resolved_argument);
    1365              :         } else {
    1366            0 :                 shape = lsp_phpdoc_array_shape_from_type(argument);
    1367            0 :                 if (!shape) {
    1368            0 :                         shape = lsp_phpdoc_object_shape_from_type(argument);
    1369              :                 }
    1370              :         }
    1371            8 :         zend_string_release(argument);
    1372            8 :         if (!shape) {
    1373            0 :                 return NULL;
    1374              :         }
    1375              : 
    1376            8 :         result = lsp_phpdoc_shape_key_or_value_union(shape, want_values, normalize);
    1377            8 :         zend_string_release(shape);
    1378              : 
    1379            8 :         return result;
    1380              : }
    1381              : 
    1382          104 : static inline zend_string *lsp_phpdoc_resolve_type_text(zend_string *text, zend_string *type, bool normalize)
    1383              : {
    1384              :         zend_string *resolved;
    1385              : 
    1386          104 :         resolved = lsp_phpdoc_type_alias(text, type, normalize);
    1387          104 :         if (resolved) {
    1388           20 :                 return resolved;
    1389              :         }
    1390              : 
    1391           84 :         return lsp_phpdoc_utility_alias_type(text, type, normalize);
    1392              : }
    1393              : 
    1394           36 : static inline size_t lsp_phpdoc_attribute_scan_start(zend_string *text, size_t offset)
    1395              : {
    1396              :         const char *value;
    1397              :         size_t start;
    1398              : 
    1399           36 :         value = ZSTR_VAL(text);
    1400           36 :         start = offset > ZSTR_LEN(text) ? ZSTR_LEN(text) : offset;
    1401          772 :         while (start > 0) {
    1402          772 :                 if (value[start - 1] == ';' || value[start - 1] == '{' || value[start - 1] == '}') {
    1403              :                         break;
    1404              :                 }
    1405          736 :                 start--;
    1406              :         }
    1407              : 
    1408           36 :         return start;
    1409              : }
    1410              : 
    1411            4 : static inline bool lsp_phpdoc_matching_square_bracket(const char *start, const char *end, const char **close)
    1412              : {
    1413              :         const char *p;
    1414              :         uint32_t depth;
    1415              :         char quote;
    1416              :         bool escaped;
    1417              : 
    1418            4 :         depth = 0;
    1419            4 :         quote = '\0';
    1420            4 :         escaped = false;
    1421          236 :         for (p = start; p < end; p++) {
    1422          236 :                 if (quote != '\0') {
    1423          172 :                         if (escaped) {
    1424            0 :                                 escaped = false;
    1425          172 :                         } else if (*p == '\\') {
    1426            0 :                                 escaped = true;
    1427          172 :                         } else if (*p == quote) {
    1428           16 :                                 quote = '\0';
    1429              :                         }
    1430          172 :                         continue;
    1431              :                 }
    1432              : 
    1433           64 :                 if (*p == '\'' || *p == '"') {
    1434           16 :                         quote = *p;
    1435           16 :                         continue;
    1436              :                 }
    1437           48 :                 if (*p == '[') {
    1438            4 :                         depth++;
    1439           44 :                 } else if (*p == ']' && depth > 0) {
    1440            4 :                         depth--;
    1441            4 :                         if (depth == 0) {
    1442            4 :                                 *close = p;
    1443            4 :                                 return true;
    1444              :                         }
    1445              :                 }
    1446              :         }
    1447              : 
    1448            0 :         return false;
    1449              : }
    1450              : 
    1451            4 : static inline const char *lsp_phpdoc_attribute_array_literal_start(const char *attribute_start, const char *attribute_end, const char *shape_name)
    1452              : {
    1453              :         const char *match, *p;
    1454              :         size_t shape_name_length;
    1455              : 
    1456            4 :         shape_name_length = strlen(shape_name);
    1457            4 :         match = attribute_start;
    1458            4 :         while ((match = php_memnstr(match, shape_name, shape_name_length, (char *) attribute_end)) != NULL) {
    1459            4 :                 if ((match == attribute_start || !lsp_doc_is_identifier_char(match[-1])) &&
    1460            4 :                         (match + shape_name_length == attribute_end || !lsp_doc_is_identifier_char(match[shape_name_length]))
    1461              :                 ) {
    1462            4 :                         p = match + shape_name_length;
    1463            4 :                         while (p < attribute_end && isspace((unsigned char) *p)) {
    1464            0 :                                 p++;
    1465              :                         }
    1466            4 :                         if (p < attribute_end && *p == '(') {
    1467            4 :                                 p++;
    1468            4 :                                 while (p < attribute_end && *p != '[') {
    1469            0 :                                         p++;
    1470              :                                 }
    1471            4 :                                 if (p < attribute_end && *p == '[') {
    1472            4 :                                         return p;
    1473              :                                 }
    1474              :                         }
    1475              :                 }
    1476            0 :                 match += shape_name_length;
    1477              :         }
    1478              : 
    1479            0 :         return NULL;
    1480              : }
    1481              : 
    1482            4 : static inline zend_string *lsp_phpdoc_array_literal_shape_type(const char *literal_start, const char *literal_end, const char *shape_kind, bool normalize)
    1483              : {
    1484              :         const char *p, *key_start, *key_end, *value_start, *value_end;
    1485              :         zend_string *value_type;
    1486            4 :         smart_str shape = {0};
    1487              :         char quote;
    1488              :         bool appended;
    1489              : 
    1490            4 :         p = literal_start + 1;
    1491            4 :         appended = false;
    1492            4 :         smart_str_appends(&shape, shape_kind);
    1493            4 :         smart_str_appendc(&shape, '{');
    1494           12 :         while (p < literal_end) {
    1495           12 :                 while (p < literal_end && (isspace((unsigned char) *p) || *p == ',')) {
    1496            4 :                         p++;
    1497              :                 }
    1498            8 :                 if (p >= literal_end) {
    1499            0 :                         break;
    1500              :                 }
    1501              : 
    1502            8 :                 if (*p == '\'' || *p == '"') {
    1503            8 :                         quote = *p++;
    1504            8 :                         key_start = p;
    1505           52 :                         while (p < literal_end && *p != quote) {
    1506           44 :                                 p++;
    1507              :                         }
    1508            8 :                         key_end = p;
    1509            8 :                         if (p < literal_end) {
    1510            8 :                                 p++;
    1511              :                         }
    1512              :                 } else {
    1513            0 :                         key_start = p;
    1514            0 :                         while (p < literal_end && !isspace((unsigned char) *p) && *p != '=' && *p != ',' && *p != ']') {
    1515            0 :                                 p++;
    1516              :                         }
    1517            0 :                         key_end = p;
    1518              :                 }
    1519              : 
    1520           16 :                 while (p < literal_end && isspace((unsigned char) *p)) {
    1521            8 :                         p++;
    1522              :                 }
    1523            8 :                 if (p + 1 >= literal_end || p[0] != '=' || p[1] != '>') {
    1524            0 :                         p = lsp_phpdoc_shape_value_end(p, literal_end);
    1525            0 :                         if (p < literal_end && *p == ',') {
    1526            0 :                                 p++;
    1527              :                         }
    1528            0 :                         continue;
    1529              :                 }
    1530            8 :                 p += 2;
    1531           16 :                 while (p < literal_end && isspace((unsigned char) *p)) {
    1532            8 :                         p++;
    1533              :                 }
    1534              : 
    1535            8 :                 value_start = p;
    1536            8 :                 if (p < literal_end && (*p == '\'' || *p == '"')) {
    1537            8 :                         quote = *p++;
    1538            8 :                         value_start = p;
    1539          120 :                         while (p < literal_end && *p != quote) {
    1540          112 :                                 p++;
    1541              :                         }
    1542            8 :                         value_end = p;
    1543            8 :                         if (p < literal_end) {
    1544            8 :                                 p++;
    1545              :                         }
    1546              :                 } else {
    1547            0 :                         value_end = lsp_phpdoc_shape_value_end(value_start, literal_end);
    1548            0 :                         p = value_end;
    1549              :                 }
    1550              : 
    1551            8 :                 if (key_end > key_start && value_end > value_start) {
    1552            8 :                         if (appended) {
    1553            4 :                                 smart_str_appendl(&shape, ", ", sizeof(", ") - 1);
    1554              :                         }
    1555            8 :                         lsp_phpdoc_append_shape_key_union_part(&shape, key_start, key_end);
    1556            8 :                         smart_str_appendl(&shape, ": ", sizeof(": ") - 1);
    1557            8 :                         value_type = lsp_phpdoc_type_slice(value_start, value_end, normalize);
    1558            8 :                         smart_str_append(&shape, value_type);
    1559            8 :                         zend_string_release(value_type);
    1560            8 :                         appended = true;
    1561              :                 }
    1562              : 
    1563            8 :                 while (p < literal_end && *p != ',') {
    1564            0 :                         if (!isspace((unsigned char) *p)) {
    1565            0 :                                 break;
    1566              :                         }
    1567            0 :                         p++;
    1568              :                 }
    1569            8 :                 if (p < literal_end && *p == ',') {
    1570            4 :                         p++;
    1571              :                 }
    1572              :         }
    1573            4 :         smart_str_appendc(&shape, '}');
    1574            4 :         smart_str_0(&shape);
    1575              : 
    1576            4 :         if (!appended) {
    1577            0 :                 smart_str_free(&shape);
    1578            0 :                 return NULL;
    1579              :         }
    1580              : 
    1581            4 :         return shape.s;
    1582              : }
    1583              : 
    1584           36 : extern zend_string *lsp_phpdoc_attribute_shape_type_before(zend_string *text, size_t offset, bool normalize)
    1585              : {
    1586              :         const char *value, *scan_start, *scan_end, *attribute_start, *literal_start, *literal_end;
    1587              :         size_t start_offset;
    1588              :         zend_string *shape;
    1589              : 
    1590           36 :         start_offset = lsp_phpdoc_attribute_scan_start(text, offset);
    1591           36 :         value = ZSTR_VAL(text);
    1592           36 :         scan_start = value + start_offset;
    1593           36 :         scan_end = value + (offset > ZSTR_LEN(text) ? ZSTR_LEN(text) : offset);
    1594           36 :         attribute_start = scan_start;
    1595           36 :         while ((attribute_start = php_memnstr(attribute_start, "#[", sizeof("#[") - 1, (char *) scan_end)) != NULL) {
    1596            4 :                 literal_start = lsp_phpdoc_attribute_array_literal_start(attribute_start, scan_end, "ArrayShape");
    1597            4 :                 if (literal_start && lsp_phpdoc_matching_square_bracket(literal_start, scan_end, &literal_end)) {
    1598            4 :                         shape = lsp_phpdoc_array_literal_shape_type(literal_start, literal_end, "array", normalize);
    1599            4 :                         if (shape) {
    1600            4 :                                 return shape;
    1601              :                         }
    1602              :                 }
    1603              : 
    1604            0 :                 literal_start = lsp_phpdoc_attribute_array_literal_start(attribute_start, scan_end, "ObjectShape");
    1605            0 :                 if (literal_start && lsp_phpdoc_matching_square_bracket(literal_start, scan_end, &literal_end)) {
    1606            0 :                         shape = lsp_phpdoc_array_literal_shape_type(literal_start, literal_end, "object", normalize);
    1607            0 :                         if (shape) {
    1608            0 :                                 return shape;
    1609              :                         }
    1610              :                 }
    1611              : 
    1612            0 :                 attribute_start += sizeof("#[") - 1;
    1613              :         }
    1614              : 
    1615           32 :         return NULL;
    1616              : }
    1617              : 
    1618          164 : extern void lsp_phpdoc_add_variable_type_completions(lsp_server *server, zval *items, zend_string *text, zend_string *prefix)
    1619              : {
    1620          164 :         const char *cursor = ZSTR_VAL(text), *end = ZSTR_VAL(text) + ZSTR_LEN(text), *source = lsp_primary_analyzer_source(server);
    1621              :         zend_string *variable, *type;
    1622              : 
    1623          196 :         while (lsp_doc_find_next_type(&cursor, end, &variable, &type)) {
    1624           32 :                 if (lsp_matches_prefix_string(variable, prefix)) {
    1625            4 :                         lsp_add_completion_item_ex(items, variable, 6, type, source);
    1626              :                 }
    1627              : 
    1628           32 :                 zend_string_release(variable);
    1629           32 :                 zend_string_release(type);
    1630              :         }
    1631          164 : }
    1632              : 
    1633           16 : extern void lsp_phpdoc_add_variable_type_completion_edits(lsp_server *server, zval *items, zend_string *text, zend_string *prefix, size_t prefix_start, size_t offset)
    1634              : {
    1635           16 :         const char *cursor = ZSTR_VAL(text), *end = ZSTR_VAL(text) + ZSTR_LEN(text), *source = lsp_primary_analyzer_source(server);
    1636              :         zend_string *variable, *type, *detail;
    1637              : 
    1638           32 :         while (lsp_doc_find_next_type(&cursor, end, &variable, &type)) {
    1639           16 :                 if (lsp_matches_prefix_string(variable, prefix)) {
    1640            8 :                         detail = strpprintf(0, "variable %s: %s", ZSTR_VAL(variable), ZSTR_VAL(type));
    1641            8 :                         lsp_add_variable_completion_item_ex(items, variable, detail, source, text, prefix_start, offset);
    1642            8 :                         zend_string_release(detail);
    1643              :                 }
    1644              : 
    1645           16 :                 zend_string_release(variable);
    1646           16 :                 zend_string_release(type);
    1647              :         }
    1648           16 : }
    1649              : 
    1650          836 : static inline zend_string *lsp_phpdoc_type_for_word_between(zend_string *text, const char *cursor_start, const char *cursor_end, zend_string *word, bool normalize)
    1651              : {
    1652          836 :         const char *word_value = ZSTR_VAL(word), *cursor = cursor_start, *end = cursor_end;
    1653              :         zend_string *variable, *type, *resolved;
    1654          836 :         size_t word_length = ZSTR_LEN(word);
    1655              :         bool match;
    1656              : 
    1657         1036 :         while (lsp_doc_find_next_type_ex(&cursor, end, &variable, &type, normalize)) {
    1658          284 :                 match = ZSTR_LEN(variable) == word_length && strncasecmp(ZSTR_VAL(variable), word_value, word_length) == 0;
    1659          284 :                 if (!match && word_length > 0 && word_value[0] != '$') {
    1660           16 :                         match = ZSTR_LEN(variable) == word_length + 1 && ZSTR_VAL(variable)[0] == '$' &&
    1661            4 :                                 strncasecmp(ZSTR_VAL(variable) + 1, word_value, word_length) == 0
    1662              :                         ;
    1663              :                 }
    1664              : 
    1665          284 :                 zend_string_release(variable);
    1666              : 
    1667          284 :                 if (match) {
    1668           84 :                         resolved = lsp_phpdoc_resolve_type_text(text, type, normalize);
    1669           84 :                         if (resolved) {
    1670           20 :                                 zend_string_release(type);
    1671           20 :                                 return resolved;
    1672              :                         }
    1673           64 :                         return type;
    1674              :                 }
    1675              : 
    1676          200 :                 zend_string_release(type);
    1677              :         }
    1678              : 
    1679          752 :         return NULL;
    1680              : }
    1681              : 
    1682          588 : extern zend_string *lsp_phpdoc_type_for_word(zend_string *text, zend_string *word)
    1683              : {
    1684          588 :         return lsp_phpdoc_type_for_word_between(text, ZSTR_VAL(text), ZSTR_VAL(text) + ZSTR_LEN(text), word, true);
    1685              : }
    1686              : 
    1687           20 : extern zend_string *lsp_phpdoc_type_for_word_range(zend_string *text, size_t start_offset, size_t end_offset, zend_string *word)
    1688              : {
    1689              :         const char *value;
    1690              : 
    1691           20 :         if (start_offset > ZSTR_LEN(text)) {
    1692            0 :                 start_offset = ZSTR_LEN(text);
    1693              :         }
    1694              : 
    1695           20 :         if (end_offset > ZSTR_LEN(text)) {
    1696            0 :                 end_offset = ZSTR_LEN(text);
    1697              :         }
    1698              : 
    1699           20 :         if (end_offset < start_offset) {
    1700            0 :                 end_offset = start_offset;
    1701              :         }
    1702              : 
    1703           20 :         value = ZSTR_VAL(text);
    1704              : 
    1705           20 :         return lsp_phpdoc_type_for_word_between(text, value + start_offset, value + end_offset, word, true);
    1706              : }
    1707              : 
    1708          228 : extern zend_string *lsp_phpdoc_type_for_word_raw(zend_string *text, zend_string *word)
    1709              : {
    1710          228 :         return lsp_phpdoc_type_for_word_between(text, ZSTR_VAL(text), ZSTR_VAL(text) + ZSTR_LEN(text), word, false);
    1711              : }
    1712              : 
    1713          544 : extern zend_string *lsp_phpdoc_property_type_for_word(zend_string *text, zend_string *word, size_t offset)
    1714              : {
    1715          544 :         return lsp_phpdoc_property_type_for_word_ex(text, word, offset, true);
    1716              : }
    1717              : 
    1718          212 : extern zend_string *lsp_phpdoc_property_type_for_word_raw(zend_string *text, zend_string *word, size_t offset)
    1719              : {
    1720          212 :         return lsp_phpdoc_property_type_for_word_ex(text, word, offset, false);
    1721              : }
    1722              : 
    1723          312 : static inline zend_string *lsp_phpdoc_return_type_from_comment_ex(zend_string *comment, bool normalize)
    1724              : {
    1725          312 :         const char *tags[] = {"@return", "@phpstan-return", "@psalm-return"};
    1726              :         const char *cursor, *end, *p, *start, *finish, *match, *best;
    1727              :         size_t i, tag_length;
    1728              : 
    1729          312 :         cursor = ZSTR_VAL(comment);
    1730          312 :         end = cursor + ZSTR_LEN(comment);
    1731          312 :         best = NULL;
    1732          312 :         tag_length = 0;
    1733         1248 :         for (i = 0; i < sizeof(tags) / sizeof(tags[0]); i++) {
    1734          936 :                 match = strstr(cursor, tags[i]);
    1735          936 :                 if (!match || match >= end) {
    1736          664 :                         continue;
    1737              :                 }
    1738          272 :                 if (!best || match < best) {
    1739          272 :                         best = match;
    1740          272 :                         tag_length = strlen(tags[i]);
    1741              :                 }
    1742              :         }
    1743          312 :         if (!best) {
    1744           40 :                 return NULL;
    1745              :         }
    1746              : 
    1747          272 :         p = best + tag_length;
    1748          544 :         while (p < end && isspace((unsigned char) *p)) {
    1749          272 :                 p++;
    1750              :         }
    1751              : 
    1752          272 :         start = p;
    1753          272 :         finish = lsp_phpdoc_type_expression_end(start, end);
    1754          272 :         while (finish > start && isspace((unsigned char) finish[-1])) {
    1755            0 :                 finish--;
    1756              :         }
    1757              : 
    1758          272 :         if (finish <= start) {
    1759            0 :                 return NULL;
    1760              :         }
    1761              : 
    1762          272 :         return lsp_phpdoc_type_slice(start, finish, normalize);
    1763              : }
    1764              : 
    1765          200 : extern zend_string *lsp_phpdoc_return_type_from_comment(zend_string *comment)
    1766              : {
    1767          200 :         return lsp_phpdoc_return_type_from_comment_ex(comment, true);
    1768              : }
    1769              : 
    1770          112 : extern zend_string *lsp_phpdoc_return_type_from_comment_raw(zend_string *comment)
    1771              : {
    1772          112 :         return lsp_phpdoc_return_type_from_comment_ex(comment, false);
    1773              : }
    1774              : 
    1775           36 : extern bool lsp_phpdoc_type_has_array_shape(zend_string *type)
    1776              : {
    1777              :         zend_string *shape;
    1778              : 
    1779           36 :         shape = lsp_phpdoc_array_shape_from_type(type);
    1780           36 :         if (!shape) {
    1781           32 :                 return false;
    1782              :         }
    1783              : 
    1784            4 :         zend_string_release(shape);
    1785              : 
    1786            4 :         return true;
    1787              : }
    1788              : 
    1789           12 : extern zend_string *lsp_phpdoc_array_shape_key_type(zend_string *type, zend_string *key)
    1790              : {
    1791              :         const char *p, *end, *key_start, *key_end, *value_start, *value_end;
    1792              :         zend_string *shape, *key_type;
    1793              :         char quote;
    1794              : 
    1795           12 :         shape = lsp_phpdoc_array_shape_from_type(type);
    1796           12 :         if (!shape) {
    1797            4 :                 return NULL;
    1798              :         }
    1799              : 
    1800            8 :         p = ZSTR_VAL(shape);
    1801            8 :         end = p + ZSTR_LEN(shape);
    1802            8 :         while (p < end) {
    1803            8 :                 while (p < end && (isspace((unsigned char) *p) || *p == ',')) {
    1804            0 :                         p++;
    1805              :                 }
    1806              : 
    1807            8 :                 if (p >= end) {
    1808            0 :                         break;
    1809              :                 }
    1810              : 
    1811            8 :                 if (*p == '\'' || *p == '"') {
    1812            0 :                         quote = *p++;
    1813            0 :                         key_start = p;
    1814            0 :                         while (p < end && *p != quote) {
    1815            0 :                                 p++;
    1816              :                         }
    1817            0 :                         key_end = p;
    1818            0 :                         if (p < end) {
    1819            0 :                                 p++;
    1820              :                         }
    1821              :                 } else {
    1822            8 :                         key_start = p;
    1823           32 :                         while (p < end && !isspace((unsigned char) *p) && *p != ':' && *p != ',' && *p != '?') {
    1824           24 :                                 p++;
    1825              :                         }
    1826            8 :                         key_end = p;
    1827              :                 }
    1828              : 
    1829            8 :                 while (p < end && isspace((unsigned char) *p)) {
    1830            0 :                         p++;
    1831              :                 }
    1832              : 
    1833            8 :                 if (p < end && *p == '?') {
    1834            0 :                         p++;
    1835            0 :                         while (p < end && isspace((unsigned char) *p)) {
    1836            0 :                                 p++;
    1837              :                         }
    1838              :                 }
    1839              : 
    1840            8 :                 if (p >= end || *p != ':') {
    1841            0 :                         p = lsp_phpdoc_shape_value_end(p, end);
    1842            0 :                         if (p < end && *p == ',') {
    1843            0 :                                 p++;
    1844              :                         }
    1845            0 :                         continue;
    1846              :                 }
    1847              : 
    1848            8 :                 p++;
    1849            8 :                 value_start = p;
    1850            8 :                 value_end = lsp_phpdoc_shape_value_end(value_start, end);
    1851            8 :                 if (
    1852            8 :                         key_end > key_start &&
    1853            8 :                         ZSTR_LEN(key) == (size_t) (key_end - key_start) &&
    1854            8 :                         memcmp(ZSTR_VAL(key), key_start, ZSTR_LEN(key)) == 0
    1855              :                 ) {
    1856            8 :                         key_type = lsp_phpdoc_type_slice(value_start, value_end, true);
    1857            8 :                         zend_string_release(shape);
    1858              : 
    1859            8 :                         return key_type;
    1860              :                 }
    1861              : 
    1862            0 :                 p = value_end;
    1863            0 :                 if (p < end && *p == ',') {
    1864            0 :                         p++;
    1865              :                 }
    1866              :         }
    1867              : 
    1868            0 :         zend_string_release(shape);
    1869              : 
    1870            0 :         return NULL;
    1871              : }
    1872              : 
    1873          164 : extern void lsp_phpdoc_add_template_completions(lsp_server *server, zval *items, zend_string *text, zend_string *prefix)
    1874              : {
    1875          164 :         const char *cursor = ZSTR_VAL(text), *end = ZSTR_VAL(text) + ZSTR_LEN(text), *source = lsp_primary_analyzer_source(server), *p, *start, *finish;
    1876              :         zend_string *label, *detail;
    1877              : 
    1878          176 :         while ((cursor = strstr(cursor, "@template")) != NULL && cursor < end) {
    1879           12 :                 p = cursor + sizeof("@template") - 1;
    1880              : 
    1881           12 :                 if (p < end && *p == '-') {
    1882            0 :                         p++;
    1883            0 :                         while (p < end && (islower((unsigned char) *p) || *p == '-')) {
    1884            0 :                                 p++;
    1885              :                         }
    1886              :                 }
    1887              : 
    1888           24 :                 while (p < end && isspace((unsigned char) *p)) {
    1889           12 :                         p++;
    1890              :                 }
    1891              : 
    1892           12 :                 if (p >= end || !lsp_doc_is_identifier_start(*p)) {
    1893            0 :                         cursor = p;
    1894            0 :                         continue;
    1895              :                 }
    1896              : 
    1897           12 :                 start = p++;
    1898           60 :                 while (p < end && lsp_doc_is_identifier_char(*p)) {
    1899           48 :                         p++;
    1900              :                 }
    1901           12 :                 finish = p;
    1902              : 
    1903           12 :                 label = zend_string_init(start, finish - start, 0);
    1904           12 :                 if (lsp_matches_prefix_string(label, prefix)) {
    1905            4 :                         detail = zend_string_init("template type", sizeof("template type") - 1, 0);
    1906            4 :                         lsp_add_completion_item_ex(items, label, 25, detail, source);
    1907            4 :                         zend_string_release(detail);
    1908              :                 }
    1909              : 
    1910           12 :                 zend_string_release(label);
    1911           12 :                 cursor = p;
    1912              :         }
    1913          164 : }
    1914              : 
    1915          400 : static inline bool lsp_array_access_at(zend_string *text, size_t offset, zend_string **variable, zend_string **key_prefix, size_t *edit_start, size_t *edit_end, char *quote, bool *has_opening_quote)
    1916              : {
    1917              :         const char *value;
    1918              :         size_t i, j, key_start, bracket, var_end, var_start;
    1919              :         char opening_quote;
    1920              : 
    1921          400 :         *variable = NULL;
    1922          400 :         *key_prefix = NULL;
    1923          400 :         *edit_start = 0;
    1924          400 :         *edit_end = offset;
    1925          400 :         *quote = '\'';
    1926          400 :         *has_opening_quote = false;
    1927              : 
    1928          400 :         if (offset > ZSTR_LEN(text)) {
    1929            0 :                 offset = ZSTR_LEN(text);
    1930              :         }
    1931              : 
    1932          400 :         value = ZSTR_VAL(text);
    1933          400 :         i = offset;
    1934         2324 :         while (i > 0 && (isalnum((unsigned char) value[i - 1]) || value[i - 1] == '_' || value[i - 1] == '-')) {
    1935         1924 :                 i--;
    1936              :         }
    1937              : 
    1938          400 :         key_start = i;
    1939          400 :         opening_quote = '\0';
    1940          400 :         if (i > 0 && (value[i - 1] == '\'' || value[i - 1] == '"')) {
    1941           28 :                 opening_quote = value[i - 1];
    1942           28 :                 i--;
    1943              :         }
    1944              : 
    1945         1072 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    1946          672 :                 i--;
    1947              :         }
    1948              : 
    1949          400 :         if (i == 0 || value[i - 1] != '[') {
    1950          368 :                 return false;
    1951              :         }
    1952           32 :         bracket = i - 1;
    1953              : 
    1954           32 :         i = bracket;
    1955           32 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    1956            0 :                 i--;
    1957              :         }
    1958              : 
    1959           32 :         var_end = i;
    1960          240 :         while (i > 0 && lsp_doc_is_identifier_char(value[i - 1])) {
    1961          208 :                 i--;
    1962              :         }
    1963              : 
    1964           32 :         if (i == 0 || value[i - 1] != '$') {
    1965            0 :                 return false;
    1966              :         }
    1967              : 
    1968           32 :         var_start = i - 1;
    1969              : 
    1970           32 :         *variable = zend_string_init(value + var_start, var_end - var_start, 0);
    1971           32 :         *key_prefix = zend_string_init(value + key_start, offset - key_start, 0);
    1972           32 :         *edit_start = key_start;
    1973           32 :         *quote = opening_quote ? opening_quote : '\'';
    1974           32 :         *has_opening_quote = opening_quote != '\0';
    1975              : 
    1976           32 :         j = offset;
    1977           32 :         if (opening_quote) {
    1978           28 :                 if (j < ZSTR_LEN(text) && value[j] == opening_quote) {
    1979            8 :                         j++;
    1980            8 :                         while (j < ZSTR_LEN(text) && isspace((unsigned char) value[j])) {
    1981            0 :                                 j++;
    1982              :                         }
    1983            8 :                         if (j < ZSTR_LEN(text) && value[j] == ']') {
    1984            8 :                                 j++;
    1985              :                         }
    1986              :                 }
    1987              :         } else {
    1988           24 :                 while (j < ZSTR_LEN(text) && isspace((unsigned char) value[j])) {
    1989           20 :                         j++;
    1990              :                 }
    1991            4 :                 if (j < ZSTR_LEN(text) && value[j] == ']') {
    1992            0 :                         j++;
    1993              :                 }
    1994              :         }
    1995              : 
    1996           32 :         *edit_end = j;
    1997              : 
    1998           32 :         return true;
    1999              : }
    2000              : 
    2001           40 : static inline zend_string *lsp_array_shape_key_new_text(zend_string *label, char quote, bool has_opening_quote)
    2002              : {
    2003           40 :         smart_str text = {0};
    2004              :         size_t i;
    2005              :         char c;
    2006              : 
    2007           40 :         if (!has_opening_quote) {
    2008           12 :                 smart_str_appendc(&text, quote);
    2009              :         }
    2010              : 
    2011          264 :         for (i = 0; i < ZSTR_LEN(label); i++) {
    2012          224 :                 c = ZSTR_VAL(label)[i];
    2013          224 :                 if (c == quote || c == '\\') {
    2014            0 :                         smart_str_appendc(&text, '\\');
    2015              :                 }
    2016          224 :                 smart_str_appendc(&text, c);
    2017              :         }
    2018              : 
    2019           40 :         smart_str_appendc(&text, quote);
    2020           40 :         smart_str_appendc(&text, ']');
    2021           40 :         smart_str_0(&text);
    2022              : 
    2023           40 :         return text.s;
    2024              : }
    2025              : 
    2026           40 : static inline void lsp_add_array_shape_key_completion_item(zval *items, zend_string *text, size_t edit_start, size_t edit_end, zend_string *label, zend_string *detail, const char *source, char quote, bool has_opening_quote)
    2027              : {
    2028              :         zend_string *new_text;
    2029              :         zval item, data, text_edit, range;
    2030              : 
    2031           40 :         new_text = lsp_array_shape_key_new_text(label, quote, has_opening_quote);
    2032              : 
    2033           40 :         array_init(&item);
    2034           40 :         add_assoc_str(&item, "label", zend_string_copy(label));
    2035           40 :         add_assoc_long(&item, "kind", 10);
    2036           40 :         add_assoc_str(&item, "detail", zend_string_copy(detail));
    2037           40 :         add_assoc_str(&item, "filterText", zend_string_copy(label));
    2038           40 :         array_init(&text_edit);
    2039           40 :         lsp_range_from_offsets(text, edit_start, edit_end, &range);
    2040           40 :         add_assoc_zval(&text_edit, "range", &range);
    2041           40 :         add_assoc_str(&text_edit, "newText", new_text);
    2042           40 :         add_assoc_zval(&item, "textEdit", &text_edit);
    2043           40 :         array_init(&data);
    2044           40 :         add_assoc_string(&data, "source", source);
    2045           40 :         add_assoc_zval(&item, "data", &data);
    2046           40 :         add_next_index_zval(items, &item);
    2047           40 : }
    2048              : 
    2049           32 : static inline bool lsp_phpdoc_shape_for_variable(zend_string *text, zend_string *variable, zend_string **shape)
    2050              : {
    2051           32 :         const char *cursor = ZSTR_VAL(text), *end = ZSTR_VAL(text) + ZSTR_LEN(text);
    2052              :         zend_string *doc_variable, *type, *resolved_type, *extracted_shape;
    2053              : 
    2054           32 :         *shape = NULL;
    2055              : 
    2056           48 :         while (lsp_doc_find_next_type(&cursor, end, &doc_variable, &type)) {
    2057           36 :                 if (zend_string_equals(doc_variable, variable)) {
    2058           20 :                         resolved_type = lsp_phpdoc_resolve_type_text(text, type, true);
    2059           20 :                         extracted_shape = lsp_phpdoc_array_shape_from_type(resolved_type ? resolved_type : type);
    2060           20 :                         if (extracted_shape) {
    2061           20 :                                 *shape = extracted_shape;
    2062           20 :                                 zend_string_release(doc_variable);
    2063           20 :                                 if (resolved_type) {
    2064            8 :                                         zend_string_release(resolved_type);
    2065              :                                 }
    2066           20 :                                 zend_string_release(type);
    2067              : 
    2068           20 :                                 return true;
    2069              :                         }
    2070            0 :                         if (resolved_type) {
    2071            0 :                                 zend_string_release(resolved_type);
    2072              :                         }
    2073              :                 }
    2074              : 
    2075           16 :                 zend_string_release(doc_variable);
    2076           16 :                 zend_string_release(type);
    2077              :         }
    2078              : 
    2079           12 :         return false;
    2080              : }
    2081              : 
    2082          400 : extern bool lsp_phpdoc_add_array_shape_completions(lsp_server *server, zval *items, lsp_document *document, size_t offset)
    2083              : {
    2084          400 :         const char *source = lsp_primary_analyzer_source(server), *p, *end, *key_start, *key_end, *value_start, *value_end;
    2085              :         zend_string *variable, *key_prefix, *shape, *label, *detail, *inferred_type;
    2086              :         size_t edit_start, edit_end;
    2087              :         char quote, access_quote;
    2088              :         bool has_opening_quote;
    2089              : 
    2090          400 :         if (!lsp_array_access_at(document->text, offset, &variable, &key_prefix, &edit_start, &edit_end, &access_quote, &has_opening_quote)) {
    2091          368 :                 return false;
    2092              :         }
    2093              : 
    2094           32 :         shape = NULL;
    2095           32 :         inferred_type = NULL;
    2096           32 :         if (!lsp_phpdoc_shape_for_variable(document->text, variable, &shape)) {
    2097           12 :                 inferred_type = lsp_infer_variable_type(server, document, variable, offset);
    2098           12 :                 if (inferred_type) {
    2099           12 :                         shape = lsp_phpdoc_array_shape_from_type(inferred_type);
    2100           12 :                         zend_string_release(inferred_type);
    2101              :                 }
    2102              :         }
    2103              : 
    2104           32 :         if (!shape) {
    2105            0 :                 zend_string_release(variable);
    2106            0 :                 zend_string_release(key_prefix);
    2107            0 :                 return false;
    2108              :         }
    2109              : 
    2110           32 :         p = ZSTR_VAL(shape);
    2111           32 :         end = p + ZSTR_LEN(shape);
    2112          104 :         while (p < end) {
    2113          112 :                 while (p < end && (isspace((unsigned char) *p) || *p == ',')) {
    2114           40 :                         p++;
    2115              :                 }
    2116              : 
    2117           72 :                 if (p >= end) {
    2118            0 :                         break;
    2119              :                 }
    2120              : 
    2121           72 :                 if (*p == '\'' || *p == '"') {
    2122            8 :                         quote = *p++;
    2123              : 
    2124            8 :                         key_start = p;
    2125           84 :                         while (p < end && *p != quote) {
    2126           76 :                                 p++;
    2127              :                         }
    2128            8 :                         key_end = p;
    2129              : 
    2130            8 :                         if (p < end) {
    2131            8 :                                 p++;
    2132              :                         }
    2133              :                 } else {
    2134           64 :                         key_start = p;
    2135          396 :                         while (p < end && !isspace((unsigned char) *p) && *p != ':' && *p != ',' && *p != '?') {
    2136          332 :                                 p++;
    2137              :                         }
    2138           64 :                         key_end = p;
    2139              :                 }
    2140              : 
    2141           72 :                 while (p < end && isspace((unsigned char) *p)) {
    2142            0 :                         p++;
    2143              :                 }
    2144              : 
    2145           72 :                 if (p < end && *p == '?') {
    2146            0 :                         p++;
    2147            0 :                         while (p < end && isspace((unsigned char) *p)) {
    2148            0 :                                 p++;
    2149              :                         }
    2150              :                 }
    2151              : 
    2152           72 :                 if (p >= end || *p != ':') {
    2153            0 :                         p = lsp_phpdoc_shape_value_end(p, end);
    2154            0 :                         if (p < end && *p == ',') {
    2155            0 :                                 p++;
    2156              :                         }
    2157            0 :                         continue;
    2158              :                 }
    2159              : 
    2160           72 :                 if (p < end && *p == ':') {
    2161           72 :                         p++;
    2162              :                 }
    2163              : 
    2164           72 :                 value_start = p;
    2165           72 :                 value_end = lsp_phpdoc_shape_value_end(value_start, end);
    2166           72 :                 if (key_end <= key_start) {
    2167            0 :                         p = value_end;
    2168            0 :                         if (p < end && *p == ',') {
    2169            0 :                                 p++;
    2170              :                         }
    2171            0 :                         continue;
    2172              :                 }
    2173              : 
    2174           72 :                 label = zend_string_init(key_start, key_end - key_start, 0);
    2175           72 :                 if (lsp_matches_prefix_string(label, key_prefix)) {
    2176           40 :                         detail = zend_string_init("array-shape key", sizeof("array-shape key") - 1, 0);
    2177           40 :                         lsp_add_array_shape_key_completion_item(items, document->text, edit_start, edit_end, label, detail, source, access_quote, has_opening_quote);
    2178           40 :                         zend_string_release(detail);
    2179              :                 }
    2180              : 
    2181           72 :                 zend_string_release(label);
    2182           72 :                 p = value_end;
    2183           72 :                 if (p < end && *p == ',') {
    2184           40 :                         p++;
    2185              :                 }
    2186              :         }
    2187              : 
    2188           32 :         zend_string_release(shape);
    2189           32 :         zend_string_release(variable);
    2190           32 :         zend_string_release(key_prefix);
    2191              : 
    2192           32 :         return true;
    2193              : }
    2194              : 
    2195         1156 : extern bool lsp_member_access_arrow(zend_string *text, size_t offset, zend_string *prefix, size_t *arrow_start, size_t *receiver_end)
    2196              : {
    2197         1156 :         const char *value = ZSTR_VAL(text);
    2198              :         size_t i, start;
    2199              : 
    2200         1156 :         if (offset > ZSTR_LEN(text)) {
    2201            0 :                 offset = ZSTR_LEN(text);
    2202              :         }
    2203              : 
    2204         1156 :         i = offset >= ZSTR_LEN(prefix) ? offset - ZSTR_LEN(prefix) : 0;
    2205         1184 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    2206           28 :                 i--;
    2207              :         }
    2208              : 
    2209         1156 :         if (i >= 2 && value[i - 2] == '-' && value[i - 1] == '>') {
    2210          876 :                 start = i - 2;
    2211          280 :         } else if (i >= 3 && value[i - 3] == '?' && value[i - 2] == '-' && value[i - 1] == '>') {
    2212            0 :                 start = i - 3;
    2213              :         } else {
    2214          280 :                 return false;
    2215              :         }
    2216              : 
    2217          876 :         if (arrow_start) {
    2218            0 :                 *arrow_start = start;
    2219              :         }
    2220              : 
    2221          876 :         i = start;
    2222          876 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    2223            0 :                 i--;
    2224              :         }
    2225              : 
    2226          876 :         *receiver_end = i;
    2227              : 
    2228          876 :         return true;
    2229              : }
    2230              : 
    2231          780 : extern bool lsp_member_access_context(zend_string *text, size_t offset, zend_string *prefix, zend_string **receiver, zend_string **member_prefix)
    2232              : {
    2233          780 :         const char *value = ZSTR_VAL(text);
    2234              :         size_t i, receiver_end, receiver_start;
    2235              : 
    2236          780 :         *receiver = NULL;
    2237          780 :         *member_prefix = NULL;
    2238              : 
    2239          780 :         if (!lsp_member_access_arrow(text, offset, prefix, NULL, &receiver_end)) {
    2240           80 :                 return false;
    2241              :         }
    2242              : 
    2243          700 :         i = receiver_end;
    2244         3012 :         while (i > 0 && lsp_doc_is_identifier_char(value[i - 1])) {
    2245         2312 :                 i--;
    2246              :         }
    2247              : 
    2248          700 :         if (i == 0 || value[i - 1] != '$') {
    2249          180 :                 return false;
    2250              :         }
    2251              : 
    2252          520 :         receiver_start = i - 1;
    2253              : 
    2254          520 :         *receiver = zend_string_init(value + receiver_start, receiver_end - receiver_start, 0);
    2255          520 :         *member_prefix = zend_string_copy(prefix);
    2256              : 
    2257          520 :         return true;
    2258              : }
    2259              : 
    2260          156 : static inline zend_string *lsp_phpdoc_object_shape_for_receiver(lsp_server *server, lsp_document *document, zend_string *receiver, size_t offset)
    2261              : {
    2262              :         zend_string *type, *shape;
    2263              : 
    2264          156 :         type = lsp_phpdoc_type_for_word_raw(document->text, receiver);
    2265          156 :         if (!type) {
    2266          152 :                 type = lsp_phpdoc_property_type_for_word_raw(document->text, receiver, offset);
    2267              :         }
    2268          156 :         if (!type) {
    2269          152 :                 type = lsp_infer_variable_type(server, document, receiver, offset);
    2270              :         }
    2271          156 :         if (!type) {
    2272           56 :                 return NULL;
    2273              :         }
    2274              : 
    2275          100 :         shape = lsp_phpdoc_object_shape_from_type(type);
    2276          100 :         zend_string_release(type);
    2277              : 
    2278          100 :         return shape;
    2279              : }
    2280              : 
    2281            4 : static inline void lsp_phpdoc_add_object_shape_item(zval *items, zend_string *label, zend_string *type, const char *source)
    2282              : {
    2283              :         zend_string *detail;
    2284              : 
    2285            4 :         detail = strpprintf(0, "object-shape property: %s", ZSTR_VAL(type));
    2286            4 :         lsp_add_completion_item_ex(items, label, 10, detail, source);
    2287            4 :         zend_string_release(detail);
    2288            4 : }
    2289              : 
    2290          212 : extern bool lsp_phpdoc_add_object_shape_member_completions(lsp_server *server, zval *items, lsp_document *document, size_t offset, zend_string *prefix)
    2291              : {
    2292              :         const char *source, *p, *end, *key_start, *key_end, *value_start, *value_end;
    2293              :         zend_string *receiver, *member_prefix, *shape, *label, *type;
    2294              :         char quote;
    2295              :         bool added;
    2296              : 
    2297          212 :         if (!lsp_member_access_context(document->text, offset, prefix, &receiver, &member_prefix)) {
    2298           56 :                 return false;
    2299              :         }
    2300              : 
    2301          156 :         source = lsp_primary_analyzer_source(server);
    2302          156 :         shape = lsp_phpdoc_object_shape_for_receiver(server, document, receiver, offset);
    2303          156 :         zend_string_release(receiver);
    2304          156 :         if (!shape) {
    2305          152 :                 zend_string_release(member_prefix);
    2306          152 :                 return false;
    2307              :         }
    2308              : 
    2309            4 :         added = false;
    2310            4 :         p = ZSTR_VAL(shape);
    2311            4 :         end = p + ZSTR_LEN(shape);
    2312           12 :         while (p < end) {
    2313           12 :                 while (p < end && (isspace((unsigned char) *p) || *p == ',')) {
    2314            4 :                         p++;
    2315              :                 }
    2316            8 :                 if (p >= end) {
    2317            0 :                         break;
    2318              :                 }
    2319              : 
    2320            8 :                 if (*p == '\'' || *p == '"') {
    2321            0 :                         quote = *p++;
    2322            0 :                         key_start = p;
    2323            0 :                         while (p < end && *p != quote) {
    2324            0 :                                 p++;
    2325              :                         }
    2326            0 :                         key_end = p;
    2327            0 :                         if (p < end) {
    2328            0 :                                 p++;
    2329              :                         }
    2330              :                 } else {
    2331            8 :                         key_start = p;
    2332           44 :                         while (p < end && !isspace((unsigned char) *p) && *p != ':' && *p != ',' && *p != '?') {
    2333           36 :                                 p++;
    2334              :                         }
    2335            8 :                         key_end = p;
    2336              :                 }
    2337              : 
    2338            8 :                 while (p < end && isspace((unsigned char) *p)) {
    2339            0 :                         p++;
    2340              :                 }
    2341            8 :                 if (p < end && *p == '?') {
    2342            0 :                         p++;
    2343            0 :                         while (p < end && isspace((unsigned char) *p)) {
    2344            0 :                                 p++;
    2345              :                         }
    2346              :                 }
    2347            8 :                 if (p >= end || *p != ':') {
    2348            0 :                         p = lsp_phpdoc_shape_value_end(p, end);
    2349            0 :                         if (p < end && *p == ',') {
    2350            0 :                                 p++;
    2351              :                         }
    2352            0 :                         continue;
    2353              :                 }
    2354              : 
    2355            8 :                 p++;
    2356           16 :                 while (p < end && isspace((unsigned char) *p)) {
    2357            8 :                         p++;
    2358              :                 }
    2359            8 :                 value_start = p;
    2360            8 :                 value_end = lsp_phpdoc_shape_value_end(value_start, end);
    2361            8 :                 if (key_end > key_start) {
    2362            8 :                         label = zend_string_init(key_start, key_end - key_start, 0);
    2363            8 :                         if (lsp_matches_prefix_string(label, member_prefix)) {
    2364            4 :                                 type = lsp_phpdoc_type_slice(value_start, value_end, false);
    2365            4 :                                 lsp_phpdoc_add_object_shape_item(items, label, type, source);
    2366            4 :                                 zend_string_release(type);
    2367            4 :                                 added = true;
    2368              :                         }
    2369            8 :                         zend_string_release(label);
    2370              :                 }
    2371              : 
    2372            8 :                 p = value_end;
    2373            8 :                 if (p < end && *p == ',') {
    2374            4 :                         p++;
    2375              :                 }
    2376              :         }
    2377              : 
    2378            4 :         zend_string_release(shape);
    2379            4 :         zend_string_release(member_prefix);
    2380              : 
    2381            4 :         return added;
    2382              : }
    2383              : 
    2384          168 : extern bool lsp_find_matching_open_paren(zend_string *text, size_t close_offset, size_t *open_offset)
    2385              : {
    2386          168 :         const char *value = ZSTR_VAL(text);
    2387          168 :         size_t i, depth = 0;
    2388              : 
    2389          168 :         if (close_offset >= ZSTR_LEN(text) || value[close_offset] != ')') {
    2390            0 :                 return false;
    2391              :         }
    2392              : 
    2393          336 :         for (i = close_offset + 1; i > 0; i--) {
    2394          336 :                 if (value[i - 1] == ')') {
    2395          168 :                         depth++;
    2396          168 :                 } else if (value[i - 1] == '(') {
    2397          168 :                         if (depth == 0) {
    2398            0 :                                 return false;
    2399              :                         }
    2400              : 
    2401          168 :                         depth--;
    2402              : 
    2403          168 :                         if (depth == 0) {
    2404          168 :                                 *open_offset = i - 1;
    2405              : 
    2406          168 :                                 return true;
    2407              :                         }
    2408              :                 }
    2409              :         }
    2410              : 
    2411            0 :         return false;
    2412              : }
    2413              : 
    2414           80 : extern bool lsp_find_matching_open_bracket(zend_string *text, size_t close_offset, size_t *open_offset)
    2415              : {
    2416           80 :         const char *value = ZSTR_VAL(text);
    2417           80 :         size_t i, depth = 0;
    2418              : 
    2419           80 :         if (close_offset >= ZSTR_LEN(text) || value[close_offset] != ']') {
    2420            0 :                 return false;
    2421              :         }
    2422              : 
    2423          272 :         for (i = close_offset + 1; i > 0; i--) {
    2424          272 :                 if (value[i - 1] == ']') {
    2425           80 :                         depth++;
    2426          192 :                 } else if (value[i - 1] == '[') {
    2427           80 :                         if (depth == 0) {
    2428            0 :                                 return false;
    2429              :                         }
    2430              : 
    2431           80 :                         depth--;
    2432              : 
    2433           80 :                         if (depth == 0) {
    2434           80 :                                 *open_offset = i - 1;
    2435              : 
    2436           80 :                                 return true;
    2437              :                         }
    2438              :                 }
    2439              :         }
    2440              : 
    2441            0 :         return false;
    2442              : }
    2443              : 
    2444          176 : extern bool lsp_parse_method_call_before_offset(zend_string *text, size_t offset, zend_string **method_name, size_t *receiver_end)
    2445              : {
    2446          176 :         const char *value = ZSTR_VAL(text);
    2447              :         size_t i, close_offset, open_offset, method_start, method_end;
    2448              : 
    2449          176 :         *method_name = NULL;
    2450          176 :         *receiver_end = 0;
    2451              : 
    2452          176 :         if (offset > ZSTR_LEN(text)) {
    2453            0 :                 offset = ZSTR_LEN(text);
    2454              :         }
    2455              : 
    2456          176 :         i = offset;
    2457          176 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    2458            0 :                 i--;
    2459              :         }
    2460              : 
    2461          176 :         if (i == 0 || value[i - 1] != ')') {
    2462           32 :                 return false;
    2463              :         }
    2464              : 
    2465          144 :         close_offset = i - 1;
    2466          144 :         if (!lsp_find_matching_open_paren(text, close_offset, &open_offset)) {
    2467            0 :                 return false;
    2468              :         }
    2469              : 
    2470          144 :         i = open_offset;
    2471          144 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    2472            0 :                 i--;
    2473              :         }
    2474              : 
    2475          144 :         method_end = i;
    2476         1244 :         while (i > 0 && lsp_doc_is_identifier_char(value[i - 1])) {
    2477         1100 :                 i--;
    2478              :         }
    2479              : 
    2480          144 :         method_start = i;
    2481          144 :         if (method_start == method_end) {
    2482            0 :                 return false;
    2483              :         }
    2484              : 
    2485          144 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    2486            0 :                 i--;
    2487              :         }
    2488              : 
    2489          144 :         if (i >= 2 && value[i - 2] == '-' && value[i - 1] == '>') {
    2490          140 :                 i -= 2;
    2491            4 :         } else if (i >= 3 && value[i - 3] == '?' && value[i - 2] == '-' && value[i - 1] == '>') {
    2492            0 :                 i -= 3;
    2493              :         } else {
    2494            4 :                 return false;
    2495              :         }
    2496              : 
    2497          140 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    2498            0 :                 i--;
    2499              :         }
    2500              : 
    2501          140 :         *method_name = zend_string_init(value + method_start, method_end - method_start, 0);
    2502          140 :         *receiver_end = i;
    2503              : 
    2504          140 :         return true;
    2505              : }
    2506              : 
    2507           40 : extern bool lsp_parse_this_method_call_ending_at(zend_string *text, size_t offset, zend_string **method_name)
    2508              : {
    2509           40 :         const char *value = ZSTR_VAL(text);
    2510              :         zend_string *parsed_method;
    2511              :         size_t receiver_end, i, receiver_start;
    2512              : 
    2513           40 :         *method_name = NULL;
    2514              : 
    2515           40 :         if (!lsp_parse_method_call_before_offset(text, offset, &parsed_method, &receiver_end)) {
    2516            8 :                 return false;
    2517              :         }
    2518              : 
    2519           32 :         i = receiver_end;
    2520          160 :         while (i > 0 && lsp_doc_is_identifier_char(value[i - 1])) {
    2521          128 :                 i--;
    2522              :         }
    2523              : 
    2524           32 :         if (i == 0 || value[i - 1] != '$') {
    2525            0 :                 zend_string_release(parsed_method);
    2526              : 
    2527            0 :                 return false;
    2528              :         }
    2529              : 
    2530           32 :         receiver_start = i - 1;
    2531           32 :         if (receiver_end - receiver_start != sizeof("$this") - 1 ||
    2532           32 :                 memcmp(value + receiver_start, "$this", sizeof("$this") - 1) != 0
    2533              :         ) {
    2534            0 :                 zend_string_release(parsed_method);
    2535              : 
    2536            0 :                 return false;
    2537              :         }
    2538              : 
    2539           32 :         *method_name = parsed_method;
    2540              : 
    2541           32 :         return true;
    2542              : }
    2543              : 
    2544           56 : extern bool lsp_this_method_call_member_access_context(zend_string *text, size_t offset, zend_string *prefix, zend_string **method_name, zend_string **member_prefix)
    2545              : {
    2546           56 :         const char *value = ZSTR_VAL(text);
    2547              :         size_t receiver_end, close_offset, open_offset, i, method_start, method_end, call_receiver_start, call_receiver_end;
    2548              : 
    2549           56 :         *method_name = NULL;
    2550           56 :         *member_prefix = NULL;
    2551              : 
    2552           56 :         if (!lsp_member_access_arrow(text, offset, prefix, NULL, &receiver_end)) {
    2553           40 :                 return false;
    2554              :         }
    2555              : 
    2556           16 :         i = receiver_end;
    2557           16 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    2558            0 :                 i--;
    2559              :         }
    2560              : 
    2561           16 :         if (i == 0 || value[i - 1] != ')') {
    2562           16 :                 return false;
    2563              :         }
    2564              : 
    2565            0 :         close_offset = i - 1;
    2566            0 :         if (!lsp_find_matching_open_paren(text, close_offset, &open_offset)) {
    2567            0 :                 return false;
    2568              :         }
    2569              : 
    2570            0 :         i = open_offset;
    2571            0 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    2572            0 :                 i--;
    2573              :         }
    2574              : 
    2575            0 :         method_end = i;
    2576            0 :         while (i > 0 && lsp_doc_is_identifier_char(value[i - 1])) {
    2577            0 :                 i--;
    2578              :         }
    2579              : 
    2580            0 :         method_start = i;
    2581            0 :         if (method_start == method_end) {
    2582            0 :                 return false;
    2583              :         }
    2584              : 
    2585            0 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    2586            0 :                 i--;
    2587              :         }
    2588              : 
    2589            0 :         if (i >= 2 && value[i - 2] == '-' && value[i - 1] == '>') {
    2590            0 :                 i -= 2;
    2591            0 :         } else if (i >= 3 && value[i - 3] == '?' && value[i - 2] == '-' && value[i - 1] == '>') {
    2592            0 :                 i -= 3;
    2593              :         } else {
    2594            0 :                 return false;
    2595              :         }
    2596              : 
    2597            0 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
    2598            0 :                 i--;
    2599              :         }
    2600              : 
    2601            0 :         call_receiver_end = i;
    2602              : 
    2603            0 :         while (i > 0 && lsp_doc_is_identifier_char(value[i - 1])) {
    2604            0 :                 i--;
    2605              :         }
    2606              : 
    2607            0 :         if (i == 0 || value[i - 1] != '$') {
    2608            0 :                 return false;
    2609              :         }
    2610              : 
    2611            0 :         call_receiver_start = i - 1;
    2612            0 :         if (call_receiver_end - call_receiver_start != sizeof("$this") - 1 ||
    2613            0 :                 memcmp(value + call_receiver_start, "$this", sizeof("$this") - 1) != 0
    2614              :         ) {
    2615            0 :                 return false;
    2616              :         }
    2617              : 
    2618            0 :         *method_name = zend_string_init(value + method_start, method_end - method_start, 0);
    2619            0 :         *member_prefix = zend_string_copy(prefix);
    2620              : 
    2621            0 :         return true;
    2622              : }
    2623              : 
    2624          432 : extern zend_string *lsp_infer_new_assignment_class(zend_string *text, zend_string *receiver, size_t offset)
    2625              : {
    2626          432 :         const char *value = ZSTR_VAL(text), *end = value + (offset > ZSTR_LEN(text) ? ZSTR_LEN(text) : offset),
    2627          432 :                 *p = value, *match, *q, *class_start, *class_end
    2628              :         ;
    2629          432 :         zend_string *class_name = NULL;
    2630              : 
    2631         2764 :         while (p < end) {
    2632         2756 :                 match = strstr(p, ZSTR_VAL(receiver));
    2633         2756 :                 if (!match || match >= end) {
    2634              :                         break;
    2635              :                 }
    2636              : 
    2637         2332 :                 q = match + ZSTR_LEN(receiver);
    2638         4108 :                 while (q < end && isspace((unsigned char) *q)) {
    2639         1776 :                         q++;
    2640              :                 }
    2641              : 
    2642         2332 :                 if (q >= end || *q != '=') {
    2643         2108 :                         p = match + 1;
    2644         2108 :                         continue;
    2645              :                 }
    2646              : 
    2647          224 :                 q++;
    2648          448 :                 while (q < end && isspace((unsigned char) *q)) {
    2649          224 :                         q++;
    2650              :                 }
    2651              : 
    2652          224 :                 if (q + 3 > end || strncasecmp(q, "new", 3) != 0 || (q + 3 < end && lsp_doc_is_identifier_char(q[3]))) {
    2653          112 :                         p = match + 1;
    2654          112 :                         continue;
    2655              :                 }
    2656              : 
    2657          112 :                 q += 3;
    2658          224 :                 while (q < end && isspace((unsigned char) *q)) {
    2659          112 :                         q++;
    2660              :                 }
    2661              : 
    2662          112 :                 class_start = q;
    2663          820 :                 while (q < end && (lsp_doc_is_identifier_char(*q) || *q == '\\')) {
    2664          708 :                         q++;
    2665              :                 }
    2666              : 
    2667          112 :                 class_end = q;
    2668          112 :                 if (class_end > class_start) {
    2669          112 :                         if (class_name) {
    2670            0 :                                 zend_string_release(class_name);
    2671              :                         }
    2672          112 :                         class_name = zend_string_init(class_start, class_end - class_start, 0);
    2673              :                 }
    2674          112 :                 p = q;
    2675              :         }
    2676              : 
    2677          432 :         return class_name;
    2678              : }
    2679              : 
    2680         1896 : extern size_t lsp_current_statement_scan_limit(zend_string *text, size_t offset)
    2681              : {
    2682         1896 :         const char *value = ZSTR_VAL(text);
    2683         1896 :         size_t limit, length = ZSTR_LEN(text);
    2684              : 
    2685         1896 :         if (offset > length) {
    2686            0 :                 offset = length;
    2687              :         }
    2688              : 
    2689         1896 :         limit = offset;
    2690        16596 :         while (limit < length && value[limit] != ';' && value[limit] != '\n' && value[limit] != '{' && value[limit] != '}') {
    2691        14700 :                 limit++;
    2692              :         }
    2693              : 
    2694         1896 :         return limit;
    2695              : }
    2696              : 
    2697          100 : extern zend_string *lsp_type_generic_argument(zend_string *type, uint32_t target_index)
    2698              : {
    2699          100 :         const char *value = ZSTR_VAL(type), *end = value + ZSTR_LEN(type), *open, *p, *arg_start;
    2700          100 :         uint32_t depth = 0, index = 0;
    2701              : 
    2702          100 :         open = memchr(value, '<', ZSTR_LEN(type));
    2703          100 :         if (!open) {
    2704            4 :                 return NULL;
    2705              :         }
    2706              : 
    2707           96 :         arg_start = open + 1;
    2708          644 :         for (p = open + 1; p < end; p++) {
    2709          644 :                 if (*p == '<') {
    2710            0 :                         depth++;
    2711          644 :                 } else if (*p == '>') {
    2712           96 :                         if (depth == 0) {
    2713           96 :                                 return index == target_index ? lsp_string_trim_slice(arg_start, p) : NULL;
    2714              :                         }
    2715            0 :                         depth--;
    2716          548 :                 } else if (*p == ',' && depth == 0) {
    2717           16 :                         if (index == target_index) {
    2718            0 :                                 return lsp_string_trim_slice(arg_start, p);
    2719              :                         }
    2720           16 :                         index++;
    2721           16 :                         arg_start = p + 1;
    2722              :                 }
    2723              :         }
    2724              : 
    2725            0 :         return NULL;
    2726              : }
    2727              : 
    2728          176 : static inline bool lsp_type_name_equals_literal(const char *start, const char *end, const char *literal)
    2729              : {
    2730          176 :         size_t literal_length = strlen(literal);
    2731              : 
    2732          176 :         while (start < end && isspace((unsigned char) *start)) {
    2733            0 :                 start++;
    2734              :         }
    2735              : 
    2736          176 :         while (end > start && isspace((unsigned char) end[-1])) {
    2737            0 :                 end--;
    2738              :         }
    2739              : 
    2740          220 :         return (size_t) (end - start) == literal_length &&
    2741           44 :                 strncasecmp(start, literal, literal_length) == 0
    2742              :         ;
    2743              : }
    2744              : 
    2745           52 : extern zend_string *lsp_type_array_element_type(zend_string *type)
    2746              : {
    2747           52 :         const char *value = ZSTR_VAL(type), *end = value + ZSTR_LEN(type), *open;
    2748              :         zend_string *argument;
    2749              : 
    2750           52 :         while (end > value && isspace((unsigned char) end[-1])) {
    2751            0 :                 end--;
    2752              :         }
    2753           52 :         if (end - value > 2 && end[-1] == ']' && end[-2] == '[') {
    2754            8 :                 return lsp_string_trim_slice(value, end - 2);
    2755              :         }
    2756              : 
    2757           44 :         open = memchr(value, '<', end - value);
    2758           44 :         if (!open) {
    2759            0 :                 return NULL;
    2760              :         }
    2761              : 
    2762           88 :         if (lsp_type_name_equals_literal(value, open, "array") ||
    2763           44 :                 lsp_type_name_equals_literal(value, open, "non-empty-array")
    2764              :         ) {
    2765            0 :                 argument = lsp_type_generic_argument(type, 1);
    2766            0 :                 if (argument) {
    2767            0 :                         return argument;
    2768              :                 }
    2769              : 
    2770            0 :                 return lsp_type_generic_argument(type, 0);
    2771              :         }
    2772              : 
    2773           88 :         if (lsp_type_name_equals_literal(value, open, "list") ||
    2774           44 :                 lsp_type_name_equals_literal(value, open, "non-empty-list")
    2775              :         ) {
    2776           44 :                 return lsp_type_generic_argument(type, 0);
    2777              :         }
    2778              : 
    2779            0 :         return NULL;
    2780              : }
    2781              : 
    2782            4 : extern bool lsp_type_equals(zend_string *left, zend_string *right)
    2783              : {
    2784            8 :         return ZSTR_LEN(left) == ZSTR_LEN(right) &&
    2785            4 :                 strncasecmp(ZSTR_VAL(left), ZSTR_VAL(right), ZSTR_LEN(left)) == 0
    2786              :         ;
    2787              : }
    2788              : 
    2789         1188 : static inline bool lsp_type_is_builtin(zend_string *type)
    2790              : {
    2791         1188 :         const char *value = ZSTR_VAL(type);
    2792         1188 :         size_t length = ZSTR_LEN(type);
    2793              : 
    2794         1188 :         return (length == sizeof("array") - 1 && strncasecmp(value, "array", length) == 0) ||
    2795         1188 :                 (length == sizeof("callable") - 1 && strncasecmp(value, "callable", length) == 0) ||
    2796         1188 :                 (length == sizeof("bool") - 1 && strncasecmp(value, "bool", length) == 0) ||
    2797         1188 :                 (length == sizeof("boolean") - 1 && strncasecmp(value, "boolean", length) == 0) ||
    2798         1188 :                 (length == sizeof("float") - 1 && strncasecmp(value, "float", length) == 0) ||
    2799         1188 :                 (length == sizeof("double") - 1 && strncasecmp(value, "double", length) == 0) ||
    2800         1188 :                 (length == sizeof("int") - 1 && strncasecmp(value, "int", length) == 0) ||
    2801         1188 :                 (length == sizeof("integer") - 1 && strncasecmp(value, "integer", length) == 0) ||
    2802         1188 :                 (length == sizeof("iterable") - 1 && strncasecmp(value, "iterable", length) == 0) ||
    2803         1188 :                 (length == sizeof("mixed") - 1 && strncasecmp(value, "mixed", length) == 0) ||
    2804         1188 :                 (length == sizeof("never") - 1 && strncasecmp(value, "never", length) == 0) ||
    2805         1188 :                 (length == sizeof("null") - 1 && strncasecmp(value, "null", length) == 0) ||
    2806         1188 :                 (length == sizeof("object") - 1 && strncasecmp(value, "object", length) == 0) ||
    2807         1184 :                 (length == sizeof("resource") - 1 && strncasecmp(value, "resource", length) == 0) ||
    2808         1184 :                 (length == sizeof("self") - 1 && strncasecmp(value, "self", length) == 0) ||
    2809         1184 :                 (length == sizeof("static") - 1 && strncasecmp(value, "static", length) == 0) ||
    2810         2672 :                 (length == sizeof("string") - 1 && strncasecmp(value, "string", length) == 0) ||
    2811          296 :                 (length == sizeof("void") - 1 && strncasecmp(value, "void", length) == 0)
    2812              :         ;
    2813              : }
    2814              : 
    2815          208 : static inline bool lsp_phpdoc_import_alias_matches(zend_string *alias, zend_string *import_name, const char *type_value, size_t type_segment_length)
    2816              : {
    2817              :         const char *alias_value;
    2818              :         size_t alias_length;
    2819              : 
    2820          208 :         if (alias) {
    2821            0 :                 alias_value = ZSTR_VAL(alias);
    2822            0 :                 alias_length = ZSTR_LEN(alias);
    2823              :         } else {
    2824          208 :                 alias_value = lsp_basename_from_fqcn(ZSTR_VAL(import_name), ZSTR_LEN(import_name), &alias_length);
    2825              :         }
    2826              : 
    2827          308 :         return alias_length == type_segment_length &&
    2828          100 :                 strncasecmp(alias_value, type_value, type_segment_length) == 0
    2829              :         ;
    2830              : }
    2831              : 
    2832          208 : static inline zend_string *lsp_phpdoc_import_full_name(zend_string *prefix, zend_string *name)
    2833              : {
    2834          208 :         if (prefix && ZSTR_LEN(prefix) > 0) {
    2835            0 :                 return strpprintf(0, "%s\\%s", ZSTR_VAL(prefix), ZSTR_VAL(name));
    2836              :         }
    2837              : 
    2838          208 :         return zend_string_copy(name);
    2839              : }
    2840              : 
    2841          100 : static inline zend_string *lsp_phpdoc_resolved_import_name(zend_string *import_name, zend_string *type, const char *type_segment_end, size_t type_segment_length)
    2842              : {
    2843              :         zend_string *resolved;
    2844              :         size_t suffix_length;
    2845              : 
    2846          100 :         suffix_length = type_segment_end ? ZSTR_LEN(type) - type_segment_length : 0;
    2847          100 :         resolved = zend_string_alloc(ZSTR_LEN(import_name) + suffix_length, 0);
    2848          100 :         memcpy(ZSTR_VAL(resolved), ZSTR_VAL(import_name), ZSTR_LEN(import_name));
    2849          100 :         if (suffix_length > 0) {
    2850            0 :                 memcpy(ZSTR_VAL(resolved) + ZSTR_LEN(import_name), type_segment_end, suffix_length);
    2851              :         }
    2852          100 :         ZSTR_VAL(resolved)[ZSTR_LEN(resolved)] = '\0';
    2853              : 
    2854          100 :         return resolved;
    2855              : }
    2856              : 
    2857          208 : static inline zend_string *lsp_resolve_imported_class_name_from_use_ast(zend_ast *use_ast, zend_string *prefix, zend_string *type, const char *type_value, size_t type_segment_length, const char *type_segment_end)
    2858              : {
    2859              :         zend_ast_list *list;
    2860              :         zend_ast *elem;
    2861              :         zend_string *name, *alias, *import_name, *resolved;
    2862              :         uint32_t i, elem_kind;
    2863              : 
    2864          208 :         if (!use_ast || use_ast->kind != ZEND_AST_USE || !zend_ast_is_list(use_ast)) {
    2865            0 :                 return NULL;
    2866              :         }
    2867              : 
    2868          208 :         list = zend_ast_get_list(use_ast);
    2869          316 :         for (i = 0; i < list->children; i++) {
    2870          208 :                 elem = list->child[i];
    2871          208 :                 if (!elem || elem->kind != ZEND_AST_USE_ELEM) {
    2872            0 :                         continue;
    2873              :                 }
    2874              : 
    2875          208 :                 elem_kind = elem->attr != 0 ? elem->attr : use_ast->attr;
    2876          208 :                 if (elem_kind == 0) {
    2877            0 :                         elem_kind = ZEND_SYMBOL_CLASS;
    2878              :                 }
    2879          208 :                 if (elem_kind != ZEND_SYMBOL_CLASS) {
    2880            0 :                         continue;
    2881              :                 }
    2882              : 
    2883          208 :                 name = lsp_ast_string_value(elem->child[0]);
    2884          208 :                 if (!name) {
    2885            0 :                         continue;
    2886              :                 }
    2887              : 
    2888          208 :                 alias = lsp_ast_string_value(elem->child[1]);
    2889          208 :                 import_name = lsp_phpdoc_import_full_name(prefix, name);
    2890          208 :                 if (lsp_phpdoc_import_alias_matches(alias, import_name, type_value, type_segment_length)) {
    2891          100 :                         resolved = lsp_phpdoc_resolved_import_name(import_name, type, type_segment_end, type_segment_length);
    2892          100 :                         zend_string_release(import_name);
    2893              : 
    2894          100 :                         return resolved;
    2895              :                 }
    2896              : 
    2897          108 :                 zend_string_release(import_name);
    2898              :         }
    2899              : 
    2900          108 :         return NULL;
    2901              : }
    2902              : 
    2903         1092 : static inline zend_string *lsp_resolve_imported_class_name_from_ast(zend_ast *ast, zend_string *type, const char *type_value, size_t type_segment_length, const char *type_segment_end)
    2904              : {
    2905              :         zend_ast_list *list;
    2906              :         zend_string *prefix, *resolved;
    2907              :         uint32_t i, count;
    2908              : 
    2909         1092 :         if (!ast) {
    2910          184 :                 return NULL;
    2911              :         }
    2912              : 
    2913          908 :         if (ast->kind == ZEND_AST_USE) {
    2914          208 :                 return lsp_resolve_imported_class_name_from_use_ast(ast, NULL, type, type_value, type_segment_length, type_segment_end);
    2915              :         }
    2916              : 
    2917          700 :         if (ast->kind == ZEND_AST_GROUP_USE) {
    2918            0 :                 prefix = lsp_ast_string_value(ast->child[0]);
    2919              : 
    2920            0 :                 return lsp_resolve_imported_class_name_from_use_ast(ast->child[1], prefix, type, type_value, type_segment_length, type_segment_end);
    2921              :         }
    2922              : 
    2923          700 :         if (zend_ast_is_list(ast)) {
    2924          184 :                 list = zend_ast_get_list(ast);
    2925          624 :                 for (i = 0; i < list->children; i++) {
    2926          540 :                         resolved = lsp_resolve_imported_class_name_from_ast(list->child[i], type, type_value, type_segment_length, type_segment_end);
    2927          540 :                         if (resolved) {
    2928          100 :                                 return resolved;
    2929              :                         }
    2930              :                 }
    2931              : 
    2932           84 :                 return NULL;
    2933              :         }
    2934              : 
    2935          516 :         if (zend_ast_is_special(ast) || php_ver_abstract.ast_is_opaque_node(ast->kind)) {
    2936          332 :                 return NULL;
    2937              :         }
    2938              : 
    2939          184 :         count = zend_ast_get_num_children(ast);
    2940          552 :         for (i = 0; i < count; i++) {
    2941          368 :                 resolved = lsp_resolve_imported_class_name_from_ast(ast->child[i], type, type_value, type_segment_length, type_segment_end);
    2942          368 :                 if (resolved) {
    2943            0 :                         return resolved;
    2944              :                 }
    2945              :         }
    2946              : 
    2947          184 :         return NULL;
    2948              : }
    2949              : 
    2950         2712 : static inline bool lsp_import_resolve_token_is_ignored(zval *token)
    2951              : {
    2952         4552 :         return lsp_token_name_equals(token, "T_WHITESPACE") ||
    2953         4552 :                 lsp_token_name_equals(token, "T_COMMENT") ||
    2954         1840 :                 lsp_token_name_equals(token, "T_DOC_COMMENT")
    2955              :         ;
    2956              : }
    2957              : 
    2958          168 : static inline bool lsp_import_resolve_token_is_name_part(zval *token)
    2959              : {
    2960          336 :         return lsp_token_name_equals(token, "T_STRING") ||
    2961          168 :                 lsp_token_name_equals(token, "T_NAME_QUALIFIED") ||
    2962            0 :                 lsp_token_name_equals(token, "T_NAME_FULLY_QUALIFIED") ||
    2963          336 :                 lsp_token_name_equals(token, "T_NAME_RELATIVE") ||
    2964            0 :                 lsp_token_name_equals(token, "T_NS_SEPARATOR")
    2965              :         ;
    2966              : }
    2967              : 
    2968         1332 : static inline bool lsp_import_resolve_token_is_class_like(zval *token)
    2969              : {
    2970         2540 :         return lsp_token_name_equals(token, "T_CLASS") ||
    2971         2416 :                 lsp_token_name_equals(token, "T_INTERFACE") ||
    2972         3748 :                 lsp_token_name_equals(token, "T_TRAIT") ||
    2973         1208 :                 lsp_token_name_equals(token, "T_ENUM")
    2974              :         ;
    2975              : }
    2976              : 
    2977          336 : static inline void lsp_import_resolve_smart_str_reset(smart_str *value)
    2978              : {
    2979          336 :         smart_str_free(value);
    2980          336 :         memset(value, 0, sizeof(smart_str));
    2981          336 : }
    2982              : 
    2983          168 : static inline void lsp_import_resolve_append_name_token(smart_str *name, zval *token)
    2984              : {
    2985              :         zend_string *text;
    2986              : 
    2987          168 :         text = lsp_token_string(token, "text");
    2988          168 :         if (!text) {
    2989            0 :                 return;
    2990              :         }
    2991              : 
    2992          168 :         smart_str_append(name, text);
    2993              : }
    2994              : 
    2995          168 : static inline void lsp_import_resolve_alias_from_import(zend_string *import_name, zend_string *alias, const char **alias_start, size_t *alias_length)
    2996              : {
    2997              :         const char *value, *start, *end, *p;
    2998              : 
    2999          168 :         if (alias) {
    3000            0 :                 *alias_start = ZSTR_VAL(alias);
    3001            0 :                 *alias_length = ZSTR_LEN(alias);
    3002              : 
    3003            0 :                 return;
    3004              :         }
    3005              : 
    3006          168 :         value = ZSTR_VAL(import_name);
    3007          168 :         start = value;
    3008          168 :         end = value + ZSTR_LEN(import_name);
    3009          168 :         if (start < end && *start == '\\') {
    3010            0 :                 start++;
    3011              :         }
    3012              : 
    3013         1468 :         for (p = end; p > start; p--) {
    3014         1468 :                 if (p[-1] == '\\') {
    3015          168 :                         start = p;
    3016          168 :                         break;
    3017              :                 }
    3018              :         }
    3019              : 
    3020          168 :         *alias_start = start;
    3021          168 :         *alias_length = (size_t) (end - start);
    3022              : }
    3023              : 
    3024          168 : static inline zend_string *lsp_import_resolve_match(zend_string *import_name, zend_string *alias, zend_string *type, size_t type_segment_length, const char *type_segment_end)
    3025              : {
    3026              :         const char *alias_start, *import_start, *import_end, *type_value;
    3027          168 :         smart_str resolved = {0};
    3028              :         size_t alias_length, suffix_length;
    3029              : 
    3030          168 :         lsp_import_resolve_alias_from_import(import_name, alias, &alias_start, &alias_length);
    3031          168 :         type_value = ZSTR_VAL(type);
    3032          168 :         if (alias_length != type_segment_length || strncasecmp(alias_start, type_value, alias_length) != 0) {
    3033          116 :                 return NULL;
    3034              :         }
    3035              : 
    3036           52 :         import_start = ZSTR_VAL(import_name);
    3037           52 :         import_end = import_start + ZSTR_LEN(import_name);
    3038           52 :         if (import_start < import_end && *import_start == '\\') {
    3039            0 :                 import_start++;
    3040              :         }
    3041              : 
    3042           52 :         if (import_start >= import_end) {
    3043            0 :                 return NULL;
    3044              :         }
    3045              : 
    3046           52 :         smart_str_appendl(&resolved, import_start, import_end - import_start);
    3047           52 :         if (type_segment_end) {
    3048            0 :                 suffix_length = (size_t) ((ZSTR_VAL(type) + ZSTR_LEN(type)) - type_segment_end);
    3049            0 :                 smart_str_appendl(&resolved, type_segment_end, suffix_length);
    3050              :         }
    3051              : 
    3052           52 :         smart_str_0(&resolved);
    3053              : 
    3054           52 :         return resolved.s;
    3055              : }
    3056              : 
    3057          168 : static inline zend_string *lsp_import_resolve_candidate_match(smart_str *prefix, smart_str *name, zend_string *alias, zend_string *type, size_t type_segment_length, const char *type_segment_end, bool in_group)
    3058              : {
    3059              :         const char *prefix_value, *name_value;
    3060              :         zend_string *import_name, *resolved;
    3061          168 :         smart_str full_name = {0};
    3062              :         size_t prefix_length;
    3063              : 
    3064          168 :         if (!name->s || ZSTR_LEN(name->s) == 0) {
    3065            0 :                 return NULL;
    3066              :         }
    3067              : 
    3068          168 :         import_name = name->s;
    3069          168 :         if (in_group && prefix->s && ZSTR_LEN(prefix->s) > 0) {
    3070            0 :                 prefix_value = ZSTR_VAL(prefix->s);
    3071            0 :                 prefix_length = ZSTR_LEN(prefix->s);
    3072            0 :                 name_value = ZSTR_VAL(name->s);
    3073              : 
    3074            0 :                 smart_str_appendl(&full_name, prefix_value, prefix_length);
    3075            0 :                 if (prefix_value[prefix_length - 1] != '\\' && ZSTR_LEN(name->s) > 0 && name_value[0] != '\\') {
    3076            0 :                         smart_str_appendc(&full_name, '\\');
    3077              :                 }
    3078            0 :                 smart_str_append(&full_name, name->s);
    3079            0 :                 smart_str_0(&full_name);
    3080            0 :                 import_name = full_name.s;
    3081              :         }
    3082              : 
    3083          168 :         resolved = lsp_import_resolve_match(import_name, alias, type, type_segment_length, type_segment_end);
    3084              : 
    3085          168 :         if (full_name.s) {
    3086            0 :                 smart_str_free(&full_name);
    3087              :         }
    3088              : 
    3089          168 :         return resolved;
    3090              : }
    3091              : 
    3092          168 : static inline zend_string *lsp_resolve_imported_class_name_from_use_tokens(HashTable *tokens, uint32_t start, zend_string *type, size_t type_segment_length, const char *type_segment_end)
    3093              : {
    3094              :         zend_string *alias, *resolved;
    3095              :         zval *token;
    3096          168 :         smart_str prefix = {0}, name = {0};
    3097              :         uint32_t i, count;
    3098              :         bool in_group, reading_alias;
    3099              : 
    3100          168 :         alias = NULL;
    3101          168 :         resolved = NULL;
    3102          168 :         in_group = false;
    3103          168 :         reading_alias = false;
    3104          168 :         count = zend_hash_num_elements(tokens);
    3105          336 :         for (i = start; i < count; i++) {
    3106          336 :                 token = zend_hash_index_find(tokens, i);
    3107          336 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || lsp_import_resolve_token_is_ignored(token)) {
    3108            0 :                         continue;
    3109              :                 }
    3110              : 
    3111          336 :                 if (lsp_token_is_char(token, ';')) {
    3112          168 :                         resolved = lsp_import_resolve_candidate_match(&prefix, &name, alias, type, type_segment_length, type_segment_end, in_group);
    3113              : 
    3114          168 :                         break;
    3115              :                 }
    3116              : 
    3117          168 :                 if (lsp_token_is_char(token, '{')) {
    3118            0 :                         if (name.s) {
    3119            0 :                                 smart_str_0(&name);
    3120            0 :                                 smart_str_append(&prefix, name.s);
    3121            0 :                                 smart_str_0(&prefix);
    3122            0 :                                 lsp_import_resolve_smart_str_reset(&name);
    3123              :                         }
    3124              : 
    3125            0 :                         in_group = true;
    3126            0 :                         alias = NULL;
    3127            0 :                         reading_alias = false;
    3128              : 
    3129            0 :                         continue;
    3130              :                 }
    3131              : 
    3132          168 :                 if (lsp_token_is_char(token, ',') || lsp_token_is_char(token, '}')) {
    3133            0 :                         resolved = lsp_import_resolve_candidate_match(&prefix, &name, alias, type, type_segment_length, type_segment_end, in_group);
    3134            0 :                         if (resolved) {
    3135            0 :                                 break;
    3136              :                         }
    3137              : 
    3138            0 :                         lsp_import_resolve_smart_str_reset(&name);
    3139              : 
    3140            0 :                         alias = NULL;
    3141            0 :                         reading_alias = false;
    3142              : 
    3143            0 :                         if (lsp_token_is_char(token, '}')) {
    3144            0 :                                 in_group = false;
    3145              :                         }
    3146              : 
    3147            0 :                         continue;
    3148              :                 }
    3149              : 
    3150          168 :                 if (lsp_token_name_equals(token, "T_AS")) {
    3151            0 :                         reading_alias = true;
    3152            0 :                         alias = NULL;
    3153              : 
    3154            0 :                         continue;
    3155              :                 }
    3156              : 
    3157          168 :                 if (reading_alias) {
    3158            0 :                         if (lsp_import_resolve_token_is_name_part(token)) {
    3159            0 :                                 alias = lsp_token_string(token, "text");
    3160            0 :                                 reading_alias = false;
    3161              :                         }
    3162              : 
    3163            0 :                         continue;
    3164              :                 }
    3165              : 
    3166          168 :                 if (lsp_import_resolve_token_is_name_part(token)) {
    3167          168 :                         lsp_import_resolve_append_name_token(&name, token);
    3168              :                 }
    3169              :         }
    3170              : 
    3171          168 :         lsp_import_resolve_smart_str_reset(&prefix);
    3172          168 :         lsp_import_resolve_smart_str_reset(&name);
    3173              : 
    3174          168 :         return resolved;
    3175              : }
    3176              : 
    3177          176 : static inline zend_string *lsp_resolve_imported_class_name_from_tokenized_text(zend_string *text, zend_string *type, size_t type_segment_length, const char *type_segment_end)
    3178              : {
    3179              :         zend_string *resolved;
    3180              :         zval tokens_zv, *token, *next_token;
    3181              :         HashTable *tokens;
    3182              :         uint32_t i, j, count;
    3183              : 
    3184          176 :         ZVAL_UNDEF(&tokens_zv);
    3185          176 :         lsp_lsparrot_tokens_to_zval(&tokens_zv, text);
    3186          176 :         if (Z_TYPE(tokens_zv) != IS_ARRAY) {
    3187            0 :                 if (!Z_ISUNDEF(tokens_zv)) {
    3188            0 :                         zval_ptr_dtor(&tokens_zv);
    3189              :                 }
    3190              : 
    3191            0 :                 return NULL;
    3192              :         }
    3193              : 
    3194          176 :         tokens = Z_ARRVAL(tokens_zv);
    3195          176 :         count = zend_hash_num_elements(tokens);
    3196          176 :         resolved = NULL;
    3197         2040 :         for (i = 0; i < count; i++) {
    3198         2040 :                 token = zend_hash_index_find(tokens, i);
    3199         2040 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || lsp_import_resolve_token_is_ignored(token)) {
    3200          708 :                         continue;
    3201              :                 }
    3202              : 
    3203         1332 :                 if (lsp_import_resolve_token_is_class_like(token)) {
    3204          124 :                         break;
    3205              :                 }
    3206              : 
    3207         1208 :                 if (!lsp_token_name_equals(token, "T_USE")) {
    3208         1040 :                         continue;
    3209              :                 }
    3210              : 
    3211          336 :                 for (j = i + 1; j < count; j++) {
    3212          336 :                         next_token = zend_hash_index_find(tokens, j);
    3213          336 :                         if (!next_token || Z_TYPE_P(next_token) != IS_ARRAY || !lsp_import_resolve_token_is_ignored(next_token)) {
    3214              :                                 break;
    3215              :                         }
    3216              :                 }
    3217              : 
    3218          168 :                 if (j >= count) {
    3219            0 :                         break;
    3220              :                 }
    3221              : 
    3222          168 :                 next_token = zend_hash_index_find(tokens, j);
    3223          168 :                 if (!next_token || Z_TYPE_P(next_token) != IS_ARRAY) {
    3224            0 :                         continue;
    3225              :                 }
    3226              : 
    3227          168 :                 if (lsp_token_name_equals(next_token, "T_FUNCTION") || lsp_token_name_equals(next_token, "T_CONST")) {
    3228            0 :                         continue;
    3229              :                 }
    3230              : 
    3231          168 :                 resolved = lsp_resolve_imported_class_name_from_use_tokens(tokens, j, type, type_segment_length, type_segment_end);
    3232          168 :                 if (resolved) {
    3233           52 :                         break;
    3234              :                 }
    3235              :         }
    3236              : 
    3237          176 :         zval_ptr_dtor(&tokens_zv);
    3238              : 
    3239          176 :         return resolved;
    3240              : }
    3241              : 
    3242          816 : static inline zend_string *lsp_resolve_imported_class_name(zend_string *text, zend_string *type)
    3243              : {
    3244              :         const char *type_value, *type_segment_end;
    3245              :         zend_arena *ast_arena;
    3246              :         zend_ast *ast;
    3247              :         zend_string *resolved;
    3248              :         size_t type_segment_length;
    3249              : 
    3250          816 :         if (!strstr(ZSTR_VAL(text), "use")) {
    3251          540 :                 return NULL;
    3252              :         }
    3253              : 
    3254          276 :         type_value = ZSTR_VAL(type);
    3255          276 :         type_segment_end = memchr(type_value, '\\', ZSTR_LEN(type));
    3256          276 :         type_segment_length = type_segment_end ? (size_t) (type_segment_end - type_value) : ZSTR_LEN(type);
    3257              : 
    3258          276 :         ast = lsp_compile_string_to_ast_silent(text, ZSTR_EMPTY_ALLOC(), &ast_arena);
    3259          276 :         if (!ast) {
    3260           92 :                 return lsp_resolve_imported_class_name_from_tokenized_text(text, type, type_segment_length, type_segment_end);
    3261              :         }
    3262              : 
    3263          184 :         resolved = lsp_resolve_imported_class_name_from_ast(ast, type, type_value, type_segment_length, type_segment_end);
    3264          184 :         lsp_compiled_ast_destroy(ast, ast_arena);
    3265          184 :         if (resolved) {
    3266          100 :                 return resolved;
    3267              :         }
    3268              : 
    3269           84 :         return lsp_resolve_imported_class_name_from_tokenized_text(text, type, type_segment_length, type_segment_end);
    3270              : }
    3271              : 
    3272              : /* lsp_resolve_class_name re-parses the whole document to walk its use
    3273              :  * statements; features resolve many names per request against the same text,
    3274              :  * so memoize (text, base name) -> resolved FQCN with the same string-identity
    3275              :  * keying as the token cache. */
    3276              : #define LSP_RESOLVE_CACHE_SIZE 64
    3277              : 
    3278              : typedef struct _lsp_resolve_cache_entry {
    3279              :         zend_string *text;
    3280              :         zend_string *base;
    3281              :         zend_string *resolved;
    3282              : } lsp_resolve_cache_entry;
    3283              : 
    3284              : static lsp_resolve_cache_entry lsp_resolve_cache[LSP_RESOLVE_CACHE_SIZE];
    3285              : static uint32_t lsp_resolve_cache_next = 0;
    3286              : 
    3287         1152 : static inline bool lsp_resolve_cache_lookup(zend_string *text, zend_string *base, zend_string **resolved)
    3288              : {
    3289              :         uint32_t i;
    3290              : 
    3291        52072 :         for (i = 0; i < LSP_RESOLVE_CACHE_SIZE; i++) {
    3292        51280 :                 if (lsp_resolve_cache[i].text == text && lsp_resolve_cache[i].base && zend_string_equals(lsp_resolve_cache[i].base, base)) {
    3293          360 :                         *resolved = zend_string_copy(lsp_resolve_cache[i].resolved);
    3294              : 
    3295          360 :                         return true;
    3296              :                 }
    3297              :         }
    3298              : 
    3299          792 :         return false;
    3300              : }
    3301              : 
    3302          792 : static inline void lsp_resolve_cache_store(zend_string *text, zend_string *base, zend_string *resolved)
    3303              : {
    3304              :         lsp_resolve_cache_entry *slot;
    3305              : 
    3306          792 :         slot = &lsp_resolve_cache[lsp_resolve_cache_next];
    3307          792 :         lsp_resolve_cache_next = (lsp_resolve_cache_next + 1) % LSP_RESOLVE_CACHE_SIZE;
    3308              : 
    3309          792 :         if (slot->text) {
    3310            0 :                 zend_string_release(slot->text);
    3311            0 :                 zend_string_release(slot->base);
    3312            0 :                 zend_string_release(slot->resolved);
    3313              :         }
    3314              : 
    3315          792 :         slot->text = zend_string_copy(text);
    3316          792 :         slot->base = zend_string_copy(base);
    3317          792 :         slot->resolved = zend_string_copy(resolved);
    3318          792 : }
    3319              : 
    3320          548 : extern void lsp_resolve_cache_clear(void)
    3321              : {
    3322              :         uint32_t i;
    3323              : 
    3324        35620 :         for (i = 0; i < LSP_RESOLVE_CACHE_SIZE; i++) {
    3325        35072 :                 if (lsp_resolve_cache[i].text) {
    3326          792 :                         zend_string_release(lsp_resolve_cache[i].text);
    3327          792 :                         zend_string_release(lsp_resolve_cache[i].base);
    3328          792 :                         zend_string_release(lsp_resolve_cache[i].resolved);
    3329          792 :                         lsp_resolve_cache[i].text = NULL;
    3330          792 :                         lsp_resolve_cache[i].base = NULL;
    3331          792 :                         lsp_resolve_cache[i].resolved = NULL;
    3332              :                 }
    3333              :         }
    3334              : 
    3335          548 :         lsp_resolve_cache_next = 0;
    3336          548 : }
    3337              : 
    3338         1184 : static inline bool lsp_type_is_fully_qualified(zend_string *type)
    3339              : {
    3340         1184 :         const char *p = ZSTR_VAL(type), *end = p + ZSTR_LEN(type);
    3341              : 
    3342         1192 :         while (p < end && isspace((unsigned char) *p)) {
    3343            8 :                 p++;
    3344              :         }
    3345              : 
    3346         1184 :         while (p < end && (*p == '?' || *p == '(')) {
    3347            0 :                 p++;
    3348              :         }
    3349              : 
    3350         1184 :         while (p < end && isspace((unsigned char) *p)) {
    3351            0 :                 p++;
    3352              :         }
    3353              : 
    3354         1184 :         return p < end && *p == '\\';
    3355              : }
    3356              : 
    3357         1164 : extern zend_string *lsp_resolve_class_name(zend_string *text, zend_string *type)
    3358              : {
    3359         1164 :         zend_string *base = lsp_type_class_name(type), *resolved, *namespace_name;
    3360              : 
    3361         1164 :         if (!base || lsp_type_is_builtin(base)) {
    3362            4 :                 if (base) {
    3363            4 :                         zend_string_release(base);
    3364              :                 }
    3365              : 
    3366            4 :                 return NULL;
    3367              :         }
    3368              : 
    3369              :         /* Fully qualified names bypass import/namespace resolution entirely.
    3370              :          * lsp_type_class_name strips the leading backslash, so the check has to
    3371              :          * look at the raw type text. */
    3372         1160 :         if (lsp_type_is_fully_qualified(type)) {
    3373            8 :                 return base;
    3374              :         }
    3375              : 
    3376         1152 :         if (lsp_resolve_cache_lookup(text, base, &resolved)) {
    3377          360 :                 zend_string_release(base);
    3378              : 
    3379          360 :                 return resolved;
    3380              :         }
    3381              : 
    3382              :         /* Imports and the current namespace take precedence over globally loaded
    3383              :          * classes: a builtin sharing the short name of an imported class (e.g.
    3384              :          * `use App\Domain\Exception`) must not shadow the import. */
    3385          792 :         resolved = lsp_resolve_imported_class_name(text, base);
    3386          792 :         if (!resolved) {
    3387          640 :                 namespace_name = lsp_document_namespace(text);
    3388          640 :                 if (namespace_name != zend_empty_string) {
    3389              :                         /* Unqualified names inside a namespace resolve to the current
    3390              :                          * namespace, matching PHP's own name resolution rules. */
    3391          624 :                         resolved = strpprintf(0, "%s\\%s", ZSTR_VAL(namespace_name), ZSTR_VAL(base));
    3392          624 :                         zend_string_release(namespace_name);
    3393              :                 } else {
    3394           16 :                         resolved = zend_string_copy(base);
    3395              :                 }
    3396              :         }
    3397              : 
    3398          792 :         lsp_resolve_cache_store(text, base, resolved);
    3399          792 :         zend_string_release(base);
    3400              : 
    3401          792 :         return resolved;
    3402              : }
    3403              : 
    3404              : /* Offset-aware variant for files declaring several namespaces: unqualified
    3405              :  * names must resolve against the namespace governing the offset, not the
    3406              :  * file's first namespace. Single-namespace files (the overwhelmingly common
    3407              :  * case) delegate to the memoized lsp_resolve_class_name. Imports are still
    3408              :  * collected file-wide, an accepted approximation. */
    3409          896 : extern zend_string *lsp_resolve_class_name_at(zend_string *text, zend_string *type, size_t offset)
    3410              : {
    3411              :         zend_string *base, *resolved, *namespace_name;
    3412          896 :         bool multiple = false;
    3413              : 
    3414          896 :         namespace_name = lsp_document_namespace_at_ex(text, offset, &multiple);
    3415          896 :         if (!multiple) {
    3416          872 :                 if (namespace_name != zend_empty_string) {
    3417          820 :                         zend_string_release(namespace_name);
    3418              :                 }
    3419              : 
    3420          872 :                 return lsp_resolve_class_name(text, type);
    3421              :         }
    3422              : 
    3423           24 :         base = lsp_type_class_name(type);
    3424           24 :         if (!base || lsp_type_is_builtin(base)) {
    3425            0 :                 if (base) {
    3426            0 :                         zend_string_release(base);
    3427              :                 }
    3428            0 :                 if (namespace_name != zend_empty_string) {
    3429            0 :                         zend_string_release(namespace_name);
    3430              :                 }
    3431              : 
    3432            0 :                 return NULL;
    3433              :         }
    3434              : 
    3435           24 :         if (lsp_type_is_fully_qualified(type)) {
    3436            0 :                 if (namespace_name != zend_empty_string) {
    3437            0 :                         zend_string_release(namespace_name);
    3438              :                 }
    3439              : 
    3440            0 :                 return base;
    3441              :         }
    3442              : 
    3443           24 :         resolved = lsp_resolve_imported_class_name(text, base);
    3444           24 :         if (!resolved) {
    3445           24 :                 if (namespace_name != zend_empty_string) {
    3446           24 :                         resolved = strpprintf(0, "%s\\%s", ZSTR_VAL(namespace_name), ZSTR_VAL(base));
    3447              :                 } else {
    3448            0 :                         resolved = zend_string_copy(base);
    3449              :                 }
    3450              :         }
    3451              : 
    3452           24 :         if (namespace_name != zend_empty_string) {
    3453           24 :                 zend_string_release(namespace_name);
    3454              :         }
    3455           24 :         zend_string_release(base);
    3456              : 
    3457           24 :         return resolved;
    3458              : }
    3459              : 
    3460         1576 : extern const char *lsp_primary_analyzer_source(lsp_server *server)
    3461              : {
    3462         1576 :         if (server->phpstan_enabled) {
    3463          172 :                 return "phpstan";
    3464              :         }
    3465              : 
    3466         1404 :         if (server->psalm_enabled) {
    3467           16 :                 return "psalm";
    3468              :         }
    3469              : 
    3470         1388 :         if (server->psalm_ls_enabled) {
    3471           72 :                 return "psalm-ls";
    3472              :         }
    3473              : 
    3474         1316 :         return "lsparrot";
    3475              : }
    3476              : 
    3477         5448 : extern bool lsp_doc_is_identifier_start(char c)
    3478              : {
    3479         5448 :         return isalpha((unsigned char) c) || c == '_' || (unsigned char) c >= 0x80;
    3480              : }
    3481              : 
    3482        64924 : extern bool lsp_doc_is_identifier_char(char c)
    3483              : {
    3484        64924 :         return isalnum((unsigned char) c) || c == '_' || (unsigned char) c >= 0x80;
    3485              : }
    3486              : 
    3487          504 : extern char lsp_type_constraint_completion_kind(zend_string *text, size_t offset, zend_string *prefix)
    3488              : {
    3489          504 :         const char *value = ZSTR_VAL(text), *cursor, *end, *token_start;
    3490              :         size_t prefix_start, start, token_length;
    3491          504 :         char filter_kind = '\0';
    3492              : 
    3493          504 :         if (offset > ZSTR_LEN(text)) {
    3494            0 :                 offset = ZSTR_LEN(text);
    3495              :         }
    3496              : 
    3497          504 :         prefix_start = offset >= ZSTR_LEN(prefix) ? offset - ZSTR_LEN(prefix) : 0;
    3498          504 :         start = prefix_start;
    3499         6180 :         while (start > 0 && value[start - 1] != '\n' && value[start - 1] != ';' && value[start - 1] != '{' && value[start - 1] != '(' && value[start - 1] != ')') {
    3500         5676 :                 start--;
    3501              :         }
    3502              : 
    3503          504 :         cursor = value + start;
    3504          504 :         end = value + prefix_start;
    3505          924 :         while (cursor < end) {
    3506         4432 :                 while (cursor < end && !lsp_doc_is_identifier_start(*cursor)) {
    3507         3568 :                         cursor++;
    3508              :                 }
    3509              : 
    3510          864 :                 if (cursor >= end) {
    3511          444 :                         break;
    3512              :                 }
    3513              : 
    3514          420 :                 token_start = cursor++;
    3515         2108 :                 while (cursor < end && lsp_doc_is_identifier_char(*cursor)) {
    3516         1688 :                         cursor++;
    3517              :                 }
    3518              : 
    3519          420 :                 token_length = cursor - token_start;
    3520          420 :                 if ((token_length == sizeof("extends") - 1 && strncasecmp(token_start, "extends", token_length) == 0) ||
    3521            4 :                         (token_length == sizeof("implements") - 1 && strncasecmp(token_start, "implements", token_length) == 0)
    3522              :                 ) {
    3523            8 :                         filter_kind = token_length == sizeof("extends") - 1 ? LSP_SYMBOL_CLASS : LSP_SYMBOL_INTERFACE;
    3524              :                 }
    3525              :         }
    3526              : 
    3527          504 :         return filter_kind;
    3528              : }
        

Generated by: LCOV version 2.0-1