Walk an `if_statement` (or `else_if_clause`) honoring narrowing. * * Tree-sitter-php's `if_statement` emits children as: an optional `if` * keyword, a parenthesized_expression (condition), and one or more * statement-shaped bodies (the if-body, optionally followed by * else_if_clause / else_clause). We narrow on the condition then walk * EVERY non-condition named child as the body — only the
| 2242 | * scope sequentially for the remainder of the enclosing function. |
| 2243 | */ |
| 2244 | static void process_if_statement(PHPLSPContext *ctx, TSNode node) { |
| 2245 | /* Find condition: first parenthesized_expression child. */ |
| 2246 | TSNode cond; |
| 2247 | memset(&cond, 0, sizeof(cond)); |
| 2248 | uint32_t nc = ts_node_child_count(node); |
| 2249 | for (uint32_t i = 0; i < nc; i++) { |
| 2250 | TSNode c = ts_node_child(node, i); |
| 2251 | if (ts_node_is_null(c) || !ts_node_is_named(c)) |
| 2252 | continue; |
| 2253 | if (strcmp(ts_node_type(c), "parenthesized_expression") == 0) { |
| 2254 | cond = c; |
| 2255 | break; |
| 2256 | } |
| 2257 | } |
| 2258 | /* Collect all conjunctive narrowings: `$x instanceof Foo && is_int($y)` |
| 2259 | * narrows BOTH $x and $y in the if-body. */ |
| 2260 | php_narrowing_t nws[8] = {{0}}; |
| 2261 | int nw_count = ts_node_is_null(cond) ? 0 : parse_narrowing_collect(ctx, cond, nws, 8); |
| 2262 | bool has_nw = nw_count > 0; |
| 2263 | |
| 2264 | /* Detect negative narrowing: condition is `!P` and P is a narrowing |
| 2265 | * predicate. We unwrap one level of unary `!`. */ |
| 2266 | php_narrowing_t neg_nw = {0}; |
| 2267 | bool has_neg_nw = false; |
| 2268 | if (!ts_node_is_null(cond)) { |
| 2269 | TSNode inner; |
| 2270 | memset(&inner, 0, sizeof(inner)); |
| 2271 | /* Unwrap parenthesized_expression. */ |
| 2272 | TSNode probe = cond; |
| 2273 | for (int loop = 0; loop < 4; loop++) { |
| 2274 | if (strcmp(ts_node_type(probe), "parenthesized_expression") == 0) { |
| 2275 | uint32_t pnc = ts_node_child_count(probe); |
| 2276 | TSNode next; |
| 2277 | memset(&next, 0, sizeof(next)); |
| 2278 | for (uint32_t i = 0; i < pnc; i++) { |
| 2279 | TSNode c = ts_node_child(probe, i); |
| 2280 | if (!ts_node_is_null(c) && ts_node_is_named(c)) { |
| 2281 | next = c; |
| 2282 | break; |
| 2283 | } |
| 2284 | } |
| 2285 | if (ts_node_is_null(next)) |
| 2286 | break; |
| 2287 | probe = next; |
| 2288 | } else { |
| 2289 | break; |
| 2290 | } |
| 2291 | } |
| 2292 | if (strcmp(ts_node_type(probe), "unary_op_expression") == 0) { |
| 2293 | /* Find the operator child; if it's `!`, parse the inner expr. */ |
| 2294 | TSNode op = ts_node_child_by_field_name(probe, "operator", 8); |
| 2295 | char *opt = !ts_node_is_null(op) ? php_node_text(ctx, op) : NULL; |
| 2296 | if (opt && strcmp(opt, "!") == 0) { |
| 2297 | TSNode operand = ts_node_child_by_field_name(probe, "operand", 7); |
| 2298 | if (ts_node_is_null(operand)) { |
| 2299 | uint32_t unc = ts_node_child_count(probe); |
| 2300 | for (uint32_t i = 0; i < unc; i++) { |
| 2301 | TSNode c = ts_node_child(probe, i); |
no test coverage detected