LCOV - code coverage report
Current view: top level - ext - lsp_server_member_cache.c (source / functions) Coverage Total Hit
Test: Extension code coverage Lines: 87.1 % 711 619
Test Date: 2026-07-13 00:58:38 Functions: 100.0 % 35 35

            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          444 : static inline bool lsp_class_member_cache_entry_is_fresh(zval *entry, zend_string *path, lsp_document *document, bool stat_valid, zend_stat_t *st)
      17              : {
      18              :         zend_long document_version, mtime, mtime_nsec, size;
      19              :         zend_string *cached_path;
      20              : 
      21          444 :         if (!entry || Z_TYPE_P(entry) != IS_ARRAY) {
      22          228 :                 return false;
      23              :         }
      24              : 
      25          216 :         cached_path = lsp_array_string(entry, "path");
      26          216 :         if (!cached_path || !zend_string_equals(cached_path, path)) {
      27            0 :                 return false;
      28              :         }
      29              : 
      30          216 :         if (document) {
      31           20 :                 document_version = lsp_array_long(entry, "documentVersion", -1);
      32              : 
      33           20 :                 return document_version == document->version;
      34              :         }
      35              : 
      36          196 :         if (!stat_valid) {
      37            0 :                 return false;
      38              :         }
      39              : 
      40          196 :         mtime = lsp_array_long(entry, "mtime", -1);
      41          196 :         mtime_nsec = lsp_array_long(entry, "mtimeNsec", -1);
      42          196 :         size = lsp_array_long(entry, "size", -1);
      43              : 
      44          196 :         return mtime == (zend_long) st->st_mtime &&
      45          392 :                 mtime_nsec == lsp_stat_mtime_nsec(st) &&
      46          196 :                 size == (zend_long) st->st_size
      47              :         ;
      48              : }
      49              : 
      50          348 : static inline lsp_method_visibility lsp_cached_method_visibility(HashTable *tokens, uint32_t index, zend_string *text, zend_long body_depth)
      51              : {
      52              :         zend_long i;
      53              :         zval *token;
      54              : 
      55         1016 :         for (i = (zend_long) index - 1; i >= 0; i--) {
      56         1012 :                 token = zend_hash_index_find(tokens, (zend_ulong) i);
      57         1012 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
      58            0 :                         continue;
      59              :                 }
      60              : 
      61         1012 :                 if (!lsp_token_at_depth(text, token, body_depth)) {
      62           56 :                         continue;
      63              :                 }
      64              : 
      65          956 :                 if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '{') || lsp_token_is_char(token, '}')) {
      66              :                         break;
      67              :                 }
      68              : 
      69          952 :                 if (lsp_token_name_equals(token, "T_PRIVATE")) {
      70           20 :                         return LSP_METHOD_VISIBILITY_PRIVATE;
      71              :                 }
      72              : 
      73          932 :                 if (lsp_token_name_equals(token, "T_PROTECTED")) {
      74           20 :                         return LSP_METHOD_VISIBILITY_PROTECTED;
      75              :                 }
      76              : 
      77          912 :                 if (lsp_token_name_equals(token, "T_PUBLIC")) {
      78          300 :                         return LSP_METHOD_VISIBILITY_PUBLIC;
      79              :                 }
      80              :         }
      81              : 
      82            8 :         return LSP_METHOD_VISIBILITY_PUBLIC;
      83              : }
      84              : 
      85          380 : static inline bool lsp_cached_method_is_static(HashTable *tokens, uint32_t index, zend_string *text, zend_long body_depth)
      86              : {
      87              :         zend_long i;
      88              :         zval *token;
      89              : 
      90         3964 :         for (i = (zend_long) index - 1; i >= 0; i--) {
      91         3808 :                 token = zend_hash_index_find(tokens, (zend_ulong) i);
      92         3808 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
      93            0 :                         continue;
      94              :                 }
      95              : 
      96         3808 :                 if (!lsp_token_at_depth(text, token, body_depth)) {
      97         2444 :                         continue;
      98              :                 }
      99              : 
     100         1364 :                 if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '{') || lsp_token_is_char(token, '}')) {
     101              :                         break;
     102              :                 }
     103              : 
     104         1224 :                 if (lsp_token_name_equals(token, "T_STATIC")) {
     105           84 :                         return true;
     106              :                 }
     107              :         }
     108              : 
     109          296 :         return false;
     110              : }
     111              : 
     112          164 : static inline bool lsp_promoted_property_segment_has_visibility(HashTable *tokens, uint32_t index, zend_string *text, zend_long body_depth)
     113              : {
     114              :         zend_long i;
     115              :         zval *token;
     116              : 
     117          776 :         for (i = (zend_long) index - 1; i >= 0; i--) {
     118          776 :                 token = zend_hash_index_find(tokens, (zend_ulong) i);
     119          776 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
     120            0 :                         continue;
     121              :                 }
     122              : 
     123          776 :                 if (!lsp_token_at_depth(text, token, body_depth)) {
     124          344 :                         continue;
     125              :                 }
     126              : 
     127          432 :                 if (lsp_token_is_char(token, ',') || lsp_token_is_char(token, '(')) {
     128              :                         break;
     129              :                 }
     130              : 
     131          340 :                 if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '{') || lsp_token_is_char(token, '}')) {
     132           60 :                         return false;
     133              :                 }
     134              : 
     135          552 :                 if (lsp_token_name_equals(token, "T_PUBLIC") ||
     136          544 :                         lsp_token_name_equals(token, "T_PROTECTED") ||
     137          272 :                         lsp_token_name_equals(token, "T_PRIVATE")
     138              :                 ) {
     139           12 :                         return true;
     140              :                 }
     141              :         }
     142              : 
     143           92 :         return false;
     144              : }
     145              : 
     146           12 : static inline bool lsp_promoted_property_constructor_param_start(HashTable *tokens, uint32_t index, zend_string *text, zend_long body_depth, size_t *param_start)
     147              : {
     148              :         zend_long i, j;
     149              :         zend_string *name;
     150              :         zval *token;
     151              :         bool saw_construct;
     152              : 
     153           12 :         *param_start = 0;
     154          128 :         for (i = (zend_long) index - 1; i >= 0; i--) {
     155          128 :                 token = zend_hash_index_find(tokens, (zend_ulong) i);
     156          128 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
     157            0 :                         continue;
     158              :                 }
     159              : 
     160          128 :                 if (!lsp_token_at_depth(text, token, body_depth)) {
     161            0 :                         continue;
     162              :                 }
     163              : 
     164          128 :                 if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '{') || lsp_token_is_char(token, '}')) {
     165            0 :                         return false;
     166              :                 }
     167              : 
     168          128 :                 if (!lsp_token_is_char(token, '(')) {
     169          116 :                         continue;
     170              :                 }
     171              : 
     172           12 :                 *param_start = (size_t) lsp_token_long(token, "offset", 0);
     173           12 :                 saw_construct = false;
     174           36 :                 for (j = i - 1; j >= 0; j--) {
     175           36 :                         token = zend_hash_index_find(tokens, (zend_ulong) j);
     176           36 :                         if (!token || Z_TYPE_P(token) != IS_ARRAY) {
     177            0 :                                 continue;
     178              :                         }
     179              : 
     180           36 :                         if (!lsp_token_at_depth(text, token, body_depth)) {
     181            0 :                                 continue;
     182              :                         }
     183              : 
     184           36 :                         if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '{') || lsp_token_is_char(token, '}')) {
     185            0 :                                 return false;
     186              :                         }
     187              : 
     188           36 :                         if (!saw_construct && lsp_token_name_equals(token, "T_STRING")) {
     189           12 :                                 name = lsp_token_string(token, "text");
     190           12 :                                 if (!name || !zend_string_equals_literal(name, "__construct")) {
     191            0 :                                         return false;
     192              :                                 }
     193           12 :                                 saw_construct = true;
     194           12 :                                 continue;
     195              :                         }
     196              : 
     197           24 :                         if (saw_construct && lsp_token_name_equals(token, "T_FUNCTION")) {
     198           12 :                                 return true;
     199              :                         }
     200              :                 }
     201              : 
     202            0 :                 return false;
     203              :         }
     204              : 
     205            0 :         return false;
     206              : }
     207              : 
     208          164 : static inline bool lsp_token_is_promoted_property_declaration(HashTable *tokens, uint32_t index, zend_string *text, zend_long body_depth, size_t *param_start)
     209              : {
     210          164 :         if (!lsp_promoted_property_segment_has_visibility(tokens, index, text, body_depth)) {
     211          152 :                 *param_start = 0;
     212              : 
     213          152 :                 return false;
     214              :         }
     215              : 
     216           12 :         return lsp_promoted_property_constructor_param_start(tokens, index, text, body_depth, param_start);
     217              : }
     218              : 
     219           64 : extern bool lsp_token_is_promoted_property(HashTable *tokens, uint32_t index, zend_string *text, zend_long body_depth, size_t *param_start)
     220              : {
     221           64 :         return lsp_token_is_promoted_property_declaration(tokens, index, text, body_depth, param_start);
     222              : }
     223              : 
     224           24 : static inline zend_string *lsp_member_cache_entry_member_detail(zval *entry, zend_string *member)
     225              : {
     226              :         zend_string *label, *detail;
     227              :         zval *collection, *item;
     228           24 :         const char *keys[3] = { "properties", "methods", "constants" };
     229              :         size_t key_index;
     230              : 
     231           60 :         for (key_index = 0; key_index < 3; key_index++) {
     232           52 :                 collection = zend_hash_str_find(Z_ARRVAL_P(entry), keys[key_index], strlen(keys[key_index]));
     233           52 :                 if (!collection || Z_TYPE_P(collection) != IS_ARRAY) {
     234            0 :                         continue;
     235              :                 }
     236              : 
     237           76 :                 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(collection), item) {
     238           40 :                         label = lsp_array_string(item, "label");
     239           40 :                         if (!label || !zend_string_equals(label, member)) {
     240           24 :                                 continue;
     241              :                         }
     242              : 
     243           16 :                         detail = lsp_array_string(item, "detail");
     244           16 :                         if (detail) {
     245           16 :                                 return zend_string_copy(detail);
     246              :                         }
     247              :                 } ZEND_HASH_FOREACH_END();
     248              :         }
     249              : 
     250            8 :         return NULL;
     251              : }
     252              : 
     253           20 : static zend_string *lsp_inherited_member_detail_ex(lsp_server *server, zend_string *class_name, zend_string *member, HashTable *visited, uint32_t depth)
     254              : {
     255           20 :         zend_string *current, *next, *detail = NULL;
     256              :         zval *entry, *parent, *traits, *trait_zv;
     257              : 
     258           20 :         if (depth > 16) {
     259            0 :                 return NULL;
     260              :         }
     261              : 
     262           20 :         current = zend_string_copy(class_name);
     263           28 :         while (current) {
     264           24 :                 if (zend_hash_exists(visited, current)) {
     265            0 :                         break;
     266              :                 }
     267              : 
     268           24 :                 zend_hash_add_empty_element(visited, current);
     269           24 :                 entry = lsp_class_member_cache_entry(server, current);
     270           24 :                 if (!entry || Z_TYPE_P(entry) != IS_ARRAY) {
     271              :                         break;
     272              :                 }
     273              : 
     274           24 :                 detail = lsp_member_cache_entry_member_detail(entry, member);
     275           24 :                 if (detail) {
     276           16 :                         break;
     277              :                 }
     278              : 
     279            8 :                 traits = zend_hash_str_find(Z_ARRVAL_P(entry), "traits", sizeof("traits") - 1);
     280            8 :                 if (traits && Z_TYPE_P(traits) == IS_ARRAY) {
     281            8 :                         ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(traits), trait_zv) {
     282            0 :                                 if (Z_TYPE_P(trait_zv) == IS_STRING) {
     283            0 :                                         detail = lsp_inherited_member_detail_ex(server, Z_STR_P(trait_zv), member, visited, depth + 1);
     284            0 :                                         if (detail) {
     285            0 :                                                 break;
     286              :                                         }
     287              :                                 }
     288              :                         } ZEND_HASH_FOREACH_END();
     289              :                 }
     290            8 :                 if (detail) {
     291            0 :                         break;
     292              :                 }
     293              : 
     294            8 :                 parent = zend_hash_str_find(Z_ARRVAL_P(entry), "parent", sizeof("parent") - 1);
     295            8 :                 next = parent && Z_TYPE_P(parent) == IS_STRING && Z_STRLEN_P(parent) > 0 ? zend_string_copy(Z_STR_P(parent)) : NULL;
     296            8 :                 zend_string_release(current);
     297            8 :                 current = next;
     298              :         }
     299              : 
     300           20 :         if (current) {
     301           16 :                 zend_string_release(current);
     302              :         }
     303              : 
     304           20 :         return detail;
     305              : }
     306              : 
     307              : /* Declaration detail (signature, property type, const/case) for a member of
     308              :  * `class_name`, searching the ancestor chain and traits. */
     309           20 : extern zend_string *lsp_inherited_member_detail(lsp_server *server, zend_string *class_name, zend_string *member)
     310              : {
     311              :         HashTable visited;
     312              :         zend_string *detail;
     313              : 
     314           20 :         zend_hash_init(&visited, 8, NULL, NULL, 0);
     315           20 :         detail = lsp_inherited_member_detail_ex(server, class_name, member, &visited, 0);
     316           20 :         zend_hash_destroy(&visited);
     317              : 
     318           20 :         return detail;
     319              : }
     320              : 
     321          260 : static inline void lsp_cache_class_method(zval *methods, zend_string *label, zend_string *detail, bool is_static, lsp_method_visibility visibility)
     322              : {
     323              :         zval method;
     324              : 
     325          260 :         array_init(&method);
     326          260 :         add_assoc_str(&method, "label", zend_string_copy(label));
     327          260 :         add_assoc_str(&method, "detail", zend_string_copy(detail));
     328          260 :         add_assoc_bool(&method, "static", is_static);
     329          260 :         add_assoc_long(&method, "visibility", (zend_long) visibility);
     330          260 :         add_next_index_zval(methods, &method);
     331          260 : }
     332              : 
     333           12 : static inline void lsp_cache_class_constant(zval *constants, zend_string *label, bool is_case, lsp_method_visibility visibility)
     334              : {
     335              :         zend_string *detail;
     336              :         zval constant;
     337              : 
     338           12 :         detail = strpprintf(0, "%s %s", is_case ? "case" : "const", ZSTR_VAL(label));
     339           12 :         array_init(&constant);
     340           12 :         add_assoc_str(&constant, "label", zend_string_copy(label));
     341           12 :         add_assoc_str(&constant, "detail", detail);
     342           12 :         add_assoc_bool(&constant, "case", is_case);
     343           12 :         add_assoc_long(&constant, "visibility", (zend_long) visibility);
     344           12 :         add_next_index_zval(constants, &constant);
     345           12 : }
     346              : 
     347              : /* Collect the names declared by a `const A = 1, B = 2;` statement (a name is
     348              :  * the T_STRING immediately before '='), or by an `enum case Name`. */
     349           12 : static inline void lsp_cache_class_constants_from(zval *constants, HashTable *tokens, uint32_t index, uint32_t count, bool is_case, lsp_method_visibility visibility)
     350              : {
     351           12 :         zend_string *last_name = NULL, *text;
     352              :         zval *token;
     353              :         uint32_t j;
     354              : 
     355           60 :         for (j = index + 1; j < count; j++) {
     356           60 :                 token = zend_hash_index_find(tokens, j);
     357           60 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
     358            0 :                         continue;
     359              :                 }
     360              : 
     361           60 :                 if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '}')) {
     362            4 :                         if (is_case && last_name) {
     363            0 :                                 lsp_cache_class_constant(constants, last_name, true, visibility);
     364              :                         }
     365              : 
     366            4 :                         return;
     367              :                 }
     368              : 
     369           56 :                 if (lsp_token_name_equals(token, "T_STRING")) {
     370           12 :                         text = lsp_token_string(token, "text");
     371           12 :                         if (text) {
     372           12 :                                 last_name = text;
     373              :                         }
     374           12 :                         continue;
     375              :                 }
     376              : 
     377           44 :                 if (lsp_token_is_char(token, '=') && last_name) {
     378           12 :                         lsp_cache_class_constant(constants, last_name, is_case, visibility);
     379           12 :                         if (is_case) {
     380              :                                 /* A backed case declares exactly one name; later T_STRINGs
     381              :                                  * belong to the value expression. */
     382            8 :                                 return;
     383              :                         }
     384            4 :                         last_name = NULL;
     385              :                 }
     386              :         }
     387              : }
     388              : 
     389           56 : static inline void lsp_cache_class_property(zval *properties, zend_string *label, zend_string *detail, bool is_static, lsp_method_visibility visibility)
     390              : {
     391              :         zval property;
     392              : 
     393           56 :         array_init(&property);
     394           56 :         add_assoc_str(&property, "label", zend_string_copy(label));
     395           56 :         add_assoc_str(&property, "detail", zend_string_copy(detail));
     396           56 :         add_assoc_bool(&property, "static", is_static);
     397           56 :         add_assoc_long(&property, "visibility", (zend_long) visibility);
     398           56 :         add_next_index_zval(properties, &property);
     399           56 : }
     400              : 
     401          280 : static inline void lsp_add_cached_class_member_completions_ex(lsp_server *server, zval *items, zval *entry, zend_string *member_prefix, bool public_only)
     402              : {
     403          280 :         const char *source = lsp_primary_analyzer_source(server);
     404              :         zend_long visibility;
     405              :         zend_string *label, *detail;
     406              :         zval *methods, *method, *properties, *property, *static_value, *visibility_value;
     407              :         bool property_static;
     408              : 
     409          280 :         methods = zend_hash_str_find(Z_ARRVAL_P(entry), "methods", sizeof("methods") - 1);
     410          280 :         if (methods && Z_TYPE_P(methods) == IS_ARRAY) {
     411          544 :                 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(methods), method) {
     412          264 :                         if (Z_TYPE_P(method) != IS_ARRAY) {
     413            0 :                                 continue;
     414              :                         }
     415              : 
     416          264 :                         label = lsp_array_string(method, "label");
     417          264 :                         detail = lsp_array_string(method, "detail");
     418          264 :                         visibility_value = zend_hash_str_find(Z_ARRVAL_P(method), "visibility", sizeof("visibility") - 1);
     419          264 :                         visibility = visibility_value && Z_TYPE_P(visibility_value) == IS_LONG ? Z_LVAL_P(visibility_value) : (zend_long) LSP_METHOD_VISIBILITY_PUBLIC;
     420              : 
     421          264 :                         if (!label ||
     422          264 :                                 !detail ||
     423          524 :                                 zend_string_equals_literal(label, "__construct") ||
     424          188 :                                 (public_only && visibility != (zend_long) LSP_METHOD_VISIBILITY_PUBLIC) ||
     425          260 :                                 !lsp_matches_prefix_string(label, member_prefix)
     426              :                         ) {
     427           72 :                                 continue;
     428              :                         }
     429              : 
     430          192 :                         lsp_add_completion_item_ex(items, label, 2, detail, source);
     431              :                 } ZEND_HASH_FOREACH_END();
     432              :         }
     433              : 
     434          280 :         properties = zend_hash_str_find(Z_ARRVAL_P(entry), "properties", sizeof("properties") - 1);
     435          280 :         if (!properties || Z_TYPE_P(properties) != IS_ARRAY) {
     436            0 :                 return;
     437              :         }
     438              : 
     439          332 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(properties), property) {
     440           52 :                 if (Z_TYPE_P(property) != IS_ARRAY) {
     441            0 :                         continue;
     442              :                 }
     443              : 
     444           52 :                 static_value = zend_hash_str_find(Z_ARRVAL_P(property), "static", sizeof("static") - 1);
     445           52 :                 visibility_value = zend_hash_str_find(Z_ARRVAL_P(property), "visibility", sizeof("visibility") - 1);
     446           52 :                 property_static = static_value && zend_is_true(static_value);
     447           52 :                 visibility = visibility_value && Z_TYPE_P(visibility_value) == IS_LONG ? Z_LVAL_P(visibility_value) : (zend_long) LSP_METHOD_VISIBILITY_PUBLIC;
     448           52 :                 if (property_static || (public_only && visibility != (zend_long) LSP_METHOD_VISIBILITY_PUBLIC)) {
     449            8 :                         continue;
     450              :                 }
     451              : 
     452           44 :                 label = lsp_array_string(property, "label");
     453           44 :                 detail = lsp_array_string(property, "detail");
     454              : 
     455           44 :                 if (!label || !detail || !lsp_matches_prefix_string(label, member_prefix)) {
     456           12 :                         continue;
     457              :                 }
     458              : 
     459           32 :                 lsp_add_completion_item_ex(items, label, 10, detail, source);
     460              :         } ZEND_HASH_FOREACH_END();
     461              : }
     462              : 
     463          104 : static inline void lsp_add_cached_class_member_completions(lsp_server *server, zval *items, zval *entry, zend_string *member_prefix)
     464              : {
     465          104 :         lsp_add_cached_class_member_completions_ex(server, items, entry, member_prefix, false);
     466          104 : }
     467              : 
     468          228 : static inline void lsp_add_entry_trait_member_completions(lsp_server *server, zval *items, zval *entry, zend_string *member_prefix, HashTable *visited, bool public_only)
     469              : {
     470              :         zend_string *trait_name;
     471              :         zval *traits, *trait_zv, *trait_entry;
     472              : 
     473          228 :         traits = zend_hash_str_find(Z_ARRVAL_P(entry), "traits", sizeof("traits") - 1);
     474          228 :         if (!traits || Z_TYPE_P(traits) != IS_ARRAY) {
     475            0 :                 return;
     476              :         }
     477              : 
     478          240 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(traits), trait_zv) {
     479           12 :                 if (Z_TYPE_P(trait_zv) != IS_STRING) {
     480            0 :                         continue;
     481              :                 }
     482              : 
     483           12 :                 trait_name = Z_STR_P(trait_zv);
     484           12 :                 if (zend_hash_exists(visited, trait_name)) {
     485            0 :                         continue;
     486              :                 }
     487              : 
     488           12 :                 zend_hash_add_empty_element(visited, trait_name);
     489              : 
     490           12 :                 trait_entry = lsp_class_member_cache_entry(server, trait_name);
     491           12 :                 if (!trait_entry || Z_TYPE_P(trait_entry) != IS_ARRAY) {
     492            0 :                         continue;
     493              :                 }
     494              : 
     495           12 :                 lsp_add_cached_class_member_completions_ex(server, items, trait_entry, member_prefix, public_only);
     496           12 :                 lsp_add_entry_trait_member_completions(server, items, trait_entry, member_prefix, visited, public_only);
     497              :         } ZEND_HASH_FOREACH_END();
     498              : }
     499              : 
     500          168 : static inline void lsp_add_cached_static_class_member_completions_ex(lsp_server *server, zval *items, zval *entry, zend_string *member_prefix, bool public_only)
     501              : {
     502          168 :         const char *source = lsp_primary_analyzer_source(server);
     503              :         zend_long visibility;
     504              :         zend_string *label, *detail, *static_label;
     505              :         zval *methods, *method, *static_value, *properties, *property, *constants, *constant, *case_value;
     506              :         bool property_static;
     507              : 
     508          168 :         methods = zend_hash_str_find(Z_ARRVAL_P(entry), "methods", sizeof("methods") - 1);
     509          168 :         if (methods && Z_TYPE_P(methods) == IS_ARRAY) {
     510          436 :                 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(methods), method) {
     511          268 :                         if (Z_TYPE_P(method) != IS_ARRAY) {
     512            0 :                                 continue;
     513              :                         }
     514              : 
     515          268 :                         static_value = zend_hash_str_find(Z_ARRVAL_P(method), "static", sizeof("static") - 1);
     516          268 :                         visibility = lsp_array_long(method, "visibility", (zend_long) LSP_METHOD_VISIBILITY_PUBLIC);
     517          268 :                         if (!static_value ||
     518          268 :                                 !zend_is_true(static_value) ||
     519          176 :                                 visibility == (zend_long) LSP_METHOD_VISIBILITY_PRIVATE ||
     520          168 :                                 (public_only && visibility != (zend_long) LSP_METHOD_VISIBILITY_PUBLIC)
     521              :                         ) {
     522           92 :                                 continue;
     523              :                         }
     524              : 
     525          176 :                         label = lsp_array_string(method, "label");
     526          176 :                         detail = lsp_array_string(method, "detail");
     527          176 :                         if (!label || !detail || !lsp_matches_prefix_string(label, member_prefix)) {
     528          116 :                                 continue;
     529              :                         }
     530              : 
     531           60 :                         lsp_add_completion_item_ex(items, label, 2, detail, source);
     532              :                 } ZEND_HASH_FOREACH_END();
     533              :         }
     534              : 
     535          168 :         properties = zend_hash_str_find(Z_ARRVAL_P(entry), "properties", sizeof("properties") - 1);
     536          168 :         if (properties && Z_TYPE_P(properties) == IS_ARRAY) {
     537          296 :                 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(properties), property) {
     538          128 :                         if (Z_TYPE_P(property) != IS_ARRAY) {
     539            0 :                                 continue;
     540              :                         }
     541              : 
     542          128 :                         static_value = zend_hash_str_find(Z_ARRVAL_P(property), "static", sizeof("static") - 1);
     543          128 :                         visibility = lsp_array_long(property, "visibility", (zend_long) LSP_METHOD_VISIBILITY_PUBLIC);
     544          128 :                         property_static = static_value && zend_is_true(static_value);
     545          128 :                         if (!property_static ||
     546           52 :                                 visibility == (zend_long) LSP_METHOD_VISIBILITY_PRIVATE ||
     547           44 :                                 (public_only && visibility != (zend_long) LSP_METHOD_VISIBILITY_PUBLIC)
     548              :                         ) {
     549           76 :                                 continue;
     550              :                         }
     551              : 
     552           52 :                         label = lsp_array_string(property, "label");
     553           52 :                         detail = lsp_array_string(property, "detail");
     554           52 :                         if (!label || !detail) {
     555            0 :                                 continue;
     556              :                         }
     557              : 
     558           52 :                         static_label = strpprintf(0, "$%s", ZSTR_VAL(label));
     559           52 :                         if (lsp_matches_prefix_string(static_label, member_prefix)) {
     560           28 :                                 lsp_add_completion_item_ex(items, static_label, 10, detail, source);
     561              :                         }
     562              : 
     563           52 :                         zend_string_release(static_label);
     564              :                 } ZEND_HASH_FOREACH_END();
     565              :         }
     566              : 
     567          168 :         constants = zend_hash_str_find(Z_ARRVAL_P(entry), "constants", sizeof("constants") - 1);
     568          168 :         if (constants && Z_TYPE_P(constants) == IS_ARRAY) {
     569          148 :                 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(constants), constant) {
     570            8 :                         if (Z_TYPE_P(constant) != IS_ARRAY) {
     571            0 :                                 continue;
     572              :                         }
     573              : 
     574            8 :                         visibility = lsp_array_long(constant, "visibility", (zend_long) LSP_METHOD_VISIBILITY_PUBLIC);
     575            8 :                         if (visibility == (zend_long) LSP_METHOD_VISIBILITY_PRIVATE ||
     576            8 :                                 (public_only && visibility != (zend_long) LSP_METHOD_VISIBILITY_PUBLIC)
     577              :                         ) {
     578            0 :                                 continue;
     579              :                         }
     580              : 
     581            8 :                         label = lsp_array_string(constant, "label");
     582            8 :                         detail = lsp_array_string(constant, "detail");
     583            8 :                         if (!label || !detail || !lsp_matches_prefix_string(label, member_prefix)) {
     584            0 :                                 continue;
     585              :                         }
     586              : 
     587            8 :                         case_value = zend_hash_str_find(Z_ARRVAL_P(constant), "case", sizeof("case") - 1);
     588            8 :                         lsp_add_completion_item_ex(items, label, case_value && zend_is_true(case_value) ? 20 : 21, detail, source);
     589              :                 } ZEND_HASH_FOREACH_END();
     590              :         }
     591          168 : }
     592              : 
     593           28 : static inline void lsp_add_cached_static_class_member_completions(lsp_server *server, zval *items, zval *entry, zend_string *member_prefix)
     594              : {
     595           28 :         lsp_add_cached_static_class_member_completions_ex(server, items, entry, member_prefix, true);
     596           28 : }
     597              : 
     598          140 : static inline void lsp_add_entry_trait_static_member_completions(lsp_server *server, zval *items, zval *entry, zend_string *member_prefix, HashTable *visited, bool public_only)
     599              : {
     600              :         zend_string *trait_name;
     601              :         zval *traits, *trait_zv, *trait_entry;
     602              : 
     603          140 :         traits = zend_hash_str_find(Z_ARRVAL_P(entry), "traits", sizeof("traits") - 1);
     604          140 :         if (!traits || Z_TYPE_P(traits) != IS_ARRAY) {
     605            0 :                 return;
     606              :         }
     607              : 
     608          152 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(traits), trait_zv) {
     609           12 :                 if (Z_TYPE_P(trait_zv) != IS_STRING) {
     610            0 :                         continue;
     611              :                 }
     612              : 
     613           12 :                 trait_name = Z_STR_P(trait_zv);
     614           12 :                 if (zend_hash_exists(visited, trait_name)) {
     615            0 :                         continue;
     616              :                 }
     617              : 
     618           12 :                 zend_hash_add_empty_element(visited, trait_name);
     619              : 
     620           12 :                 trait_entry = lsp_class_member_cache_entry(server, trait_name);
     621           12 :                 if (!trait_entry || Z_TYPE_P(trait_entry) != IS_ARRAY) {
     622            0 :                         continue;
     623              :                 }
     624              : 
     625           12 :                 lsp_add_cached_static_class_member_completions_ex(server, items, trait_entry, member_prefix, public_only);
     626           12 :                 lsp_add_entry_trait_static_member_completions(server, items, trait_entry, member_prefix, visited, public_only);
     627              :         } ZEND_HASH_FOREACH_END();
     628              : }
     629              : 
     630              : static inline void lsp_add_reflection_method_completions(lsp_server *server, zval *items, zend_string *text, size_t offset, zend_string *prefix)
     631              : {
     632              :         const char *source = lsp_primary_analyzer_source(server);
     633              :         zend_string *receiver, *member_prefix, *class_name, *method_name, *detail;
     634              :         zend_class_entry *ce;
     635              :         zend_function *function;
     636              : 
     637              :         if (!server->phpstan_enabled && !server->psalm_enabled && !server->psalm_ls_enabled) {
     638              :                 return;
     639              :         }
     640              : 
     641              :         if (!lsp_member_access_context(text, offset, prefix, &receiver, &member_prefix)) {
     642              :                 return;
     643              :         }
     644              : 
     645              :         class_name = lsp_infer_new_assignment_class(text, receiver, offset);
     646              :         if (!class_name) {
     647              :                 zend_string_release(receiver);
     648              :                 zend_string_release(member_prefix);
     649              : 
     650              :                 return;
     651              :         }
     652              : 
     653              :         ce = zend_lookup_class(class_name);
     654              :         if (ce) {
     655              :                 ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->function_table, method_name, function) {
     656              :                         if (!method_name || (function->common.fn_flags & ZEND_ACC_PUBLIC) == 0) {
     657              :                                 continue;
     658              :                         }
     659              : 
     660              :                         if (!lsp_matches_prefix_string(method_name, member_prefix)) {
     661              :                                 continue;
     662              :                         }
     663              : 
     664              :                         detail = strpprintf(0, "%s(...)", ZSTR_VAL(method_name));
     665              :                         lsp_add_completion_item_ex(items, method_name, 2, detail, source);
     666              :                         zend_string_release(detail);
     667              :                 } ZEND_HASH_FOREACH_END();
     668              :         }
     669              : 
     670              :         zend_string_release(class_name);
     671              :         zend_string_release(receiver);
     672              :         zend_string_release(member_prefix);
     673              : }
     674              : 
     675           12 : static inline zend_class_entry *lsp_lookup_runtime_class(zend_string *class_name)
     676              : {
     677              :         zend_class_entry *ce;
     678              :         zend_string *lookup_name;
     679              : 
     680           12 :         if (ZSTR_LEN(class_name) == 0) {
     681            0 :                 return NULL;
     682              :         }
     683              : 
     684           12 :         if (ZSTR_VAL(class_name)[0] == '\\') {
     685            0 :                 if (ZSTR_LEN(class_name) == 1) {
     686            0 :                         return NULL;
     687              :                 }
     688            0 :                 lookup_name = zend_string_init(ZSTR_VAL(class_name) + 1, ZSTR_LEN(class_name) - 1, 0);
     689              :         } else {
     690           12 :                 lookup_name = zend_string_copy(class_name);
     691              :         }
     692              : 
     693           12 :         ce = zend_lookup_class(lookup_name);
     694           12 :         zend_string_release(lookup_name);
     695              : 
     696           12 :         return ce;
     697              : }
     698              : 
     699            4 : static inline void lsp_add_reflection_class_method_completions(lsp_server *server, zval *items, zend_class_entry *ce, zend_string *member_prefix, bool public_only, bool static_only, bool include_constructor)
     700              : {
     701            4 :         const char *source = lsp_primary_analyzer_source(server);
     702              :         zend_function *function;
     703              :         zend_string *method_name, *label, *detail;
     704              :         uint32_t flags;
     705              : 
     706           48 :         ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->function_table, method_name, function) {
     707           44 :                 if (!function) {
     708            0 :                         continue;
     709              :                 }
     710              : 
     711           44 :                 flags = function->common.fn_flags;
     712           44 :                 if ((flags & ZEND_ACC_PRIVATE) != 0 ||
     713           40 :                         (public_only && (flags & ZEND_ACC_PUBLIC) == 0) ||
     714            0 :                         (static_only && (flags & ZEND_ACC_STATIC) == 0)
     715              :                 ) {
     716            4 :                         continue;
     717              :                 }
     718              : 
     719           40 :                 label = function->common.function_name ? function->common.function_name : method_name;
     720           40 :                 if (!label) {
     721            0 :                         continue;
     722              :                 }
     723              : 
     724           40 :                 if (!include_constructor && zend_string_equals_literal(label, "__construct")) {
     725            0 :                         continue;
     726              :                 }
     727              : 
     728           40 :                 if (!lsp_matches_prefix_string(label, member_prefix)) {
     729            0 :                         continue;
     730              :                 }
     731              : 
     732           40 :                 detail = strpprintf(0, "%s(...)", ZSTR_VAL(label));
     733           40 :                 lsp_add_completion_item_ex(items, label, 2, detail, source);
     734           40 :                 zend_string_release(detail);
     735              :         } ZEND_HASH_FOREACH_END();
     736            4 : }
     737              : 
     738           12 : extern void lsp_add_parent_reflection_class_member_completions(lsp_server *server, zval *items, zend_string *class_name, zend_string *member_prefix)
     739              : {
     740              :         zend_class_entry *ce;
     741              : 
     742           12 :         ce = lsp_lookup_runtime_class(class_name);
     743           12 :         if (!ce || ce->type != ZEND_INTERNAL_CLASS) {
     744            8 :                 return;
     745              :         }
     746              : 
     747            4 :         lsp_add_reflection_class_method_completions(server, items, ce, member_prefix, false, false, true);
     748              : }
     749              : 
     750          156 : extern void lsp_add_inherited_public_project_class_member_completions(lsp_server *server, zval *items, zend_string *class_name, zend_string *member_prefix)
     751              : {
     752              :         zend_string *current, *next;
     753              :         zval *entry, *parent;
     754              :         HashTable visited;
     755              : 
     756          156 :         zend_hash_init(&visited, 8, NULL, NULL, 0);
     757              : 
     758          156 :         current = zend_string_copy(class_name);
     759          320 :         while (current) {
     760          176 :                 if (zend_hash_exists(&visited, current)) {
     761            0 :                         break;
     762              :                 }
     763              : 
     764          176 :                 zend_hash_add_empty_element(&visited, current);
     765          176 :                 entry = lsp_class_member_cache_entry(server, current);
     766          176 :                 if (!entry || Z_TYPE_P(entry) != IS_ARRAY) {
     767              :                         break;
     768              :                 }
     769              : 
     770          164 :                 lsp_add_cached_class_member_completions_ex(server, items, entry, member_prefix, true);
     771          164 :                 parent = zend_hash_str_find(Z_ARRVAL_P(entry), "parent", sizeof("parent") - 1);
     772          164 :                 next = parent && Z_TYPE_P(parent) == IS_STRING && Z_STRLEN_P(parent) > 0 ? zend_string_copy(Z_STR_P(parent)) : NULL;
     773          164 :                 lsp_add_entry_trait_member_completions(server, items, entry, member_prefix, &visited, true);
     774          164 :                 zend_string_release(current);
     775          164 :                 current = next;
     776              :         }
     777              : 
     778          156 :         if (current) {
     779           12 :                 zend_string_release(current);
     780              :         }
     781              : 
     782          156 :         zend_hash_destroy(&visited);
     783          156 : }
     784              : 
     785           16 : extern void lsp_member_cache_invalidate_path(lsp_server *server, zend_string *path)
     786              : {
     787              :         zend_string *class_name, *entry_path, **doomed;
     788              :         zval *entry;
     789           16 :         uint32_t doomed_count = 0, doomed_capacity = 8, i;
     790              : 
     791           16 :         if (!path || zend_hash_num_elements(&server->member_cache) == 0) {
     792           16 :                 return;
     793              :         }
     794              : 
     795            0 :         doomed = safe_emalloc(doomed_capacity, sizeof(zend_string *), 0);
     796              : 
     797            0 :         ZEND_HASH_FOREACH_STR_KEY_VAL(&server->member_cache, class_name, entry) {
     798            0 :                 if (!class_name || Z_TYPE_P(entry) != IS_ARRAY) {
     799            0 :                         continue;
     800              :                 }
     801              : 
     802            0 :                 entry_path = lsp_array_string(entry, "path");
     803            0 :                 if (!entry_path || !zend_string_equals(entry_path, path)) {
     804            0 :                         continue;
     805              :                 }
     806              : 
     807            0 :                 if (doomed_count >= doomed_capacity) {
     808            0 :                         doomed_capacity *= 2;
     809            0 :                         doomed = safe_erealloc(doomed, doomed_capacity, sizeof(zend_string *), 0);
     810              :                 }
     811              : 
     812            0 :                 doomed[doomed_count++] = zend_string_copy(class_name);
     813              :         } ZEND_HASH_FOREACH_END();
     814              : 
     815            0 :         for (i = 0; i < doomed_count; i++) {
     816            0 :                 zend_hash_del(&server->member_cache, doomed[i]);
     817            0 :                 zend_string_release(doomed[i]);
     818              :         }
     819              : 
     820            0 :         efree(doomed);
     821              : }
     822              : 
     823          500 : extern zval *lsp_class_member_cache_entry(lsp_server *server, zend_string *class_name)
     824              : {
     825              :         lsp_document *document;
     826              :         lsp_method_visibility visibility;
     827          500 :         zend_long body_depth = 0;
     828          500 :         zend_string *path, *contents, *label, *detail, *parent_class = NULL, *variable;
     829              :         zend_stat_t st;
     830              :         zval entry, methods, properties, traits, constants, tokens_zv, *cached, *token, *name_token;
     831              :         HashTable *tokens;
     832              :         uint32_t i, count, name_index;
     833          500 :         size_t class_start = 0, body_start = 0, body_end = 0, promoted_param_start = 0;
     834              :         bool stat_valid, owns_contents, method_static, property_static, promoted_property;
     835              : 
     836          500 :         path = lsp_find_project_symbol_path(server, LSP_SYMBOL_CLASS, class_name);
     837          500 :         if (!path) {
     838           56 :                 return NULL;
     839              :         }
     840              : 
     841          444 :         document = lsp_document_for_path(server, path);
     842          444 :         stat_valid = document ? false : VCWD_STAT(ZSTR_VAL(path), &st) == 0;
     843              : 
     844          444 :         cached = zend_hash_find(&server->member_cache, class_name);
     845          444 :         if (lsp_class_member_cache_entry_is_fresh(cached, path, document, stat_valid, &st)) {
     846          212 :                 zend_string_release(path);
     847              : 
     848          212 :                 return cached;
     849              :         }
     850              : 
     851          232 :         if (cached) {
     852            4 :                 zend_hash_del(&server->member_cache, class_name);
     853              :         }
     854              : 
     855          232 :         array_init(&entry);
     856          232 :         array_init(&methods);
     857          232 :         array_init(&properties);
     858          232 :         array_init(&traits);
     859          232 :         array_init(&constants);
     860          232 :         ZVAL_UNDEF(&tokens_zv);
     861              : 
     862          232 :         contents = document ? zend_string_copy(document->text) : lsp_read_file(path);
     863          232 :         owns_contents = document || contents != zend_empty_string;
     864          232 :         if (owns_contents) {
     865          232 :                 lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
     866              : 
     867          232 :                 if (Z_TYPE(tokens_zv) == IS_ARRAY && lsp_find_class_header_for_name(contents, class_name, &class_start, &body_start, &body_end, &body_depth)) {
     868          232 :                         lsp_phpdoc_cache_class_methods(&methods, contents, class_start);
     869          232 :                         lsp_phpdoc_cache_class_properties(&properties, contents, class_start);
     870          232 :                         tokens = Z_ARRVAL(tokens_zv);
     871          232 :                         count = zend_hash_num_elements(tokens);
     872              : 
     873        11904 :                         for (i = 0; i < count; i++) {
     874        11672 :                                 token = zend_hash_index_find(tokens, i);
     875        11672 :                                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_in_bounds(token, body_start, body_end)) {
     876         4996 :                                         continue;
     877              :                                 }
     878              : 
     879         6676 :                                 if (!lsp_token_name_equals(token, "T_FUNCTION") || !lsp_token_at_depth(contents, token, body_depth)) {
     880         6412 :                                         if ((lsp_token_name_equals(token, "T_CONST") || lsp_token_name_equals(token, "T_CASE")) && lsp_token_at_depth(contents, token, body_depth)) {
     881           12 :                                                 visibility = lsp_cached_method_visibility(tokens, i, contents, body_depth);
     882           12 :                                                 if (visibility != LSP_METHOD_VISIBILITY_PRIVATE) {
     883           12 :                                                         lsp_cache_class_constants_from(&constants, tokens, i, count, lsp_token_name_equals(token, "T_CASE"), visibility);
     884              :                                                 }
     885           12 :                                                 continue;
     886              :                                         }
     887              : 
     888         6400 :                                         promoted_property = false;
     889         6400 :                                         promoted_param_start = 0;
     890         6400 :                                         if (lsp_token_name_equals(token, "T_VARIABLE") && lsp_token_at_depth(contents, token, body_depth)) {
     891          144 :                                                 if (!lsp_token_is_property_declaration(tokens, i, contents, body_depth)) {
     892           80 :                                                         promoted_property = lsp_token_is_promoted_property_declaration(tokens, i, contents, body_depth, &promoted_param_start);
     893           80 :                                                         if (!promoted_property) {
     894           72 :                                                                 continue;
     895              :                                                         }
     896              :                                                 }
     897              : 
     898           72 :                                                 visibility = lsp_cached_method_visibility(tokens, i, contents, body_depth);
     899           72 :                                                 if (visibility == LSP_METHOD_VISIBILITY_PRIVATE) {
     900           16 :                                                         continue;
     901              :                                                 }
     902           56 :                                                 property_static = promoted_property ? false : lsp_cached_method_is_static(tokens, i, contents, body_depth);
     903           56 :                                                 variable = lsp_token_string(token, "text");
     904           56 :                                                 if (!variable || ZSTR_LEN(variable) <= 1) {
     905            0 :                                                         continue;
     906              :                                                 }
     907              : 
     908           56 :                                                 label = zend_string_init(ZSTR_VAL(variable) + 1, ZSTR_LEN(variable) - 1, 0);
     909           56 :                                                 detail = promoted_property
     910            8 :                                                         ? lsp_promoted_property_completion_detail(contents, token, promoted_param_start)
     911           56 :                                                         : lsp_property_completion_detail(contents, token, property_static)
     912              :                                                 ;
     913           56 :                                                 if (!detail) {
     914            0 :                                                         continue;
     915              :                                                 }
     916           56 :                                                 lsp_cache_class_property(&properties, label, detail, property_static, visibility);
     917           56 :                                                 zend_string_release(detail);
     918           56 :                                                 zend_string_release(label);
     919              :                                         }
     920         6312 :                                         continue;
     921              :                                 }
     922              : 
     923          264 :                                 visibility = lsp_cached_method_visibility(tokens, i, contents, body_depth);
     924          264 :                                 if (visibility == LSP_METHOD_VISIBILITY_PRIVATE) {
     925            4 :                                         continue;
     926              :                                 }
     927              : 
     928          260 :                                 method_static = lsp_cached_method_is_static(tokens, i, contents, body_depth);
     929          260 :                                 name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
     930              : 
     931          260 :                                 label = lsp_token_string(name_token, "text");
     932          260 :                                 if (!label) {
     933            0 :                                         continue;
     934              :                                 }
     935              : 
     936          260 :                                 detail = lsp_function_signature_detail(contents, name_token, tokens, name_index, body_depth, NULL);
     937          260 :                                 if (!detail) {
     938            0 :                                         detail = strpprintf(0, "%s(...)", ZSTR_VAL(label));
     939              :                                 }
     940              : 
     941          260 :                                 lsp_cache_class_method(&methods, label, detail, method_static, visibility);
     942          260 :                                 zend_string_release(detail);
     943              :                         }
     944              : 
     945          232 :                         parent_class = lsp_class_extends_name(contents, class_start, body_start);
     946              : 
     947          232 :                         zval_ptr_dtor(&traits);
     948          232 :                         array_init(&traits);
     949          232 :                         lsp_collect_class_trait_names_in_bounds(contents, body_start, body_end, body_depth, &traits);
     950              :                 }
     951              : 
     952          232 :                 if (!Z_ISUNDEF(tokens_zv)) {
     953          232 :                         zval_ptr_dtor(&tokens_zv);
     954              :                 }
     955              : 
     956          232 :                 if (contents != zend_empty_string || document) {
     957          232 :                         zend_string_release(contents);
     958              :                 }
     959              :         }
     960              : 
     961          232 :         add_assoc_str(&entry, "path", zend_string_copy(path));
     962          232 :         add_assoc_long(&entry, "documentVersion", document ? document->version : -1);
     963          232 :         add_assoc_long(&entry, "mtime", stat_valid ? (zend_long) st.st_mtime : -1);
     964          232 :         add_assoc_long(&entry, "mtimeNsec", stat_valid ? lsp_stat_mtime_nsec(&st) : -1);
     965          232 :         add_assoc_long(&entry, "size", stat_valid ? (zend_long) st.st_size : -1);
     966          232 :         add_assoc_zval(&entry, "methods", &methods);
     967          232 :         add_assoc_zval(&entry, "properties", &properties);
     968          232 :         add_assoc_zval(&entry, "traits", &traits);
     969          232 :         add_assoc_zval(&entry, "constants", &constants);
     970              : 
     971          232 :         if (parent_class) {
     972           52 :                 add_assoc_str(&entry, "parent", parent_class);
     973              :         } else {
     974          180 :                 add_assoc_null(&entry, "parent");
     975              :         }
     976              : 
     977          232 :         cached = zend_hash_update(&server->member_cache, class_name, &entry);
     978          232 :         zend_string_release(path);
     979              : 
     980          232 :         return cached;
     981              : }
     982              : 
     983           28 : extern void lsp_add_inherited_project_class_member_completions(lsp_server *server, zval *items, zend_string *class_name, zend_string *member_prefix)
     984              : {
     985              :         zend_string *current, *next;
     986              :         zval *entry, *parent;
     987              :         HashTable visited;
     988              : 
     989           28 :         zend_hash_init(&visited, 8, NULL, NULL, 0);
     990              : 
     991           28 :         current = zend_string_copy(class_name);
     992           76 :         while (current) {
     993           48 :                 if (zend_hash_exists(&visited, current)) {
     994            0 :                         break;
     995              :                 }
     996              : 
     997           48 :                 zend_hash_add_empty_element(&visited, current);
     998           48 :                 entry = lsp_class_member_cache_entry(server, current);
     999           48 :                 if (!entry || Z_TYPE_P(entry) != IS_ARRAY) {
    1000              :                         break;
    1001              :                 }
    1002              : 
    1003           48 :                 lsp_add_cached_class_member_completions(server, items, entry, member_prefix);
    1004           48 :                 parent = zend_hash_str_find(Z_ARRVAL_P(entry), "parent", sizeof("parent") - 1);
    1005           48 :                 next = parent && Z_TYPE_P(parent) == IS_STRING && Z_STRLEN_P(parent) > 0 ? zend_string_copy(Z_STR_P(parent)) : NULL;
    1006           48 :                 lsp_add_entry_trait_member_completions(server, items, entry, member_prefix, &visited, false);
    1007           48 :                 zend_string_release(current);
    1008           48 :                 current = next;
    1009              :         }
    1010              : 
    1011           28 :         if (current) {
    1012            0 :                 zend_string_release(current);
    1013              :         }
    1014              : 
    1015           28 :         zend_hash_destroy(&visited);
    1016           28 : }
    1017              : 
    1018          104 : static inline void lsp_add_static_project_class_member_completions_ex(lsp_server *server, zval *items, zend_string *class_name, zend_string *member_prefix, bool public_only)
    1019              : {
    1020              :         zend_string *current, *next;
    1021              :         zval *entry, *parent;
    1022              :         HashTable visited;
    1023              : 
    1024          104 :         zend_hash_init(&visited, 8, NULL, NULL, 0);
    1025              : 
    1026          104 :         current = zend_string_copy(class_name);
    1027          232 :         while (current) {
    1028          164 :                 if (zend_hash_exists(&visited, current)) {
    1029            0 :                         break;
    1030              :                 }
    1031              : 
    1032          164 :                 zend_hash_add_empty_element(&visited, current);
    1033          164 :                 entry = lsp_class_member_cache_entry(server, current);
    1034          164 :                 if (!entry || Z_TYPE_P(entry) != IS_ARRAY) {
    1035              :                         break;
    1036              :                 }
    1037              : 
    1038          128 :                 lsp_add_cached_static_class_member_completions_ex(server, items, entry, member_prefix, public_only);
    1039          128 :                 parent = zend_hash_str_find(Z_ARRVAL_P(entry), "parent", sizeof("parent") - 1);
    1040          128 :                 next = parent && Z_TYPE_P(parent) == IS_STRING && Z_STRLEN_P(parent) > 0 ? zend_string_copy(Z_STR_P(parent)) : NULL;
    1041          128 :                 lsp_add_entry_trait_static_member_completions(server, items, entry, member_prefix, &visited, public_only);
    1042          128 :                 zend_string_release(current);
    1043          128 :                 current = next;
    1044              :         }
    1045              : 
    1046          104 :         if (current) {
    1047           36 :                 zend_string_release(current);
    1048              :         }
    1049              : 
    1050          104 :         zend_hash_destroy(&visited);
    1051          104 : }
    1052              : 
    1053           92 : extern void lsp_add_static_project_class_member_completions(lsp_server *server, zval *items, zend_string *class_name, zend_string *member_prefix)
    1054              : {
    1055           92 :         lsp_add_static_project_class_member_completions_ex(server, items, class_name, member_prefix, true);
    1056           92 : }
    1057              : 
    1058              : /* parent:: legally calls inherited INSTANCE methods (parent::render(),
    1059              :  * parent::__construct()); walk the ancestor chain adding non-private
    1060              :  * methods, constructors included. */
    1061           12 : extern void lsp_add_inherited_project_class_method_completions(lsp_server *server, zval *items, zend_string *class_name, zend_string *member_prefix)
    1062              : {
    1063           12 :         const char *source = lsp_primary_analyzer_source(server);
    1064              :         zend_long visibility;
    1065              :         zend_string *current, *next, *label, *detail;
    1066              :         zval *entry, *parent, *methods, *method;
    1067              :         HashTable visited;
    1068              : 
    1069           12 :         zend_hash_init(&visited, 8, NULL, NULL, 0);
    1070              : 
    1071           12 :         current = zend_string_copy(class_name);
    1072           16 :         while (current) {
    1073           12 :                 if (zend_hash_exists(&visited, current)) {
    1074            0 :                         break;
    1075              :                 }
    1076              : 
    1077           12 :                 zend_hash_add_empty_element(&visited, current);
    1078           12 :                 entry = lsp_class_member_cache_entry(server, current);
    1079           12 :                 if (!entry || Z_TYPE_P(entry) != IS_ARRAY) {
    1080              :                         break;
    1081              :                 }
    1082              : 
    1083            4 :                 methods = zend_hash_str_find(Z_ARRVAL_P(entry), "methods", sizeof("methods") - 1);
    1084            4 :                 if (methods && Z_TYPE_P(methods) == IS_ARRAY) {
    1085           16 :                         ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(methods), method) {
    1086           12 :                                 if (Z_TYPE_P(method) != IS_ARRAY) {
    1087            0 :                                         continue;
    1088              :                                 }
    1089              : 
    1090           12 :                                 label = lsp_array_string(method, "label");
    1091           12 :                                 detail = lsp_array_string(method, "detail");
    1092           12 :                                 visibility = lsp_array_long(method, "visibility", (zend_long) LSP_METHOD_VISIBILITY_PUBLIC);
    1093           12 :                                 if (!label ||
    1094           12 :                                         !detail ||
    1095           12 :                                         visibility == (zend_long) LSP_METHOD_VISIBILITY_PRIVATE ||
    1096           12 :                                         !lsp_matches_prefix_string(label, member_prefix)
    1097              :                                 ) {
    1098            0 :                                         continue;
    1099              :                                 }
    1100              : 
    1101           12 :                                 lsp_add_completion_item_ex(items, label, 2, detail, source);
    1102              :                         } ZEND_HASH_FOREACH_END();
    1103              :                 }
    1104              : 
    1105            4 :                 lsp_add_entry_trait_member_completions(server, items, entry, member_prefix, &visited, false);
    1106            4 :                 parent = zend_hash_str_find(Z_ARRVAL_P(entry), "parent", sizeof("parent") - 1);
    1107            4 :                 next = parent && Z_TYPE_P(parent) == IS_STRING && Z_STRLEN_P(parent) > 0 ? zend_string_copy(Z_STR_P(parent)) : NULL;
    1108            4 :                 zend_string_release(current);
    1109            4 :                 current = next;
    1110              :         }
    1111              : 
    1112           12 :         if (current) {
    1113            8 :                 zend_string_release(current);
    1114              :         }
    1115              : 
    1116           12 :         zend_hash_destroy(&visited);
    1117           12 : }
    1118              : 
    1119           12 : extern void lsp_add_inherited_static_project_class_member_completions(lsp_server *server, zval *items, zend_string *class_name, zend_string *member_prefix)
    1120              : {
    1121           12 :         lsp_add_static_project_class_member_completions_ex(server, items, class_name, member_prefix, false);
    1122           12 : }
    1123              : 
    1124           84 : extern void lsp_add_current_class_phpdoc_member_completions(lsp_server *server, zval *items, lsp_document *document, size_t class_start, zend_string *member_prefix, bool static_access)
    1125              : {
    1126              :         zval entry, methods, properties;
    1127              : 
    1128           84 :         array_init(&entry);
    1129           84 :         array_init(&methods);
    1130           84 :         array_init(&properties);
    1131           84 :         lsp_phpdoc_cache_class_methods(&methods, document->text, class_start);
    1132           84 :         lsp_phpdoc_cache_class_properties(&properties, document->text, class_start);
    1133           84 :         add_assoc_zval(&entry, "methods", &methods);
    1134           84 :         add_assoc_zval(&entry, "properties", &properties);
    1135              : 
    1136           84 :         if (static_access) {
    1137           28 :                 lsp_add_cached_static_class_member_completions(server, items, &entry, member_prefix);
    1138              :         } else {
    1139           56 :                 lsp_add_cached_class_member_completions(server, items, &entry, member_prefix);
    1140              :         }
    1141              : 
    1142           84 :         zval_ptr_dtor(&entry);
    1143           84 : }
    1144              : 
    1145           28 : extern void lsp_add_current_static_member_completions(zval *items, lsp_document *document, size_t body_start, size_t body_end, zend_long body_depth, zend_string *member_prefix)
    1146              : {
    1147              :         zend_string *label, *variable, *detail;
    1148              :         zval *tokens_zv, *token, *name_token;
    1149              :         HashTable *tokens;
    1150              :         uint32_t i, count, name_index;
    1151              : 
    1152           28 :         tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
    1153           28 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
    1154            0 :                 return;
    1155              :         }
    1156              : 
    1157           28 :         tokens = Z_ARRVAL_P(tokens_zv);
    1158              : 
    1159           28 :         count = zend_hash_num_elements(tokens);
    1160         2300 :         for (i = 0; i < count; i++) {
    1161         2272 :                 token = zend_hash_index_find(tokens, i);
    1162         2272 :                 label = NULL;
    1163         2272 :                 detail = NULL;
    1164              : 
    1165         2272 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_in_bounds(token, body_start, body_end)) {
    1166          644 :                         continue;
    1167              :                 }
    1168              : 
    1169         1672 :                 if (lsp_token_name_equals(token, "T_FUNCTION") &&
    1170           88 :                         lsp_token_at_depth(document->text, token, body_depth) &&
    1171           44 :                         lsp_cached_method_is_static(tokens, i, document->text, body_depth)
    1172              :                 ) {
    1173           16 :                         name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
    1174           16 :                         label = lsp_token_string(name_token, "text");
    1175           16 :                         if (!label || zend_string_equals_literal(label, "__construct") || !lsp_matches_prefix_string(label, member_prefix)) {
    1176           12 :                                 continue;
    1177              :                         }
    1178              : 
    1179            4 :                         detail = lsp_function_signature_detail(document->text, name_token, tokens, name_index, body_depth, NULL);
    1180            4 :                         if (!detail) {
    1181            0 :                                 detail = strpprintf(0, "%s(...)", ZSTR_VAL(label));
    1182              :                         }
    1183              : 
    1184            4 :                         lsp_add_completion_item_ex(items, label, 2, detail, "lsparrot");
    1185            4 :                         zend_string_release(detail);
    1186              : 
    1187            4 :                         continue;
    1188              :                 }
    1189              : 
    1190         1704 :                 if (lsp_token_name_equals(token, "T_VARIABLE") &&
    1191          136 :                         lsp_token_at_depth(document->text, token, body_depth) &&
    1192           72 :                         lsp_token_is_property_declaration(tokens, i, document->text, body_depth) &&
    1193           28 :                         lsp_cached_method_is_static(tokens, i, document->text, body_depth)
    1194              :                 ) {
    1195           28 :                         variable = lsp_token_string(token, "text");
    1196           28 :                         if (!variable || ZSTR_LEN(variable) <= 1 || !lsp_matches_prefix_string(variable, member_prefix)) {
    1197           20 :                                 continue;
    1198              :                         }
    1199              : 
    1200            8 :                         detail = lsp_property_completion_detail(document->text, token, true);
    1201            8 :                         if (!detail) {
    1202            0 :                                 continue;
    1203              :                         }
    1204            8 :                         lsp_add_completion_item_ex(items, variable, 10, detail, "lsparrot");
    1205            8 :                         zend_string_release(detail);
    1206              :                 }
    1207              :         }
    1208              : }
    1209              : 
    1210          212 : extern void lsp_add_inferred_member_completions(lsp_server *server, zval *items, lsp_document *document, size_t offset, zend_string *prefix)
    1211              : {
    1212          212 :         const char *source = lsp_primary_analyzer_source(server);
    1213              :         zend_class_entry *ce;
    1214              :         zend_function *function;
    1215              :         zend_string *member_prefix, *class_name, *method_name, *detail;
    1216              : 
    1217          212 :         if (!lsp_member_access_class_context(server, document, offset, prefix, &class_name, &member_prefix)) {
    1218           60 :                 return;
    1219              :         }
    1220              : 
    1221          152 :         ce = zend_lookup_class(class_name);
    1222          152 :         if (ce) {
    1223          212 :                 ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->function_table, method_name, function) {
    1224          200 :                         if (!method_name || (function->common.fn_flags & ZEND_ACC_PUBLIC) == 0) {
    1225            0 :                                 continue;
    1226              :                         }
    1227              : 
    1228          200 :                         if (zend_string_equals_literal(method_name, "__construct")) {
    1229            8 :                                 continue;
    1230              :                         }
    1231              : 
    1232          192 :                         if (!lsp_matches_prefix_string(method_name, member_prefix)) {
    1233            0 :                                 continue;
    1234              :                         }
    1235              : 
    1236          192 :                         detail = strpprintf(0, "%s(...)", ZSTR_VAL(method_name));
    1237              : 
    1238          192 :                         lsp_add_completion_item_ex(items, method_name, 2, detail, source);
    1239              : 
    1240          192 :                         zend_string_release(detail);
    1241              :                 } ZEND_HASH_FOREACH_END();
    1242              :         }
    1243              : 
    1244          152 :         lsp_add_inherited_public_project_class_member_completions(server, items, class_name, member_prefix);
    1245              : 
    1246          152 :         zend_string_release(class_name);
    1247          152 :         zend_string_release(member_prefix);
    1248              : }
    1249              : 
    1250          212 : extern void lsp_add_this_member_completions(lsp_server *server, zval *items, lsp_document *document, size_t offset, zend_string *prefix)
    1251              : {
    1252          212 :         zend_long body_depth = 0;
    1253              :         zend_string *receiver, *member_prefix, *label, *variable, *detail, *parent_class;
    1254              :         zval *tokens_zv, *token, *name_token, *trait_zv, traits;
    1255              :         HashTable *tokens;
    1256              :         uint32_t i, count, name_index;
    1257          212 :         size_t class_start = 0, body_start = 0, body_end = 0, promoted_param_start = 0;
    1258              :         bool promoted_property;
    1259              : 
    1260          212 :         if (!lsp_member_access_context(document->text, offset, prefix, &receiver, &member_prefix)) {
    1261          156 :                 return;
    1262              :         }
    1263              : 
    1264          156 :         if (!zend_string_equals_literal(receiver, "$this")) {
    1265          100 :                 zend_string_release(receiver);
    1266          100 :                 zend_string_release(member_prefix);
    1267              : 
    1268          100 :                 return;
    1269              :         }
    1270              : 
    1271           56 :         zend_string_release(receiver);
    1272              : 
    1273           56 :         if (!lsp_find_enclosing_class_header(document->text, offset, &class_start, &body_start, &body_end, &body_depth)) {
    1274            0 :                 zend_string_release(member_prefix);
    1275              : 
    1276            0 :                 return;
    1277              :         }
    1278              : 
    1279           56 :         tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
    1280           56 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
    1281            0 :                 zend_string_release(member_prefix);
    1282              : 
    1283            0 :                 return;
    1284              :         }
    1285              : 
    1286           56 :         tokens = Z_ARRVAL_P(tokens_zv);
    1287           56 :         count = zend_hash_num_elements(tokens);
    1288         4108 :         for (i = 0; i < count; i++) {
    1289         4052 :                 token = zend_hash_index_find(tokens, i);
    1290         4052 :                 label = NULL;
    1291         4052 :                 detail = NULL;
    1292              : 
    1293         4052 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_in_bounds(token, body_start, body_end)) {
    1294         1744 :                         continue;
    1295              :                 }
    1296              : 
    1297         2308 :                 if (lsp_token_name_equals(token, "T_FUNCTION") && lsp_token_at_depth(document->text, token, body_depth)) {
    1298           92 :                         name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
    1299           92 :                         label = lsp_token_string(name_token, "text");
    1300           92 :                         if (!label || !lsp_matches_prefix_string(label, member_prefix)) {
    1301           52 :                                 continue;
    1302              :                         }
    1303              : 
    1304           40 :                         detail = lsp_function_signature_detail(document->text, name_token, tokens, name_index, body_depth, NULL);
    1305           40 :                         if (!detail) {
    1306            0 :                                 detail = strpprintf(0, "%s(...)", ZSTR_VAL(label));
    1307              :                         }
    1308              : 
    1309           40 :                         lsp_add_completion_item_ex(items, label, 2, detail, "lsparrot");
    1310           40 :                         zend_string_release(detail);
    1311           40 :                         continue;
    1312              :                 }
    1313              : 
    1314         2216 :                 promoted_property = false;
    1315         2216 :                 promoted_param_start = 0;
    1316         2216 :                 if (lsp_token_name_equals(token, "T_VARIABLE") && lsp_token_at_depth(document->text, token, body_depth)) {
    1317           40 :                         if (!lsp_token_is_property_declaration(tokens, i, document->text, body_depth)) {
    1318           20 :                                 promoted_property = lsp_token_is_promoted_property_declaration(tokens, i, document->text, body_depth, &promoted_param_start);
    1319           20 :                                 if (!promoted_property) {
    1320           20 :                                         continue;
    1321              :                                 }
    1322              :                         }
    1323              : 
    1324           20 :                         variable = lsp_token_string(token, "text");
    1325           20 :                         if (!variable || ZSTR_LEN(variable) <= 1) {
    1326            0 :                                 continue;
    1327              :                         }
    1328              : 
    1329           20 :                         label = zend_string_init(ZSTR_VAL(variable) + 1, ZSTR_LEN(variable) - 1, 0);
    1330           20 :                         if (!lsp_matches_prefix_string(label, member_prefix)) {
    1331           12 :                                 zend_string_release(label);
    1332           12 :                                 continue;
    1333              :                         }
    1334              : 
    1335            8 :                         detail = promoted_property
    1336            0 :                                 ? lsp_promoted_property_completion_detail(document->text, token, promoted_param_start)
    1337            8 :                                 : lsp_property_completion_detail(document->text, token, false)
    1338              :                         ;
    1339            8 :                         if (!detail) {
    1340            0 :                                 zend_string_release(label);
    1341            0 :                                 continue;
    1342              :                         }
    1343            8 :                         lsp_add_completion_item_ex(items, label, 10, detail, "lsparrot");
    1344            8 :                         zend_string_release(detail);
    1345            8 :                         zend_string_release(label);
    1346              :                 }
    1347              :         }
    1348              : 
    1349           56 :         lsp_add_current_class_phpdoc_member_completions(server, items, document, class_start, member_prefix, false);
    1350           56 :         parent_class = lsp_class_extends_name(document->text, class_start, body_start);
    1351           56 :         if (parent_class) {
    1352           28 :                 lsp_add_inherited_project_class_member_completions(server, items, parent_class, member_prefix);
    1353           28 :                 zend_string_release(parent_class);
    1354              :         }
    1355              : 
    1356              :         /* Members brought in by the current class's own traits. */
    1357           56 :         array_init(&traits);
    1358           56 :         lsp_collect_class_trait_names_in_bounds(document->text, body_start, body_end, body_depth, &traits);
    1359           56 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL(traits), trait_zv) {
    1360            0 :                 if (Z_TYPE_P(trait_zv) == IS_STRING) {
    1361            0 :                         lsp_add_inherited_project_class_member_completions(server, items, Z_STR_P(trait_zv), member_prefix);
    1362              :                 }
    1363              :         } ZEND_HASH_FOREACH_END();
    1364           56 :         zval_ptr_dtor(&traits);
    1365              : 
    1366           56 :         zend_string_release(member_prefix);
    1367              : }
        

Generated by: LCOV version 2.0-1