phpantom_lsp 0.7.0

Fast PHP language server with deep type intelligence. Generics, Laravel, PHPStan annotations. Ready in an instant.
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
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
use super::docblock::is_navigable_type;
use super::extraction::extract_symbol_map;
use super::*;

// ── SymbolMap::lookup tests ─────────────────────────────────────────

fn make_span(start: u32, end: u32, name: &str) -> SymbolSpan {
    SymbolSpan {
        start,
        end,
        kind: SymbolKind::ClassReference {
            name: name.to_string(),
            is_fqn: false,
        },
    }
}

#[test]
fn lookup_empty_map_returns_none() {
    let map = SymbolMap::default();
    assert!(map.lookup(0).is_none());
    assert!(map.lookup(100).is_none());
}

#[test]
fn lookup_hit_at_start() {
    let map = SymbolMap {
        spans: vec![make_span(10, 15, "Foo")],
        ..Default::default()
    };
    assert!(map.lookup(10).is_some());
    assert_eq!(map.lookup(10).unwrap().start, 10);
}

#[test]
fn lookup_hit_at_end_minus_one() {
    let map = SymbolMap {
        spans: vec![make_span(10, 15, "Foo")],
        ..Default::default()
    };
    assert!(map.lookup(14).is_some());
}

#[test]
fn lookup_miss_at_end() {
    let map = SymbolMap {
        spans: vec![make_span(10, 15, "Foo")],
        ..Default::default()
    };
    assert!(map.lookup(15).is_none());
}

#[test]
fn lookup_miss_before_first_span() {
    let map = SymbolMap {
        spans: vec![make_span(10, 15, "Foo")],
        ..Default::default()
    };
    assert!(map.lookup(5).is_none());
}

#[test]
fn lookup_miss_in_gap() {
    let map = SymbolMap {
        spans: vec![make_span(10, 15, "Foo"), make_span(20, 25, "Bar")],
        ..Default::default()
    };
    assert!(map.lookup(17).is_none());
}

#[test]
fn lookup_correct_span_in_sequence() {
    let map = SymbolMap {
        spans: vec![
            make_span(10, 15, "Foo"),
            make_span(20, 25, "Bar"),
            make_span(30, 35, "Baz"),
        ],
        ..Default::default()
    };
    let result = map.lookup(22).unwrap();
    if let SymbolKind::ClassReference { ref name, .. } = result.kind {
        assert_eq!(name, "Bar");
    } else {
        panic!("Expected ClassReference");
    }
}

// ── is_navigable_type tests ─────────────────────────────────────────

#[test]
fn scalar_types_are_not_navigable() {
    assert!(!is_navigable_type("int"));
    assert!(!is_navigable_type("string"));
    assert!(!is_navigable_type("bool"));
    assert!(!is_navigable_type("void"));
    assert!(!is_navigable_type("null"));
    assert!(!is_navigable_type("mixed"));
    assert!(!is_navigable_type("array"));
    assert!(!is_navigable_type("callable"));
    assert!(!is_navigable_type("float"));
    assert!(!is_navigable_type("never"));
    assert!(!is_navigable_type("iterable"));
    assert!(!is_navigable_type("true"));
    assert!(!is_navigable_type("false"));
    assert!(!is_navigable_type("resource"));
    assert!(!is_navigable_type("object"));
}

#[test]
fn class_names_are_navigable() {
    assert!(is_navigable_type("Foo"));
    assert!(is_navigable_type("Collection"));
    assert!(is_navigable_type("App\\Models\\User"));
    assert!(is_navigable_type("ResponseInterface"));
}

#[test]
fn case_insensitive_scalar_check() {
    assert!(!is_navigable_type("INT"));
    assert!(!is_navigable_type("String"));
    assert!(!is_navigable_type("BOOL"));
}

#[test]
fn empty_name_is_not_navigable() {
    assert!(!is_navigable_type(""));
}

// ── extract_symbol_map integration tests ────────────────────────────

fn parse_and_extract(php: &str) -> SymbolMap {
    let arena = bumpalo::Bump::new();
    let file_id = mago_database::file::FileId::new("test.php");
    let program = mago_syntax::parser::parse_file_content(&arena, file_id, php);
    extract_symbol_map(program, php)
}

#[test]
fn class_declaration_produces_class_declaration() {
    let php = "<?php\nclass Foo {}\n";
    let map = parse_and_extract(php);
    let hit = map.lookup(php.find("Foo").unwrap() as u32);
    assert!(hit.is_some());
    if let SymbolKind::ClassDeclaration { ref name } = hit.unwrap().kind {
        assert_eq!(name, "Foo");
    } else {
        panic!("Expected ClassDeclaration, got {:?}", hit.unwrap().kind);
    }
}

#[test]
fn extends_produces_class_reference() {
    let php = "<?php\nclass Foo extends Bar {}\n";
    let map = parse_and_extract(php);
    let bar_offset = php.find("Bar").unwrap() as u32;
    let hit = map.lookup(bar_offset);
    assert!(hit.is_some());
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Bar");
    } else {
        panic!("Expected ClassReference for Bar");
    }
}

#[test]
fn extends_fqn_sets_is_fqn() {
    let php = "<?php\nclass Foo extends \\App\\Bar {}\n";
    let map = parse_and_extract(php);
    // Find "\\App\\Bar" — the `\` at the start
    let fqn_offset = php.find("\\App\\Bar").unwrap() as u32;
    let hit = map.lookup(fqn_offset);
    assert!(hit.is_some(), "Should have a span at the FQN");
    if let SymbolKind::ClassReference { ref name, is_fqn } = hit.unwrap().kind {
        assert_eq!(name, "App\\Bar");
        assert!(is_fqn, "FQN should be marked as is_fqn");
    } else {
        panic!(
            "Expected ClassReference for FQN, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn implements_produces_class_reference() {
    let php = "<?php\nclass Foo implements Baz, Qux {}\n";
    let map = parse_and_extract(php);

    let baz_offset = php.find("Baz").unwrap() as u32;
    let hit = map.lookup(baz_offset);
    assert!(hit.is_some());
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Baz");
    } else {
        panic!("Expected ClassReference for Baz");
    }

    let qux_offset = php.find("Qux").unwrap() as u32;
    let hit = map.lookup(qux_offset);
    assert!(hit.is_some());
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Qux");
    } else {
        panic!("Expected ClassReference for Qux");
    }
}

#[test]
fn variable_produces_variable_span() {
    let php = "<?php\nfunction test() { $foo = 1; }\n";
    let map = parse_and_extract(php);
    let offset = php.find("$foo").unwrap() as u32;
    let hit = map.lookup(offset);
    assert!(hit.is_some());
    if let SymbolKind::Variable { ref name } = hit.unwrap().kind {
        assert_eq!(name, "foo");
    } else {
        panic!("Expected Variable");
    }
}

#[test]
fn function_call_produces_function_call_span() {
    let php = "<?php\nfunction test() { strlen('hello'); }\n";
    let map = parse_and_extract(php);
    let offset = php.find("strlen").unwrap() as u32;
    let hit = map.lookup(offset);
    assert!(hit.is_some());
    if let SymbolKind::FunctionCall { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "strlen");
    } else {
        panic!("Expected FunctionCall, got {:?}", hit.unwrap().kind);
    }
}

#[test]
fn method_call_produces_member_access() {
    let php = "<?php\nclass Foo { function test() { $this->bar(); } }\n";
    let map = parse_and_extract(php);
    let bar_offset = php.find("bar").unwrap() as u32;
    let hit = map.lookup(bar_offset);
    assert!(hit.is_some());
    if let SymbolKind::MemberAccess {
        ref subject_text,
        ref member_name,
        is_static,
        is_method_call,
        ..
    } = hit.unwrap().kind
    {
        assert_eq!(member_name, "bar");
        assert_eq!(subject_text, "$this");
        assert!(!is_static);
        assert!(is_method_call);
    } else {
        panic!("Expected MemberAccess");
    }
}

#[test]
fn static_method_call_produces_member_access() {
    let php = "<?php\nclass Foo { function test() { self::create(); } }\n";
    let map = parse_and_extract(php);
    let offset = php.find("create").unwrap() as u32;
    let hit = map.lookup(offset);
    assert!(hit.is_some());
    if let SymbolKind::MemberAccess {
        ref subject_text,
        ref member_name,
        is_static,
        is_method_call,
        ..
    } = hit.unwrap().kind
    {
        assert_eq!(member_name, "create");
        assert_eq!(subject_text, "self");
        assert!(is_static);
        assert!(is_method_call);
    } else {
        panic!("Expected MemberAccess");
    }
}

#[test]
fn property_access_produces_member_access() {
    let php = "<?php\nclass Foo { function test() { $this->name; } }\n";
    let map = parse_and_extract(php);
    let arrow_pos = php.find("->name").unwrap();
    let name_offset = (arrow_pos + 2) as u32;
    let hit = map.lookup(name_offset);
    assert!(hit.is_some());
    if let SymbolKind::MemberAccess {
        ref member_name,
        is_method_call,
        ..
    } = hit.unwrap().kind
    {
        assert_eq!(member_name, "name");
        assert!(!is_method_call);
    } else {
        panic!("Expected MemberAccess");
    }
}

#[test]
fn type_hint_produces_class_reference() {
    let php = "<?php\nfunction test(Foo $x): Bar { }\n";
    let map = parse_and_extract(php);

    let foo_offset = php.find("Foo").unwrap() as u32;
    let hit = map.lookup(foo_offset);
    assert!(hit.is_some());
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Foo");
    } else {
        panic!("Expected ClassReference for Foo");
    }

    let bar_offset = php.find("Bar").unwrap() as u32;
    let hit = map.lookup(bar_offset);
    assert!(hit.is_some());
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Bar");
    } else {
        panic!("Expected ClassReference for Bar");
    }
}

#[test]
fn scalar_type_hint_not_in_map() {
    let php = "<?php\nfunction test(int $x): string { }\n";
    let map = parse_and_extract(php);

    let int_offset = php.find("int").unwrap() as u32;
    assert!(map.lookup(int_offset).is_none());

    let string_offset = php.find("string").unwrap() as u32;
    assert!(map.lookup(string_offset).is_none());
}

#[test]
fn new_expression_produces_class_reference() {
    let php = "<?php\nfunction test() { $x = new Foo(); }\n";
    let map = parse_and_extract(php);
    let foo_offset = php.find("Foo").unwrap() as u32;
    let hit = map.lookup(foo_offset);
    assert!(hit.is_some());
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Foo");
    } else {
        panic!("Expected ClassReference for Foo");
    }
}

#[test]
fn catch_type_produces_class_reference() {
    let php = "<?php\ntry {} catch (RuntimeException $e) {}\n";
    let map = parse_and_extract(php);
    let offset = php.find("RuntimeException").unwrap() as u32;
    let hit = map.lookup(offset);
    assert!(hit.is_some());
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "RuntimeException");
    } else {
        panic!("Expected ClassReference");
    }
}

#[test]
fn self_keyword_produces_self_static_parent() {
    let php = "<?php\nclass Foo { function test(): self { } }\n";
    let map = parse_and_extract(php);
    let offset = php.find("self").unwrap() as u32;
    let hit = map.lookup(offset);
    assert!(hit.is_some());
    if let SymbolKind::SelfStaticParent(kind) = &hit.unwrap().kind {
        assert_eq!(*kind, SelfStaticParentKind::Self_);
    } else {
        panic!("Expected SelfStaticParent");
    }
}

#[test]
fn whitespace_offset_returns_none() {
    let php = "<?php\nclass Foo    {}\n";
    let map = parse_and_extract(php);
    let foo_end = php.find("Foo").unwrap() + 3;
    let hit = map.lookup((foo_end + 1) as u32);
    assert!(hit.is_none());
}

#[test]
fn string_interior_not_navigable() {
    let php = "<?php\n$x = 'SomeClass';\n";
    let map = parse_and_extract(php);
    let some_offset = php.find("SomeClass").unwrap() as u32;
    let hit = map.lookup(some_offset);
    if let Some(span) = hit
        && let SymbolKind::ClassReference { .. } = &span.kind
    {
        panic!("Should not produce ClassReference inside a string literal");
    }
}

#[test]
fn chained_method_call_subject_text() {
    let php = "<?php\nclass Foo { function test() { $this->getService()->find(); } }\n";
    let map = parse_and_extract(php);
    let find_offset = php.find("find").unwrap() as u32;
    let hit = map.lookup(find_offset);
    assert!(hit.is_some());
    if let SymbolKind::MemberAccess {
        ref subject_text,
        ref member_name,
        ..
    } = hit.unwrap().kind
    {
        assert_eq!(member_name, "find");
        assert_eq!(subject_text, "$this->getService()");
    } else {
        panic!("Expected MemberAccess");
    }
}

#[test]
fn class_constant_access_produces_member_access() {
    let php = "<?php\nclass Foo { function test() { self::MY_CONST; } }\n";
    let map = parse_and_extract(php);
    let offset = php.find("MY_CONST").unwrap() as u32;
    let hit = map.lookup(offset);
    assert!(hit.is_some());
    if let SymbolKind::MemberAccess {
        ref member_name,
        is_static,
        ..
    } = hit.unwrap().kind
    {
        assert_eq!(member_name, "MY_CONST");
        assert!(is_static);
    } else {
        panic!("Expected MemberAccess");
    }
}

#[test]
fn trait_use_produces_class_reference() {
    let php = "<?php\nclass Foo { use SomeTrait; }\n";
    let map = parse_and_extract(php);
    let offset = php.find("SomeTrait").unwrap() as u32;
    let hit = map.lookup(offset);
    assert!(hit.is_some());
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "SomeTrait");
    } else {
        panic!("Expected ClassReference");
    }
}

#[test]
fn docblock_param_class_reference() {
    let php = concat!(
        "<?php\n",
        "class Foo {\n",
        "    /**\n",
        "     * @param UserService $service\n",
        "     * @return ResponseInterface\n",
        "     */\n",
        "    public function test($service) {}\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let user_service_offset = php.find("UserService").unwrap() as u32;
    let hit = map.lookup(user_service_offset);
    assert!(hit.is_some(), "Should find UserService in docblock");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "UserService");
    } else {
        panic!("Expected ClassReference for UserService");
    }

    let response_offset = php.find("ResponseInterface").unwrap() as u32;
    let hit = map.lookup(response_offset);
    assert!(hit.is_some(), "Should find ResponseInterface in docblock");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "ResponseInterface");
    } else {
        panic!("Expected ClassReference for ResponseInterface");
    }
}

#[test]
fn docblock_scalar_param_not_navigable() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @param string $name\n",
        " */\n",
        "function test($name) {}\n",
    );
    let map = parse_and_extract(php);
    let string_offset = php.find("string").unwrap() as u32;
    let hit = map.lookup(string_offset);
    if let Some(span) = hit
        && let SymbolKind::ClassReference { .. } = &span.kind
    {
        panic!("Scalar type 'string' should not produce a ClassReference");
    }
}

#[test]
fn nullable_type_hint_produces_class_reference() {
    let php = "<?php\nfunction test(?Foo $x) {}\n";
    let map = parse_and_extract(php);
    let foo_offset = php.find("Foo").unwrap() as u32;
    let hit = map.lookup(foo_offset);
    assert!(hit.is_some());
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Foo");
    } else {
        panic!("Expected ClassReference for nullable Foo");
    }
}

#[test]
fn interface_declaration_produces_declaration() {
    let php = "<?php\ninterface Serializable {}\n";
    let map = parse_and_extract(php);
    let offset = php.find("Serializable").unwrap() as u32;
    let hit = map.lookup(offset);
    assert!(hit.is_some());
    if let SymbolKind::ClassDeclaration { ref name } = hit.unwrap().kind {
        assert_eq!(name, "Serializable");
    } else {
        panic!("Expected ClassDeclaration, got {:?}", hit.unwrap().kind);
    }
}

#[test]
fn enum_declaration_produces_declaration() {
    let php = "<?php\nenum Color { case Red; case Blue; }\n";
    let map = parse_and_extract(php);
    let offset = php.find("Color").unwrap() as u32;
    let hit = map.lookup(offset);
    assert!(hit.is_some());
    if let SymbolKind::ClassDeclaration { ref name } = hit.unwrap().kind {
        assert_eq!(name, "Color");
    } else {
        panic!("Expected ClassDeclaration, got {:?}", hit.unwrap().kind);
    }
}

#[test]
fn enum_case_produces_member_declaration() {
    let php = "<?php\nenum Color { case Red; case Blue; }\n";
    let map = parse_and_extract(php);

    // Unit enum case `Red`
    let red_offset = php.find("Red").unwrap() as u32;
    let hit = map.lookup(red_offset);
    assert!(hit.is_some(), "Expected a symbol span for enum case Red");
    if let SymbolKind::MemberDeclaration {
        ref name,
        is_static,
    } = hit.unwrap().kind
    {
        assert_eq!(name, "Red");
        assert!(is_static, "Enum cases are accessed statically");
    } else {
        panic!(
            "Expected MemberDeclaration for enum case Red, got {:?}",
            hit.unwrap().kind
        );
    }

    // Unit enum case `Blue`
    let blue_offset = php.find("Blue").unwrap() as u32;
    let hit = map.lookup(blue_offset);
    assert!(hit.is_some(), "Expected a symbol span for enum case Blue");
    if let SymbolKind::MemberDeclaration {
        ref name,
        is_static,
    } = hit.unwrap().kind
    {
        assert_eq!(name, "Blue");
        assert!(is_static, "Enum cases are accessed statically");
    } else {
        panic!(
            "Expected MemberDeclaration for enum case Blue, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn backed_enum_case_produces_member_declaration() {
    let php = "<?php\nenum TaskType: int { case Task = 1; case Issue = 2; }\n";
    let map = parse_and_extract(php);

    let issue_offset = php.find("Issue").unwrap() as u32;
    let hit = map.lookup(issue_offset);
    assert!(
        hit.is_some(),
        "Expected a symbol span for backed enum case Issue"
    );
    if let SymbolKind::MemberDeclaration {
        ref name,
        is_static,
    } = hit.unwrap().kind
    {
        assert_eq!(name, "Issue");
        assert!(is_static, "Enum cases are accessed statically");
    } else {
        panic!(
            "Expected MemberDeclaration for backed enum case Issue, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn closure_param_type_hint() {
    let php = "<?php\n$f = function(Foo $x): Bar {};\n";
    let map = parse_and_extract(php);

    let foo_offset = php.find("Foo").unwrap() as u32;
    let hit = map.lookup(foo_offset);
    assert!(hit.is_some());
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Foo");
    } else {
        panic!("Expected ClassReference for Foo");
    }
}

#[test]
fn instanceof_rhs_produces_class_reference() {
    let php = "<?php\nfunction test($x) { if ($x instanceof Foo) {} }\n";
    let map = parse_and_extract(php);
    let foo_offset = php.find("Foo").unwrap() as u32;
    let hit = map.lookup(foo_offset);
    assert!(hit.is_some());
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Foo");
    } else {
        panic!("Expected ClassReference for instanceof Foo");
    }
}

#[test]
fn docblock_union_type_produces_multiple_references() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @return Foo|Bar\n",
        " */\n",
        "function test() {}\n",
    );
    let map = parse_and_extract(php);

    let foo_offset = php.find("Foo").unwrap() as u32;
    let hit = map.lookup(foo_offset);
    assert!(hit.is_some(), "Should find Foo in union return type");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Foo");
    }

    let bar_offset = php.find("Bar").unwrap() as u32;
    let hit = map.lookup(bar_offset);
    assert!(hit.is_some(), "Should find Bar in union return type");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Bar");
    }
}

#[test]
fn docblock_nullable_type() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @return ?Foo\n",
        " */\n",
        "function test() {}\n",
    );
    let map = parse_and_extract(php);
    let foo_offset = php.find("Foo").unwrap() as u32;
    let hit = map.lookup(foo_offset);
    assert!(hit.is_some(), "Should find Foo in nullable return type");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Foo");
    }
}

#[test]
fn docblock_fqn_type() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @return \\App\\Models\\User\n",
        " */\n",
        "function test() {}\n",
    );
    let map = parse_and_extract(php);
    let user_offset = php.find("\\App\\Models\\User").unwrap() as u32;
    let hit = map.lookup(user_offset);
    assert!(hit.is_some(), "Should find FQN type in docblock");
    if let SymbolKind::ClassReference { ref name, is_fqn } = hit.unwrap().kind {
        assert_eq!(name, "App\\Models\\User");
        assert!(is_fqn, "Docblock FQN type should have is_fqn = true");
    } else {
        panic!("Expected ClassReference, got {:?}", hit.unwrap().kind);
    }
}

#[test]
fn docblock_this_produces_self_static_parent() {
    let php = concat!(
        "<?php\n",
        "class Foo {\n",
        "    /**\n",
        "     * @return Collection<Item, $this>\n",
        "     */\n",
        "    public function items() {}\n",
        "}\n",
    );
    let map = parse_and_extract(php);
    let this_offset = php.find("$this").unwrap() as u32;
    let hit = map.lookup(this_offset);
    assert!(hit.is_some(), "Should find $this in docblock generic arg");
    if let SymbolKind::SelfStaticParent(kind) = &hit.unwrap().kind {
        assert_eq!(*kind, SelfStaticParentKind::This);
    } else {
        panic!(
            "Expected SelfStaticParent for $this, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn attribute_class_reference() {
    let php = concat!(
        "<?php\n",
        "#[\\Illuminate\\Database\\Eloquent\\Attributes\\CollectedBy(ReviewCollection::class)]\n",
        "class Review {}\n",
    );
    let map = parse_and_extract(php);

    // The attribute class name should be a ClassReference.
    let attr_offset = php
        .find("\\Illuminate\\Database\\Eloquent\\Attributes\\CollectedBy")
        .unwrap() as u32;
    let hit = map.lookup(attr_offset);
    assert!(hit.is_some(), "Should find attribute class reference");
    if let SymbolKind::ClassReference { ref name, is_fqn } = hit.unwrap().kind {
        assert_eq!(
            name,
            "Illuminate\\Database\\Eloquent\\Attributes\\CollectedBy"
        );
        assert!(is_fqn, "Attribute FQN should have is_fqn = true");
    } else {
        panic!(
            "Expected ClassReference for attribute, got {:?}",
            hit.unwrap().kind
        );
    }

    // The argument `ReviewCollection::class` should produce a ClassReference for ReviewCollection.
    let rc_offset = php.find("ReviewCollection").unwrap() as u32;
    let hit = map.lookup(rc_offset);
    assert!(
        hit.is_some(),
        "Should find ReviewCollection in attribute argument"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "ReviewCollection");
    } else {
        panic!(
            "Expected ClassReference for ReviewCollection, got {:?}",
            hit.unwrap().kind
        );
    }

    // The class declaration name `Review` should be ClassDeclaration, not ClassReference.
    let review_offset = php.find("class Review").unwrap() as u32 + 6; // skip "class "
    let hit = map.lookup(review_offset);
    assert!(hit.is_some(), "Should find Review declaration");
    if let SymbolKind::ClassDeclaration { ref name } = hit.unwrap().kind {
        assert_eq!(name, "Review");
    } else {
        panic!(
            "Expected ClassDeclaration for Review, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn fqn_type_hint_in_parameter() {
    let php = "<?php\nfunction test(\\Illuminate\\Support\\Collection $c) {}\n";
    let map = parse_and_extract(php);
    let fqn_offset = php.find("\\Illuminate\\Support\\Collection").unwrap() as u32;
    let hit = map.lookup(fqn_offset);
    assert!(hit.is_some(), "Should find FQN type hint in parameter");
    if let SymbolKind::ClassReference { ref name, is_fqn } = hit.unwrap().kind {
        assert_eq!(name, "Illuminate\\Support\\Collection");
        assert!(is_fqn, "FQN parameter type hint should have is_fqn = true");
    } else {
        panic!(
            "Expected ClassReference for FQN param hint, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn fqn_extends_class_reference() {
    let php = "<?php\nclass Review extends \\Illuminate\\Database\\Eloquent\\Model {}\n";
    let map = parse_and_extract(php);
    let fqn_offset = php.find("\\Illuminate\\Database\\Eloquent\\Model").unwrap() as u32;
    let hit = map.lookup(fqn_offset);
    assert!(hit.is_some(), "Should find FQN in extends clause");
    if let SymbolKind::ClassReference { ref name, is_fqn } = hit.unwrap().kind {
        assert_eq!(name, "Illuminate\\Database\\Eloquent\\Model");
        assert!(is_fqn, "FQN extends should have is_fqn = true");
    } else {
        panic!(
            "Expected ClassReference for FQN extends, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn fqn_lookup_at_middle_of_name() {
    // Verify that the symbol span covers the ENTIRE FQN so that
    // clicking anywhere within it (not just at the leading `\`)
    // resolves correctly.
    let php = concat!(
        "<?php\n",
        "#[\\Illuminate\\Database\\Eloquent\\Attributes\\CollectedBy(ReviewCollection::class)]\n",
        "class Review extends \\Illuminate\\Database\\Eloquent\\Model\n",
        "{\n",
        "    /** @return \\Illuminate\\Database\\Eloquent\\Relations\\HasMany<Review, $this> */\n",
        "    public function replies(): mixed { return $this->hasMany(Review::class); }\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    // ── Attribute FQN: click on "CollectedBy" (last segment) ──
    let cb_offset = php.find("CollectedBy").unwrap() as u32;
    let hit = map.lookup(cb_offset);
    assert!(
        hit.is_some(),
        "Should find attribute FQN when cursor is on 'CollectedBy'"
    );
    if let SymbolKind::ClassReference { ref name, is_fqn } = hit.unwrap().kind {
        assert_eq!(
            name,
            "Illuminate\\Database\\Eloquent\\Attributes\\CollectedBy"
        );
        assert!(is_fqn);
    } else {
        panic!("Expected ClassReference, got {:?}", hit.unwrap().kind);
    }

    // ── Attribute FQN: click on "Database" (middle segment) ──
    // Find the first "Database" which is inside the attribute
    let db_attr_offset = php.find("Database").unwrap() as u32;
    let hit = map.lookup(db_attr_offset);
    assert!(
        hit.is_some(),
        "Should find attribute FQN when cursor is on 'Database'"
    );
    if let SymbolKind::ClassReference { ref name, is_fqn } = hit.unwrap().kind {
        assert_eq!(
            name,
            "Illuminate\\Database\\Eloquent\\Attributes\\CollectedBy"
        );
        assert!(is_fqn);
    } else {
        panic!("Expected ClassReference, got {:?}", hit.unwrap().kind);
    }

    // ── Extends FQN: click on "Model" (last segment) ──
    let model_offset = php.find("Model\n").unwrap() as u32;
    let hit = map.lookup(model_offset);
    assert!(
        hit.is_some(),
        "Should find extends FQN when cursor is on 'Model'"
    );
    if let SymbolKind::ClassReference { ref name, is_fqn } = hit.unwrap().kind {
        assert_eq!(name, "Illuminate\\Database\\Eloquent\\Model");
        assert!(is_fqn);
    } else {
        panic!("Expected ClassReference, got {:?}", hit.unwrap().kind);
    }

    // ── Extends FQN: click on "Eloquent" (middle segment) ──
    // The second "Eloquent" is in the extends clause
    let extends_line_start = php.find("class Review extends").unwrap();
    let eloquent_in_extends =
        php[extends_line_start..].find("Eloquent").unwrap() + extends_line_start;
    let hit = map.lookup(eloquent_in_extends as u32);
    assert!(
        hit.is_some(),
        "Should find extends FQN when cursor is on 'Eloquent'"
    );
    if let SymbolKind::ClassReference { ref name, is_fqn } = hit.unwrap().kind {
        assert_eq!(name, "Illuminate\\Database\\Eloquent\\Model");
        assert!(is_fqn);
    } else {
        panic!("Expected ClassReference, got {:?}", hit.unwrap().kind);
    }

    // ── Docblock FQN: click on "HasMany" (last segment) ──
    let hm_offset = php.find("HasMany").unwrap() as u32;
    let hit = map.lookup(hm_offset);
    assert!(
        hit.is_some(),
        "Should find docblock FQN when cursor is on 'HasMany'"
    );
    if let SymbolKind::ClassReference { ref name, is_fqn } = hit.unwrap().kind {
        assert_eq!(name, "Illuminate\\Database\\Eloquent\\Relations\\HasMany");
        assert!(is_fqn);
    } else {
        panic!("Expected ClassReference, got {:?}", hit.unwrap().kind);
    }

    // ── Docblock FQN: click on "Relations" (middle segment) ──
    let rel_offset = php.find("Relations").unwrap() as u32;
    let hit = map.lookup(rel_offset);
    assert!(
        hit.is_some(),
        "Should find docblock FQN when cursor is on 'Relations'"
    );
    if let SymbolKind::ClassReference { ref name, is_fqn } = hit.unwrap().kind {
        assert_eq!(name, "Illuminate\\Database\\Eloquent\\Relations\\HasMany");
        assert!(is_fqn);
    } else {
        panic!("Expected ClassReference, got {:?}", hit.unwrap().kind);
    }

    // ── Docblock $this inside generic args ──
    let docblock_start = php.find("/** @return").unwrap();
    let this_in_doc = php[docblock_start..].find("$this").unwrap() + docblock_start;
    let hit = map.lookup(this_in_doc as u32);
    assert!(hit.is_some(), "Should find $this in docblock generic arg");
    if let SymbolKind::SelfStaticParent(kind) = &hit.unwrap().kind {
        assert_eq!(*kind, SelfStaticParentKind::This);
    } else {
        panic!(
            "Expected SelfStaticParent for $this, got {:?}",
            hit.unwrap().kind
        );
    }
}

// ── @template tag tests ─────────────────────────────────────────────

#[test]
fn template_tag_bound_type_produces_class_reference() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @template-covariant TNode of AstNode\n",
        " */\n",
        "class Foo {}\n",
    );
    let map = parse_and_extract(php);
    let ast_offset = php.find("AstNode").unwrap() as u32;
    let hit = map.lookup(ast_offset);
    assert!(
        hit.is_some(),
        "Should find bound type AstNode in @template tag"
    );
    if let SymbolKind::ClassReference { ref name, is_fqn } = hit.unwrap().kind {
        assert_eq!(name, "AstNode");
        assert!(!is_fqn);
    } else {
        panic!(
            "Expected ClassReference for AstNode, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn template_tag_without_bound_produces_no_span() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @template T\n",
        " */\n",
        "class Foo {}\n",
    );
    let map = parse_and_extract(php);
    // "T" should NOT produce a ClassReference — it's a parameter name.
    let t_offset = php.find(" T\n").unwrap() as u32 + 1; // offset of 'T'
    let hit = map.lookup(t_offset);
    assert!(
        hit.is_none(),
        "Template parameter name should not be navigable"
    );
}

#[test]
fn template_tag_fqn_bound() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @template T of \\App\\Contracts\\Renderable\n",
        " */\n",
        "class Foo {}\n",
    );
    let map = parse_and_extract(php);
    let r_offset = php.find("\\App\\Contracts\\Renderable").unwrap() as u32;
    let hit = map.lookup(r_offset);
    assert!(hit.is_some(), "Should find FQN bound type");
    if let SymbolKind::ClassReference { ref name, is_fqn } = hit.unwrap().kind {
        assert_eq!(name, "App\\Contracts\\Renderable");
        assert!(is_fqn);
    } else {
        panic!("Expected ClassReference, got {:?}", hit.unwrap().kind);
    }
}

#[test]
fn template_covariant_and_contravariant_tags() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @template-covariant TOut of Output\n",
        " * @template-contravariant TIn of Input\n",
        " */\n",
        "class Foo {}\n",
    );
    let map = parse_and_extract(php);

    let out_offset = php.find("Output").unwrap() as u32;
    let hit = map.lookup(out_offset);
    assert!(hit.is_some(), "Should find Output bound");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Output");
    } else {
        panic!("Expected ClassReference for Output");
    }

    let in_offset = php.find("Input").unwrap() as u32;
    let hit = map.lookup(in_offset);
    assert!(hit.is_some(), "Should find Input bound");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Input");
    } else {
        panic!("Expected ClassReference for Input");
    }
}

#[test]
fn phpstan_template_tag() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @phpstan-template T of Collection\n",
        " */\n",
        "class Foo {}\n",
    );
    let map = parse_and_extract(php);
    let c_offset = php.find("Collection").unwrap() as u32;
    let hit = map.lookup(c_offset);
    assert!(
        hit.is_some(),
        "Should find Collection bound from @phpstan-template"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Collection");
    } else {
        panic!("Expected ClassReference for Collection");
    }
}

// ── VarDefSite extraction tests ─────────────────────────────────────

#[test]
fn var_def_assignment_in_function() {
    let php = "<?php\nfunction foo() {\n    $x = 42;\n}\n";
    let map = parse_and_extract(php);
    assert!(
        !map.var_defs.is_empty(),
        "Should have at least one VarDefSite"
    );
    let def = map
        .var_defs
        .iter()
        .find(|d| d.name == "x")
        .expect("Should find $x def");
    assert_eq!(def.kind, VarDefKind::Assignment);
    // The definition should be inside the function scope.
    assert_ne!(
        def.scope_start, 0,
        "scope_start should be function body brace, not top-level"
    );
}

#[test]
fn var_def_parameter_in_function() {
    let php = "<?php\nfunction greet(string $name) {\n    echo $name;\n}\n";
    let map = parse_and_extract(php);
    let def = map
        .var_defs
        .iter()
        .find(|d| d.name == "name" && d.kind == VarDefKind::Parameter);
    assert!(def.is_some(), "Should find parameter $name as VarDefSite");
}

#[test]
fn var_def_foreach_key_and_value() {
    let php = "<?php\nfunction f() {\n    foreach ($items as $key => $val) { }\n}\n";
    let map = parse_and_extract(php);
    let key_def = map
        .var_defs
        .iter()
        .find(|d| d.name == "key" && d.kind == VarDefKind::Foreach);
    let val_def = map
        .var_defs
        .iter()
        .find(|d| d.name == "val" && d.kind == VarDefKind::Foreach);
    assert!(key_def.is_some(), "Should find foreach key $key");
    assert!(val_def.is_some(), "Should find foreach value $val");
}

#[test]
fn var_def_catch_variable() {
    let php = "<?php\nfunction f() {\n    try { } catch (Exception $e) { }\n}\n";
    let map = parse_and_extract(php);
    let def = map
        .var_defs
        .iter()
        .find(|d| d.name == "e" && d.kind == VarDefKind::Catch);
    assert!(def.is_some(), "Should find catch variable $e");
}

#[test]
fn var_def_static_variable() {
    let php = "<?php\nfunction f() {\n    static $counter = 0;\n}\n";
    let map = parse_and_extract(php);
    let def = map
        .var_defs
        .iter()
        .find(|d| d.name == "counter" && d.kind == VarDefKind::StaticDecl);
    assert!(def.is_some(), "Should find static variable $counter");
}

#[test]
fn var_def_global_variable() {
    let php = "<?php\nfunction f() {\n    global $db;\n}\n";
    let map = parse_and_extract(php);
    let def = map
        .var_defs
        .iter()
        .find(|d| d.name == "db" && d.kind == VarDefKind::GlobalDecl);
    assert!(def.is_some(), "Should find global variable $db");
}

#[test]
fn var_def_array_destructuring() {
    let php = "<?php\nfunction f() {\n    [$a, $b] = explode(',', $str);\n}\n";
    let map = parse_and_extract(php);
    let a_def = map
        .var_defs
        .iter()
        .find(|d| d.name == "a" && d.kind == VarDefKind::ArrayDestructuring);
    let b_def = map
        .var_defs
        .iter()
        .find(|d| d.name == "b" && d.kind == VarDefKind::ArrayDestructuring);
    assert!(a_def.is_some(), "Should find $a from array destructuring");
    assert!(b_def.is_some(), "Should find $b from array destructuring");
}

#[test]
fn var_def_list_destructuring() {
    let php = "<?php\nfunction f() {\n    list($a, $b) = func();\n}\n";
    let map = parse_and_extract(php);
    let a_def = map
        .var_defs
        .iter()
        .find(|d| d.name == "a" && d.kind == VarDefKind::ListDestructuring);
    let b_def = map
        .var_defs
        .iter()
        .find(|d| d.name == "b" && d.kind == VarDefKind::ListDestructuring);
    assert!(a_def.is_some(), "Should find $a from list destructuring");
    assert!(b_def.is_some(), "Should find $b from list destructuring");
}

#[test]
fn var_def_method_parameter() {
    let php =
        "<?php\nclass Foo {\n    public function bar(int $x) {\n        return $x;\n    }\n}\n";
    let map = parse_and_extract(php);
    let def = map
        .var_defs
        .iter()
        .find(|d| d.name == "x" && d.kind == VarDefKind::Parameter);
    assert!(def.is_some(), "Should find method parameter $x");
}

#[test]
fn var_def_closure_parameter() {
    let php = "<?php\nfunction f() {\n    $fn = function (string $s) { return $s; };\n}\n";
    let map = parse_and_extract(php);
    let def = map
        .var_defs
        .iter()
        .find(|d| d.name == "s" && d.kind == VarDefKind::Parameter);
    assert!(def.is_some(), "Should find closure parameter $s");
}

#[test]
fn var_def_arrow_function_parameter() {
    let php = "<?php\nfunction f() {\n    $fn = fn(int $n) => $n * 2;\n}\n";
    let map = parse_and_extract(php);
    let def = map
        .var_defs
        .iter()
        .find(|d| d.name == "n" && d.kind == VarDefKind::Parameter);
    assert!(def.is_some(), "Should find arrow function parameter $n");
}

// ── Scope tracking tests ────────────────────────────────────────────

#[test]
fn scopes_populated_for_function() {
    let php = "<?php\nfunction foo() {\n    $x = 1;\n}\n";
    let map = parse_and_extract(php);
    assert!(
        !map.scopes.is_empty(),
        "Should have at least one scope for the function body"
    );
}

#[test]
fn scopes_populated_for_method() {
    let php = "<?php\nclass A {\n    public function m() {\n        $y = 2;\n    }\n}\n";
    let map = parse_and_extract(php);
    assert!(
        !map.scopes.is_empty(),
        "Should have at least one scope for the method body"
    );
}

#[test]
fn scopes_populated_for_closure() {
    let php = "<?php\nfunction f() {\n    $fn = function () { $z = 3; };\n}\n";
    let map = parse_and_extract(php);
    // One for the outer function, one for the closure.
    assert!(
        map.scopes.len() >= 2,
        "Should have scopes for both function and closure"
    );
}

#[test]
fn find_enclosing_scope_top_level() {
    let php = "<?php\n$x = 1;\n";
    let map = parse_and_extract(php);
    // Top-level offset should return scope_start 0.
    assert_eq!(map.find_enclosing_scope(7), 0);
}

#[test]
fn find_enclosing_scope_inside_function() {
    let php = "<?php\nfunction foo() {\n    $x = 1;\n}\n";
    let map = parse_and_extract(php);
    // Offset inside the function body should return the function's scope_start.
    let body_offset = php.find('{').unwrap() as u32;
    let x_offset = php.find("$x").unwrap() as u32;
    let scope = map.find_enclosing_scope(x_offset);
    assert_eq!(
        scope, body_offset,
        "Should find the function body as the enclosing scope"
    );
}

// ── find_var_definition tests ───────────────────────────────────────

#[test]
fn find_var_definition_returns_most_recent() {
    let php = "<?php\nfunction f() {\n    $x = 1;\n    $x = 2;\n    echo $x;\n}\n";
    let map = parse_and_extract(php);
    let echo_x_offset = php.rfind("$x").unwrap() as u32;
    let scope = map.find_enclosing_scope(echo_x_offset);
    let def = map.find_var_definition("x", echo_x_offset, scope);
    assert!(def.is_some(), "Should find a definition for $x");
    // The most recent definition should be `$x = 2;` not `$x = 1;`
    let second_assign_offset = php.find("$x = 2").unwrap() as u32;
    assert_eq!(
        def.unwrap().offset,
        second_assign_offset,
        "Should find the second assignment"
    );
}

#[test]
fn find_var_definition_parameter_found() {
    let php = "<?php\nfunction greet(string $name) {\n    echo $name;\n}\n";
    let map = parse_and_extract(php);
    let echo_name_offset = php.rfind("$name").unwrap() as u32;
    let scope = map.find_enclosing_scope(echo_name_offset);
    let def = map.find_var_definition("name", echo_name_offset, scope);
    assert!(def.is_some(), "Should find parameter $name");
    assert_eq!(def.unwrap().kind, VarDefKind::Parameter);
}

#[test]
fn find_var_definition_none_when_no_def() {
    let php = "<?php\nfunction f() {\n    echo $undefined;\n}\n";
    let map = parse_and_extract(php);
    let offset = php.find("$undefined").unwrap() as u32;
    let scope = map.find_enclosing_scope(offset);
    let def = map.find_var_definition("undefined", offset, scope);
    assert!(def.is_none(), "Should return None for undefined variable");
}

#[test]
fn find_var_definition_respects_scope() {
    let php = concat!(
        "<?php\n",
        "function outer() {\n",
        "    $x = 'outer';\n",
        "    $fn = function () {\n",
        "        echo $x;\n", // $x not defined in closure scope
        "    };\n",
        "}\n",
    );
    let map = parse_and_extract(php);
    let echo_x_offset = php.rfind("$x").unwrap() as u32;
    let scope = map.find_enclosing_scope(echo_x_offset);
    let def = map.find_var_definition("x", echo_x_offset, scope);
    // $x is defined in outer scope, not closure scope, so should be None.
    assert!(def.is_none(), "Should not find $x from a different scope");
}

#[test]
fn assignment_effective_from_excludes_rhs() {
    // In `$x = $x + 1;`, the RHS $x should see the *previous* definition,
    // not the one being written.
    let php = concat!(
        "<?php\n",
        "function f() {\n",
        "    $x = 10;\n",
        "    $x = $x + 1;\n",
        "}\n",
    );
    let map = parse_and_extract(php);
    // The RHS `$x` in `$x = $x + 1;`
    let rhs_x_offset = php.rfind("$x + 1").unwrap() as u32;
    let scope = map.find_enclosing_scope(rhs_x_offset);
    let def = map.find_var_definition("x", rhs_x_offset, scope);
    assert!(def.is_some(), "Should find a definition for RHS $x");
    // Should point to `$x = 10;`, not `$x = $x + 1;`
    let first_assign_offset = php.find("$x = 10").unwrap() as u32;
    assert_eq!(
        def.unwrap().offset,
        first_assign_offset,
        "RHS $x should see the first assignment, not the one being written"
    );
}

#[test]
fn assignment_effective_from_excludes_rhs_in_constructor_args() {
    // In `$request = new Foo(arg: $request->uuid)`, the `$request`
    // inside the constructor arguments should see the *previous* definition
    // (the parameter), not the assignment being written.  PHP evaluates
    // all RHS arguments before performing the assignment.
    let php = concat!(
        "<?php\n",
        "function f(Foo $request) {\n",
        "    $request = new Bar(\n",
        "        name: $request->uuid,\n",
        "    );\n",
        "}\n",
    );
    let map = parse_and_extract(php);
    // The `$request` in `$request->uuid` (inside the constructor args)
    let rhs_request_offset = php.find("$request->uuid").unwrap() as u32;
    let scope = map.find_enclosing_scope(rhs_request_offset);
    let def = map.find_var_definition("request", rhs_request_offset, scope);
    assert!(def.is_some(), "Should find a definition for RHS $request");
    // Should point to the parameter `$request`, not the assignment LHS.
    let param_offset = php.find("$request)").unwrap() as u32;
    assert_eq!(
        def.unwrap().offset,
        param_offset,
        "RHS $request inside constructor args should see the parameter, not the assignment"
    );
    assert_eq!(def.unwrap().kind, VarDefKind::Parameter);
}

// ── is_at_var_definition tests ──────────────────────────────────────

#[test]
fn is_at_var_definition_on_assignment_lhs() {
    let php = "<?php\nfunction f() {\n    $x = 42;\n}\n";
    let map = parse_and_extract(php);
    let x_offset = php.find("$x = 42").unwrap() as u32;
    assert!(
        map.is_at_var_definition("x", x_offset),
        "Should detect cursor on assignment LHS as at-definition"
    );
    // One byte into the token (on the 'x')
    assert!(
        map.is_at_var_definition("x", x_offset + 1),
        "Should detect cursor on 'x' of '$x' as at-definition"
    );
}

#[test]
fn is_at_var_definition_on_parameter() {
    let php = "<?php\nfunction greet(string $name) {\n    echo $name;\n}\n";
    let map = parse_and_extract(php);
    let param_offset = php.find("$name)").unwrap() as u32;
    assert!(
        map.is_at_var_definition("name", param_offset),
        "Should detect cursor on parameter as at-definition"
    );
}

#[test]
fn is_at_var_definition_false_on_usage() {
    let php = "<?php\nfunction f() {\n    $x = 42;\n    echo $x;\n}\n";
    let map = parse_and_extract(php);
    let echo_x_offset = php.rfind("$x").unwrap() as u32;
    assert!(
        !map.is_at_var_definition("x", echo_x_offset),
        "Should NOT detect cursor on variable usage as at-definition"
    );
}

#[test]
fn nested_array_destructuring_var_defs() {
    let php = "<?php\nfunction f() {\n    [[$a, $b], $c] = getData();\n}\n";
    let map = parse_and_extract(php);
    let a_def = map
        .var_defs
        .iter()
        .find(|d| d.name == "a" && d.kind == VarDefKind::ArrayDestructuring);
    let b_def = map
        .var_defs
        .iter()
        .find(|d| d.name == "b" && d.kind == VarDefKind::ArrayDestructuring);
    let c_def = map
        .var_defs
        .iter()
        .find(|d| d.name == "c" && d.kind == VarDefKind::ArrayDestructuring);
    assert!(a_def.is_some(), "Should find $a from nested destructuring");
    assert!(b_def.is_some(), "Should find $b from nested destructuring");
    assert!(c_def.is_some(), "Should find $c from outer destructuring");
}

#[test]
fn var_defs_sorted_by_scope_start_then_offset() {
    let php = concat!(
        "<?php\n",
        "function a() {\n",
        "    $x = 1;\n",
        "    $y = 2;\n",
        "}\n",
        "function b() {\n",
        "    $z = 3;\n",
        "}\n",
    );
    let map = parse_and_extract(php);
    // Verify the var_defs are sorted by (scope_start, offset).
    for window in map.var_defs.windows(2) {
        let (a, b) = (&window[0], &window[1]);
        assert!(
            (a.scope_start, a.offset) <= (b.scope_start, b.offset),
            "var_defs should be sorted by (scope_start, offset): ({}, {}) vs ({}, {})",
            a.scope_start,
            a.offset,
            b.scope_start,
            b.offset,
        );
    }
}

#[test]
fn top_level_var_def_has_scope_start_zero() {
    let php = "<?php\n$global = 'hello';\n";
    let map = parse_and_extract(php);
    let def = map.var_defs.iter().find(|d| d.name == "global");
    assert!(def.is_some(), "Should find top-level $global");
    assert_eq!(
        def.unwrap().scope_start,
        0,
        "Top-level definitions should have scope_start 0"
    );
}

// ── Template param definition lookup tests ──────────────────────────

#[test]
fn template_param_def_recorded_for_class() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @template TKey of array-key\n",
        " * @template TModel\n",
        " */\n",
        "class Collection {\n",
        "    /** @return array<TKey, TModel> */\n",
        "    public function all(): array { return []; }\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    assert!(
        map.template_defs.len() >= 2,
        "Should record at least 2 template defs, got {}",
        map.template_defs.len()
    );

    let tkey = map.template_defs.iter().find(|d| d.name == "TKey");
    assert!(tkey.is_some(), "Should find TKey template def");
    let tkey = tkey.unwrap();
    assert_eq!(
        &php[tkey.name_offset as usize..(tkey.name_offset + 4) as usize],
        "TKey",
        "name_offset should point to the TKey text"
    );
    assert_eq!(
        tkey.bound,
        Some(crate::php_type::PhpType::parse("array-key")),
        "TKey should have bound 'array-key'"
    );

    let tmodel = map.template_defs.iter().find(|d| d.name == "TModel");
    assert!(tmodel.is_some(), "Should find TModel template def");
    assert_eq!(tmodel.unwrap().bound, None, "TModel should have no bound");
}

#[test]
fn template_param_def_lookup_in_same_class() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @template TKey of array-key\n",
        " * @template TModel\n",
        " */\n",
        "class Collection {\n",
        "    /** @return array<TKey, TModel> */\n",
        "    public function all(): array { return []; }\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    // Cursor inside the class body (on the @return line) should find TKey
    let return_line_offset = php.find("@return").unwrap() as u32;
    let found = map.find_template_def("TKey", return_line_offset);
    assert!(
        found.is_some(),
        "Should find TKey from within the class body"
    );
    assert_eq!(found.unwrap().name, "TKey");

    let found = map.find_template_def("TModel", return_line_offset);
    assert!(
        found.is_some(),
        "Should find TModel from within the class body"
    );
}

#[test]
fn template_param_def_not_found_outside_scope() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @template T\n",
        " */\n",
        "class Foo {}\n",
        "class Bar {}\n",
    );
    let map = parse_and_extract(php);

    let bar_offset = php.find("class Bar").unwrap() as u32;
    let found = map.find_template_def("T", bar_offset);
    assert!(found.is_none(), "T should NOT be found outside Foo's scope");
}

#[test]
fn template_param_def_method_level() {
    let php = concat!(
        "<?php\n",
        "class Mapper {\n",
        "    /**\n",
        "     * @template T\n",
        "     * @param T $item\n",
        "     * @return T\n",
        "     */\n",
        "    public function wrap(object $item): object { return $item; }\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let t_def = map.template_defs.iter().find(|d| d.name == "T");
    assert!(t_def.is_some(), "Should find method-level template T");

    // Should be findable from within the method's docblock
    let param_line = php.find("@param T").unwrap() as u32;
    let found = map.find_template_def("T", param_line);
    assert!(
        found.is_some(),
        "Should find T from within the method docblock"
    );
}

#[test]
fn callable_param_type_spans_are_tight() {
    // Verify that TKey inside `callable(TValue, TKey): mixed` gets a
    // ClassReference span that covers exactly `TKey`, not `TKey): mixed …`.
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @template TKey of array-key\n",
        " * @template TValue\n",
        " */\n",
        "class Col {\n",
        "    /**\n",
        "     * @param callable(TValue, TKey): mixed $callback\n",
        "     * @return static\n",
        "     */\n",
        "    public function each(callable $callback): static { return $this; }\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    // Find the byte offset of `TKey` inside the @param callable line.
    let param_line = php.find("@param callable(TValue, TKey): mixed").unwrap();
    let tkey_offset = php[param_line..].find("TKey").unwrap() + param_line;

    // There should be a ClassReference span starting exactly at tkey_offset.
    let span = map
        .spans
        .iter()
        .find(|s| s.start == tkey_offset as u32)
        .unwrap_or_else(|| {
            panic!(
                "Should find a span starting at TKey offset {}; spans: {:?}",
                tkey_offset,
                map.spans
                    .iter()
                    .filter(|s| {
                        matches!(
                            &s.kind,
                            SymbolKind::ClassReference { name, .. } if name == "TKey" || name == "TValue"
                        )
                    })
                    .collect::<Vec<_>>()
            )
        });

    assert_eq!(
        span.end - span.start,
        4,
        "TKey span should be exactly 4 bytes wide, but was {} ({}..{}), covering {:?}",
        span.end - span.start,
        span.start,
        span.end,
        &php[span.start as usize..span.end as usize],
    );

    assert!(
        matches!(&span.kind, SymbolKind::ClassReference { name, .. } if name == "TKey"),
        "span at TKey offset should be a ClassReference for TKey, got {:?}",
        span.kind,
    );
}

// ── $this as SelfStaticParent tests ─────────────────────────────────

#[test]
fn this_variable_emits_self_static_parent() {
    let php = concat!(
        "<?php\n",
        "class Foo {\n",
        "    public function bar(): void {\n",
        "        $this->baz();\n",
        "    }\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    // Find the $this token (not the one inside ->baz subject_text)
    let this_offset = php.find("$this->baz").unwrap() as u32;
    let hit = map.lookup(this_offset);
    assert!(hit.is_some(), "Should find a span at $this");
    match &hit.unwrap().kind {
        SymbolKind::SelfStaticParent(kind) => {
            assert_eq!(
                *kind,
                SelfStaticParentKind::This,
                "$this should map to This variant"
            );
        }
        other => panic!("Expected SelfStaticParent for $this, got {:?}", other),
    }
}

#[test]
fn this_variable_standalone_emits_self_static_parent() {
    // `$this` on its own (not as part of ->)
    let php = concat!(
        "<?php\n",
        "class Foo {\n",
        "    public function bar(): self {\n",
        "        return $this;\n",
        "    }\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let this_offset = php.find("$this;").unwrap() as u32;
    let hit = map.lookup(this_offset);
    assert!(hit.is_some(), "Should find a span at standalone $this");
    match &hit.unwrap().kind {
        SymbolKind::SelfStaticParent(kind) => {
            assert_eq!(*kind, SelfStaticParentKind::This);
        }
        other => panic!(
            "Expected SelfStaticParent for standalone $this, got {:?}",
            other
        ),
    }
}

#[test]
fn regular_variable_still_emits_variable() {
    let php = "<?php\nfunction f() { $x = 1; }\n";
    let map = parse_and_extract(php);

    let x_offset = php.find("$x").unwrap() as u32;
    let hit = map.lookup(x_offset);
    assert!(hit.is_some());
    match &hit.unwrap().kind {
        SymbolKind::Variable { name } => {
            assert_eq!(name, "x", "$x should still emit Variable");
        }
        other => panic!("Expected Variable for $x, got {:?}", other),
    }
}

// ── Array suffix stripping tests ────────────────────────────────────

#[test]
fn docblock_array_suffix_type_produces_class_reference() {
    let php = concat!(
        "<?php\n",
        "class AstNode {}\n",
        "class Foo {\n",
        "    /** @return AstNode[] */\n",
        "    public function getChildren(): array { return []; }\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    // Find the AstNode in the @return tag (not the class declaration)
    let docblock_start = php.find("/** @return").unwrap();
    let ast_in_doc = php[docblock_start..].find("AstNode").unwrap() + docblock_start;
    let hit = map.lookup(ast_in_doc as u32);
    assert!(hit.is_some(), "Should find AstNode in @return AstNode[]");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(
            name, "AstNode",
            "Name should be 'AstNode' without [] suffix"
        );
    } else {
        panic!(
            "Expected ClassReference for AstNode[], got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn docblock_array_suffix_span_excludes_brackets() {
    let php = concat!(
        "<?php\n",
        "class Item {}\n",
        "class Holder {\n",
        "    /** @var Item[] */\n",
        "    public array $items = [];\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let docblock_start = php.find("/** @var").unwrap();
    let item_in_doc = php[docblock_start..].find("Item").unwrap() + docblock_start;
    let hit = map.lookup(item_in_doc as u32);
    assert!(hit.is_some(), "Should find Item in @var Item[]");
    let span = hit.unwrap();
    let span_text = &php[span.start as usize..span.end as usize];
    assert_eq!(
        span_text, "Item",
        "Span should cover 'Item' only, not 'Item[]'"
    );
}

// ── Conditional return type tests ───────────────────────────────────

#[test]
fn conditional_return_type_all_parts_get_spans() {
    // PHPStan conditional return type:
    //   ($abstract is class-string<TClass> ? TClass : Container)
    // All three type positions (class-string<TClass>, TClass, Container)
    // should produce ClassReference spans.
    let php = concat!(
        "<?php\n",
        "class Container {\n",
        "    /**\n",
        "     * @template TClass\n",
        "     * @param string|null $abstract\n",
        "     * @return ($abstract is class-string<TClass> ? TClass : Container)\n",
        "     */\n",
        "    public function make($abstract) {}\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    // Find offsets of each TClass and Container in the @return line.
    let return_line_start = php.find("@return").unwrap();

    // First TClass — inside class-string<TClass>
    let first_tclass_offset = php[return_line_start..].find("TClass").unwrap() + return_line_start;
    let hit1 = map.lookup(first_tclass_offset as u32);
    assert!(
        hit1.is_some(),
        "Should find first TClass (inside class-string<TClass>)"
    );
    let span1 = hit1.unwrap();
    assert_eq!(&php[span1.start as usize..span1.end as usize], "TClass");

    // Second TClass — the true branch of the conditional
    let after_first = first_tclass_offset + "TClass".len();
    let second_tclass_offset = php[after_first..].find("TClass").unwrap() + after_first;
    let hit2 = map.lookup(second_tclass_offset as u32);
    assert!(
        hit2.is_some(),
        "Should find second TClass (true branch of conditional)"
    );
    let span2 = hit2.unwrap();
    assert_eq!(&php[span2.start as usize..span2.end as usize], "TClass");
    assert_ne!(
        span1.start, span2.start,
        "The two TClass spans should be at different offsets"
    );

    // Container — the false branch of the conditional
    let container_in_return =
        php[return_line_start..].find("Container").unwrap() + return_line_start;
    let hit3 = map.lookup(container_in_return as u32);
    assert!(
        hit3.is_some(),
        "Should find Container (false branch of conditional)"
    );
    let span3 = hit3.unwrap();
    assert_eq!(&php[span3.start as usize..span3.end as usize], "Container");
}

#[test]
fn conditional_return_type_with_not_keyword() {
    let php = concat!(
        "<?php\n",
        "class Foo {\n",
        "    /**\n",
        "     * @return ($x is not null ? Foo : Bar)\n",
        "     */\n",
        "    public function test($x) {}\n",
        "}\n",
        "class Bar {}\n",
    );
    let map = parse_and_extract(php);

    let return_start = php.find("@return").unwrap();
    let foo_in_return = php[return_start..].find("Foo").unwrap() + return_start;
    let bar_in_return = php[return_start..].find("Bar").unwrap() + return_start;

    let hit_foo = map.lookup(foo_in_return as u32);
    assert!(
        hit_foo.is_some(),
        "Should find Foo in true branch of conditional with 'is not'"
    );

    let hit_bar = map.lookup(bar_in_return as u32);
    assert!(
        hit_bar.is_some(),
        "Should find Bar in false branch of conditional with 'is not'"
    );
}

// ── var_def_kind_at tests ───────────────────────────────────────────

#[test]
fn var_def_kind_at_returns_parameter() {
    let php = concat!(
        "<?php\n",
        "class Ctrl {\n",
        "    public function handle(Request $req) {}\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let dollar_offset = php.find("$req").unwrap() as u32;
    let kind = map.var_def_kind_at("req", dollar_offset);
    assert_eq!(
        kind,
        Some(&VarDefKind::Parameter),
        "Should detect $req as Parameter"
    );

    let kind2 = map.var_def_kind_at("req", dollar_offset + 1);
    assert_eq!(
        kind2,
        Some(&VarDefKind::Parameter),
        "Should detect cursor on 'r' as Parameter"
    );
}

#[test]
fn var_def_kind_at_returns_catch() {
    let php = concat!(
        "<?php\n",
        "function f() {\n",
        "    try {} catch (\\Exception $e) {}\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let dollar_offset = php.find("$e)").unwrap() as u32;
    let kind = map.var_def_kind_at("e", dollar_offset);
    assert_eq!(kind, Some(&VarDefKind::Catch), "Should detect $e as Catch");
}

#[test]
fn var_def_kind_at_returns_foreach() {
    let php = concat!(
        "<?php\n",
        "function f() {\n",
        "    foreach ($items as $item) {}\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let dollar_offset = php.find("$item)").unwrap() as u32;
    let kind = map.var_def_kind_at("item", dollar_offset);
    assert_eq!(
        kind,
        Some(&VarDefKind::Foreach),
        "Should detect $item as Foreach"
    );
}

#[test]
fn var_def_kind_at_returns_none_on_usage() {
    let php = concat!(
        "<?php\n",
        "function f() {\n",
        "    $x = 1;\n",
        "    echo $x;\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let echo_x_offset = php.rfind("$x").unwrap() as u32;
    let kind = map.var_def_kind_at("x", echo_x_offset);
    assert!(kind.is_none(), "Should return None for variable usage site");
}

#[test]
fn docblock_callable_return_type_produces_class_reference() {
    let php = concat!(
        "<?php\n",
        "class Pencil {}\n",
        "class Factory {\n",
        "    /** @var \\Closure(): Pencil $supplier */\n",
        "    private $supplier;\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    // The return type `Pencil` of `\Closure(): Pencil` should be a
    // navigable ClassReference, not swallowed into the Closure span.
    let docblock_start = php.find("/** @var").unwrap();
    let pencil_in_doc = php[docblock_start..].find("Pencil").unwrap() + docblock_start;
    let hit = map.lookup(pencil_in_doc as u32);
    assert!(hit.is_some(), "Should find Pencil in callable return type");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Pencil");
    } else {
        panic!(
            "Expected ClassReference for Pencil, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn docblock_callable_param_types_produce_class_references() {
    let php = concat!(
        "<?php\n",
        "class Request {}\n",
        "class Response {}\n",
        "class Handler {\n",
        "    /** @var callable(Request): Response $handler */\n",
        "    private $handler;\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let docblock_start = php.find("/** @var").unwrap();

    // Parameter type `Request` should be navigable.
    let request_in_doc = php[docblock_start..].find("Request").unwrap() + docblock_start;
    let hit = map.lookup(request_in_doc as u32);
    assert!(hit.is_some(), "Should find Request in callable param type");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Request");
    } else {
        panic!(
            "Expected ClassReference for Request, got {:?}",
            hit.unwrap().kind
        );
    }

    // Return type `Response` should be navigable.
    let response_in_doc = php[docblock_start..].find("Response").unwrap() + docblock_start;
    let hit = map.lookup(response_in_doc as u32);
    assert!(
        hit.is_some(),
        "Should find Response in callable return type"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Response");
    } else {
        panic!(
            "Expected ClassReference for Response, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn docblock_closure_fqn_callable_produces_class_reference() {
    let php = concat!(
        "<?php\n",
        "class Result {}\n",
        "class Worker {\n",
        "    /** @param \\Closure(int): Result $cb */\n",
        "    public function run($cb) {}\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let docblock_start = php.find("/** @param").unwrap();

    // `\Closure` should be a navigable ClassReference with is_fqn=true.
    let closure_in_doc = php[docblock_start..].find("\\Closure").unwrap() + docblock_start;
    let hit = map.lookup(closure_in_doc as u32);
    assert!(hit.is_some(), "Should find \\Closure as a ClassReference");
    if let SymbolKind::ClassReference { ref name, is_fqn } = hit.unwrap().kind {
        assert_eq!(name, "Closure");
        assert!(is_fqn, "\\Closure should be FQN");
    } else {
        panic!("Expected ClassReference for Closure");
    }

    // `Result` should also be navigable.
    let result_in_doc = php[docblock_start..].find("Result").unwrap() + docblock_start;
    let hit = map.lookup(result_in_doc as u32);
    assert!(hit.is_some(), "Should find Result in callable return type");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Result");
    } else {
        panic!("Expected ClassReference for Result");
    }
}

#[test]
fn template_param_in_use_tag_generic_arg() {
    // `TModel` inside `@use Foo<TModel>` should produce a ClassReference span,
    // and the template def scope should cover it so hover can resolve it.
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @template TModel of \\stdClass\n",
        " */\n",
        "class Builder {\n",
        "    /** @use SomeTrait<TModel> */\n",
        "    use SomeTrait;\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    // TModel inside the generic arg should be in the symbol map.
    let use_line_offset = php.find("@use").unwrap();
    let tmodel_in_use = php[use_line_offset..].find("TModel").unwrap() + use_line_offset;
    let hit = map.lookup(tmodel_in_use as u32);
    assert!(
        hit.is_some(),
        "Should find TModel in @use generic arg in symbol map"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "TModel");
    } else {
        panic!(
            "Expected ClassReference for TModel, got {:?}",
            hit.unwrap().kind
        );
    }

    // The template def scope should cover the TModel usage.
    let found = map.find_template_def("TModel", tmodel_in_use as u32);
    assert!(
        found.is_some(),
        "Template def for TModel should cover the @use line"
    );
}

#[test]
fn static_keyword_in_generic_arg_produces_span() {
    // `static` inside `@return Builder<static>` should produce a
    // SelfStaticParent span so that hover works.
    let php = concat!(
        "<?php\n",
        "class Model {\n",
        "    /** @return Builder<static> */\n",
        "    public static function query() {}\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let return_line_offset = php.find("@return").unwrap();
    let static_in_generic = php[return_line_offset..].find("static").unwrap() + return_line_offset;
    let hit = map.lookup(static_in_generic as u32);
    assert!(
        hit.is_some(),
        "Should find `static` in generic arg in symbol map"
    );
    match &hit.unwrap().kind {
        SymbolKind::SelfStaticParent(kind) => {
            assert_eq!(*kind, SelfStaticParentKind::Static);
        }
        SymbolKind::ClassReference { name, .. } => {
            // `static` is in NON_NAVIGABLE, so it should NOT be a ClassReference.
            panic!(
                "static should be SelfStaticParent, not ClassReference({})",
                name
            );
        }
        other => {
            panic!("Expected SelfStaticParent for static, got {:?}", other);
        }
    }
}

#[test]
fn docblock_parenthesized_callable_in_union_produces_class_reference() {
    // `(\Closure(static): mixed)|string|array` — the outer parens are
    // grouping parens, not a conditional type.  `\Closure` inside should
    // still be recognised as a navigable ClassReference.
    let php = concat!(
        "<?php\n",
        "class Builder {\n",
        "    /**\n",
        "     * @param  (\\Closure(static): mixed)|string|array  $column\n",
        "     * @return $this\n",
        "     */\n",
        "    public function where($column) {}\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let docblock_start = php.find("/**").unwrap();

    // `\Closure` inside the parenthesized callable should be a ClassReference.
    let closure_in_doc = php[docblock_start..].find("\\Closure").unwrap() + docblock_start;
    let hit = map.lookup(closure_in_doc as u32);
    assert!(
        hit.is_some(),
        "Should find \\Closure inside parenthesized callable as ClassReference"
    );
    if let SymbolKind::ClassReference { ref name, is_fqn } = hit.unwrap().kind {
        assert_eq!(name, "Closure");
        assert!(is_fqn, "\\Closure should be FQN");
        // The span should cover exactly `\Closure`, not `(\Closure`.
        let span = hit.unwrap();
        let span_text = &php[span.start as usize..span.end as usize];
        assert_eq!(span_text, "\\Closure", "Span should cover only \\Closure");
    } else {
        panic!(
            "Expected ClassReference for \\Closure, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn class_const_class_in_property_default_produces_class_reference() {
    // `Foo::class` inside a property default value should produce a
    // ClassReference span for `Foo`.
    let php = concat!(
        "<?php\n",
        "class Foo {}\n",
        "class Bar {\n",
        "    protected $casts = [\n",
        "        'icing' => Foo::class,\n",
        "    ];\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let foo_in_casts = php.find("Foo::class").unwrap();
    let hit = map.lookup(foo_in_casts as u32);
    assert!(
        hit.is_some(),
        "Should find Foo in Foo::class as a ClassReference"
    );
    if let SymbolKind::ClassReference { name, .. } = &hit.unwrap().kind {
        assert_eq!(name, "Foo");
    } else {
        panic!(
            "Expected ClassReference for Foo, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn multiline_docblock_generic_arg_produces_class_reference() {
    // A class name inside a multiline `@return` generic type should
    // produce a ClassReference span.  Template parameters used inside
    // the multiline type should also be navigable.
    let php = concat!(
        "<?php\n",
        "class SomeCollection {}\n",
        "/**\n",
        " * @template TValue\n",
        " */\n",
        "class Demo {\n",
        "    /**\n",
        "     * @return array<\n",
        "     *   string,\n",
        "     *   SomeCollection<int, TValue>\n",
        "     * >\n",
        "     */\n",
        "    public function grouped() {}\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let doc_start = php.find("     * @return").unwrap();
    let some_coll = php[doc_start..].find("SomeCollection").unwrap() + doc_start;
    let hit = map.lookup(some_coll as u32);
    assert!(
        hit.is_some(),
        "Should find SomeCollection in multiline @return generic arg"
    );
    let span = hit.unwrap();
    if let SymbolKind::ClassReference { name, .. } = &span.kind {
        assert_eq!(name, "SomeCollection");
        // Verify the span text matches the original source exactly.
        let span_text = &php[span.start as usize..span.end as usize];
        assert_eq!(
            span_text, "SomeCollection",
            "Span text should be exactly 'SomeCollection', got {:?}",
            span_text
        );
    } else {
        panic!(
            "Expected ClassReference for SomeCollection, got {:?}",
            span.kind
        );
    }

    // TValue inside the multiline generic arg should also be navigable.
    let tvalue_pos = php[doc_start..].find("TValue").unwrap() + doc_start;
    let hit2 = map.lookup(tvalue_pos as u32);
    assert!(
        hit2.is_some(),
        "Should find TValue in multiline @return generic arg"
    );
    let span2 = hit2.unwrap();
    if let SymbolKind::ClassReference { name, .. } = &span2.kind {
        assert_eq!(name, "TValue");
        let span_text = &php[span2.start as usize..span2.end as usize];
        assert_eq!(
            span_text, "TValue",
            "Span text should be exactly 'TValue', got {:?}",
            span_text
        );
    } else {
        panic!("Expected ClassReference for TValue, got {:?}", span2.kind);
    }

    // The template def scope should cover the TValue usage.
    let found = map.find_template_def("TValue", tvalue_pos as u32);
    assert!(
        found.is_some(),
        "Template def for TValue should cover the multiline @return usage"
    );
}

#[test]
fn phpstan_assert_tag_produces_class_reference() {
    // `@phpstan-assert-if-false Rock $value` should produce a
    // ClassReference span for `Rock`.
    let php = concat!(
        "<?php\n",
        "class Rock {}\n",
        "class Checker {\n",
        "    /** @phpstan-assert-if-false Rock $value */\n",
        "    public function isValid($value): bool { return true; }\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let doc_start = php.find("@phpstan-assert-if-false").unwrap();
    let rock_pos = php[doc_start..].find("Rock").unwrap() + doc_start;
    let hit = map.lookup(rock_pos as u32);
    assert!(
        hit.is_some(),
        "Should find Rock in @phpstan-assert-if-false as a ClassReference"
    );
    let span = hit.unwrap();
    if let SymbolKind::ClassReference { name, .. } = &span.kind {
        assert_eq!(name, "Rock");
        let span_text = &php[span.start as usize..span.end as usize];
        assert_eq!(span_text, "Rock");
    } else {
        panic!("Expected ClassReference for Rock, got {:?}", span.kind);
    }
}

// ── Top-level const statement ───────────────────────────────────────────────

#[test]
fn top_level_const_value_produces_class_reference() {
    let php = "<?php\nconst MY_CLASS = Foo::class;\n";
    let map = parse_and_extract(php);
    let foo_offset = php.find("Foo").unwrap() as u32;
    let hit = map.lookup(foo_offset);
    assert!(hit.is_some(), "Should find Foo in const value expression");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Foo");
    } else {
        panic!(
            "Expected ClassReference for Foo, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn top_level_const_multiple_items() {
    let php = "<?php\nconst A = Foo::class, B = Bar::class;\n";
    let map = parse_and_extract(php);

    let foo_offset = php.find("Foo").unwrap() as u32;
    let hit_foo = map.lookup(foo_offset);
    assert!(hit_foo.is_some(), "Should find Foo in first const item");
    if let SymbolKind::ClassReference { ref name, .. } = hit_foo.unwrap().kind {
        assert_eq!(name, "Foo");
    } else {
        panic!("Expected ClassReference for Foo");
    }

    let bar_offset = php.find("Bar").unwrap() as u32;
    let hit_bar = map.lookup(bar_offset);
    assert!(hit_bar.is_some(), "Should find Bar in second const item");
    if let SymbolKind::ClassReference { ref name, .. } = hit_bar.unwrap().kind {
        assert_eq!(name, "Bar");
    } else {
        panic!("Expected ClassReference for Bar");
    }
}

// ── Anonymous class ─────────────────────────────────────────────────────────

#[test]
fn anonymous_class_extends_produces_class_reference() {
    let php = concat!(
        "<?php\n",
        "function test() {\n",
        "    return new class extends Foo implements Bar {};\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let foo_offset = php.find("Foo").unwrap() as u32;
    let hit = map.lookup(foo_offset);
    assert!(hit.is_some(), "Should find Foo in anonymous class extends");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Foo");
    } else {
        panic!("Expected ClassReference for Foo");
    }

    let bar_offset = php.find("Bar").unwrap() as u32;
    let hit2 = map.lookup(bar_offset);
    assert!(
        hit2.is_some(),
        "Should find Bar in anonymous class implements"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit2.unwrap().kind {
        assert_eq!(name, "Bar");
    } else {
        panic!("Expected ClassReference for Bar");
    }
}

#[test]
fn anonymous_class_members_are_extracted() {
    let php = concat!(
        "<?php\n",
        "function test() {\n",
        "    return new class {\n",
        "        public function run(Baz $b) {}\n",
        "    };\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let baz_offset = php.find("Baz").unwrap() as u32;
    let hit = map.lookup(baz_offset);
    assert!(
        hit.is_some(),
        "Should find Baz in anonymous class method param type"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Baz");
    } else {
        panic!("Expected ClassReference for Baz");
    }
}

#[test]
fn anonymous_class_constructor_args_are_extracted() {
    let php = concat!(
        "<?php\n",
        "function test() {\n",
        "    return new class(new Qux()) {};\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let qux_offset = php.find("Qux").unwrap() as u32;
    let hit = map.lookup(qux_offset);
    assert!(
        hit.is_some(),
        "Should find Qux in anonymous class constructor arg"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Qux");
    } else {
        panic!("Expected ClassReference for Qux");
    }
}

// ── Language constructs ─────────────────────────────────────────────────────

#[test]
fn construct_isset_extracts_inner_expressions() {
    let php = "<?php\nfunction t() { isset($foo->bar); }\n";
    let map = parse_and_extract(php);

    let bar_offset = php.find("bar").unwrap() as u32;
    let hit = map.lookup(bar_offset);
    assert!(hit.is_some(), "Should find bar in isset()");
    if let SymbolKind::MemberAccess {
        ref member_name, ..
    } = hit.unwrap().kind
    {
        assert_eq!(member_name, "bar");
    } else {
        panic!("Expected MemberAccess for bar, got {:?}", hit.unwrap().kind);
    }
}

#[test]
fn construct_empty_extracts_inner_expression() {
    let php = "<?php\nfunction t() { empty($foo->name); }\n";
    let map = parse_and_extract(php);

    let name_offset = php.find("name").unwrap() as u32;
    let hit = map.lookup(name_offset);
    assert!(hit.is_some(), "Should find name in empty()");
    if let SymbolKind::MemberAccess {
        ref member_name, ..
    } = hit.unwrap().kind
    {
        assert_eq!(member_name, "name");
    } else {
        panic!(
            "Expected MemberAccess for name, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn construct_print_extracts_inner_expression() {
    let php = "<?php\nfunction t(Foo $x) { print $x->label; }\n";
    let map = parse_and_extract(php);

    let label_offset = php.find("label").unwrap() as u32;
    let hit = map.lookup(label_offset);
    assert!(hit.is_some(), "Should find label in print expression");
    if let SymbolKind::MemberAccess {
        ref member_name, ..
    } = hit.unwrap().kind
    {
        assert_eq!(member_name, "label");
    } else {
        panic!("Expected MemberAccess for label");
    }
}

// ── Composite strings (interpolation) ───────────────────────────────────────

#[test]
fn composite_string_extracts_variable() {
    let php = "<?php\nfunction t() { $x = 1; echo \"val={$x}\"; }\n";
    let map = parse_and_extract(php);

    // The $x inside the interpolated string should have a Variable span.
    // Find the second occurrence of $x (the one inside the string).
    let first_x = php.find("$x").unwrap();
    let second_x = php[first_x + 2..].find("$x").unwrap() + first_x + 2;
    let hit = map.lookup(second_x as u32);
    assert!(hit.is_some(), "Should find $x inside interpolated string");
    if let SymbolKind::Variable { ref name } = hit.unwrap().kind {
        assert_eq!(name, "x");
    } else {
        panic!("Expected Variable for $x, got {:?}", hit.unwrap().kind);
    }
}

#[test]
fn composite_string_braced_expression_extracts_member_access() {
    let php = "<?php\nfunction t() { echo \"name={$obj->name}\"; }\n";
    let map = parse_and_extract(php);

    let name_offset = php.find("name}").unwrap() as u32;
    let hit = map.lookup(name_offset);
    assert!(
        hit.is_some(),
        "Should find member access inside braced string expression"
    );
    if let SymbolKind::MemberAccess {
        ref member_name, ..
    } = hit.unwrap().kind
    {
        assert_eq!(member_name, "name");
    } else {
        panic!(
            "Expected MemberAccess for name, got {:?}",
            hit.unwrap().kind
        );
    }
}

// ── Array append ────────────────────────────────────────────────────────────

#[test]
fn array_append_extracts_array_variable() {
    let php = "<?php\nfunction t() { $arr[] = 1; }\n";
    let map = parse_and_extract(php);

    let arr_offset = php.find("$arr").unwrap() as u32;
    let hit = map.lookup(arr_offset);
    assert!(hit.is_some(), "Should find $arr in array append LHS");
    if let SymbolKind::Variable { ref name } = hit.unwrap().kind {
        assert_eq!(name, "arr");
    } else {
        panic!("Expected Variable for $arr, got {:?}", hit.unwrap().kind);
    }
}

// ── Standalone constant access ──────────────────────────────────────────────

#[test]
fn constant_access_produces_constant_reference() {
    let php = "<?php\nfunction t() { echo PHP_EOL; }\n";
    let map = parse_and_extract(php);

    let eol_offset = php.find("PHP_EOL").unwrap() as u32;
    let hit = map.lookup(eol_offset);
    assert!(hit.is_some(), "Should find PHP_EOL as ConstantReference");
    if let SymbolKind::ConstantReference { ref name } = hit.unwrap().kind {
        assert_eq!(name, "PHP_EOL");
    } else {
        panic!(
            "Expected ConstantReference for PHP_EOL, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn static_class_constant_access_produces_class_reference() {
    // `\App\MyClass::FOO` — the class part should be a ClassReference.
    let php = "<?php\nfunction t() { echo \\App\\MyClass::FOO; }\n";
    let map = parse_and_extract(php);

    let class_offset = php.find("\\App\\MyClass").unwrap() as u32;
    let hit = map.lookup(class_offset);
    assert!(hit.is_some(), "Should find \\App\\MyClass");
    if let SymbolKind::ClassReference { ref name, is_fqn } = hit.unwrap().kind {
        assert_eq!(name, "App\\MyClass");
        assert!(is_fqn);
    } else {
        panic!(
            "Expected ClassReference for App\\MyClass, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn namespaced_standalone_constant_produces_constant_reference() {
    // A standalone namespaced constant like `PHPStan\PHP_VERSION_ID` is a
    // ConstantAccess in the parser — it must NOT be emitted as a
    // ClassReference, which would cause false "unknown class" diagnostics.
    let php = "<?php\nfunction t() { echo \\PHPStan\\PHP_VERSION_ID; }\n";
    let map = parse_and_extract(php);

    let const_offset = php.find("\\PHPStan\\PHP_VERSION_ID").unwrap() as u32;
    let hit = map.lookup(const_offset);
    assert!(
        hit.is_some(),
        "Should find \\PHPStan\\PHP_VERSION_ID as ConstantReference"
    );
    if let SymbolKind::ConstantReference { ref name } = hit.unwrap().kind {
        assert_eq!(name, "PHPStan\\PHP_VERSION_ID");
    } else {
        panic!(
            "Expected ConstantReference for PHPStan\\PHP_VERSION_ID, got {:?}",
            hit.unwrap().kind
        );
    }
}

// ── Pipe operator ───────────────────────────────────────────────────────────

// Note: PHP 8.5 pipe operator support. The parser may or may not handle
// this syntax depending on the mago_syntax version. The extraction code
// is in place for when it does.

// ── First-class callable / partial application ──────────────────────────────

#[test]
fn first_class_callable_function_produces_function_call() {
    let php = "<?php\nfunction t() { $fn = strlen(...); }\n";
    let map = parse_and_extract(php);

    let strlen_offset = php.find("strlen").unwrap() as u32;
    let hit = map.lookup(strlen_offset);
    assert!(hit.is_some(), "Should find strlen in first-class callable");
    if let SymbolKind::FunctionCall { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "strlen");
    } else {
        panic!(
            "Expected FunctionCall for strlen, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn first_class_callable_static_method_produces_member_access() {
    let php = "<?php\nfunction t() { $fn = Foo::bar(...); }\n";
    let map = parse_and_extract(php);

    // `Foo` should be a ClassReference.
    let foo_offset = php.find("Foo").unwrap() as u32;
    let hit_foo = map.lookup(foo_offset);
    assert!(hit_foo.is_some(), "Should find Foo class reference");
    if let SymbolKind::ClassReference { ref name, .. } = hit_foo.unwrap().kind {
        assert_eq!(name, "Foo");
    } else {
        panic!("Expected ClassReference for Foo");
    }

    // `bar` should be a MemberAccess.
    let bar_offset = php.find("bar").unwrap() as u32;
    let hit_bar = map.lookup(bar_offset);
    assert!(
        hit_bar.is_some(),
        "Should find bar in static first-class callable"
    );
    if let SymbolKind::MemberAccess {
        ref member_name,
        is_static,
        is_method_call,
        ..
    } = hit_bar.unwrap().kind
    {
        assert_eq!(member_name, "bar");
        assert!(is_static);
        assert!(is_method_call);
    } else {
        panic!(
            "Expected MemberAccess for bar, got {:?}",
            hit_bar.unwrap().kind
        );
    }
}

#[test]
fn first_class_callable_instance_method_produces_member_access() {
    let php = "<?php\nfunction t($obj) { $fn = $obj->baz(...); }\n";
    let map = parse_and_extract(php);

    let baz_offset = php.find("baz").unwrap() as u32;
    let hit = map.lookup(baz_offset);
    assert!(
        hit.is_some(),
        "Should find baz in instance first-class callable"
    );
    if let SymbolKind::MemberAccess {
        ref member_name,
        is_static,
        is_method_call,
        ..
    } = hit.unwrap().kind
    {
        assert_eq!(member_name, "baz");
        assert!(!is_static);
        assert!(is_method_call);
    } else {
        panic!("Expected MemberAccess for baz, got {:?}", hit.unwrap().kind);
    }
}

// ── Echo tag (short open tag) ───────────────────────────────────────────────

// Note: `<?= $expr ?>` parsing depends on the parser treating it as an
// EchoTag statement. The extraction code handles it; a full integration
// test would require a PHP file with short echo tags that the parser
// recognises.

// ── Declare statement ───────────────────────────────────────────────────────

#[test]
fn declare_statement_body_extracts_symbols() {
    let php = concat!(
        "<?php\n",
        "declare(strict_types=1);\n",
        "function test(Foo $x) {}\n",
    );
    let map = parse_and_extract(php);

    let foo_offset = php.find("Foo").unwrap() as u32;
    let hit = map.lookup(foo_offset);
    assert!(hit.is_some(), "Should find Foo after declare statement");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Foo");
    } else {
        panic!("Expected ClassReference for Foo");
    }
}

#[test]
fn docblock_array_shape_value_type_produces_class_reference() {
    let php = concat!(
        "<?php\n",
        "class Pen {}\n",
        "/**\n",
        " * @return array{logger: Pen, debug: bool}\n",
        " */\n",
        "function getConfig(): array { return []; }\n",
    );
    let map = parse_and_extract(php);

    let pen_offset = php.find("Pen, debug").unwrap() as u32;
    let hit = map.lookup(pen_offset);
    assert!(
        hit.is_some(),
        "Should find Pen inside array shape value type"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Pen");
    } else {
        panic!(
            "Expected ClassReference for Pen, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn docblock_array_shape_multiple_class_values() {
    let php = concat!(
        "<?php\n",
        "class Logger {}\n",
        "class Mailer {}\n",
        "/**\n",
        " * @return array{log: Logger, mail: Mailer}\n",
        " */\n",
        "function services(): array { return []; }\n",
    );
    let map = parse_and_extract(php);

    // Check Logger
    let logger_offset = php.find("Logger, mail").unwrap() as u32;
    let hit = map.lookup(logger_offset);
    assert!(hit.is_some(), "Should find Logger in array shape");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Logger");
    } else {
        panic!("Expected ClassReference for Logger");
    }

    // Check Mailer
    let mailer_offset = php.find("Mailer}").unwrap() as u32;
    let hit = map.lookup(mailer_offset);
    assert!(hit.is_some(), "Should find Mailer in array shape");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Mailer");
    } else {
        panic!("Expected ClassReference for Mailer");
    }
}

#[test]
fn docblock_object_shape_value_type_produces_class_reference() {
    let php = concat!(
        "<?php\n",
        "class User {}\n",
        "/**\n",
        " * @return object{owner: User, active: bool}\n",
        " */\n",
        "function getProfile(): object { return (object)[]; }\n",
    );
    let map = parse_and_extract(php);

    let user_offset = php.find("User, active").unwrap() as u32;
    let hit = map.lookup(user_offset);
    assert!(
        hit.is_some(),
        "Should find User inside object shape value type"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "User");
    } else {
        panic!("Expected ClassReference for User");
    }
}

#[test]
fn docblock_array_shape_scalar_value_not_navigable() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @return array{name: string, count: int}\n",
        " */\n",
        "function getData(): array { return []; }\n",
    );
    let map = parse_and_extract(php);

    let string_offset = php.find("string, count").unwrap() as u32;
    let hit = map.lookup(string_offset);
    assert!(
        hit.is_none(),
        "Scalar 'string' inside array shape should not be navigable"
    );
}

#[test]
fn docblock_array_shape_optional_key_value_produces_class_reference() {
    let php = concat!(
        "<?php\n",
        "class Widget {}\n",
        "/**\n",
        " * @return array{item?: Widget}\n",
        " */\n",
        "function maybeWidget(): array { return []; }\n",
    );
    let map = parse_and_extract(php);

    let widget_offset = php.find("Widget}").unwrap() as u32;
    let hit = map.lookup(widget_offset);
    assert!(
        hit.is_some(),
        "Should find Widget in optional array shape entry"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Widget");
    } else {
        panic!("Expected ClassReference for Widget");
    }
}

#[test]
fn docblock_array_shape_nested_generic_value() {
    let php = concat!(
        "<?php\n",
        "class Item {}\n",
        "/**\n",
        " * @return array{items: list<Item>, total: int}\n",
        " */\n",
        "function paginated(): array { return []; }\n",
    );
    let map = parse_and_extract(php);

    let item_offset = php.find("Item>, total").unwrap() as u32;
    let hit = map.lookup(item_offset);
    assert!(
        hit.is_some(),
        "Should find Item inside generic within array shape"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Item");
    } else {
        panic!("Expected ClassReference for Item");
    }
}

#[test]
fn arrow_fn_body_scope_suppresses_outer_call_site() {
    let src = concat!(
        "<?php\n",
        "class Collection {\n",
        "    /** @param callable(mixed): mixed $callback */\n",
        "    public function each(callable $callback): static { return $this; }\n",
        "}\n",
        "class ReviewModel {\n",
        "    public float $percentage = 0.0;\n",
        "    public int $count = 0;\n",
        "}\n",
        "$ratings = new Collection();\n",
        "$total = 10;\n",
        "$ratings->each(fn(ReviewModel $model): float => $model->percentage = $total > 0 ? ($model->count / $total) * 100 : 0.0);\n",
    );
    let sm = parse_and_extract(src);

    // Find the `each(` call site.
    let each_cs = sm
        .call_sites
        .iter()
        .find(|cs| cs.call_expression.contains("each"))
        .expect("should have a call site for each()");

    // The cursor at col 60 on line 11 is inside the arrow fn body.
    // Compute a byte offset in the arrow fn body region (past `=>`).
    let arrow_body_marker = src.find("$model->percentage").unwrap() as u32;

    // There should be a body scope (arrow fn `=>`) starting inside each()'s args.
    let has_nested = sm.is_inside_nested_scope_of_call(arrow_body_marker, each_cs);
    assert!(
        has_nested,
        "Cursor at offset {} should be inside a nested body scope of each() call (args {}..{}), body_scopes: {:?}",
        arrow_body_marker, each_cs.args_start, each_cs.args_end, sm.body_scopes,
    );
}

// ── @see tag symbol extraction tests ────────────────────────────────────────

#[test]
fn see_tag_class_reference() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @see UserService\n",
        " */\n",
        "class Foo {}\n",
    );
    let map = parse_and_extract(php);

    let offset = php.find("UserService").unwrap() as u32;
    let hit = map.lookup(offset);
    assert!(hit.is_some(), "Should find UserService from @see tag");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "UserService");
    } else {
        panic!("Expected ClassReference for @see UserService");
    }
}

#[test]
fn see_tag_fqn_class_reference() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @see \\App\\Models\\User\n",
        " */\n",
        "class Foo {}\n",
    );
    let map = parse_and_extract(php);

    let offset = php.find("\\App\\Models\\User").unwrap() as u32;
    let hit = map.lookup(offset);
    assert!(hit.is_some(), "Should find FQN class from @see tag");
    if let SymbolKind::ClassReference {
        ref name, is_fqn, ..
    } = hit.unwrap().kind
    {
        assert_eq!(name, "App\\Models\\User");
        assert!(is_fqn, "Leading backslash should set is_fqn");
    } else {
        panic!("Expected ClassReference for @see \\App\\Models\\User");
    }
}

#[test]
fn see_tag_member_method() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @see Order::getTotal()\n",
        " */\n",
        "class Foo {}\n",
    );
    let map = parse_and_extract(php);

    // The class part should produce a ClassReference.
    let class_offset = php.find("Order").unwrap() as u32;
    let hit = map.lookup(class_offset);
    assert!(
        hit.is_some(),
        "Should find Order from @see Order::getTotal()"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Order");
    } else {
        panic!("Expected ClassReference for Order");
    }

    // The member part should produce a MemberAccess.
    let member_offset = php.find("getTotal").unwrap() as u32;
    let hit = map.lookup(member_offset);
    assert!(
        hit.is_some(),
        "Should find getTotal from @see Order::getTotal()"
    );
    if let SymbolKind::MemberAccess {
        ref subject_text,
        ref member_name,
        is_static,
        ..
    } = hit.unwrap().kind
    {
        assert_eq!(subject_text, "Order");
        assert_eq!(member_name, "getTotal");
        assert!(is_static, "@see members are treated as static access");
    } else {
        panic!("Expected MemberAccess for getTotal");
    }
}

#[test]
fn see_tag_member_property() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @see Order::$channel_type\n",
        " */\n",
        "class Foo {}\n",
    );
    let map = parse_and_extract(php);

    // The class part should produce a ClassReference.
    let class_offset = php.find("Order").unwrap() as u32;
    let hit = map.lookup(class_offset);
    assert!(hit.is_some(), "Should find Order from @see tag");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Order");
    } else {
        panic!("Expected ClassReference for Order");
    }

    // The $channel_type part should produce a MemberAccess.
    let prop_offset = php.find("$channel_type").unwrap() as u32;
    let hit = map.lookup(prop_offset);
    assert!(hit.is_some(), "Should find $channel_type from @see tag");
    if let SymbolKind::MemberAccess {
        ref subject_text,
        ref member_name,
        is_static,
        ..
    } = hit.unwrap().kind
    {
        assert_eq!(subject_text, "Order");
        assert_eq!(member_name, "channel_type");
        assert!(is_static);
    } else {
        panic!("Expected MemberAccess for $channel_type");
    }
}

#[test]
fn see_tag_member_constant() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @see Order::STATUS_PENDING\n",
        " */\n",
        "class Foo {}\n",
    );
    let map = parse_and_extract(php);

    let const_offset = php.find("STATUS_PENDING").unwrap() as u32;
    let hit = map.lookup(const_offset);
    assert!(
        hit.is_some(),
        "Should find STATUS_PENDING from @see Order::STATUS_PENDING"
    );
    if let SymbolKind::MemberAccess {
        ref subject_text,
        ref member_name,
        is_static,
        ..
    } = hit.unwrap().kind
    {
        assert_eq!(subject_text, "Order");
        assert_eq!(member_name, "STATUS_PENDING");
        assert!(is_static);
    } else {
        panic!("Expected MemberAccess for STATUS_PENDING");
    }
}

#[test]
fn see_tag_url_skipped() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @see https://example.com/docs\n",
        " */\n",
        "class Foo {}\n",
    );
    let map = parse_and_extract(php);

    let offset = php.find("https://example.com").unwrap() as u32;
    let hit = map.lookup(offset);
    // URLs should NOT produce a symbol span.
    assert!(
        hit.is_none()
            || !matches!(
                hit.unwrap().kind,
                SymbolKind::ClassReference { .. } | SymbolKind::FunctionCall { .. }
            ),
        "URL in @see should not produce a navigable symbol"
    );
}

#[test]
fn see_tag_function_reference() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @see fixture()\n",
        " */\n",
        "class Foo {}\n",
    );
    let map = parse_and_extract(php);

    let offset = php.find("fixture").unwrap() as u32;
    let hit = map.lookup(offset);
    assert!(hit.is_some(), "Should find fixture from @see fixture()");
    if let SymbolKind::FunctionCall { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "fixture");
    } else {
        panic!(
            "Expected FunctionCall for fixture, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn see_tag_inline_form() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * Wraps {@see UserService} with caching.\n",
        " */\n",
        "class Foo {}\n",
    );
    let map = parse_and_extract(php);

    let offset = php.find("UserService").unwrap() as u32;
    let hit = map.lookup(offset);
    assert!(hit.is_some(), "Should find UserService from inline @see");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "UserService");
    } else {
        panic!("Expected ClassReference for inline @see UserService");
    }
}

#[test]
fn see_tag_inline_function_reference() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * Wraps {@see fixture()} with extra logic.\n",
        " */\n",
        "class Foo {}\n",
    );
    let map = parse_and_extract(php);

    let offset = php.find("fixture").unwrap() as u32;
    let hit = map.lookup(offset);
    assert!(
        hit.is_some(),
        "Should find fixture from inline {{@see fixture()}}"
    );
    if let SymbolKind::FunctionCall { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "fixture");
    } else {
        panic!(
            "Expected FunctionCall for inline @see fixture(), got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn see_tag_inline_member_reference() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * Uses {@see Order::getTotal()} internally.\n",
        " */\n",
        "class Foo {}\n",
    );
    let map = parse_and_extract(php);

    let class_offset = php.find("Order").unwrap() as u32;
    let hit = map.lookup(class_offset);
    assert!(hit.is_some(), "Should find Order from inline @see");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Order");
    } else {
        panic!("Expected ClassReference for Order in inline @see");
    }

    let member_offset = php.find("getTotal").unwrap() as u32;
    let hit = map.lookup(member_offset);
    assert!(hit.is_some(), "Should find getTotal from inline @see");
    if let SymbolKind::MemberAccess {
        ref member_name, ..
    } = hit.unwrap().kind
    {
        assert_eq!(member_name, "getTotal");
    } else {
        panic!("Expected MemberAccess for getTotal in inline @see");
    }
}

#[test]
fn see_tag_scalar_type_not_navigable() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @see string\n",
        " */\n",
        "class Foo {}\n",
    );
    let map = parse_and_extract(php);

    // Find the "string" that is part of the @see tag, not the one
    // from the class keyword.
    let see_line = php.find("@see string").unwrap();
    let string_offset = (see_line + "@see ".len()) as u32;
    let hit = map.lookup(string_offset);
    assert!(
        hit.is_none()
            || !matches!(
                hit.unwrap().kind,
                SymbolKind::ClassReference { .. } | SymbolKind::FunctionCall { .. }
            ),
        "Scalar type 'string' in @see should not produce a navigable symbol"
    );
}

#[test]
fn see_tag_multiple_on_same_docblock() {
    let php = concat!(
        "<?php\n",
        "/**\n",
        " * @see UserService\n",
        " * @see OrderService\n",
        " */\n",
        "class Foo {}\n",
    );
    let map = parse_and_extract(php);

    let user_offset = php.find("UserService").unwrap() as u32;
    let hit = map.lookup(user_offset);
    assert!(hit.is_some(), "Should find UserService");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "UserService");
    } else {
        panic!("Expected ClassReference for UserService");
    }

    let order_offset = php.find("OrderService").unwrap() as u32;
    let hit = map.lookup(order_offset);
    assert!(hit.is_some(), "Should find OrderService");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "OrderService");
    } else {
        panic!("Expected ClassReference for OrderService");
    }
}

#[test]
fn see_tag_on_method_docblock() {
    let php = concat!(
        "<?php\n",
        "class Foo {\n",
        "    /**\n",
        "     * @see BarService\n",
        "     */\n",
        "    public function test() {}\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let offset = php.find("BarService").unwrap() as u32;
    let hit = map.lookup(offset);
    assert!(hit.is_some(), "Should find BarService on method docblock");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "BarService");
    } else {
        panic!("Expected ClassReference for BarService");
    }
}

#[test]
fn see_tag_on_property_docblock() {
    let php = concat!(
        "<?php\n",
        "class Foo {\n",
        "    /**\n",
        "     * @see CacheDriver\n",
        "     */\n",
        "    private $cache;\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let offset = php.find("CacheDriver").unwrap() as u32;
    let hit = map.lookup(offset);
    assert!(
        hit.is_some(),
        "Should find CacheDriver on property docblock"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "CacheDriver");
    } else {
        panic!("Expected ClassReference for CacheDriver");
    }
}

#[test]
fn property_attribute_class_reference() {
    let php = concat!(
        "<?php\n",
        "class Foo {\n",
        "    #[\\Assert\\NotBlank]\n",
        "    public string $name;\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let attr_offset = php.find("\\Assert\\NotBlank").unwrap() as u32;
    let hit = map.lookup(attr_offset);
    assert!(
        hit.is_some(),
        "Should find attribute class reference on property"
    );
    if let SymbolKind::ClassReference { ref name, is_fqn } = hit.unwrap().kind {
        assert_eq!(name, "Assert\\NotBlank");
        assert!(is_fqn);
    } else {
        panic!(
            "Expected ClassReference for property attribute, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn hooked_property_attribute_class_reference() {
    let php = concat!(
        "<?php\n",
        "class Foo {\n",
        "    #[\\Assert\\Email]\n",
        "    public string $email {\n",
        "        set { $this->email = strtolower($value); }\n",
        "    }\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let attr_offset = php.find("\\Assert\\Email").unwrap() as u32;
    let hit = map.lookup(attr_offset);
    assert!(
        hit.is_some(),
        "Should find attribute class reference on hooked property"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Assert\\Email");
    } else {
        panic!(
            "Expected ClassReference for hooked property attribute, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn class_constant_attribute_class_reference() {
    let php = concat!(
        "<?php\n",
        "class Foo {\n",
        "    #[\\Deprecated]\n",
        "    const OLD_NAME = 'old';\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let attr_offset = php.find("\\Deprecated").unwrap() as u32;
    let hit = map.lookup(attr_offset);
    assert!(
        hit.is_some(),
        "Should find attribute class reference on class constant"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Deprecated");
    } else {
        panic!(
            "Expected ClassReference for constant attribute, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn parameter_attribute_class_reference() {
    let php = concat!(
        "<?php\n",
        "function handle(#[\\SensitiveParameter] string $secret): void {}\n",
    );
    let map = parse_and_extract(php);

    let attr_offset = php.find("\\SensitiveParameter").unwrap() as u32;
    let hit = map.lookup(attr_offset);
    assert!(
        hit.is_some(),
        "Should find attribute class reference on parameter"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "SensitiveParameter");
    } else {
        panic!(
            "Expected ClassReference for parameter attribute, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn method_parameter_attribute_class_reference() {
    let php = concat!(
        "<?php\n",
        "class Svc {\n",
        "    public function run(#[\\Autowire] Logger $log): void {}\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let attr_offset = php.find("\\Autowire").unwrap() as u32;
    let hit = map.lookup(attr_offset);
    assert!(
        hit.is_some(),
        "Should find attribute class reference on method parameter"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Autowire");
    } else {
        panic!(
            "Expected ClassReference for method parameter attribute, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn enum_case_attribute_class_reference() {
    let php = concat!(
        "<?php\n",
        "enum Status: string {\n",
        "    #[\\Deprecated]\n",
        "    case Legacy = 'legacy';\n",
        "    case Active = 'active';\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let attr_offset = php.find("\\Deprecated").unwrap() as u32;
    let hit = map.lookup(attr_offset);
    assert!(
        hit.is_some(),
        "Should find attribute class reference on enum case"
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Deprecated");
    } else {
        panic!(
            "Expected ClassReference for enum case attribute, got {:?}",
            hit.unwrap().kind
        );
    }
}

#[test]
fn multiple_property_attributes() {
    let php = concat!(
        "<?php\n",
        "class Foo {\n",
        "    #[\\Assert\\NotBlank, \\Assert\\Length(max: 255)]\n",
        "    public string $title;\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    let nb_offset = php.find("\\Assert\\NotBlank").unwrap() as u32;
    let hit = map.lookup(nb_offset);
    assert!(hit.is_some(), "Should find first property attribute");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Assert\\NotBlank");
    } else {
        panic!("Expected ClassReference for first attribute");
    }

    let len_offset = php.find("\\Assert\\Length").unwrap() as u32;
    let hit = map.lookup(len_offset);
    assert!(hit.is_some(), "Should find second property attribute");
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Assert\\Length");
    } else {
        panic!("Expected ClassReference for second attribute");
    }
}

#[test]
fn docblock_star_wildcard_in_generic_produces_correct_spans() {
    // PHPStan `*` wildcards in generic positions must not cause the
    // entire type string to become a single broken ClassReference.
    // Instead, the base class and other named generic args should
    // produce individual navigable spans.
    let php = concat!(
        "<?php\n",
        "class Foo {\n",
        "    /**\n",
        "     * @param Relation<Model, *, *>|string $rel\n",
        "     */\n",
        "    public function bar($rel): void {}\n",
        "}\n",
    );
    let map = parse_and_extract(php);

    // Find the `Relation` class reference span.
    let relation_offset = php.find("Relation<").unwrap() as u32;
    let hit = map.lookup(relation_offset);
    assert!(
        hit.is_some(),
        "Should find ClassReference for Relation at offset {}",
        relation_offset
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Relation", "Base class name should be 'Relation'");
    } else {
        panic!(
            "Expected ClassReference for Relation, got {:?}",
            hit.unwrap().kind
        );
    }

    // Find the `Model` class reference inside the generic args.
    let model_offset = php.find("Model, *").unwrap() as u32;
    let hit = map.lookup(model_offset);
    assert!(
        hit.is_some(),
        "Should find ClassReference for Model at offset {}",
        model_offset
    );
    if let SymbolKind::ClassReference { ref name, .. } = hit.unwrap().kind {
        assert_eq!(name, "Model", "Generic arg should be 'Model'");
    } else {
        panic!(
            "Expected ClassReference for Model, got {:?}",
            hit.unwrap().kind
        );
    }

    // Verify that no span has the full unparsed type string as its name
    // (this was the bug: the entire "Relation<Model, *, *>|string" was
    // emitted as one ClassReference).
    for span in &map.spans {
        if let SymbolKind::ClassReference { ref name, .. } = span.kind {
            assert!(
                !name.contains('<') && !name.contains('*'),
                "ClassReference name must not contain raw generic syntax or wildcards, got: {}",
                name
            );
        }
    }
}