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
use crate::common::create_test_backend;
use tower_lsp::LanguageServer;
use tower_lsp::lsp_types::*;

// ─── Tests: Stored byte offsets and offset_to_position ──────────────────────

/// Verify that every MethodInfo, PropertyInfo, and ConstantInfo created from
/// a real PHP parse has `name_offset > 0`.
#[tokio::test]
async fn test_parsed_members_have_nonzero_name_offset() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///test_offsets.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Greeter {\n",
        "    const GREETING = 'Hello';\n",
        "    public string $name = 'World';\n",
        "    public static int $count = 0;\n",
        "\n",
        "    public function greet(): string {\n",
        "        return self::GREETING . ' ' . $this->name;\n",
        "    }\n",
        "\n",
        "    private static function increment(): void {\n",
        "        self::$count++;\n",
        "    }\n",
        "}\n",
    );

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text: text.to_string(),
        },
    };
    backend.did_open(open_params).await;

    let classes = backend
        .get_classes_for_uri(uri.as_str())
        .expect("Should have parsed classes");
    assert_eq!(classes.len(), 1);
    let cls = &classes[0];
    assert_eq!(cls.name, "Greeter");

    // All methods should have name_offset > 0
    for method in &cls.methods {
        assert!(
            method.name_offset > 0,
            "Method '{}' should have name_offset > 0, got {}",
            method.name,
            method.name_offset
        );
    }

    // All properties should have name_offset > 0
    for prop in &cls.properties {
        assert!(
            prop.name_offset > 0,
            "Property '{}' should have name_offset > 0, got {}",
            prop.name,
            prop.name_offset
        );
    }

    // All constants should have name_offset > 0
    for constant in &cls.constants {
        assert!(
            constant.name_offset > 0,
            "Constant '{}' should have name_offset > 0, got {}",
            constant.name,
            constant.name_offset
        );
    }
}

/// Verify that keyword_offset is populated for all class-like kinds.
#[tokio::test]
async fn test_keyword_offset_populated_for_all_class_kinds() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///test_keyword_offsets.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class MyClass {\n",
        "    public function foo(): void {}\n",
        "}\n",
        "\n",
        "interface MyInterface {\n",
        "    public function bar(): void;\n",
        "}\n",
        "\n",
        "trait MyTrait {\n",
        "    public function baz(): void {}\n",
        "}\n",
        "\n",
        "enum MyEnum {\n",
        "    case Alpha;\n",
        "    case Beta;\n",
        "}\n",
    );

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text: text.to_string(),
        },
    };
    backend.did_open(open_params).await;

    let classes = backend
        .get_classes_for_uri(uri.as_str())
        .expect("Should have parsed classes");

    assert!(classes.len() >= 4, "Expected 4 class-like declarations");

    for cls in &classes {
        assert!(
            cls.keyword_offset > 0,
            "{:?} '{}' should have keyword_offset > 0, got {}",
            cls.kind,
            cls.name,
            cls.keyword_offset
        );
    }

    // Verify each points to the right keyword in the source
    let class_info = classes.iter().find(|c| c.name == "MyClass").unwrap();
    let kw_byte = class_info.keyword_offset as usize;
    assert_eq!(
        &text[kw_byte..kw_byte + 5],
        "class",
        "keyword_offset for MyClass should point to 'class'"
    );

    let iface_info = classes.iter().find(|c| c.name == "MyInterface").unwrap();
    let kw_byte = iface_info.keyword_offset as usize;
    assert_eq!(
        &text[kw_byte..kw_byte + 9],
        "interface",
        "keyword_offset for MyInterface should point to 'interface'"
    );

    let trait_info = classes.iter().find(|c| c.name == "MyTrait").unwrap();
    let kw_byte = trait_info.keyword_offset as usize;
    assert_eq!(
        &text[kw_byte..kw_byte + 5],
        "trait",
        "keyword_offset for MyTrait should point to 'trait'"
    );

    let enum_info = classes.iter().find(|c| c.name == "MyEnum").unwrap();
    let kw_byte = enum_info.keyword_offset as usize;
    assert_eq!(
        &text[kw_byte..kw_byte + 4],
        "enum",
        "keyword_offset for MyEnum should point to 'enum'"
    );
}

/// Verify that name_offset for methods, properties, and constants points to
/// the correct token in the source text.
#[tokio::test]
async fn test_name_offset_points_to_correct_token() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///test_name_tokens.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Demo {\n",
        "    const STATUS_ACTIVE = 1;\n",
        "    public string $title = '';\n",
        "\n",
        "    public function doSomething(): void {}\n",
        "}\n",
    );

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text: text.to_string(),
        },
    };
    backend.did_open(open_params).await;

    let classes = backend
        .get_classes_for_uri(uri.as_str())
        .expect("Should have parsed classes");
    let cls = &classes[0];

    // Check method name offset
    let method = cls
        .methods
        .iter()
        .find(|m| m.name == "doSomething")
        .unwrap();
    let off = method.name_offset as usize;
    assert_eq!(
        &text[off..off + "doSomething".len()],
        "doSomething",
        "Method name_offset should point to 'doSomething'"
    );

    // Check constant name offset
    let constant = cls
        .constants
        .iter()
        .find(|c| c.name == "STATUS_ACTIVE")
        .unwrap();
    let off = constant.name_offset as usize;
    assert_eq!(
        &text[off..off + "STATUS_ACTIVE".len()],
        "STATUS_ACTIVE",
        "Constant name_offset should point to 'STATUS_ACTIVE'"
    );

    // Check property name offset — points to the `$` of `$title`
    let prop = cls.properties.iter().find(|p| p.name == "title").unwrap();
    let off = prop.name_offset as usize;
    assert_eq!(
        &text[off..off + "$title".len()],
        "$title",
        "Property name_offset should point to '$title'"
    );
}

/// Verify that enum case name_offset points to the case name token.
#[tokio::test]
async fn test_enum_case_name_offset() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///test_enum_case_offset.php").unwrap();
    let text = concat!(
        "<?php\n",
        "enum Color {\n",
        "    case Red;\n",
        "    case Green;\n",
        "    case Blue;\n",
        "}\n",
    );

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text: text.to_string(),
        },
    };
    backend.did_open(open_params).await;

    let classes = backend
        .get_classes_for_uri(uri.as_str())
        .expect("Should have parsed classes");
    let cls = &classes[0];

    for constant in &cls.constants {
        let off = constant.name_offset as usize;
        let name_len = constant.name.len();
        assert_eq!(
            &text[off..off + name_len],
            constant.name,
            "Enum case '{}' name_offset should point to its name",
            constant.name
        );
    }
}

/// Verify that promoted constructor properties have correct name_offset.
#[tokio::test]
async fn test_promoted_property_name_offset() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///test_promoted.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Point {\n",
        "    public function __construct(\n",
        "        public readonly float $x,\n",
        "        public readonly float $y,\n",
        "        private string $label = 'origin',\n",
        "    ) {}\n",
        "}\n",
    );

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text: text.to_string(),
        },
    };
    backend.did_open(open_params).await;

    let classes = backend
        .get_classes_for_uri(uri.as_str())
        .expect("Should have parsed classes");
    let cls = &classes[0];

    // Should have 3 promoted properties: x, y, label
    assert!(
        cls.properties.len() >= 3,
        "Expected at least 3 promoted properties, got {}",
        cls.properties.len()
    );

    for prop in &cls.properties {
        assert!(
            prop.name_offset > 0,
            "Promoted property '{}' should have name_offset > 0",
            prop.name
        );
        // The offset should point to `$` + name in the source
        let off = prop.name_offset as usize;
        let expected = format!("${}", prop.name);
        assert_eq!(
            &text[off..off + expected.len()],
            expected,
            "Promoted property '{}' name_offset should point to '{}' in source",
            prop.name,
            expected
        );
    }
}

/// Verify that offset_to_position produces the same line/character as the
/// old text-search approach for a representative class.
#[tokio::test]
async fn test_offset_to_position_matches_text_search() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///test_offset_pos.php").unwrap();
    let text = concat!(
        "<?php\n",
        "abstract class AbstractHandler {\n",
        "    const MAX_RETRIES = 3;\n",
        "    protected int $retryCount = 0;\n",
        "\n",
        "    abstract public function handle(): void;\n",
        "\n",
        "    final public function retry(): bool {\n",
        "        return $this->retryCount < self::MAX_RETRIES;\n",
        "    }\n",
        "}\n",
    );

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text: text.to_string(),
        },
    };
    backend.did_open(open_params).await;

    // Click on "MAX_RETRIES" in `self::MAX_RETRIES` on line 8
    let params = GotoDefinitionParams {
        text_document_position_params: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier { uri: uri.clone() },
            position: Position {
                line: 8,
                character: 45,
            },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
    };

    let result = backend.goto_definition(params).await.unwrap();
    assert!(
        result.is_some(),
        "Should resolve self::MAX_RETRIES to its declaration"
    );

    match result.unwrap() {
        GotoDefinitionResponse::Scalar(location) => {
            assert_eq!(location.uri, uri);
            // "const MAX_RETRIES" is on line 2
            assert_eq!(
                location.range.start.line, 2,
                "MAX_RETRIES should be found on line 2"
            );
        }
        other => panic!("Expected Scalar location, got: {:?}", other),
    }

    // Also test method go-to-definition
    let params = GotoDefinitionParams {
        text_document_position_params: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier { uri: uri.clone() },
            // Place cursor on "retryCount" in `$this->retryCount` on line 8
            position: Position {
                line: 8,
                character: 25,
            },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
    };

    let result = backend.goto_definition(params).await.unwrap();
    assert!(
        result.is_some(),
        "Should resolve $this->retryCount to its declaration"
    );

    match result.unwrap() {
        GotoDefinitionResponse::Scalar(location) => {
            assert_eq!(location.uri, uri);
            // "protected int $retryCount" is on line 3
            assert_eq!(
                location.range.start.line, 3,
                "retryCount property should be found on line 3"
            );
        }
        other => panic!("Expected Scalar location, got: {:?}", other),
    }
}

/// Verify that a class name inside a string literal does NOT cause a false
/// match when using the AST-driven keyword_offset path.
///
/// The old text-search approach would match `class Foo` inside a string
/// like `"class Foo {...}"`, but the AST-based approach only stores offsets
/// for actual declarations.
#[tokio::test]
async fn test_class_name_inside_string_does_not_false_match() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///test_string_false_match.php").unwrap();
    // The string literal on line 3 contains "class Greeter" — this should
    // NOT be treated as a class definition.
    let text = concat!(
        "<?php\n",
        "class Renderer {\n",
        "    public function template(): string {\n",
        "        return \"class Greeter { public function greet() {} }\";\n",
        "    }\n",
        "}\n",
        "\n",
        "class Greeter {\n",
        "    public function greet(): string {\n",
        "        return 'hello';\n",
        "    }\n",
        "}\n",
        "\n",
        "function main(): void {\n",
        "    $g = new Greeter();\n",
        "    $g->greet();\n",
        "}\n",
    );

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text: text.to_string(),
        },
    };
    backend.did_open(open_params).await;

    // The real `class Greeter` is on line 7, NOT line 3 (which is inside
    // a string).  Verify keyword_offset points to line 7.
    let classes = backend
        .get_classes_for_uri(uri.as_str())
        .expect("Should have parsed classes");

    let greeter = classes.iter().find(|c| c.name == "Greeter").unwrap();
    assert!(
        greeter.keyword_offset > 0,
        "Greeter should have keyword_offset > 0"
    );

    // Count newlines before keyword_offset to determine the line
    let kw_off = greeter.keyword_offset as usize;
    let line_num = text[..kw_off].matches('\n').count();
    assert_eq!(
        line_num, 7,
        "keyword_offset should point to line 7 (the real class Greeter), not to the string literal on line 3"
    );

    // Verify go-to-definition for `new Greeter()` on line 14 lands on line 7
    let params = GotoDefinitionParams {
        text_document_position_params: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier { uri: uri.clone() },
            position: Position {
                line: 14,
                character: 14,
            },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
    };

    let result = backend.goto_definition(params).await.unwrap();
    assert!(
        result.is_some(),
        "Should resolve Greeter to its declaration"
    );

    match result.unwrap() {
        GotoDefinitionResponse::Scalar(location) => {
            assert_eq!(location.uri, uri);
            assert_eq!(
                location.range.start.line, 7,
                "Greeter definition should be on line 7, not inside the string on line 3"
            );
        }
        other => panic!("Expected Scalar location, got: {:?}", other),
    }
}

/// Verify that a method name mentioned inside a heredoc does NOT interfere
/// with go-to-definition via the AST offset path.
#[tokio::test]
async fn test_member_name_in_heredoc_does_not_false_match() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///test_heredoc.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Logger {\n",
        "    public function write(string $msg): void {}\n",
        "\n",
        "    public function template(): string {\n",
        "        return <<<EOT\n",
        "function write() { /* not a real declaration */ }\n",
        "EOT;\n",
        "    }\n",
        "}\n",
        "\n",
        "function main(): void {\n",
        "    $logger = new Logger();\n",
        "    $logger->write('test');\n",
        "}\n",
    );

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text: text.to_string(),
        },
    };
    backend.did_open(open_params).await;

    // Click on "write" in `$logger->write('test')` on line 13
    let params = GotoDefinitionParams {
        text_document_position_params: TextDocumentPositionParams {
            text_document: TextDocumentIdentifier { uri: uri.clone() },
            position: Position {
                line: 13,
                character: 15,
            },
        },
        work_done_progress_params: WorkDoneProgressParams::default(),
        partial_result_params: PartialResultParams::default(),
    };

    let result = backend.goto_definition(params).await.unwrap();
    assert!(
        result.is_some(),
        "Should resolve $logger->write() to its declaration"
    );

    match result.unwrap() {
        GotoDefinitionResponse::Scalar(location) => {
            assert_eq!(location.uri, uri);
            // The real `public function write(...)` is on line 2, not line 6
            assert_eq!(
                location.range.start.line, 2,
                "write() should resolve to line 2, not to the heredoc on line 6"
            );
        }
        other => panic!("Expected Scalar location, got: {:?}", other),
    }
}

/// Verify that go-to-definition works for a trait member via $this.
#[tokio::test]
async fn test_trait_member_offset() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///test_trait_offset.php").unwrap();
    let text = concat!(
        "<?php\n",
        "trait Timestampable {\n",
        "    public string $createdAt = '';\n",
        "    public function touch(): void {}\n",
        "}\n",
    );

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text: text.to_string(),
        },
    };
    backend.did_open(open_params).await;

    let classes = backend
        .get_classes_for_uri(uri.as_str())
        .expect("Should have parsed classes");
    let trait_info = classes.iter().find(|c| c.name == "Timestampable").unwrap();

    // Trait keyword_offset should point to "trait"
    let kw = trait_info.keyword_offset as usize;
    assert_eq!(&text[kw..kw + 5], "trait");

    // Method name_offset should point to "touch"
    let method = trait_info
        .methods
        .iter()
        .find(|m| m.name == "touch")
        .unwrap();
    let off = method.name_offset as usize;
    assert_eq!(&text[off..off + 5], "touch");

    // Property name_offset should point to "$createdAt"
    let prop = trait_info
        .properties
        .iter()
        .find(|p| p.name == "createdAt")
        .unwrap();
    let off = prop.name_offset as usize;
    assert_eq!(&text[off..off + 10], "$createdAt");
}

/// Verify that name_offset works correctly for a final class with comments,
/// heredocs, and string mentions of class names — a comprehensive stress test.
#[tokio::test]
async fn test_offsets_with_comments_heredocs_and_strings() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///test_stress.php").unwrap();
    let text = concat!(
        "<?php\n",
        "// class FakeClass { const FAKE = 1; }\n",
        "/* class AnotherFake {} */\n",
        "\n",
        "final class RealClass {\n",
        "    /** The status constant */\n",
        "    const STATUS = 'active';\n",
        "\n",
        "    // $fakeProperty is not real\n",
        "    public string $realProp = 'value';\n",
        "\n",
        "    /**\n",
        "     * Does something.\n",
        "     * function fakeMethod() {}\n",
        "     */\n",
        "    public function realMethod(): string {\n",
        "        $str = 'function realMethod() { const STATUS = 0; }';\n",
        "        return $str;\n",
        "    }\n",
        "}\n",
    );

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text: text.to_string(),
        },
    };
    backend.did_open(open_params).await;

    let classes = backend
        .get_classes_for_uri(uri.as_str())
        .expect("Should have parsed classes");

    // Only RealClass should be extracted — FakeClass and AnotherFake are
    // inside comments and should not appear.
    assert_eq!(classes.len(), 1, "Only RealClass should be parsed");
    let cls = &classes[0];
    assert_eq!(cls.name, "RealClass");

    // keyword_offset should point to "class" (the `final` modifier is
    // separate — the keyword token is `class` itself).
    let kw = cls.keyword_offset as usize;
    assert_eq!(&text[kw..kw + 5], "class");

    // Verify method offset
    let method = cls.methods.iter().find(|m| m.name == "realMethod").unwrap();
    let off = method.name_offset as usize;
    assert_eq!(&text[off..off + "realMethod".len()], "realMethod");

    // Verify constant offset
    let constant = cls.constants.iter().find(|c| c.name == "STATUS").unwrap();
    let off = constant.name_offset as usize;
    assert_eq!(&text[off..off + "STATUS".len()], "STATUS");

    // Verify property offset
    let prop = cls
        .properties
        .iter()
        .find(|p| p.name == "realProp")
        .unwrap();
    let off = prop.name_offset as usize;
    assert_eq!(&text[off..off + "$realProp".len()], "$realProp");
}

/// Verify that backed enum cases have correct name_offset.
#[tokio::test]
async fn test_backed_enum_case_offsets() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///test_backed_enum.php").unwrap();
    let text = concat!(
        "<?php\n",
        "enum Suit: string {\n",
        "    case Hearts = 'H';\n",
        "    case Diamonds = 'D';\n",
        "    case Clubs = 'C';\n",
        "    case Spades = 'S';\n",
        "}\n",
    );

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text: text.to_string(),
        },
    };
    backend.did_open(open_params).await;

    let classes = backend
        .get_classes_for_uri(uri.as_str())
        .expect("Should have parsed classes");
    let cls = &classes[0];
    assert_eq!(cls.name, "Suit");

    let expected_cases = ["Hearts", "Diamonds", "Clubs", "Spades"];
    for case_name in &expected_cases {
        let constant = cls
            .constants
            .iter()
            .find(|c| c.name == *case_name)
            .unwrap_or_else(|| panic!("Should find case '{}'", case_name));

        let off = constant.name_offset as usize;
        assert_eq!(
            &text[off..off + case_name.len()],
            *case_name,
            "Enum case '{}' name_offset should point to its name",
            case_name
        );
    }
}

/// Verify that go-to-definition for self::CONSTANT uses the offset path
/// and resolves to the right line (interface context).
#[tokio::test]
async fn test_interface_constant_definition_via_offset() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///test_iface_const.php").unwrap();
    let text = concat!(
        "<?php\n",
        "interface Configurable {\n",
        "    const VERSION = '1.0';\n",
        "    public function configure(): void;\n",
        "}\n",
        "\n",
        "class App implements Configurable {\n",
        "    public function configure(): void {\n",
        "        echo self::VERSION;\n",
        "    }\n",
        "}\n",
    );

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text: text.to_string(),
        },
    };
    backend.did_open(open_params).await;

    let classes = backend
        .get_classes_for_uri(uri.as_str())
        .expect("Should have parsed classes");

    let iface = classes.iter().find(|c| c.name == "Configurable").unwrap();
    let kw = iface.keyword_offset as usize;
    assert_eq!(&text[kw..kw + 9], "interface");

    let version_const = iface
        .constants
        .iter()
        .find(|c| c.name == "VERSION")
        .unwrap();
    let off = version_const.name_offset as usize;
    assert_eq!(&text[off..off + 7], "VERSION");
}

/// Verify anonymous classes do NOT have keyword_offset set (they use 0).
#[tokio::test]
async fn test_anonymous_class_keyword_offset_is_zero() {
    let backend = create_test_backend();

    let uri = Url::parse("file:///test_anon_class.php").unwrap();
    let text = concat!(
        "<?php\n",
        "class Outer {\n",
        "    public function makeAnon(): object {\n",
        "        return new class {\n",
        "            public function inner(): string {\n",
        "                return 'anon';\n",
        "            }\n",
        "        };\n",
        "    }\n",
        "}\n",
    );

    let open_params = DidOpenTextDocumentParams {
        text_document: TextDocumentItem {
            uri: uri.clone(),
            language_id: "php".to_string(),
            version: 1,
            text: text.to_string(),
        },
    };
    backend.did_open(open_params).await;

    let classes = backend
        .get_classes_for_uri(uri.as_str())
        .expect("Should have parsed classes");

    let outer = classes.iter().find(|c| c.name == "Outer").unwrap();
    assert!(
        outer.keyword_offset > 0,
        "Named class Outer should have keyword_offset > 0"
    );

    let anon = classes
        .iter()
        .find(|c| c.name.starts_with("__anonymous@"))
        .unwrap();
    assert_eq!(
        anon.keyword_offset, 0,
        "Anonymous class should have keyword_offset == 0"
    );

    // But the anonymous class's methods should still have valid name_offsets
    let inner_method = anon.methods.iter().find(|m| m.name == "inner").unwrap();
    assert!(
        inner_method.name_offset > 0,
        "Anonymous class method 'inner' should have name_offset > 0"
    );
    let off = inner_method.name_offset as usize;
    assert_eq!(&text[off..off + 5], "inner");
}