mir-analyzer 0.66.1

Analysis engine for the mir PHP static analyzer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
/// Type narrowing — refines variable types based on conditional expressions.
///
/// Given a condition expression and a branch direction (true/false), this
/// module updates the `FlowState` to narrow variable types accordingly.
use php_ast::ast::{AssignOp, BinaryOp, UnaryPrefixOp};
use php_ast::owned::ExprKind;

use mir_types::Atomic;

use crate::db::MirDatabase;
use crate::flow_state::FlowState;

mod arrays;
mod assertions;
mod class_const_compare;
mod class_introspection;
mod core;
mod enum_class;
mod instanceof_core;
mod instanceof_disjuncts;
mod literals;
mod strings;
mod type_fn;

use arrays::{
    array_access_base_target, extract_array_key_first_or_last_arg,
    extract_array_key_first_or_last_static_prop_arg, extract_haystack_type,
    haystack_admits_null_loosely, in_array_loose_narrowing_is_safe, narrow_array_count_condition,
    narrow_array_emptiness_condition, narrow_array_key_exists_condition,
    narrow_array_key_first_or_last_null, narrow_container_non_null_non_false,
    narrow_empty_shape_key, narrow_in_array_condition, narrow_isset_shape_key,
    narrow_isset_shape_key_false, narrow_prop_array_key_first_or_last_null,
    narrow_static_prop_array_key_first_or_last_null, narrow_to_haystack_values,
    strip_haystack_null,
};
pub(crate) use assertions::apply_one_assertion;
use assertions::{
    apply_docblock_assertions, apply_method_docblock_assertions, method_call_receiver_fqcn,
    resolve_static_call_class_fqcn,
};
use class_const_compare::narrow_from_static_or_class_const_comparison;
pub(crate) use class_const_compare::narrow_match_arm_from_dynamic_class_const;
use class_introspection::{
    extract_dynamic_class_const_static_prop_var, extract_dynamic_class_const_var,
    extract_get_class_arg, extract_get_class_static_prop_arg, extract_get_debug_type_arg,
    extract_get_debug_type_static_prop_arg, extract_get_parent_class_arg,
    extract_get_parent_class_static_prop_arg, extract_gettype_arg, extract_gettype_static_prop_arg,
    narrow_from_get_debug_type_literal, narrow_from_get_parent_class_literal,
    narrow_from_gettype_literal, narrow_static_prop_from_get_debug_type_literal,
    narrow_static_prop_from_gettype_literal,
};
pub(crate) use core::{
    apply_prop_narrowed, chained_prop_receiver_key, extract_any_prop_access,
    extract_chained_prop_access, extract_class_fqcn_from_expr, extract_expr_guard_key,
    extract_prop_access, extract_static_prop_access, is_numeric_string,
    narrow_receiver_non_null_on_prop_match, resolve_prop_current_type,
    resolve_static_prop_current_type, MatchSubject,
};
use core::{
    extract_class_name, extract_null_coalesce, extract_nullsafe_prop_access, extract_var_name,
    narrow_count_or_strlen_equality, promote_assignment_effects, same_literal, set_narrowed,
    ScalarArgTarget, UnionNarrowExt,
};
use enum_class::{
    extract_enum_value_case, narrow_prop_to_specific_class, narrow_static_prop_to_specific_class,
    narrow_var_to_literal_enum_case, narrow_var_to_specific_class,
};
use instanceof_core::{
    filter_out_instanceof_match, filter_out_is_a_string_match,
    narrow_instanceof_preserving_subtypes, narrow_prop_instanceof, narrow_prop_is_a,
    narrow_prop_is_subclass_of, narrow_static_prop_instanceof, narrow_static_prop_is_a,
    narrow_static_prop_is_subclass_of, narrow_strict_subclass_of, partition_is_a_string_like,
};
pub(crate) use instanceof_disjuncts::{
    narrow_instanceof_disjuncts, narrow_mixed_disjuncts, narrow_mixed_prop_disjuncts,
    narrow_mixed_static_prop_disjuncts, narrow_prop_instanceof_disjuncts,
    narrow_prop_type_fn_disjuncts, narrow_static_prop_instanceof_disjuncts,
    narrow_static_prop_type_fn_disjuncts, narrow_type_fn_disjuncts,
};
use instanceof_disjuncts::{narrow_or_instanceof_true, narrow_or_isset_true};
use literals::{
    atom_safe_for_loose_int_narrowing, bool_narrow_type, expr_is_nonempty_string_literal,
    extract_int_literal, flip_comparison_op, is_truthy_bool_literal, literal_int_narrow_type,
    literal_string_narrow_type, narrow_prop_bool, narrow_prop_int_comparison,
    narrow_prop_literal_int, narrow_prop_literal_string, narrow_prop_loose_bool,
    narrow_prop_loose_int, narrow_prop_loose_null, narrow_static_prop_int_comparison,
    narrow_string_to_non_empty, narrow_var_bool, narrow_var_int_comparison, narrow_var_literal_int,
    narrow_var_literal_string, narrow_var_loose_bool, narrow_var_loose_int, narrow_var_loose_null,
    narrow_var_null,
};
use strings::{
    extract_strlen_arg, extract_strlen_static_prop_arg, narrow_prop_string_strlen_comparison,
    narrow_static_prop_string_strlen_comparison, narrow_string_false_comparable_condition,
    narrow_string_strlen_comparison,
};
use type_fn::{narrow_from_type_fn, narrow_prop_from_type_fn, narrow_static_prop_from_type_fn};

// ---------------------------------------------------------------------------
// Public entry point
// ---------------------------------------------------------------------------

/// Narrow the types in `ctx` as if `expr` evaluates to `is_true`.
pub fn narrow_from_condition(
    expr: &php_ast::owned::Expr,
    ctx: &mut FlowState,
    is_true: bool,
    db: &dyn MirDatabase,
    file: &str,
) {
    match &expr.kind {
        // Parenthesized — unwrap and narrow the inner expression
        ExprKind::Parenthesized(inner) => {
            narrow_from_condition(inner, ctx, is_true, db, file);
        }

        // !expr  →  narrow as if expr is !is_true
        ExprKind::UnaryPrefix(u) if u.op == UnaryPrefixOp::BooleanNot => {
            narrow_from_condition(&u.operand, ctx, !is_true, db, file);
        }

        // $a && $b  →  if true: narrow both; if false: no constraint
        ExprKind::Binary(b) if b.op == BinaryOp::BooleanAnd || b.op == BinaryOp::LogicalAnd => {
            if is_true {
                narrow_from_condition(&b.left, ctx, true, db, file);
                narrow_from_condition(&b.right, ctx, true, db, file);
                // When A && B is true, both sides were evaluated.
                // Promote variables from possibly_assigned to assigned for side effects in each.
                promote_assignment_effects(&b.left, ctx, db, file);
                promote_assignment_effects(&b.right, ctx, db, file);
            }
        }

        // $a || $b  →  if false: narrow both; if true: try to narrow same-var instanceof union
        ExprKind::Binary(b) if b.op == BinaryOp::BooleanOr || b.op == BinaryOp::LogicalOr => {
            if !is_true {
                narrow_from_condition(&b.left, ctx, false, db, file);
                narrow_from_condition(&b.right, ctx, false, db, file);
                // When A || B is false, both sides were evaluated.
                // Promote variables from possibly_assigned to assigned for side effects in each.
                promote_assignment_effects(&b.left, ctx, db, file);
                promote_assignment_effects(&b.right, ctx, db, file);
            } else {
                // For `$x instanceof A || $x instanceof B` in true-branch: narrow $x to A|B
                narrow_or_instanceof_true(&b.left, &b.right, ctx, db, file);

                // For `!isset($x) || RHS` in true-branch: narrow RHS as if isset($x) is true
                narrow_or_isset_true(&b.left, &b.right, ctx, db, file);
            }
        }

        // $x === null / $x !== null
        ExprKind::Binary(b) if b.op == BinaryOp::Identical || b.op == BinaryOp::NotIdentical => {
            let is_identical = b.op == BinaryOp::Identical;
            let effective_true = if is_identical { is_true } else { !is_true };

            // `count($arr) === N` / `strlen($str) !== N`, etc. — independent of
            // the rest of this arm, mirrors the `<`/`<=`/`>`/`>=` handling below.
            narrow_count_or_strlen_equality(ctx, db, file, &b.left, &b.right, b.op, is_true);

            // `($x ?? FALLBACK) === FALLBACK` — on the false branch, $x was defined
            // Must be checked before literal comparisons because `b.right` matching a literal
            // would otherwise consume the arm before we check for NullCoalesce on `b.left`.
            if let Some(nc) = extract_null_coalesce(&b.left) {
                if let Some(var_name) = extract_var_name(&nc.left) {
                    if !effective_true && same_literal(&nc.right, &b.right) {
                        let current = ctx.get_var(&var_name);
                        ctx.set_var(&var_name, current.remove_null());
                    }
                } else if let Some((obj, prop)) = extract_any_prop_access(&nc.left) {
                    if !effective_true && same_literal(&nc.right, &b.right) {
                        let current = resolve_prop_current_type(ctx, &obj, &prop, db, file);
                        let narrowed = current.remove_null();
                        apply_prop_narrowed(ctx, &obj, &prop, current, narrowed, false);
                        // A non-FALLBACK result also proves the receiver itself wasn't
                        // null (a null receiver's `?->`/`->` access coalesces straight
                        // to FALLBACK) — without this, a later plain read of the same
                        // property re-admits null via the receiver's own nullability.
                        narrow_receiver_non_null_on_prop_match(ctx, &obj, !effective_true);
                    }
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&nc.left, ctx, db, file)
                {
                    // `(self::$prop ?? FALLBACK) === FALLBACK` — static-property
                    // counterpart of the instance-property case above (no receiver).
                    if !effective_true && same_literal(&nc.right, &b.right) {
                        let current = resolve_static_prop_current_type(ctx, &fqcn, &prop, db);
                        let narrowed = current.remove_null();
                        apply_prop_narrowed(ctx, &fqcn, &prop, current, narrowed, false);
                    }
                }
            } else if let Some(nc) = extract_null_coalesce(&b.right) {
                if let Some(var_name) = extract_var_name(&nc.left) {
                    if !effective_true && same_literal(&nc.right, &b.left) {
                        let current = ctx.get_var(&var_name);
                        ctx.set_var(&var_name, current.remove_null());
                    }
                } else if let Some((obj, prop)) = extract_any_prop_access(&nc.left) {
                    if !effective_true && same_literal(&nc.right, &b.left) {
                        let current = resolve_prop_current_type(ctx, &obj, &prop, db, file);
                        let narrowed = current.remove_null();
                        apply_prop_narrowed(ctx, &obj, &prop, current, narrowed, false);
                        narrow_receiver_non_null_on_prop_match(ctx, &obj, !effective_true);
                    }
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&nc.left, ctx, db, file)
                {
                    if !effective_true && same_literal(&nc.right, &b.left) {
                        let current = resolve_static_prop_current_type(ctx, &fqcn, &prop, db);
                        let narrowed = current.remove_null();
                        apply_prop_narrowed(ctx, &fqcn, &prop, current, narrowed, false);
                    }
                }
            }
            // `$x === null`
            else if matches!(b.right.kind, ExprKind::Null) {
                if let Some(target) = extract_array_key_first_or_last_arg(&b.left) {
                    match target {
                        ScalarArgTarget::Var(arr_var) => {
                            narrow_array_key_first_or_last_null(ctx, &arr_var, effective_true)
                        }
                        ScalarArgTarget::Prop(obj, prop) => {
                            narrow_prop_array_key_first_or_last_null(
                                ctx,
                                &obj,
                                &prop,
                                db,
                                file,
                                effective_true,
                            )
                        }
                    }
                } else if let Some((fqcn, prop)) =
                    extract_array_key_first_or_last_static_prop_arg(&b.left, ctx, db, file)
                {
                    narrow_static_prop_array_key_first_or_last_null(
                        ctx,
                        &fqcn,
                        &prop,
                        db,
                        effective_true,
                    );
                } else if let Some(name) = extract_var_name(&b.left) {
                    narrow_var_null(ctx, &name, effective_true);
                } else if let Some((obj, prop)) = extract_nullsafe_prop_access(&b.left) {
                    narrow_nullsafe_prop_null(ctx, &obj, &prop, db, file, effective_true);
                } else if let ExprKind::NullsafeMethodCall(mc) = &b.left.kind {
                    narrow_nullsafe_method_call_null(ctx, mc, db, effective_true);
                } else if let Some((obj, prop)) = extract_prop_access(&b.left) {
                    narrow_prop_null(ctx, &obj, &prop, db, file, effective_true);
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&b.left, ctx, db, file)
                {
                    narrow_static_prop_null(ctx, &fqcn, &prop, db, effective_true);
                }
            } else if matches!(b.left.kind, ExprKind::Null) {
                if let Some(target) = extract_array_key_first_or_last_arg(&b.right) {
                    match target {
                        ScalarArgTarget::Var(arr_var) => {
                            narrow_array_key_first_or_last_null(ctx, &arr_var, effective_true)
                        }
                        ScalarArgTarget::Prop(obj, prop) => {
                            narrow_prop_array_key_first_or_last_null(
                                ctx,
                                &obj,
                                &prop,
                                db,
                                file,
                                effective_true,
                            )
                        }
                    }
                } else if let Some((fqcn, prop)) =
                    extract_array_key_first_or_last_static_prop_arg(&b.right, ctx, db, file)
                {
                    narrow_static_prop_array_key_first_or_last_null(
                        ctx,
                        &fqcn,
                        &prop,
                        db,
                        effective_true,
                    );
                } else if let Some(name) = extract_var_name(&b.right) {
                    narrow_var_null(ctx, &name, effective_true);
                } else if let Some((obj, prop)) = extract_nullsafe_prop_access(&b.right) {
                    narrow_nullsafe_prop_null(ctx, &obj, &prop, db, file, effective_true);
                } else if let ExprKind::NullsafeMethodCall(mc) = &b.right.kind {
                    narrow_nullsafe_method_call_null(ctx, mc, db, effective_true);
                } else if let Some((obj, prop)) = extract_prop_access(&b.right) {
                    narrow_prop_null(ctx, &obj, &prop, db, file, effective_true);
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&b.right, ctx, db, file)
                {
                    narrow_static_prop_null(ctx, &fqcn, &prop, db, effective_true);
                }
            }
            // `$x === true` / `$x === false`
            else if matches!(b.right.kind, ExprKind::Bool(true)) {
                if let Some(name) = extract_var_name(&b.left) {
                    narrow_var_bool(ctx, &name, true, effective_true);
                } else if let Some((obj, prop)) = extract_any_prop_access(&b.left) {
                    narrow_prop_bool(ctx, &obj, &prop, db, file, true, effective_true);
                    narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&b.left, ctx, db, file)
                {
                    narrow_static_prop_bool(ctx, &fqcn, &prop, db, true, effective_true);
                } else {
                    // `is_string($x) === true` / `($x instanceof Y) === true` — the
                    // left side is guaranteed to be a real bool (not just truthy),
                    // so narrowing it as a bare condition is exactly as sound as
                    // strict equality requires; re-entering the top-level match
                    // dispatches to whatever arm (FunctionCall, Instanceof, Isset,
                    // ...) already handles that expression shape directly.
                    narrow_from_condition(&b.left, ctx, effective_true, db, file);
                }
            } else if matches!(b.right.kind, ExprKind::Bool(false)) {
                if let Some(name) = extract_var_name(&b.left) {
                    narrow_var_bool(ctx, &name, false, effective_true);
                } else if let Some((obj, prop)) = extract_any_prop_access(&b.left) {
                    narrow_prop_bool(ctx, &obj, &prop, db, file, false, effective_true);
                    narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&b.left, ctx, db, file)
                {
                    narrow_static_prop_bool(ctx, &fqcn, &prop, db, false, effective_true);
                } else {
                    // `strpos($h, $n) !== false` / `array_search($n, $h) === false`
                    narrow_from_false_comparable_call(&b.left, ctx, db, file, effective_true);
                    // `is_string($x) === false` — see the `=== true` comment above.
                    narrow_from_condition(&b.left, ctx, !effective_true, db, file);
                }
            }
            // `true === $x` / `false === $x` — symmetric; extract_var_name looks through
            // assignment exprs, so this also handles `false === ($x = expr)`.
            else if matches!(b.left.kind, ExprKind::Bool(true)) {
                if let Some(name) = extract_var_name(&b.right) {
                    narrow_var_bool(ctx, &name, true, effective_true);
                } else if let Some((obj, prop)) = extract_any_prop_access(&b.right) {
                    narrow_prop_bool(ctx, &obj, &prop, db, file, true, effective_true);
                    narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&b.right, ctx, db, file)
                {
                    narrow_static_prop_bool(ctx, &fqcn, &prop, db, true, effective_true);
                } else {
                    narrow_from_condition(&b.right, ctx, effective_true, db, file);
                }
            } else if matches!(b.left.kind, ExprKind::Bool(false)) {
                if let Some(name) = extract_var_name(&b.right) {
                    narrow_var_bool(ctx, &name, false, effective_true);
                } else if let Some((obj, prop)) = extract_any_prop_access(&b.right) {
                    narrow_prop_bool(ctx, &obj, &prop, db, file, false, effective_true);
                    narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&b.right, ctx, db, file)
                {
                    narrow_static_prop_bool(ctx, &fqcn, &prop, db, false, effective_true);
                } else {
                    narrow_from_false_comparable_call(&b.right, ctx, db, file, effective_true);
                    narrow_from_condition(&b.right, ctx, !effective_true, db, file);
                }
            }
            // `$enum->value === 'H'` narrows $enum to the specific backed-enum
            // case whose ->value equals the literal (and excludes exactly
            // that case on the false branch) — sound because PHP requires
            // distinct backing values across a backed enum's cases, so the
            // value uniquely identifies the case. Checked before the bare
            // literal-string/int arms below, which otherwise shadow it
            // whenever the literal happens to be on the same side they key
            // off of.
            else if let Some((var_name, enum_fqcn, case_name)) =
                extract_enum_value_case(&b.left, &b.right, ctx, db)
                    .or_else(|| extract_enum_value_case(&b.right, &b.left, ctx, db))
            {
                narrow_var_to_literal_enum_case(
                    db,
                    ctx,
                    &var_name,
                    &enum_fqcn,
                    &case_name,
                    effective_true,
                );
            }
            // `get_class($x) === 'ClassName'` — check before literal strings so it takes precedence
            else if let ExprKind::String(class_name_str) = &b.right.kind {
                if let Some(target) = extract_get_class_arg(&b.left) {
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    match target {
                        ScalarArgTarget::Var(name) => {
                            narrow_var_to_specific_class(ctx, &name, &fqcn, effective_true, db)
                        }
                        ScalarArgTarget::Prop(obj, prop) => {
                            narrow_prop_to_specific_class(
                                ctx,
                                &obj,
                                &prop,
                                &fqcn,
                                effective_true,
                                db,
                                file,
                            );
                            narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                        }
                    }
                } else if let Some((fqcn_recv, prop)) =
                    extract_get_class_static_prop_arg(&b.left, ctx, db, file)
                {
                    // `get_class(self::$prop) === 'ClassName'`
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    narrow_static_prop_to_specific_class(
                        ctx,
                        &fqcn_recv,
                        &prop,
                        &fqcn,
                        effective_true,
                        db,
                    );
                } else if let Some(target) = extract_gettype_arg(&b.left) {
                    narrow_from_gettype_literal(
                        ctx,
                        &target,
                        class_name_str,
                        effective_true,
                        db,
                        file,
                    );
                } else if let Some(target) = extract_get_debug_type_arg(&b.left) {
                    narrow_from_get_debug_type_literal(
                        ctx,
                        &target,
                        class_name_str,
                        effective_true,
                        db,
                        file,
                    );
                } else if let Some((fqcn, prop)) =
                    extract_gettype_static_prop_arg(&b.left, ctx, db, file)
                {
                    narrow_static_prop_from_gettype_literal(
                        ctx,
                        &fqcn,
                        &prop,
                        class_name_str,
                        effective_true,
                        db,
                    );
                } else if let Some((fqcn, prop)) =
                    extract_get_debug_type_static_prop_arg(&b.left, ctx, db, file)
                {
                    narrow_static_prop_from_get_debug_type_literal(
                        ctx,
                        &fqcn,
                        &prop,
                        class_name_str,
                        effective_true,
                        db,
                        file,
                    );
                } else if let Some(target) = extract_get_parent_class_arg(&b.left) {
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    narrow_from_get_parent_class_literal(
                        ctx,
                        &target,
                        &fqcn,
                        effective_true,
                        db,
                        file,
                    );
                } else if let Some((fqcn_recv, prop)) =
                    extract_get_parent_class_static_prop_arg(&b.left, ctx, db, file)
                {
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    narrow_static_prop_is_subclass_of(
                        ctx,
                        &fqcn_recv,
                        &prop,
                        &fqcn,
                        db,
                        effective_true,
                    );
                } else if let Some(target) = extract_dynamic_class_const_var(&b.left) {
                    // `$obj::class === 'ClassName'`
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    match target {
                        ScalarArgTarget::Var(name) => {
                            narrow_var_to_specific_class(ctx, &name, &fqcn, effective_true, db)
                        }
                        ScalarArgTarget::Prop(obj, prop) => {
                            narrow_prop_to_specific_class(
                                ctx,
                                &obj,
                                &prop,
                                &fqcn,
                                effective_true,
                                db,
                                file,
                            );
                            narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                        }
                    }
                } else if let Some((fqcn_recv, prop)) =
                    extract_dynamic_class_const_static_prop_var(&b.left, ctx, db, file)
                {
                    // `self::$prop::class === 'ClassName'`
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    narrow_static_prop_to_specific_class(
                        ctx,
                        &fqcn_recv,
                        &prop,
                        &fqcn,
                        effective_true,
                        db,
                    );
                } else if let Some(name) = extract_var_name(&b.left) {
                    // `$x === 'literal'`
                    narrow_var_literal_string(ctx, &name, class_name_str, effective_true);
                } else if let Some((obj, prop)) = extract_any_prop_access(&b.left) {
                    // `$this->prop === 'literal'`
                    narrow_prop_literal_string(
                        ctx,
                        &obj,
                        &prop,
                        db,
                        file,
                        class_name_str,
                        effective_true,
                    );
                    narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&b.left, ctx, db, file)
                {
                    // `self::$prop === 'literal'`
                    narrow_static_prop_literal_string(
                        ctx,
                        &fqcn,
                        &prop,
                        db,
                        class_name_str,
                        effective_true,
                    );
                }
            } else if let ExprKind::String(class_name_str) = &b.left.kind {
                if let Some(target) = extract_get_class_arg(&b.right) {
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    match target {
                        ScalarArgTarget::Var(name) => {
                            narrow_var_to_specific_class(ctx, &name, &fqcn, effective_true, db)
                        }
                        ScalarArgTarget::Prop(obj, prop) => {
                            narrow_prop_to_specific_class(
                                ctx,
                                &obj,
                                &prop,
                                &fqcn,
                                effective_true,
                                db,
                                file,
                            );
                            narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                        }
                    }
                } else if let Some((fqcn_recv, prop)) =
                    extract_get_class_static_prop_arg(&b.right, ctx, db, file)
                {
                    // `'ClassName' === get_class(self::$prop)`
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    narrow_static_prop_to_specific_class(
                        ctx,
                        &fqcn_recv,
                        &prop,
                        &fqcn,
                        effective_true,
                        db,
                    );
                } else if let Some(target) = extract_gettype_arg(&b.right) {
                    narrow_from_gettype_literal(
                        ctx,
                        &target,
                        class_name_str,
                        effective_true,
                        db,
                        file,
                    );
                } else if let Some(target) = extract_get_debug_type_arg(&b.right) {
                    narrow_from_get_debug_type_literal(
                        ctx,
                        &target,
                        class_name_str,
                        effective_true,
                        db,
                        file,
                    );
                } else if let Some((fqcn, prop)) =
                    extract_gettype_static_prop_arg(&b.right, ctx, db, file)
                {
                    narrow_static_prop_from_gettype_literal(
                        ctx,
                        &fqcn,
                        &prop,
                        class_name_str,
                        effective_true,
                        db,
                    );
                } else if let Some((fqcn, prop)) =
                    extract_get_debug_type_static_prop_arg(&b.right, ctx, db, file)
                {
                    narrow_static_prop_from_get_debug_type_literal(
                        ctx,
                        &fqcn,
                        &prop,
                        class_name_str,
                        effective_true,
                        db,
                        file,
                    );
                } else if let Some(target) = extract_get_parent_class_arg(&b.right) {
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    narrow_from_get_parent_class_literal(
                        ctx,
                        &target,
                        &fqcn,
                        effective_true,
                        db,
                        file,
                    );
                } else if let Some((fqcn_recv, prop)) =
                    extract_get_parent_class_static_prop_arg(&b.right, ctx, db, file)
                {
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    narrow_static_prop_is_subclass_of(
                        ctx,
                        &fqcn_recv,
                        &prop,
                        &fqcn,
                        db,
                        effective_true,
                    );
                } else if let Some(target) = extract_dynamic_class_const_var(&b.right) {
                    // `'ClassName' === $obj::class`
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    match target {
                        ScalarArgTarget::Var(name) => {
                            narrow_var_to_specific_class(ctx, &name, &fqcn, effective_true, db)
                        }
                        ScalarArgTarget::Prop(obj, prop) => {
                            narrow_prop_to_specific_class(
                                ctx,
                                &obj,
                                &prop,
                                &fqcn,
                                effective_true,
                                db,
                                file,
                            );
                            narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                        }
                    }
                } else if let Some((fqcn_recv, prop)) =
                    extract_dynamic_class_const_static_prop_var(&b.right, ctx, db, file)
                {
                    // `'ClassName' === self::$prop::class`
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    narrow_static_prop_to_specific_class(
                        ctx,
                        &fqcn_recv,
                        &prop,
                        &fqcn,
                        effective_true,
                        db,
                    );
                } else if let Some(name) = extract_var_name(&b.right) {
                    // `$x === 'literal'`
                    narrow_var_literal_string(ctx, &name, class_name_str, effective_true);
                } else if let Some((obj, prop)) = extract_any_prop_access(&b.right) {
                    // `'literal' === $this->prop`
                    narrow_prop_literal_string(
                        ctx,
                        &obj,
                        &prop,
                        db,
                        file,
                        class_name_str,
                        effective_true,
                    );
                    narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&b.right, ctx, db, file)
                {
                    // `'literal' === self::$prop`
                    narrow_static_prop_literal_string(
                        ctx,
                        &fqcn,
                        &prop,
                        db,
                        class_name_str,
                        effective_true,
                    );
                }
            }
            // `$x === 42`
            else if let ExprKind::Int(n) = &b.right.kind {
                if let Some(name) = extract_var_name(&b.left) {
                    narrow_var_literal_int(ctx, &name, *n, effective_true);
                } else if let Some((obj, prop)) = extract_any_prop_access(&b.left) {
                    narrow_prop_literal_int(ctx, &obj, &prop, db, file, *n, effective_true);
                    narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&b.left, ctx, db, file)
                {
                    narrow_static_prop_literal_int(ctx, &fqcn, &prop, db, *n, effective_true);
                }
            } else if let ExprKind::Int(n) = &b.left.kind {
                if let Some(name) = extract_var_name(&b.right) {
                    narrow_var_literal_int(ctx, &name, *n, effective_true);
                } else if let Some((obj, prop)) = extract_any_prop_access(&b.right) {
                    narrow_prop_literal_int(ctx, &obj, &prop, db, file, *n, effective_true);
                    narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&b.right, ctx, db, file)
                {
                    narrow_static_prop_literal_int(ctx, &fqcn, &prop, db, *n, effective_true);
                }
            }
            // `$arr === []` narrows $arr to empty; `$arr !== []` narrows to non-empty.
            // Checked before the enum-case/class-const guard below: a bare
            // `self::$prop === []` would otherwise match that guard's
            // "either side is a StaticPropertyAccess" condition and be
            // silently swallowed (that function only handles enum-case/
            // get_class-family comparisons, not array-emptiness).
            else if narrow_array_emptiness_condition(
                ctx,
                db,
                file,
                &b.left,
                &b.right,
                effective_true,
            ) {
            }
            // `$x === EnumName::CaseName` / get_class()/get_debug_type()/
            // get_parent_class()/$obj::class compared against `Foo::class` —
            // factored into narrow_from_static_or_class_const_comparison so
            // the loose `==`/`!=` arm can reuse it (see that function's doc
            // comment for why loose comparison is sound here specifically).
            else if matches!(b.right.kind, ExprKind::StaticPropertyAccess(_))
                || matches!(b.left.kind, ExprKind::StaticPropertyAccess(_))
                || matches!(b.right.kind, ExprKind::ClassConstAccess(_))
                || matches!(b.left.kind, ExprKind::ClassConstAccess(_))
            {
                narrow_from_static_or_class_const_comparison(ctx, b, effective_true, db, file);
            }
        }

        // $x < N, $x <= N, $x > N, $x >= N — comparison-driven integer range narrowing
        ExprKind::Binary(b)
            if matches!(
                b.op,
                BinaryOp::Less
                    | BinaryOp::LessOrEqual
                    | BinaryOp::Greater
                    | BinaryOp::GreaterOrEqual
            ) =>
        {
            // `$x < N` / `N < $x` (and `$this->prop < N` / `N < $this->prop`) —
            // normalize so the variable/property is always on the left,
            // flipping the operator when the literal was on the left instead.
            if let Some(var_name) = extract_var_name(&b.left) {
                if let Some(n) = extract_int_literal(&b.right) {
                    narrow_var_int_comparison(ctx, &var_name, b.op, n, is_true);
                }
            } else if let Some(var_name) = extract_var_name(&b.right) {
                if let Some(n) = extract_int_literal(&b.left) {
                    narrow_var_int_comparison(ctx, &var_name, flip_comparison_op(b.op), n, is_true);
                }
            } else if let Some((obj, prop)) = extract_any_prop_access(&b.left) {
                if let Some(n) = extract_int_literal(&b.right) {
                    narrow_prop_int_comparison(ctx, &obj, &prop, db, file, b.op, n, is_true);
                }
            } else if let Some((obj, prop)) = extract_any_prop_access(&b.right) {
                if let Some(n) = extract_int_literal(&b.left) {
                    narrow_prop_int_comparison(
                        ctx,
                        &obj,
                        &prop,
                        db,
                        file,
                        flip_comparison_op(b.op),
                        n,
                        is_true,
                    );
                }
            } else if let Some((fqcn, prop)) = extract_static_prop_access(&b.left, ctx, db, file) {
                if let Some(n) = extract_int_literal(&b.right) {
                    narrow_static_prop_int_comparison(ctx, &fqcn, &prop, db, b.op, n, is_true);
                }
            } else if let Some((fqcn, prop)) = extract_static_prop_access(&b.right, ctx, db, file) {
                if let Some(n) = extract_int_literal(&b.left) {
                    narrow_static_prop_int_comparison(
                        ctx,
                        &fqcn,
                        &prop,
                        db,
                        flip_comparison_op(b.op),
                        n,
                        is_true,
                    );
                }
            }
            // count($arr) op N  /  N op count($arr)
            narrow_array_count_condition(ctx, db, file, &b.left, &b.right, b.op, is_true);
            // strlen($str) op N  /  N op strlen($str) — same normalization.
            let strlen_call_on_left = extract_strlen_arg(&b.left).is_some()
                || extract_strlen_static_prop_arg(&b.left, ctx, db, file).is_some();
            let (strlen_expr, strlen_cmp_op, strlen_lit) = if strlen_call_on_left {
                (&b.left, b.op, &b.right)
            } else {
                (&b.right, flip_comparison_op(b.op), &b.left)
            };
            if let (Some(target), Some(n)) = (
                extract_strlen_arg(strlen_expr),
                extract_int_literal(strlen_lit),
            ) {
                match target {
                    ScalarArgTarget::Var(str_var) => {
                        narrow_string_strlen_comparison(ctx, &str_var, strlen_cmp_op, n, is_true)
                    }
                    ScalarArgTarget::Prop(obj, prop) => narrow_prop_string_strlen_comparison(
                        ctx,
                        &obj,
                        &prop,
                        db,
                        file,
                        strlen_cmp_op,
                        n,
                        is_true,
                    ),
                }
            } else if let (Some((fqcn, prop)), Some(n)) = (
                extract_strlen_static_prop_arg(strlen_expr, ctx, db, file),
                extract_int_literal(strlen_lit),
            ) {
                narrow_static_prop_string_strlen_comparison(
                    ctx,
                    &fqcn,
                    &prop,
                    db,
                    strlen_cmp_op,
                    n,
                    is_true,
                );
            }
        }

        // $x == null  (loose equality)
        ExprKind::Binary(b) if b.op == BinaryOp::Equal || b.op == BinaryOp::NotEqual => {
            let is_equal = b.op == BinaryOp::Equal;
            let effective_true = if is_equal { is_true } else { !is_true };

            // `count($arr) == N` / `strlen($str) != N`, etc.
            narrow_count_or_strlen_equality(ctx, db, file, &b.left, &b.right, b.op, is_true);

            // `($x ?? FALLBACK) == FALLBACK` / `!= FALLBACK` — same reasoning as the
            // strict `===`/`!==` arm: `same_literal` requires an identical literal
            // AST, so when $x is null the coalesce result is exactly FALLBACK,
            // making `FALLBACK == FALLBACK` trivially true regardless of loose vs
            // strict — a `!=` result still proves $x wasn't null. Must be checked
            // before the literal-comparison arms below for the same reason as the
            // strict arm (a literal on the other side would otherwise consume it).
            if let Some(nc) = extract_null_coalesce(&b.left) {
                if let Some(var_name) = extract_var_name(&nc.left) {
                    if !effective_true && same_literal(&nc.right, &b.right) {
                        let current = ctx.get_var(&var_name);
                        ctx.set_var(&var_name, current.remove_null());
                    }
                } else if let Some((obj, prop)) = extract_any_prop_access(&nc.left) {
                    if !effective_true && same_literal(&nc.right, &b.right) {
                        let current = resolve_prop_current_type(ctx, &obj, &prop, db, file);
                        let narrowed = current.remove_null();
                        apply_prop_narrowed(ctx, &obj, &prop, current, narrowed, false);
                        narrow_receiver_non_null_on_prop_match(ctx, &obj, !effective_true);
                    }
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&nc.left, ctx, db, file)
                {
                    if !effective_true && same_literal(&nc.right, &b.right) {
                        let current = resolve_static_prop_current_type(ctx, &fqcn, &prop, db);
                        let narrowed = current.remove_null();
                        apply_prop_narrowed(ctx, &fqcn, &prop, current, narrowed, false);
                    }
                }
            } else if let Some(nc) = extract_null_coalesce(&b.right) {
                if let Some(var_name) = extract_var_name(&nc.left) {
                    if !effective_true && same_literal(&nc.right, &b.left) {
                        let current = ctx.get_var(&var_name);
                        ctx.set_var(&var_name, current.remove_null());
                    }
                } else if let Some((obj, prop)) = extract_any_prop_access(&nc.left) {
                    if !effective_true && same_literal(&nc.right, &b.left) {
                        let current = resolve_prop_current_type(ctx, &obj, &prop, db, file);
                        let narrowed = current.remove_null();
                        apply_prop_narrowed(ctx, &obj, &prop, current, narrowed, false);
                        narrow_receiver_non_null_on_prop_match(ctx, &obj, !effective_true);
                    }
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&nc.left, ctx, db, file)
                {
                    if !effective_true && same_literal(&nc.right, &b.left) {
                        let current = resolve_static_prop_current_type(ctx, &fqcn, &prop, db);
                        let narrowed = current.remove_null();
                        apply_prop_narrowed(ctx, &fqcn, &prop, current, narrowed, false);
                    }
                }
            } else if matches!(b.right.kind, ExprKind::Null) {
                if let Some(name) = extract_var_name(&b.left) {
                    narrow_var_loose_null(ctx, &name, effective_true);
                } else if let Some((obj, prop)) = extract_any_prop_access(&b.left) {
                    narrow_prop_loose_null(ctx, &obj, &prop, db, file, effective_true);
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&b.left, ctx, db, file)
                {
                    narrow_static_prop_loose_null(ctx, &fqcn, &prop, db, effective_true);
                }
            } else if matches!(b.left.kind, ExprKind::Null) {
                if let Some(name) = extract_var_name(&b.right) {
                    narrow_var_loose_null(ctx, &name, effective_true);
                } else if let Some((obj, prop)) = extract_any_prop_access(&b.right) {
                    narrow_prop_loose_null(ctx, &obj, &prop, db, file, effective_true);
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&b.right, ctx, db, file)
                {
                    narrow_static_prop_loose_null(ctx, &fqcn, &prop, db, effective_true);
                }
            }
            // `$x == true` / `$x == false` (and negated forms) — PHP defines loose
            // comparison to a bool literal as `(bool)$x === value`, i.e. identical
            // to the truthy/falsy narrowing a bare `if ($x)` already gets.
            else if let ExprKind::Bool(value) = &b.right.kind {
                if let Some(name) = extract_var_name(&b.left) {
                    narrow_var_loose_bool(ctx, &name, *value == effective_true);
                } else if let Some((obj, prop)) = extract_any_prop_access(&b.left) {
                    narrow_prop_loose_bool(ctx, &obj, &prop, db, file, *value == effective_true);
                    // `null == true` is false, so a true match against the
                    // literal `true` also proves the receiver wasn't null —
                    // same reasoning as the strict `===` sibling arm above.
                    narrow_receiver_non_null_on_prop_match(ctx, &obj, *value == effective_true);
                } else {
                    if !value {
                        // `strpos($h, $n) == false` / `!= false` — same
                        // false-comparable narrowing as the strict `===`/`!==` arm.
                        narrow_from_false_comparable_call(&b.left, ctx, db, file, effective_true);
                    }
                    // `is_string($x) == true` / `== false` — loose comparison to a
                    // bool literal is `(bool)$x === value` for ANY $x (not just a
                    // guaranteed-bool expression), i.e. exactly the bare truthy/
                    // falsy narrowing `narrow_from_condition` itself provides.
                    narrow_from_condition(&b.left, ctx, *value == effective_true, db, file);
                }
            } else if let ExprKind::Bool(value) = &b.left.kind {
                if let Some(name) = extract_var_name(&b.right) {
                    narrow_var_loose_bool(ctx, &name, *value == effective_true);
                } else if let Some((obj, prop)) = extract_any_prop_access(&b.right) {
                    narrow_prop_loose_bool(ctx, &obj, &prop, db, file, *value == effective_true);
                    narrow_receiver_non_null_on_prop_match(ctx, &obj, *value == effective_true);
                } else {
                    if !value {
                        narrow_from_false_comparable_call(&b.right, ctx, db, file, effective_true);
                    }
                    narrow_from_condition(&b.right, ctx, *value == effective_true, db, file);
                }
            }
            // `$arr == []` / `$arr != []` — loose array equality requires identical
            // key/value pairs, so this is exactly as sound as the strict `===` case.
            else if narrow_array_emptiness_condition(
                ctx,
                db,
                file,
                &b.left,
                &b.right,
                effective_true,
            ) {
            }
            // `get_class($x) == 'ClassName'` / `get_debug_type($x) == 'ClassName'` /
            // `gettype($x) == 'ClassName'` / `$x::class == 'ClassName'` — loose `==`
            // mirrors the `===` handling below the `Identical`/`NotIdentical` arm:
            // class/type names are never numeric-looking strings, so loose
            // comparison agrees with strict comparison here.
            else if let ExprKind::String(class_name_str) = &b.right.kind {
                if let Some(target) = extract_get_class_arg(&b.left) {
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    match target {
                        ScalarArgTarget::Var(name) => {
                            narrow_var_to_specific_class(ctx, &name, &fqcn, effective_true, db)
                        }
                        ScalarArgTarget::Prop(obj, prop) => {
                            narrow_prop_to_specific_class(
                                ctx,
                                &obj,
                                &prop,
                                &fqcn,
                                effective_true,
                                db,
                                file,
                            );
                            narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                        }
                    }
                } else if let Some((fqcn_recv, prop)) =
                    extract_get_class_static_prop_arg(&b.left, ctx, db, file)
                {
                    // `get_class(self::$prop) == 'ClassName'`
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    narrow_static_prop_to_specific_class(
                        ctx,
                        &fqcn_recv,
                        &prop,
                        &fqcn,
                        effective_true,
                        db,
                    );
                } else if let Some(target) = extract_gettype_arg(&b.left) {
                    narrow_from_gettype_literal(
                        ctx,
                        &target,
                        class_name_str,
                        effective_true,
                        db,
                        file,
                    );
                } else if let Some(target) = extract_get_debug_type_arg(&b.left) {
                    narrow_from_get_debug_type_literal(
                        ctx,
                        &target,
                        class_name_str,
                        effective_true,
                        db,
                        file,
                    );
                } else if let Some((fqcn, prop)) =
                    extract_gettype_static_prop_arg(&b.left, ctx, db, file)
                {
                    narrow_static_prop_from_gettype_literal(
                        ctx,
                        &fqcn,
                        &prop,
                        class_name_str,
                        effective_true,
                        db,
                    );
                } else if let Some((fqcn, prop)) =
                    extract_get_debug_type_static_prop_arg(&b.left, ctx, db, file)
                {
                    narrow_static_prop_from_get_debug_type_literal(
                        ctx,
                        &fqcn,
                        &prop,
                        class_name_str,
                        effective_true,
                        db,
                        file,
                    );
                } else if let Some(target) = extract_get_parent_class_arg(&b.left) {
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    narrow_from_get_parent_class_literal(
                        ctx,
                        &target,
                        &fqcn,
                        effective_true,
                        db,
                        file,
                    );
                } else if let Some((fqcn_recv, prop)) =
                    extract_get_parent_class_static_prop_arg(&b.left, ctx, db, file)
                {
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    narrow_static_prop_is_subclass_of(
                        ctx,
                        &fqcn_recv,
                        &prop,
                        &fqcn,
                        db,
                        effective_true,
                    );
                } else if let Some(target) = extract_dynamic_class_const_var(&b.left) {
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    match target {
                        ScalarArgTarget::Var(name) => {
                            narrow_var_to_specific_class(ctx, &name, &fqcn, effective_true, db)
                        }
                        ScalarArgTarget::Prop(obj, prop) => {
                            narrow_prop_to_specific_class(
                                ctx,
                                &obj,
                                &prop,
                                &fqcn,
                                effective_true,
                                db,
                                file,
                            );
                            narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                        }
                    }
                } else if let Some((fqcn_recv, prop)) =
                    extract_dynamic_class_const_static_prop_var(&b.left, ctx, db, file)
                {
                    // `self::$prop::class == 'ClassName'`
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    narrow_static_prop_to_specific_class(
                        ctx,
                        &fqcn_recv,
                        &prop,
                        &fqcn,
                        effective_true,
                        db,
                    );
                }
            } else if let ExprKind::String(class_name_str) = &b.left.kind {
                if let Some(target) = extract_get_class_arg(&b.right) {
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    match target {
                        ScalarArgTarget::Var(name) => {
                            narrow_var_to_specific_class(ctx, &name, &fqcn, effective_true, db)
                        }
                        ScalarArgTarget::Prop(obj, prop) => {
                            narrow_prop_to_specific_class(
                                ctx,
                                &obj,
                                &prop,
                                &fqcn,
                                effective_true,
                                db,
                                file,
                            );
                            narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                        }
                    }
                } else if let Some((fqcn_recv, prop)) =
                    extract_get_class_static_prop_arg(&b.right, ctx, db, file)
                {
                    // `'ClassName' == get_class(self::$prop)`
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    narrow_static_prop_to_specific_class(
                        ctx,
                        &fqcn_recv,
                        &prop,
                        &fqcn,
                        effective_true,
                        db,
                    );
                } else if let Some(target) = extract_gettype_arg(&b.right) {
                    narrow_from_gettype_literal(
                        ctx,
                        &target,
                        class_name_str,
                        effective_true,
                        db,
                        file,
                    );
                } else if let Some(target) = extract_get_debug_type_arg(&b.right) {
                    narrow_from_get_debug_type_literal(
                        ctx,
                        &target,
                        class_name_str,
                        effective_true,
                        db,
                        file,
                    );
                } else if let Some((fqcn, prop)) =
                    extract_gettype_static_prop_arg(&b.right, ctx, db, file)
                {
                    narrow_static_prop_from_gettype_literal(
                        ctx,
                        &fqcn,
                        &prop,
                        class_name_str,
                        effective_true,
                        db,
                    );
                } else if let Some((fqcn, prop)) =
                    extract_get_debug_type_static_prop_arg(&b.right, ctx, db, file)
                {
                    narrow_static_prop_from_get_debug_type_literal(
                        ctx,
                        &fqcn,
                        &prop,
                        class_name_str,
                        effective_true,
                        db,
                        file,
                    );
                } else if let Some(target) = extract_get_parent_class_arg(&b.right) {
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    narrow_from_get_parent_class_literal(
                        ctx,
                        &target,
                        &fqcn,
                        effective_true,
                        db,
                        file,
                    );
                } else if let Some((fqcn_recv, prop)) =
                    extract_get_parent_class_static_prop_arg(&b.right, ctx, db, file)
                {
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    narrow_static_prop_is_subclass_of(
                        ctx,
                        &fqcn_recv,
                        &prop,
                        &fqcn,
                        db,
                        effective_true,
                    );
                } else if let Some(target) = extract_dynamic_class_const_var(&b.right) {
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    match target {
                        ScalarArgTarget::Var(name) => {
                            narrow_var_to_specific_class(ctx, &name, &fqcn, effective_true, db)
                        }
                        ScalarArgTarget::Prop(obj, prop) => {
                            narrow_prop_to_specific_class(
                                ctx,
                                &obj,
                                &prop,
                                &fqcn,
                                effective_true,
                                db,
                                file,
                            );
                            narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                        }
                    }
                } else if let Some((fqcn_recv, prop)) =
                    extract_dynamic_class_const_static_prop_var(&b.right, ctx, db, file)
                {
                    // `'ClassName' == self::$prop::class`
                    let fqcn = crate::db::resolve_name(db, file, class_name_str.as_ref());
                    narrow_static_prop_to_specific_class(
                        ctx,
                        &fqcn_recv,
                        &prop,
                        &fqcn,
                        effective_true,
                        db,
                    );
                }
            }
            // `$x == 42` / `$x != 42` — sound only when every current atom is
            // already int-like (no string/float/bool atom could loosely equal
            // the same numeric value differently than strict comparison
            // would), reusing the strict `===` arm's literal_int_narrow_type
            // once that holds.
            else if let ExprKind::Int(n) = &b.right.kind {
                if let Some(name) = extract_var_name(&b.left) {
                    narrow_var_loose_int(ctx, &name, *n, effective_true);
                } else if let Some((obj, prop)) = extract_any_prop_access(&b.left) {
                    narrow_prop_loose_int(ctx, &obj, &prop, db, file, *n, effective_true);
                    narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&b.left, ctx, db, file)
                {
                    narrow_static_prop_loose_int(ctx, &fqcn, &prop, db, *n, effective_true);
                }
            } else if let ExprKind::Int(n) = &b.left.kind {
                if let Some(name) = extract_var_name(&b.right) {
                    narrow_var_loose_int(ctx, &name, *n, effective_true);
                } else if let Some((obj, prop)) = extract_any_prop_access(&b.right) {
                    narrow_prop_loose_int(ctx, &obj, &prop, db, file, *n, effective_true);
                    narrow_receiver_non_null_on_prop_match(ctx, &obj, effective_true);
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(&b.right, ctx, db, file)
                {
                    narrow_static_prop_loose_int(ctx, &fqcn, &prop, db, *n, effective_true);
                }
            }
            // `$x == EnumName::CaseName` / `get_class($x) == Foo::class` etc. —
            // the `Foo::class`/enum-case counterpart of the string-literal arms
            // above, reusing the same logic the strict `===` arm uses (sound for
            // loose comparison — see narrow_from_static_or_class_const_comparison's
            // doc comment for why).
            else if matches!(b.right.kind, ExprKind::StaticPropertyAccess(_))
                || matches!(b.left.kind, ExprKind::StaticPropertyAccess(_))
                || matches!(b.right.kind, ExprKind::ClassConstAccess(_))
                || matches!(b.left.kind, ExprKind::ClassConstAccess(_))
            {
                narrow_from_static_or_class_const_comparison(ctx, b, effective_true, db, file);
            }
        }

        // $x instanceof ClassName  /  $this->prop instanceof ClassName
        ExprKind::Binary(b) if b.op == BinaryOp::Instanceof => {
            if let Some(var_name) = extract_var_name(&b.left) {
                if let Some(raw_name) = extract_class_name(
                    &b.right,
                    ctx.self_fqcn.as_deref(),
                    ctx.parent_fqcn.as_deref(),
                ) {
                    // Resolve the short name to its FQCN using file imports
                    let class_name = crate::db::resolve_name(db, file, &raw_name);
                    let current = ctx.get_var(&var_name);
                    let narrowed = if is_true {
                        narrow_instanceof_preserving_subtypes(
                            &current,
                            &class_name,
                            db,
                            &ctx.template_param_names,
                        )
                    } else {
                        filter_out_instanceof_match(&current, &class_name, db)
                    };
                    set_narrowed(ctx, &var_name, &current, narrowed, true);
                }
            } else if let Some((obj, prop)) = extract_nullsafe_prop_access(&b.left) {
                if let Some(raw_name) = extract_class_name(
                    &b.right,
                    ctx.self_fqcn.as_deref(),
                    ctx.parent_fqcn.as_deref(),
                ) {
                    let class_name = crate::db::resolve_name(db, file, &raw_name);
                    narrow_prop_instanceof(ctx, &obj, &prop, &class_name, db, file, is_true);
                    // `null instanceof X` is always false, so a true result also
                    // proves the receiver itself is non-null (see
                    // `narrow_nullsafe_prop_null` for the same reasoning).
                    if is_true {
                        narrow_var_null(ctx, &obj, false);
                    }
                }
            } else if let Some((obj, prop)) = extract_prop_access(&b.left) {
                if let Some(raw_name) = extract_class_name(
                    &b.right,
                    ctx.self_fqcn.as_deref(),
                    ctx.parent_fqcn.as_deref(),
                ) {
                    let class_name = crate::db::resolve_name(db, file, &raw_name);
                    narrow_prop_instanceof(ctx, &obj, &prop, &class_name, db, file, is_true);
                    // Same reasoning as the nullsafe arm above: `null instanceof X`
                    // is always false, so proving it true also proves `$obj` itself
                    // wasn't null (PHP 8 reads `$obj->prop` on a null `$obj` as a
                    // warning, still evaluating to null).
                    if is_true {
                        narrow_var_null(ctx, &obj, false);
                    }
                }
            } else if let Some((fqcn, prop)) = extract_static_prop_access(&b.left, ctx, db, file) {
                if let Some(raw_name) = extract_class_name(
                    &b.right,
                    ctx.self_fqcn.as_deref(),
                    ctx.parent_fqcn.as_deref(),
                ) {
                    let class_name = crate::db::resolve_name(db, file, &raw_name);
                    narrow_static_prop_instanceof(ctx, &fqcn, &prop, &class_name, db, is_true);
                }
            }
        }

        // is_string($x), is_int($x), is_null($x), is_array($x), etc.
        // Also handles assert($x instanceof Y) — narrows like a bare condition.
        ExprKind::FunctionCall(call) => {
            // `ExprKind::Variable` (a dynamic `$fn()` call) is deliberately not
            // matched here: `name` would be the callable variable's own
            // identifier text, not its runtime string value, so every builtin
            // check below would only ever match by coincidence (e.g. a
            // variable literally named `$is_array`) while never actually
            // calling that builtin.
            let fn_name_opt: Option<&str> = match &call.name.kind {
                ExprKind::Identifier(name) => Some(name.as_ref()),
                _ => None,
            };
            if let Some(fn_name) = fn_name_opt {
                let bare = fn_name.trim_start_matches('\\');
                if matches!(
                    bare.to_ascii_lowercase().as_str(),
                    "class_exists" | "interface_exists" | "trait_exists" | "enum_exists"
                ) {
                    // `if (class_exists(\Foo\Bar::class)) { ... }` — record \Foo\Bar as
                    // proven-to-exist in the true branch so that UndefinedClass is
                    // suppressed for all usages within the guarded block.
                    // Variable form: `if (class_exists($var)) { ... }` — narrow $var to
                    // class-string so it satisfies class-string-typed parameters.
                    // `interface_exists($var)` narrows to the more precise interface-string.
                    // `enum_exists($var)`/`trait_exists($var)` narrow like class_exists —
                    // no dedicated enum-string/trait-string atomic exists.
                    if is_true {
                        if let Some(arg_value) = call
                            .args
                            .first()
                            .and_then(|arg_expr| arg_expr.value.as_ref())
                        {
                            if let Some(fqcn) = extract_class_fqcn_from_expr(
                                arg_value,
                                ctx.self_fqcn.as_deref(),
                                ctx.static_fqcn.as_deref(),
                                ctx.parent_fqcn.as_deref(),
                                db,
                                file,
                            ) {
                                ctx.class_exists_guards.insert(fqcn);
                            } else if let Some(var_name) = extract_var_name(arg_value) {
                                let current = ctx.get_var(&var_name);
                                let narrowed = if bare.eq_ignore_ascii_case("interface_exists") {
                                    current.narrow_to_interface_string()
                                } else {
                                    current.narrow_to_class_string()
                                };
                                set_narrowed(ctx, &var_name, &current, narrowed, true);
                            } else if let Some((obj, prop)) = extract_any_prop_access(arg_value) {
                                let current = resolve_prop_current_type(ctx, &obj, &prop, db, file);
                                if !current.is_mixed() {
                                    let narrowed = if bare.eq_ignore_ascii_case("interface_exists")
                                    {
                                        current.narrow_to_interface_string()
                                    } else {
                                        current.narrow_to_class_string()
                                    };
                                    apply_prop_narrowed(ctx, &obj, &prop, current, narrowed, true);
                                    // `class_exists(null)` etc. can never be true, so a
                                    // true result also proves `$obj` itself wasn't null.
                                    narrow_receiver_non_null_on_prop_match(ctx, &obj, true);
                                }
                            } else if let Some((fqcn_recv, prop)) =
                                extract_static_prop_access(arg_value, ctx, db, file)
                            {
                                let current =
                                    resolve_static_prop_current_type(ctx, &fqcn_recv, &prop, db);
                                if !current.is_mixed() {
                                    let narrowed = if bare.eq_ignore_ascii_case("interface_exists")
                                    {
                                        current.narrow_to_interface_string()
                                    } else {
                                        current.narrow_to_class_string()
                                    };
                                    apply_prop_narrowed(
                                        ctx, &fqcn_recv, &prop, current, narrowed, true,
                                    );
                                }
                            }
                        }
                    }
                } else if bare.eq_ignore_ascii_case("defined") {
                    // `if (defined('NAME')) { ... NAME ... }` — record NAME as
                    // proven-defined in the true branch to suppress
                    // UndefinedConstant within the guarded block.
                    if is_true {
                        if let Some(value) = call.args.first().and_then(|arg| arg.value.as_ref()) {
                            if let ExprKind::String(name) = &value.kind {
                                let name = name.as_ref().trim_start_matches('\\');
                                if !name.is_empty() {
                                    ctx.defined_guards.insert(std::sync::Arc::from(name));
                                }
                            }
                        }
                    }
                } else if bare.eq_ignore_ascii_case("function_exists") {
                    // `if (function_exists('fn')) { ... fn() ... }` — record fn
                    // as proven-to-exist in the true branch to suppress
                    // UndefinedFunction within the guarded block. Combined with
                    // negation + divergence (`if (!function_exists('fn')) throw;`)
                    // this also covers the early-exit pattern.
                    if is_true {
                        if let Some(value) = call.args.first().and_then(|arg| arg.value.as_ref()) {
                            if let ExprKind::String(name) = &value.kind {
                                let name = name.as_ref().trim_start_matches('\\');
                                if !name.is_empty() {
                                    ctx.function_exists_guards
                                        .insert(std::sync::Arc::from(name));
                                }
                            }
                        }
                    }
                } else if bare.eq_ignore_ascii_case("extension_loaded") {
                    // `if (extension_loaded('ext')) { ... }` — record the extension
                    // name so that `UndefinedClass` is suppressed for any class used
                    // inside the guarded block (the caller verified the extension is
                    // present). The false-branch / early-exit pattern also works via
                    // the normal divergence+narrowing mechanism.
                    if is_true {
                        if let Some(value) = call.args.first().and_then(|arg| arg.value.as_ref()) {
                            if let ExprKind::String(ext) = &value.kind {
                                if !ext.is_empty() {
                                    ctx.extension_loaded_guards
                                        .insert(std::sync::Arc::from(ext.as_ref()));
                                }
                            }
                        }
                    }
                } else if bare.eq_ignore_ascii_case("assert") {
                    // assert($condition) — narrow as if the condition is is_true
                    if let Some(arg_value) = call
                        .args
                        .first()
                        .and_then(|arg_expr| arg_expr.value.as_ref())
                    {
                        narrow_from_condition(arg_value, ctx, is_true, db, file);
                    }
                } else if bare.eq_ignore_ascii_case("method_exists")
                    || bare.eq_ignore_ascii_case("property_exists")
                {
                    // Narrow the first arg to TObject for simple variables (existing behaviour).
                    // Additionally record `(expr_key, method_name)` in method_exists_guards for
                    // property-access first args where variable narrowing can't reach — but only
                    // for method_exists(): properties and methods are independent namespaces in
                    // PHP, so property_exists($obj, 'foo') proves nothing about a method 'foo'.
                    if let Some(arg_value) = call
                        .args
                        .first()
                        .and_then(|arg_expr| arg_expr.value.as_ref())
                    {
                        if let Some(var_name) = extract_var_name(arg_value) {
                            narrow_from_type_fn(ctx, bare, &var_name, db, is_true);
                        } else if let Some((obj, prop)) = extract_any_prop_access(arg_value) {
                            narrow_prop_from_type_fn(ctx, bare, &obj, &prop, db, file, is_true);
                        } else if let Some((fqcn, prop)) =
                            extract_static_prop_access(arg_value, ctx, db, file)
                        {
                            narrow_static_prop_from_type_fn(ctx, bare, &fqcn, &prop, db, is_true);
                        }
                        if is_true && bare.eq_ignore_ascii_case("method_exists") {
                            if let Some(expr_key) = extract_expr_guard_key(arg_value, ctx, db, file)
                            {
                                if let Some(method_value) =
                                    call.args.get(1).and_then(|a| a.value.as_ref())
                                {
                                    if let ExprKind::String(method_name) = &method_value.kind {
                                        let method_lc = std::sync::Arc::from(
                                            crate::util::php_ident_lowercase(method_name).as_str(),
                                        );
                                        ctx.method_exists_guards.insert((expr_key, method_lc));
                                    }
                                }
                            }
                        }
                        // `property_exists($obj, 'prop')` — record `(expr_key, prop)` so a
                        // later `$obj->prop` read inside the guarded block doesn't flag
                        // UndefinedProperty. Property names are case-sensitive in PHP
                        // (unlike method names above), so the literal is kept as-is.
                        if is_true && bare.eq_ignore_ascii_case("property_exists") {
                            if let Some(expr_key) = extract_expr_guard_key(arg_value, ctx, db, file)
                            {
                                if let Some(prop_value) =
                                    call.args.get(1).and_then(|a| a.value.as_ref())
                                {
                                    if let ExprKind::String(prop_name) = &prop_value.kind {
                                        ctx.property_exists_guards.insert((
                                            expr_key,
                                            std::sync::Arc::from(prop_name.as_ref()),
                                        ));
                                    }
                                }
                            }
                        }
                    }
                } else if matches!(
                    bare.to_ascii_lowercase().as_str(),
                    "array_key_exists" | "key_exists"
                ) {
                    // `key_exists()` is a built-in alias of `array_key_exists()`
                    // with identical semantics.
                    narrow_array_key_exists_condition(ctx, call, is_true, db, file);
                } else if matches!(
                    bare.to_ascii_lowercase().as_str(),
                    "str_contains" | "str_starts_with" | "str_ends_with"
                ) {
                    // str_contains($haystack, 'x') true → $haystack is non-empty-string
                    // (when the needle is a non-empty literal — a non-empty needle can
                    // only be found in a non-empty haystack).
                    if is_true {
                        if let (Some(haystack_value), Some(needle_value)) = (
                            call.args.first().and_then(|a| a.value.as_ref()),
                            call.args.get(1).and_then(|a| a.value.as_ref()),
                        ) {
                            let needle_non_empty =
                                expr_is_nonempty_string_literal(needle_value, ctx, db, file);
                            if needle_non_empty {
                                match ScalarArgTarget::extract(haystack_value) {
                                    Some(ScalarArgTarget::Var(var_name)) => {
                                        let current = ctx.get_var(&var_name);
                                        if !current.is_mixed() {
                                            let narrowed = narrow_string_to_non_empty(&current);
                                            if narrowed != current {
                                                ctx.set_var(&var_name, narrowed);
                                            }
                                        }
                                    }
                                    Some(ScalarArgTarget::Prop(obj, prop)) => {
                                        let current =
                                            resolve_prop_current_type(ctx, &obj, &prop, db, file);
                                        if !current.is_mixed() {
                                            let narrowed = narrow_string_to_non_empty(&current);
                                            apply_prop_narrowed(
                                                ctx, &obj, &prop, current, narrowed, false,
                                            );
                                        }
                                        // A non-empty needle can't be found in a null
                                        // receiver's coerced-to-"" property.
                                        narrow_receiver_non_null_on_prop_match(ctx, &obj, true);
                                    }
                                    None => {
                                        // ScalarArgTarget has no static-property
                                        // variant (tracked as S19) — extract it
                                        // call-site-locally instead, mirroring
                                        // gettype()/count()/strlen() on a static prop.
                                        if let Some((fqcn, prop)) = extract_static_prop_access(
                                            haystack_value,
                                            ctx,
                                            db,
                                            file,
                                        ) {
                                            let current = resolve_static_prop_current_type(
                                                ctx, &fqcn, &prop, db,
                                            );
                                            if !current.is_mixed() {
                                                let narrowed = narrow_string_to_non_empty(&current);
                                                apply_prop_narrowed(
                                                    ctx, &fqcn, &prop, current, narrowed, false,
                                                );
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                } else if bare.eq_ignore_ascii_case("in_array") {
                    narrow_in_array_condition(ctx, call, is_true, db, file);
                } else if bare.eq_ignore_ascii_case("is_a") {
                    // is_a($obj, 'ClassName') → instanceof semantics (includes exact class).
                    // When $allow_string (3rd arg) is truthy, the first arg may be a class-string
                    // — preserve string/class-string atoms so the true branch stays reachable
                    // and no false diverge is set in the false branch.
                    if let (Some(obj_value), Some(class_value)) = (
                        call.args.first().and_then(|a| a.value.as_ref()),
                        call.args.get(1).and_then(|a| a.value.as_ref()),
                    ) {
                        if let Some(var_name) = extract_var_name(obj_value) {
                            if let Some(class_name) = extract_class_fqcn_from_expr(
                                class_value,
                                ctx.self_fqcn.as_deref(),
                                ctx.static_fqcn.as_deref(),
                                ctx.parent_fqcn.as_deref(),
                                db,
                                file,
                            ) {
                                let allow_string = call
                                    .args
                                    .get(2)
                                    .and_then(|a| a.value.as_ref())
                                    .map(is_truthy_bool_literal)
                                    .unwrap_or(false);
                                let current = ctx.get_var(&var_name);
                                if allow_string {
                                    // When allow_string is true, string/class-string atoms are
                                    // valid is_a() true-branch values — preserve them so the
                                    // true branch stays reachable and type is not wrongly erased.
                                    let narrowed = if is_true {
                                        // Partition into string-like (kept only when consistent
                                        // with class_name) and object-like (narrowed via
                                        // instanceof) so `narrow_instanceof_preserving_subtypes`
                                        // fallback doesn't inject a spurious named-object atom
                                        // when the current type is purely string/class-string.
                                        let (mut result, obj_part) =
                                            partition_is_a_string_like(&current, &class_name, db);
                                        if !obj_part.is_empty() || current.is_mixed() {
                                            let obj_src = if obj_part.is_empty() {
                                                &current
                                            } else {
                                                &obj_part
                                            };
                                            let obj_narrowed =
                                                narrow_instanceof_preserving_subtypes(
                                                    obj_src,
                                                    &class_name,
                                                    db,
                                                    &ctx.template_param_names,
                                                );
                                            for atom in obj_narrowed.types.iter() {
                                                result.add_type(atom.clone());
                                            }
                                        }
                                        result
                                    } else {
                                        filter_out_is_a_string_match(&current, &class_name, db)
                                    };
                                    // Don't mark diverges when allow_string is set: a
                                    // class-string variable may still be a valid non-object
                                    // value that passes the test.
                                    set_narrowed(ctx, &var_name, &current, narrowed, false);
                                } else {
                                    let narrowed = if is_true {
                                        narrow_instanceof_preserving_subtypes(
                                            &current,
                                            &class_name,
                                            db,
                                            &ctx.template_param_names,
                                        )
                                    } else {
                                        filter_out_instanceof_match(&current, &class_name, db)
                                    };
                                    set_narrowed(ctx, &var_name, &current, narrowed, true);
                                }
                            }
                        } else if let Some((obj, prop)) = extract_any_prop_access(obj_value) {
                            if let Some(class_name) = extract_class_fqcn_from_expr(
                                class_value,
                                ctx.self_fqcn.as_deref(),
                                ctx.static_fqcn.as_deref(),
                                ctx.parent_fqcn.as_deref(),
                                db,
                                file,
                            ) {
                                let allow_string = call
                                    .args
                                    .get(2)
                                    .and_then(|a| a.value.as_ref())
                                    .map(is_truthy_bool_literal)
                                    .unwrap_or(false);
                                narrow_prop_is_a(
                                    ctx,
                                    &obj,
                                    &prop,
                                    &class_name,
                                    allow_string,
                                    db,
                                    file,
                                    is_true,
                                );
                                narrow_receiver_non_null_on_prop_match(ctx, &obj, is_true);
                            }
                        } else if let Some((fqcn, prop)) =
                            extract_static_prop_access(obj_value, ctx, db, file)
                        {
                            if let Some(class_name) = extract_class_fqcn_from_expr(
                                class_value,
                                ctx.self_fqcn.as_deref(),
                                ctx.static_fqcn.as_deref(),
                                ctx.parent_fqcn.as_deref(),
                                db,
                                file,
                            ) {
                                let allow_string = call
                                    .args
                                    .get(2)
                                    .and_then(|a| a.value.as_ref())
                                    .map(is_truthy_bool_literal)
                                    .unwrap_or(false);
                                narrow_static_prop_is_a(
                                    ctx,
                                    &fqcn,
                                    &prop,
                                    &class_name,
                                    allow_string,
                                    db,
                                    is_true,
                                );
                            }
                        }
                    }
                } else if bare.eq_ignore_ascii_case("is_subclass_of") {
                    // is_subclass_of($obj, 'ClassName') → strict-subclass check: the exact
                    // class itself is NOT a subclass of itself.
                    // True branch: keep only atoms that are known strict subclasses.
                    // False branch: no narrowing — is_subclass_of being false doesn't tell us
                    // the variable isn't Foo; Foo is not a subclass of itself, so Foo atoms
                    // must be kept (removing them would make a later `Foo` use diverge).
                    if let (Some(obj_value), Some(class_value)) = (
                        call.args.first().and_then(|a| a.value.as_ref()),
                        call.args.get(1).and_then(|a| a.value.as_ref()),
                    ) {
                        if let Some(var_name) = extract_var_name(obj_value) {
                            if let Some(class_name) = extract_class_fqcn_from_expr(
                                class_value,
                                ctx.self_fqcn.as_deref(),
                                ctx.static_fqcn.as_deref(),
                                ctx.parent_fqcn.as_deref(),
                                db,
                                file,
                            ) {
                                let current = ctx.get_var(&var_name);
                                if is_true {
                                    let narrowed = narrow_strict_subclass_of(
                                        &current,
                                        &class_name,
                                        db,
                                        &ctx.template_param_names,
                                    );
                                    // mark_diverges=false: the exact class being absent from
                                    // strict-subclass narrowing doesn't make the branch dead.
                                    set_narrowed(ctx, &var_name, &current, narrowed, false);
                                }
                                // False branch: leave current type unchanged.
                            }
                        } else if let Some((obj, prop)) = extract_any_prop_access(obj_value) {
                            if let Some(class_name) = extract_class_fqcn_from_expr(
                                class_value,
                                ctx.self_fqcn.as_deref(),
                                ctx.static_fqcn.as_deref(),
                                ctx.parent_fqcn.as_deref(),
                                db,
                                file,
                            ) {
                                narrow_prop_is_subclass_of(
                                    ctx,
                                    &obj,
                                    &prop,
                                    &class_name,
                                    db,
                                    file,
                                    is_true,
                                );
                                narrow_receiver_non_null_on_prop_match(ctx, &obj, is_true);
                            }
                        } else if let Some((fqcn, prop)) =
                            extract_static_prop_access(obj_value, ctx, db, file)
                        {
                            if let Some(class_name) = extract_class_fqcn_from_expr(
                                class_value,
                                ctx.self_fqcn.as_deref(),
                                ctx.static_fqcn.as_deref(),
                                ctx.parent_fqcn.as_deref(),
                                db,
                                file,
                            ) {
                                narrow_static_prop_is_subclass_of(
                                    ctx,
                                    &fqcn,
                                    &prop,
                                    &class_name,
                                    db,
                                    is_true,
                                );
                            }
                        }
                    }
                } else if apply_docblock_assertions(call, ctx, is_true, db, file, fn_name) {
                    // User-defined assertion applied.
                } else if let Some(arg_value) = call
                    .args
                    .first()
                    .and_then(|arg_expr| arg_expr.value.as_ref())
                {
                    if let Some(var_name) = extract_var_name(arg_value) {
                        narrow_from_type_fn(ctx, bare, &var_name, db, is_true);
                    } else if let Some((obj, prop)) = extract_any_prop_access(arg_value) {
                        narrow_prop_from_type_fn(ctx, bare, &obj, &prop, db, file, is_true);
                    } else if let Some((fqcn, prop)) =
                        extract_static_prop_access(arg_value, ctx, db, file)
                    {
                        narrow_static_prop_from_type_fn(ctx, bare, &fqcn, &prop, db, is_true);
                    }
                }
            }
        }

        // $obj->isFoo($x) with @psalm-assert-if-true/-if-false on isFoo() —
        // method-call counterpart of the FunctionCall arm above (which only
        // ever resolved a free function via `find_function`). Paired with
        // `NullsafeMethodCall` (`$obj?->isFoo($x)`) the same way taint.rs's
        // taint-source check already pairs both — a nullsafe call site was
        // previously silently skipped here.
        ExprKind::MethodCall(mc) | ExprKind::NullsafeMethodCall(mc) => {
            if let ExprKind::Identifier(name) = &mc.method.kind {
                let method_name_lower = crate::util::php_ident_lowercase(name);
                if let Some(fqcn) =
                    method_call_receiver_fqcn(&mc.object, ctx, db, file, &method_name_lower)
                {
                    if let Some(resolved) =
                        crate::call::method::resolve_method_from_db(db, &fqcn, &method_name_lower)
                    {
                        apply_method_docblock_assertions(
                            &mc.args, &mc.object, &resolved, ctx, is_true, db, file,
                        );
                    }
                }
            }
        }

        // Foo::isFoo($x) / self::isFoo($x) with @psalm-assert-if-true/-if-false
        // — static-call counterpart of the FunctionCall arm above.
        ExprKind::StaticMethodCall(smc) => {
            if let Some(fqcn) = resolve_static_call_class_fqcn(&smc.class, ctx, db, file) {
                if let ExprKind::Identifier(name) = &smc.method.kind {
                    let method_name_lower = crate::util::php_ident_lowercase(name);
                    if let Some(resolved) =
                        crate::call::method::resolve_method_from_db(db, &fqcn, &method_name_lower)
                    {
                        apply_method_docblock_assertions(
                            &smc.args, &smc.class, &resolved, ctx, is_true, db, file,
                        );
                    }
                }
            }
        }

        // isset($x)
        ExprKind::Isset(vars) => {
            for var_expr in vars.iter() {
                if let Some(var_name) = extract_var_name(var_expr) {
                    if is_true {
                        // remove null; mark as definitely assigned
                        let current = ctx.get_var(&var_name);
                        ctx.set_var(&var_name, current.remove_null());
                        std::sync::Arc::make_mut(&mut ctx.assigned_vars)
                            .insert(mir_types::Name::from(var_name.as_str()));
                    } else if ctx.var_is_defined(&var_name) {
                        // isset($x) is false and $x is always assigned (e.g. a
                        // parameter) → the only other way isset() can be false is
                        // $x being null.
                        narrow_var_null(ctx, &var_name, true);
                    }
                } else if is_true {
                    // `isset($base[$k])` implies `$base` is a non-null, indexable
                    // value — remove null/false from the base (variable or
                    // property receiver) so a guarded access (`preg_split()`
                    // returns array|false) does not report PossiblyInvalidArrayAccess.
                    if let Some(target) = array_access_base_target(var_expr, ctx, db, file) {
                        narrow_container_non_null_non_false(ctx, &target, db, file);
                    }
                    // For a single-level `isset($arr['key'])` on a shape-typed
                    // base, also narrow that key's OWN value type: remove null
                    // and mark it no longer optional, so a later `$arr['key']`
                    // read inside the guard isn't reported as possibly-null (the
                    // isset check just proved the key is present and non-null).
                    narrow_isset_shape_key(var_expr, ctx, db, file);
                    // `isset($this->prop)` implies the property is non-null too
                    // — the property-receiver counterpart of the bare-variable
                    // case above, since `isset()` is false for both an unset
                    // and a null-valued property.
                    if let Some((obj_var, prop)) = extract_any_prop_access(var_expr) {
                        let current = resolve_prop_current_type(ctx, &obj_var, &prop, db, file);
                        if !current.is_mixed() {
                            let narrowed = current.remove_null();
                            apply_prop_narrowed(ctx, &obj_var, &prop, current, narrowed, true);
                        }
                        // isset() is only true when the receiver was non-null too
                        // (a null receiver's ->/?-> access is itself unset/null).
                        narrow_receiver_non_null_on_prop_match(ctx, &obj_var, true);
                        // Also record a property-exists guard: `isset($obj->prop)`
                        // proves `prop` is set even when it's a dynamic/BC property
                        // undeclared on the class, so a later `$obj->prop` read
                        // inside this guard shouldn't flag UndefinedProperty.
                        ctx.property_exists_guards.insert((
                            std::sync::Arc::from(obj_var.as_str()),
                            std::sync::Arc::from(prop.as_str()),
                        ));
                    } else if let Some((fqcn, prop)) =
                        extract_static_prop_access(var_expr, ctx, db, file)
                    {
                        // `isset(self::$prop)` — static-property counterpart of the
                        // instance-property case above.
                        let current = resolve_static_prop_current_type(ctx, &fqcn, &prop, db);
                        if !current.is_mixed() {
                            let narrowed = current.remove_null();
                            apply_prop_narrowed(ctx, &fqcn, &prop, current, narrowed, true);
                        }
                    }
                } else if !is_true {
                    // `!isset($base['key'])` on a single-level shape-typed base:
                    // exclude union members where the key is provably present
                    // and non-null (isset() would have been true there).
                    narrow_isset_shape_key_false(var_expr, ctx, db, file);
                }
            }
        }

        // empty($x)
        ExprKind::Empty(var_expr) => {
            if let Some(var_name) = extract_var_name(var_expr) {
                let current = ctx.get_var(&var_name);
                let narrowed = if is_true {
                    // empty($x) is true: x is falsy
                    current.narrow_to_falsy()
                } else {
                    // empty($x) is false: x is truthy
                    current.narrow_to_truthy()
                };
                if !narrowed.is_empty() {
                    ctx.set_var(&var_name, narrowed);
                }
            } else {
                if !is_true {
                    // `!empty($base[$k])` implies `$base` is a non-null, indexable
                    // value, same as the `isset($base[$k])` case above.
                    if let Some(target) = array_access_base_target(var_expr, ctx, db, file) {
                        narrow_container_non_null_non_false(ctx, &target, db, file);
                    }
                }
                // For a single-level `empty($arr['key'])` on a shape-typed base,
                // also narrow that key's OWN value type by truthiness, mirroring
                // narrow_isset_shape_key.
                narrow_empty_shape_key(var_expr, ctx, is_true, db, file);
                // `empty($this->prop)` — property-receiver counterpart of the
                // bare-variable truthy/falsy case, mirroring the `if ($this->prop)`
                // arm below (narrow_prop_loose_bool). `empty()` inverts truthiness:
                // is_true means the property is falsy.
                if let Some((obj_var, prop)) = extract_any_prop_access(var_expr) {
                    narrow_prop_loose_bool(ctx, &obj_var, &prop, db, file, !is_true);
                    // `empty()` is false (the property is truthy) only when the
                    // receiver was non-null too — a null receiver's ->/?->
                    // access is itself falsy, so `empty()` would be true.
                    narrow_receiver_non_null_on_prop_match(ctx, &obj_var, !is_true);
                } else if let Some((fqcn, prop)) =
                    extract_static_prop_access(var_expr, ctx, db, file)
                {
                    // `empty(self::$prop)` — static-property counterpart of the
                    // instance-property case above.
                    narrow_static_prop_loose_bool(ctx, &fqcn, &prop, db, !is_true);
                }
            }
        }

        // ($x = expr) / ($x ??= expr) used as a condition
        // The assignment has already been evaluated (ctx holds the post-assignment type).
        // Narrow the target variable based on the truthiness of the expression result.
        ExprKind::Assign(a) if matches!(a.op, AssignOp::Assign | AssignOp::Coalesce) => {
            if let Some(var_name) = extract_var_name(&a.target) {
                let current = ctx.get_var(&var_name);
                let mut narrowed = if is_true {
                    current.narrow_to_truthy()
                } else {
                    current.narrow_to_falsy()
                };
                // In the true-branch the assignment definitely executed, so
                // the variable is always defined here — clear possibly_undefined.
                if is_true {
                    narrowed.possibly_undefined = false;
                }
                if !narrowed.is_empty() {
                    ctx.set_var(&var_name, narrowed);
                } else if !current.is_empty() && !current.is_mixed() {
                    ctx.diverges = true;
                }
            } else if let Some((obj_var, prop)) = extract_prop_access(&a.target) {
                // `if ($this->prop = expr)` — property-access counterpart of
                // the plain-variable case above. Reaching the assignment at
                // all (regardless of branch) already proves $obj_var was
                // non-null: PHP fatals assigning a property on a null
                // receiver, unlike a plain read.
                narrow_var_null(ctx, &obj_var, false);
                let current = resolve_prop_current_type(ctx, &obj_var, &prop, db, file);
                let mut narrowed = if is_true {
                    current.narrow_to_truthy()
                } else {
                    current.narrow_to_falsy()
                };
                if is_true {
                    narrowed.possibly_undefined = false;
                }
                apply_prop_narrowed(ctx, &obj_var, &prop, current, narrowed, true);
            } else if let Some((fqcn, prop)) = extract_static_prop_access(&a.target, ctx, db, file)
            {
                // `if (self::$prop = expr)` — static-property counterpart of
                // the instance-property case above (no receiver to narrow).
                let current = resolve_static_prop_current_type(ctx, &fqcn, &prop, db);
                let mut narrowed = if is_true {
                    current.narrow_to_truthy()
                } else {
                    current.narrow_to_falsy()
                };
                if is_true {
                    narrowed.possibly_undefined = false;
                }
                apply_prop_narrowed(ctx, &fqcn, &prop, current, narrowed, true);
            }
        }

        // if ($x)  — truthy/falsy narrowing
        _ => {
            if let Some(var_name) = extract_var_name(expr) {
                let current = ctx.get_var(&var_name);
                let narrowed = if is_true {
                    current.narrow_to_truthy()
                } else {
                    current.narrow_to_falsy()
                };
                if !narrowed.is_empty() {
                    ctx.set_var(&var_name, narrowed);
                } else if !current.is_empty()
                    && !current.is_mixed()
                    && ctx.var_is_defined(&var_name)
                {
                    // The variable's type can never satisfy this truthiness
                    // constraint → this branch is statically unreachable.
                    // Possibly-undefined variables are exempt: an unset
                    // variable reads as null (falsy), so the branch stays
                    // reachable at runtime.
                    ctx.diverges = true;
                }
            } else if let Some((obj_var, prop)) = extract_any_prop_access(expr) {
                // `if ($this->prop)` — property-receiver counterpart of the
                // bare-variable truthy/falsy case above.
                narrow_prop_loose_bool(ctx, &obj_var, &prop, db, file, is_true);
                // Truthy also proves the receiver was non-null (a null
                // receiver's ->/?-> access is itself falsy).
                narrow_receiver_non_null_on_prop_match(ctx, &obj_var, is_true);
            } else if let Some((fqcn, prop)) = extract_static_prop_access(expr, ctx, db, file) {
                // `if (self::$prop)` — static-property counterpart of the
                // instance-property case above.
                narrow_static_prop_loose_bool(ctx, &fqcn, &prop, db, is_true);
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Narrow from a call compared against the `false` literal — the idiomatic
/// way to interpret `strpos()`/`array_search()`'s `int|string|false` result,
/// since a loose truthy check misfires on a match at offset/key 0. `is_false`
/// is whether `expr === false` holds in this branch (so `!is_false` means
/// the call proved a match — a substring was found, or the needle is
/// present in the haystack). The strpos()/stripos()-family arm (string
/// domain) is split out into `strings::narrow_string_false_comparable_condition`;
/// the `array_search()` arm below stays here (array domain).
fn narrow_from_false_comparable_call(
    expr: &php_ast::owned::Expr,
    ctx: &mut FlowState,
    db: &dyn MirDatabase,
    file: &str,
    is_false: bool,
) {
    let ExprKind::FunctionCall(call) = &expr.kind else {
        return;
    };
    let fn_name_opt: Option<&str> = match &call.name.kind {
        ExprKind::Identifier(name) => Some(name.as_ref()),
        _ => None,
    };
    let Some(fn_name) = fn_name_opt else {
        return;
    };
    let bare = fn_name.trim_start_matches('\\');
    if matches!(
        bare.to_ascii_lowercase().as_str(),
        "strpos"
            | "stripos"
            | "strrpos"
            | "strripos"
            | "mb_strpos"
            | "mb_stripos"
            | "mb_strrpos"
            | "mb_strripos"
    ) {
        narrow_string_false_comparable_condition(call, ctx, db, file, is_false);
    } else if bare.eq_ignore_ascii_case("array_search") {
        // array_search($needle, $haystack) !== false / === false — same
        // haystack-literal narrowing as in_array(), keyed off the same
        // strict/loose-safety rule (see in_array_loose_narrowing_is_safe).
        let strict = call
            .args
            .get(2)
            .and_then(|a| a.value.as_ref())
            .map(is_truthy_bool_literal)
            .unwrap_or(false);
        if let (Some(needle_value), Some(haystack_value)) = (
            call.args.first().and_then(|a| a.value.as_ref()),
            call.args.get(1).and_then(|a| a.value.as_ref()),
        ) {
            strip_haystack_null(haystack_value, ctx, db, file);
            if let Some(target) = ScalarArgTarget::extract(needle_value) {
                if let Some(haystack_ty) = extract_haystack_type(haystack_value, ctx, db, file) {
                    if !is_false {
                        if let ScalarArgTarget::Prop(obj, _) = &target {
                            // array_search(null, $haystack) only matches loosely when the
                            // haystack contains a falsy literal (0, "", "0"); mirrors
                            // in_array()'s identical reasoning.
                            if strict || !haystack_admits_null_loosely(&haystack_ty) {
                                narrow_receiver_non_null_on_prop_match(ctx, obj, true);
                            }
                        }
                    }
                    let current = match &target {
                        ScalarArgTarget::Var(name) => ctx.get_var(name),
                        ScalarArgTarget::Prop(obj, prop) => {
                            resolve_prop_current_type(ctx, obj, prop, db, file)
                        }
                    };
                    let loose_safe =
                        strict || in_array_loose_narrowing_is_safe(&current, &haystack_ty);
                    if !current.is_mixed() && loose_safe {
                        let narrowed = if !is_false {
                            // Found: keep only atoms that could match a haystack value.
                            let narrowed = narrow_to_haystack_values(&current, &haystack_ty);
                            (!narrowed.is_empty() && narrowed != current).then_some(narrowed)
                        } else {
                            // Not found: safe only when current is a finite literal
                            // union — remove the matched haystack values.
                            let all_literals = !current.types.is_empty()
                                && current.types.iter().all(|a| {
                                    matches!(a, Atomic::TLiteralString(_) | Atomic::TLiteralInt(_))
                                });
                            all_literals
                                .then(|| {
                                    current.filter(|a| !haystack_ty.types.iter().any(|h| h == a))
                                })
                                .filter(|narrowed| !narrowed.is_empty() && *narrowed != current)
                        };
                        if let Some(narrowed) = narrowed {
                            match target {
                                ScalarArgTarget::Var(name) => ctx.set_var(&name, narrowed),
                                ScalarArgTarget::Prop(obj, prop) => {
                                    apply_prop_narrowed(ctx, &obj, &prop, current, narrowed, false)
                                }
                            }
                        }
                    }
                }
            } else if let Some((fqcn, prop)) =
                extract_static_prop_access(needle_value, ctx, db, file)
            {
                // ScalarArgTarget has no static-property variant (tracked as S19) —
                // extract it call-site-locally instead, mirroring the
                // str_contains-family recipe (no receiver to narrow non-null).
                if let Some(haystack_ty) = extract_haystack_type(haystack_value, ctx, db, file) {
                    let current = resolve_static_prop_current_type(ctx, &fqcn, &prop, db);
                    let loose_safe =
                        strict || in_array_loose_narrowing_is_safe(&current, &haystack_ty);
                    if !current.is_mixed() && loose_safe {
                        let narrowed = if !is_false {
                            let narrowed = narrow_to_haystack_values(&current, &haystack_ty);
                            (!narrowed.is_empty() && narrowed != current).then_some(narrowed)
                        } else {
                            let all_literals = !current.types.is_empty()
                                && current.types.iter().all(|a| {
                                    matches!(a, Atomic::TLiteralString(_) | Atomic::TLiteralInt(_))
                                });
                            all_literals
                                .then(|| {
                                    current.filter(|a| !haystack_ty.types.iter().any(|h| h == a))
                                })
                                .filter(|narrowed| !narrowed.is_empty() && *narrowed != current)
                        };
                        if let Some(narrowed) = narrowed {
                            apply_prop_narrowed(ctx, &fqcn, &prop, current, narrowed, false);
                        }
                    }
                }
            }
        }
    }
}

/// Narrow a property access `$obj->prop` by a null check.
/// PHP 8 reads a plain `->` access on a null receiver as a warning, not a
/// fatal error, evaluating to `null` just like `?->` would — so when the
/// receiver's type actually admits null, `$obj->prop` carries the exact same
/// null-source ambiguity as the nullsafe form and is narrowed identically
/// (see `narrow_nullsafe_prop_null`). When the receiver's type provably
/// excludes null, that ambiguity doesn't exist and a property-null
/// contradiction is a genuine contradiction, so divergence is still marked.
fn narrow_prop_null(
    ctx: &mut FlowState,
    obj_var: &str,
    prop: &str,
    db: &dyn MirDatabase,
    file: &str,
    is_null: bool,
) {
    if !ctx.get_var(obj_var).is_nullable() {
        narrow_prop_null_with_divergence(ctx, obj_var, prop, db, file, is_null, true);
        return;
    }
    narrow_nullsafe_prop_null(ctx, obj_var, prop, db, file, is_null);
}

fn narrow_prop_null_with_divergence(
    ctx: &mut FlowState,
    obj_var: &str,
    prop: &str,
    db: &dyn MirDatabase,
    file: &str,
    is_null: bool,
    mark_diverges: bool,
) {
    let current = resolve_prop_current_type(ctx, obj_var, prop, db, file);

    if current.is_mixed() {
        return;
    }
    let narrowed = if is_null {
        current.narrow_to_null()
    } else {
        current.remove_null()
    };
    apply_prop_narrowed(ctx, obj_var, prop, current, narrowed, mark_diverges);
}

/// Narrow a nullsafe property access (`$obj?->prop`) by a null check. Also
/// used by the plain (`->`) form via `narrow_prop_null` — both read as
/// `null` when EITHER the receiver is null OR the property's own value is
/// null, so:
/// - the `is_null=true` direction must never mark divergence: proving the
///   property's own declared type excludes null doesn't rule out the
///   receiver-null source.
/// - the `is_null=false` direction additionally proves the receiver itself
///   is non-null, since a null receiver could only ever produce `null` here.
fn narrow_nullsafe_prop_null(
    ctx: &mut FlowState,
    obj_var: &str,
    prop: &str,
    db: &dyn MirDatabase,
    file: &str,
    is_null: bool,
) {
    narrow_prop_null_with_divergence(ctx, obj_var, prop, db, file, is_null, !is_null);
    if !is_null {
        narrow_var_null(ctx, obj_var, false);
    }
}

/// Narrow a nullsafe METHOD call (`$obj?->method()`) by a null check on its
/// result. Unlike a nullsafe property access, a method call result has no
/// standalone target to narrow later — the only provable fact is the
/// receiver's own nullability, and only when the method's declared return
/// type provably excludes null (a return type that could itself be null
/// carries the same receiver-vs-own-value ambiguity `narrow_nullsafe_prop_null`
/// documents for properties, so bail out then). When it does exclude null,
/// the deduction is actually stronger than the property case: since the
/// method can never contribute a null itself, `$obj?->method() === null`
/// holds if and only if `$obj` was null — both directions narrow `$obj`, not
/// just the non-null direction. Only handles a receiver resolved to a single
/// concrete class and a non-generic return type; anything else is left
/// unnarrowed rather than risk an unsound deduction.
fn narrow_nullsafe_method_call_null(
    ctx: &mut FlowState,
    mc: &php_ast::owned::MethodCallExpr,
    db: &dyn MirDatabase,
    is_null: bool,
) {
    let Some(obj_var) = extract_var_name(&mc.object) else {
        return;
    };
    let ExprKind::Identifier(method_name) = &mc.method.kind else {
        return;
    };
    let obj_ty = ctx.get_var(&obj_var);
    // Only the receiver's non-null atoms matter here — `?Bar $bar` has
    // `Bar|null`, and the whole point is deducing $bar's nullability, not
    // requiring it to already be known non-null.
    let non_null_atoms: Vec<&Atomic> = obj_ty
        .types
        .iter()
        .filter(|t| !matches!(t, Atomic::TNull))
        .collect();
    let fqcn = match non_null_atoms.as_slice() {
        [Atomic::TNamedObject { fqcn, .. }] => *fqcn,
        _ => return,
    };
    let here = crate::db::Fqcn::from_str(db, fqcn.as_ref());
    let Some((_, method)) = crate::db::find_method_in_chain(db, here, method_name.as_ref()) else {
        return;
    };
    let Some(return_ty) = method.return_type.as_deref() else {
        return;
    };
    if return_ty.is_mixed()
        || return_ty.contains(|t| matches!(t, Atomic::TNull | Atomic::TTemplateParam { .. }))
    {
        return;
    }
    narrow_var_null(ctx, &obj_var, is_null);
}

fn narrow_static_prop_null(
    ctx: &mut FlowState,
    fqcn: &str,
    prop: &str,
    db: &dyn MirDatabase,
    is_null: bool,
) {
    let current = resolve_static_prop_current_type(ctx, fqcn, prop, db);

    if current.is_mixed() {
        return;
    }
    let narrowed = if is_null {
        current.narrow_to_null()
    } else {
        current.remove_null()
    };
    apply_prop_narrowed(ctx, fqcn, prop, current, narrowed, true);
}

/// Loose-null counterpart of `narrow_static_prop_null`, for
/// `self::$prop == null` / `!= null` (and `static::$prop`/`Class::$prop`).
/// Loose semantics differ from strict: `== null` is also true for any other
/// falsy value (not just `null` itself), so the true direction only narrows
/// to falsy — same reasoning as `narrow_var_loose_null`/`narrow_prop_loose_null`.
/// The false direction stays exactly as sound as the strict form.
fn narrow_static_prop_loose_null(
    ctx: &mut FlowState,
    fqcn: &str,
    prop: &str,
    db: &dyn MirDatabase,
    is_null: bool,
) {
    let current = resolve_static_prop_current_type(ctx, fqcn, prop, db);
    if current.is_mixed() {
        return;
    }
    let narrowed = if is_null {
        current.narrow_to_falsy()
    } else {
        current.remove_null()
    };
    apply_prop_narrowed(ctx, fqcn, prop, current, narrowed, true);
}

/// Static-property counterpart of `narrow_prop_literal_string`, for
/// `self::$prop === 'literal'` / `static::$prop === 'literal'` /
/// `Class::$prop === 'literal'`.
fn narrow_static_prop_literal_string(
    ctx: &mut FlowState,
    fqcn: &str,
    prop: &str,
    db: &dyn MirDatabase,
    value: &str,
    is_value: bool,
) {
    let current = resolve_static_prop_current_type(ctx, fqcn, prop, db);
    let narrowed = literal_string_narrow_type(&current, value, is_value);
    let mark_diverges = crate::contradiction::is_closed_precise(&current);
    apply_prop_narrowed(ctx, fqcn, prop, current, narrowed, mark_diverges);
}

/// Static-property counterpart of `narrow_prop_literal_int`, for
/// `self::$prop === 42` / `static::$prop === 42` / `Class::$prop === 42`.
fn narrow_static_prop_literal_int(
    ctx: &mut FlowState,
    fqcn: &str,
    prop: &str,
    db: &dyn MirDatabase,
    value: i64,
    is_value: bool,
) {
    let current = resolve_static_prop_current_type(ctx, fqcn, prop, db);
    let narrowed = literal_int_narrow_type(&current, value, is_value);
    let mark_diverges = crate::contradiction::is_closed_precise(&current);
    apply_prop_narrowed(ctx, fqcn, prop, current, narrowed, mark_diverges);
}

/// Loose-equality counterpart of `narrow_static_prop_literal_int`, for
/// `self::$prop == 42` / `static::$prop != 42` / `Class::$prop == 42` —
/// static-property sibling of `narrow_var_loose_int`, same safety gate.
fn narrow_static_prop_loose_int(
    ctx: &mut FlowState,
    fqcn: &str,
    prop: &str,
    db: &dyn MirDatabase,
    value: i64,
    is_value: bool,
) {
    let current = resolve_static_prop_current_type(ctx, fqcn, prop, db);
    if current.types.is_empty()
        || !current
            .types
            .iter()
            .all(|a| atom_safe_for_loose_int_narrowing(a, value))
    {
        return;
    }
    let narrowed = literal_int_narrow_type(&current, value, is_value);
    let mark_diverges = crate::contradiction::is_closed_precise(&current);
    apply_prop_narrowed(ctx, fqcn, prop, current, narrowed, mark_diverges);
}

/// Static-property counterpart of `narrow_prop_bool`, for
/// `self::$prop === true` / `static::$prop === false` / `Class::$prop === true`.
fn narrow_static_prop_bool(
    ctx: &mut FlowState,
    fqcn: &str,
    prop: &str,
    db: &dyn MirDatabase,
    value: bool,
    is_value: bool,
) {
    let current = resolve_static_prop_current_type(ctx, fqcn, prop, db);
    if current.is_mixed() {
        return;
    }
    let narrowed = bool_narrow_type(&current, value, is_value);
    // mark_diverges=false: matches narrow_var_bool/narrow_prop_bool's rationale
    // — a separate contradiction pass already owns flagging an always-true/false compare.
    apply_prop_narrowed(ctx, fqcn, prop, current, narrowed, false);
}

/// Static-property counterpart of `narrow_prop_loose_bool`, for
/// `self::$prop == true` / `false` (or `static::$prop`/`Class::$prop`).
fn narrow_static_prop_loose_bool(
    ctx: &mut FlowState,
    fqcn: &str,
    prop: &str,
    db: &dyn MirDatabase,
    want_truthy: bool,
) {
    let current = resolve_static_prop_current_type(ctx, fqcn, prop, db);
    if current.is_mixed() {
        return;
    }
    let narrowed = if want_truthy {
        current.narrow_to_truthy()
    } else {
        current.narrow_to_falsy()
    };
    // mark_diverges=false: matches narrow_prop_loose_bool's rationale — a
    // separate contradiction pass already owns flagging an always-true/false compare.
    apply_prop_narrowed(ctx, fqcn, prop, current, narrowed, false);
}