MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / process_foreach

Function process_foreach

internal/cbm/lsp/php_lsp.c:1505–1601  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1503}
1504
1505static void process_foreach(PHPLSPContext *ctx, TSNode node) {
1506 /* Try to extract the iterable expression's element type. tree-sitter-php
1507 * emits the iterable as the first sibling expression after `foreach (`,
1508 * and the loop var is the variable_name child after `as`.
1509 *
1510 * Element-type resolution rules:
1511 * - iterable is array<T> / list<T> / iterable<T> (TEMPLATE) → T
1512 * - iterable is array<K, V> (TEMPLATE arg_count=2) → V (value)
1513 * - iterable is a NAMED collection-like type whose `current()` method
1514 * has a known return type (e.g. Iterator) → that
1515 * - otherwise → UNKNOWN
1516 */
1517 TSNode iterable;
1518 memset(&iterable, 0, sizeof(iterable));
1519 TSNode loop_vars[4];
1520 int loop_var_count = 0;
1521 memset(loop_vars, 0, sizeof(loop_vars));
1522
1523 /* tree-sitter-php emits foreach_statement children in source order:
1524 * <iterable expression> ... `as` ... <loop var(s)> ... <body>
1525 * The iterable can be ANY expression: variable_name, member_call,
1526 * function_call_expression, member_access_expression, etc. The loop
1527 * vars are always variable_name. The body is compound_statement /
1528 * colon_block / a statement.
1529 *
1530 * We find the iterable as the first named child whose kind is an
1531 * expression-shape; subsequent variable_name children before the body
1532 * are the loop vars. */
1533 uint32_t nc = ts_node_child_count(node);
1534 for (uint32_t i = 0; i < nc; i++) {
1535 TSNode c = ts_node_child(node, i);
1536 if (ts_node_is_null(c) || !ts_node_is_named(c))
1537 continue;
1538 const char *k = ts_node_type(c);
1539 if (strcmp(k, "compound_statement") == 0 || strcmp(k, "colon_block") == 0 ||
1540 strcmp(k, "expression_statement") == 0 || strcmp(k, "echo_statement") == 0 ||
1541 strcmp(k, "return_statement") == 0 || strcmp(k, "if_statement") == 0 ||
1542 strcmp(k, "for_statement") == 0 || strcmp(k, "while_statement") == 0 ||
1543 strcmp(k, "switch_statement") == 0 || strcmp(k, "try_statement") == 0) {
1544 break; /* reached body */
1545 }
1546 if (ts_node_is_null(iterable)) {
1547 iterable = c;
1548 } else if (strcmp(k, "variable_name") == 0 && loop_var_count < 4) {
1549 loop_vars[loop_var_count++] = c;
1550 } else if (strcmp(k, "pair") == 0) {
1551 /* `$k => $v` — extract both variable_names; the second is the
1552 * value, gets the elem_type. */
1553 uint32_t pnc = ts_node_child_count(c);
1554 for (uint32_t j = 0; j < pnc; j++) {
1555 TSNode pc = ts_node_child(c, j);
1556 if (!ts_node_is_null(pc) && ts_node_is_named(pc) &&
1557 strcmp(ts_node_type(pc), "variable_name") == 0 && loop_var_count < 4) {
1558 loop_vars[loop_var_count++] = pc;
1559 }
1560 }
1561 }
1562 }

Callers 1

Calls 5

cbm_type_unknownFunction · 0.85
php_eval_expr_typeFunction · 0.85
php_lookup_methodFunction · 0.85
php_node_textFunction · 0.85
cbm_scope_bindFunction · 0.85

Tested by

no test coverage detected