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
//! "Generate constructor" code action.
//!
//! When the cursor is on a property declaration inside a class that has
//! no `__construct` method, this module offers two code actions:
//!
//! 1. **Generate constructor** — inserts a traditional `__construct`
//!    with parameters and `$this->name = $name;` assignments.
//! 2. **Generate promoted constructor** — removes the property
//!    declarations and inserts a constructor with promoted parameters
//!    (`public string $name`, etc.), requiring no body assignments.
//!
//! **Code action kind:** `refactor.rewrite` (both).

use std::collections::HashMap;

use bumpalo::Bump;
use mago_span::HasSpan;
use mago_syntax::ast::class_like::member::ClassLikeMember;
use mago_syntax::ast::class_like::property::{Property, PropertyItem};
use mago_syntax::ast::modifier::Modifier;
use mago_syntax::ast::*;
use tower_lsp::lsp_types::*;

use super::cursor_context::{CursorContext, MemberContext, find_cursor_context};
use super::detect_indent_from_members;
use crate::Backend;
use crate::docblock::{extract_var_type, get_docblock_text_for_node};
use crate::parser::extract_hint_type;
use crate::php_type::PhpType;
use crate::util::offset_to_position;

// ── Data types ──────────────────────────────────────────────────────────────

/// A property that qualifies for inclusion in the generated constructor.
struct QualifyingProperty {
    /// Property name without the `$` prefix.
    name: String,
    /// Structured type hint for the constructor parameter, if available.
    type_hint: Option<PhpType>,
    /// Default value text (e.g. `'active'`, `[]`), if the property has one.
    default_value: Option<String>,
    /// Visibility keyword (`"public"`, `"protected"`, `"private"`).
    /// Falls back to `"public"` when none is declared.
    visibility: &'static str,
    /// Whether the property has the `readonly` modifier.
    is_readonly: bool,
    /// Byte span of the entire property declaration (for deletion in the
    /// promoted variant).  `(start, end)` where `end` is past the trailing
    /// newline so that removing the declaration leaves no blank line.
    declaration_span: (usize, usize),
}

// ── Public API ──────────────────────────────────────────────────────────────

impl Backend {
    /// Collect "Generate constructor" and "Generate promoted constructor"
    /// code actions for the cursor position.
    ///
    /// When the cursor is on a non-static property declaration inside a
    /// class body that has no existing `__construct` method, this produces
    /// up to two code actions that insert a constructor.
    pub(crate) fn collect_generate_constructor_actions(
        &self,
        uri: &str,
        content: &str,
        params: &CodeActionParams,
        out: &mut Vec<CodeActionOrCommand>,
    ) {
        let doc_uri: Url = match uri.parse() {
            Ok(u) => u,
            Err(_) => return,
        };

        let cursor_offset = crate::util::position_to_offset(content, params.range.start);

        let arena = Bump::new();
        let file_id = mago_database::file::FileId::new("input.php");
        let program = mago_syntax::parser::parse_file_content(&arena, file_id, content);

        let ctx = find_cursor_context(&program.statements, cursor_offset);

        let all_members = match &ctx {
            CursorContext::InClassLike {
                member: MemberContext::Property(prop),
                all_members,
                ..
            } => {
                // Only offer on non-static properties — static properties
                // won't be included in the generated constructor.
                if prop.modifiers().iter().any(|m| m.is_static()) {
                    return;
                }
                *all_members
            }
            _ => return,
        };

        // If a __construct already exists, do not offer the action.
        if has_constructor(all_members) {
            return;
        }

        let trivia = program.trivia.as_slice();

        // Collect qualifying properties (non-static).
        let props = collect_qualifying_properties(all_members, content, trivia);

        if props.is_empty() {
            return;
        }

        // Detect indentation from existing class members.
        let indent = detect_indent_from_members(all_members, content);

        // Find the insertion point: after the last property declaration,
        // before any methods or other members.
        let insert_offset = find_insertion_offset(all_members, content);
        let insert_pos = offset_to_position(content, insert_offset);

        // ── Traditional constructor ─────────────────────────────────────
        {
            let constructor_text = build_constructor(&props, &indent);

            let edit = TextEdit {
                range: Range {
                    start: insert_pos,
                    end: insert_pos,
                },
                new_text: constructor_text,
            };

            let mut changes = HashMap::new();
            changes.insert(doc_uri.clone(), vec![edit]);

            out.push(CodeActionOrCommand::CodeAction(CodeAction {
                title: "Generate constructor".to_string(),
                kind: Some(CodeActionKind::REFACTOR_REWRITE),
                diagnostics: None,
                edit: Some(WorkspaceEdit {
                    changes: Some(changes),
                    document_changes: None,
                    change_annotations: None,
                }),
                command: None,
                is_preferred: Some(false),
                disabled: None,
                data: None,
            }));
        }

        // ── Promoted constructor ────────────────────────────────────────
        {
            let constructor_text = build_promoted_constructor(&props, &indent);

            // Build the list of edits: one deletion per property
            // declaration, plus one insertion for the constructor.
            // Sort deletions back-to-front so byte offsets stay valid.
            let mut edits: Vec<TextEdit> = Vec::new();

            // Delete each qualifying property declaration.
            for prop in props.iter().rev() {
                let start = offset_to_position(content, prop.declaration_span.0);
                let end = offset_to_position(content, prop.declaration_span.1);
                edits.push(TextEdit {
                    range: Range { start, end },
                    new_text: String::new(),
                });
            }

            // Insert the constructor after all property declarations
            // (same position as the traditional constructor).  This
            // keeps static properties above the constructor rather
            // than displacing them below it.
            edits.push(TextEdit {
                range: Range {
                    start: insert_pos,
                    end: insert_pos,
                },
                new_text: constructor_text,
            });

            let mut changes = HashMap::new();
            changes.insert(doc_uri, edits);

            out.push(CodeActionOrCommand::CodeAction(CodeAction {
                title: "Generate promoted constructor".to_string(),
                kind: Some(CodeActionKind::REFACTOR_REWRITE),
                diagnostics: None,
                edit: Some(WorkspaceEdit {
                    changes: Some(changes),
                    document_changes: None,
                    change_annotations: None,
                }),
                command: None,
                is_preferred: Some(false),
                disabled: None,
                data: None,
            }));
        }
    }
}

// ── Helpers ─────────────────────────────────────────────────────────────────

/// Check whether the class already has a `__construct` method.
fn has_constructor<'a>(members: &Sequence<'a, ClassLikeMember<'a>>) -> bool {
    members.iter().any(|m| {
        if let ClassLikeMember::Method(method) = m {
            method.name.value.eq_ignore_ascii_case("__construct")
        } else {
            false
        }
    })
}

/// Collect all non-static properties from the class members,
/// in declaration order.  Readonly properties are included because they
/// *must* be initialized in the constructor.
fn collect_qualifying_properties<'a>(
    members: &Sequence<'a, ClassLikeMember<'a>>,
    content: &str,
    trivia: &[Trivia<'a>],
) -> Vec<QualifyingProperty> {
    let mut result = Vec::new();

    for member in members.iter() {
        let member_prop = match member {
            ClassLikeMember::Property(p) => p,
            _ => continue,
        };
        let plain = match member_prop {
            Property::Plain(p) => p,
            _ => continue,
        };

        // Skip static properties.
        if is_static(member_prop) {
            continue;
        }

        // Extract visibility and readonly from modifiers.
        let visibility = extract_visibility(plain.modifiers.iter());
        let is_readonly = has_readonly(plain.modifiers.iter());

        // Extract the native type hint for the property.
        let native_hint = plain.hint.as_ref().map(|h| extract_hint_type(h));

        // Try to get a docblock @var type if there's no native hint
        // or if we want to use it as a fallback.
        let docblock_type =
            get_docblock_text_for_node(trivia, content, plain).and_then(extract_var_type);

        // Compute the declaration span for deletion (promoted variant).
        // Start from the beginning of the line containing the property
        // (to include leading whitespace), end past the trailing newline.
        let prop_span = member.span();
        let decl_start = find_line_start(content, prop_span.start.offset as usize);
        let decl_end = find_line_end(content, prop_span.end.offset as usize);

        for item in plain.items.iter() {
            let var_name = item.variable().name;
            let bare_name = var_name.strip_prefix('$').unwrap_or(var_name);

            // Determine the type hint for the parameter.
            let type_hint = if let Some(ref hint) = native_hint {
                Some(hint.clone())
            } else if let Some(ref doc_type) = docblock_type {
                // Only use docblock type if it's a single, non-compound type.
                // Skip complex types like `array{key: value}` or `int|string`.
                if is_simple_php_type(doc_type) {
                    Some(doc_type.clone())
                } else {
                    None::<PhpType>
                }
            } else {
                None
            };

            // Extract default value if the property has one.
            let default_value = if let PropertyItem::Concrete(concrete) = item {
                let span = concrete.value.span();
                let start = span.start.offset as usize;
                let end = span.end.offset as usize;
                content.get(start..end).map(|s| s.trim().to_string())
            } else {
                None
            };

            result.push(QualifyingProperty {
                name: bare_name.to_string(),
                type_hint,
                default_value,
                visibility,
                is_readonly,
                declaration_span: (decl_start, decl_end),
            });
        }
    }

    result
}

/// Check whether a [`PhpType`] is a simple (non-compound) type suitable
/// for use as a parameter type hint.
///
/// Accepts `Named` and `Nullable(Named)` types; rejects unions,
/// intersections, array shapes, generics, etc.
fn is_simple_php_type(ty: &PhpType) -> bool {
    match ty {
        PhpType::Named(_) => true,
        PhpType::Nullable(inner) => matches!(inner.as_ref(), PhpType::Named(_)),
        _ => false,
    }
}

/// Find the byte offset where the constructor should be inserted.
///
/// The constructor is inserted after the last property declaration and
/// before any methods or other non-property members.  If there are no
/// properties before any methods, it's inserted after the class opening
/// brace.
fn find_insertion_offset<'a>(members: &Sequence<'a, ClassLikeMember<'a>>, content: &str) -> usize {
    // Find the end of the last property declaration.
    let mut last_property_end: Option<u32> = None;
    let mut first_non_property_start: Option<u32> = None;

    for member in members.iter() {
        match member {
            ClassLikeMember::Property(_) => {
                let span = member.span();
                last_property_end = Some(span.end.offset);
            }
            ClassLikeMember::Method(_)
            | ClassLikeMember::Constant(_)
            | ClassLikeMember::TraitUse(_)
            | ClassLikeMember::EnumCase(_) => {
                if first_non_property_start.is_none() && last_property_end.is_some() {
                    first_non_property_start = Some(member.span().start.offset);
                }
                // If we haven't seen any properties yet, record this as
                // the first non-property so we know where to insert
                // relative to the class opening brace.
                if last_property_end.is_none() && first_non_property_start.is_none() {
                    first_non_property_start = Some(member.span().start.offset);
                }
            }
        }
    }

    if let Some(end) = last_property_end {
        // Insert after the last property.  Find the end of the line
        // containing the property's semicolon.
        find_line_end(content, end as usize)
    } else {
        // No properties at all — shouldn't happen because we check for
        // qualifying properties, but handle gracefully.
        0
    }
}

/// Find the start of the line containing the given offset.
fn find_line_start(content: &str, offset: usize) -> usize {
    content[..offset]
        .rfind('\n')
        .map(|pos| pos + 1)
        .unwrap_or(0)
}

/// Find the end of the line at or after the given offset (past the newline).
fn find_line_end(content: &str, offset: usize) -> usize {
    if let Some(nl) = content[offset..].find('\n') {
        offset + nl + 1
    } else {
        content.len()
    }
}

/// Build the traditional constructor source text from the qualifying
/// properties.  Each property becomes a parameter with matching type
/// hint and an assignment in the body.
fn build_constructor(props: &[QualifyingProperty], indent: &str) -> String {
    let mut result = String::new();

    result.push('\n');
    result.push_str(indent);
    result.push_str("public function __construct(");

    // Build parameter list.
    // Parameters with default values must come after required parameters.
    // We preserve declaration order but PHP requires defaults at the end,
    // so we separate them.
    let mut required_params = Vec::new();
    let mut optional_params = Vec::new();

    for prop in props {
        let mut param = String::new();

        if let Some(ref hint) = prop.type_hint {
            param.push_str(&hint.to_string());
            param.push(' ');
        }

        param.push('$');
        param.push_str(&prop.name);

        if let Some(ref default) = prop.default_value {
            param.push_str(" = ");
            param.push_str(default);
            optional_params.push(param);
        } else {
            required_params.push(param);
        }
    }

    let all_params: Vec<&str> = required_params
        .iter()
        .chain(optional_params.iter())
        .map(|s| s.as_str())
        .collect();

    result.push_str(&all_params.join(", "));
    result.push_str(")\n");
    result.push_str(indent);
    result.push_str("{\n");

    // Build assignment body — use declaration order for assignments,
    // not the reordered parameter order.
    for prop in props {
        result.push_str(indent);
        result.push_str(indent);
        result.push_str("$this->");
        result.push_str(&prop.name);
        result.push_str(" = $");
        result.push_str(&prop.name);
        result.push_str(";\n");
    }

    result.push_str(indent);
    result.push_str("}\n");

    result
}

/// Build the promoted constructor source text.  Each property becomes a
/// promoted parameter (`visibility [readonly] type $name [= default]`)
/// and the property declarations are removed by the caller.
fn build_promoted_constructor(props: &[QualifyingProperty], indent: &str) -> String {
    let mut result = String::new();

    result.push('\n');
    result.push_str(indent);
    result.push_str("public function __construct(\n");

    // Parameters with default values must come after required parameters.
    let mut required_params = Vec::new();
    let mut optional_params = Vec::new();

    for prop in props {
        let mut param = String::new();
        param.push_str(indent);
        param.push_str(indent);

        // Visibility modifier.
        param.push_str(prop.visibility);

        // Readonly modifier.
        if prop.is_readonly {
            param.push_str(" readonly");
        }

        // Type hint.
        if let Some(ref hint) = prop.type_hint {
            param.push(' ');
            param.push_str(&hint.to_string());
        }

        param.push_str(" $");
        param.push_str(&prop.name);

        if let Some(ref default) = prop.default_value {
            param.push_str(" = ");
            param.push_str(default);
            optional_params.push(param);
        } else {
            required_params.push(param);
        }
    }

    let all_params: Vec<&str> = required_params
        .iter()
        .chain(optional_params.iter())
        .map(|s| s.as_str())
        .collect();

    result.push_str(&all_params.join(",\n"));
    result.push_str(",\n");

    result.push_str(indent);
    result.push_str(") {}\n");

    result
}

/// Extract the visibility keyword from a modifier list, defaulting to
/// `"public"` if none is present.
fn extract_visibility<'a>(modifiers: impl Iterator<Item = &'a Modifier<'a>>) -> &'static str {
    for m in modifiers {
        match m {
            Modifier::Public(_) => return "public",
            Modifier::Protected(_) => return "protected",
            Modifier::Private(_) => return "private",
            _ => continue,
        }
    }
    "public"
}

/// Check if the modifier list includes `readonly`.
fn has_readonly<'a>(modifiers: impl Iterator<Item = &'a Modifier<'a>>) -> bool {
    modifiers
        .into_iter()
        .any(|m| matches!(m, Modifier::Readonly(_)))
}

/// Check if any modifier is `static`.
fn is_static(property: &Property<'_>) -> bool {
    property.modifiers().iter().any(|m| m.is_static())
}

// ── Tests ───────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;

    // ── is_simple_type ──────────────────────────────────────────────────

    #[test]
    fn simple_type_accepts_basic() {
        assert!(is_simple_php_type(&PhpType::parse("string")));
        assert!(is_simple_php_type(&PhpType::parse("int")));
        assert!(is_simple_php_type(&PhpType::parse("array")));
        assert!(is_simple_php_type(&PhpType::parse("bool")));
    }

    #[test]
    fn simple_type_accepts_nullable() {
        assert!(is_simple_php_type(&PhpType::parse("?string")));
        assert!(is_simple_php_type(&PhpType::parse("?Foo")));
    }

    #[test]
    fn simple_type_accepts_fqn() {
        assert!(is_simple_php_type(&PhpType::parse("App\\Models\\User")));
        assert!(is_simple_php_type(&PhpType::parse("?App\\Models\\User")));
    }

    #[test]
    fn simple_type_rejects_union() {
        assert!(!is_simple_php_type(&PhpType::parse("int|string")));
    }

    #[test]
    fn simple_type_rejects_intersection() {
        assert!(!is_simple_php_type(&PhpType::parse("Foo&Bar")));
    }

    #[test]
    fn simple_type_rejects_array_shape() {
        assert!(!is_simple_php_type(&PhpType::parse("array{name: string}")));
    }

    #[test]
    fn simple_type_rejects_generic() {
        assert!(!is_simple_php_type(&PhpType::parse("Collection<User>")));
    }

    #[test]
    fn simple_type_rejects_empty() {
        assert!(!is_simple_php_type(&PhpType::parse("")));
    }

    // ── build_constructor ───────────────────────────────────────────────

    fn prop(name: &str, type_hint: Option<&str>, default: Option<&str>) -> QualifyingProperty {
        QualifyingProperty {
            name: name.to_string(),
            type_hint: type_hint.map(PhpType::parse),
            default_value: default.map(|s| s.to_string()),
            visibility: "public",
            is_readonly: false,
            declaration_span: (0, 0),
        }
    }

    #[test]
    fn builds_basic_constructor() {
        let props = vec![
            prop("name", Some("string"), None),
            prop("age", Some("int"), None),
        ];

        let result = build_constructor(&props, "    ");
        assert!(result.contains("public function __construct(string $name, int $age)"));
        assert!(result.contains("$this->name = $name;"));
        assert!(result.contains("$this->age = $age;"));
    }

    #[test]
    fn builds_constructor_with_defaults() {
        let props = vec![
            prop("name", Some("string"), None),
            prop("status", Some("string"), Some("'active'")),
        ];

        let result = build_constructor(&props, "    ");
        assert!(
            result.contains("string $name, string $status = 'active'"),
            "required params before optional: {result}"
        );
    }

    #[test]
    fn defaults_reordered_before_required() {
        let props = vec![
            prop("status", Some("string"), Some("'draft'")),
            prop("name", Some("string"), None),
        ];

        let result = build_constructor(&props, "    ");
        // Required parameter $name should come before optional $status.
        let name_pos = result.find("$name").unwrap();
        let status_pos = result.find("$status").unwrap();
        assert!(
            name_pos < status_pos,
            "required params should come first: {result}"
        );
    }

    #[test]
    fn builds_constructor_without_type_hints() {
        let props = vec![prop("data", None, None)];

        let result = build_constructor(&props, "    ");
        assert!(
            result.contains("($data)"),
            "untyped param should not have type: {result}"
        );
    }

    #[test]
    fn builds_constructor_with_nullable_type() {
        let props = vec![prop("label", Some("?string"), None)];

        let result = build_constructor(&props, "    ");
        assert!(
            result.contains("?string $label"),
            "nullable type preserved: {result}"
        );
    }

    #[test]
    fn builds_constructor_with_union_type() {
        let props = vec![prop("id", Some("int|string"), None)];

        let result = build_constructor(&props, "    ");
        assert!(
            result.contains("int|string $id"),
            "union type preserved: {result}"
        );
    }

    #[test]
    fn respects_tab_indentation() {
        let props = vec![prop("name", Some("string"), None)];

        let result = build_constructor(&props, "\t");
        assert!(
            result.contains("\tpublic function __construct("),
            "should use tab indent: {result}"
        );
        assert!(
            result.contains("\t\t$this->name = $name;"),
            "body should use double tab: {result}"
        );
    }

    // ── build_promoted_constructor ───────────────────────────────────────

    fn pprop(
        name: &str,
        type_hint: Option<&str>,
        default: Option<&str>,
        visibility: &'static str,
        is_readonly: bool,
    ) -> QualifyingProperty {
        QualifyingProperty {
            name: name.to_string(),
            type_hint: type_hint.map(PhpType::parse),
            default_value: default.map(|s| s.to_string()),
            visibility,
            is_readonly,
            declaration_span: (0, 0),
        }
    }

    #[test]
    fn builds_promoted_constructor_basic() {
        let props = vec![
            pprop("name", Some("string"), None, "public", false),
            pprop("age", Some("int"), None, "private", false),
        ];

        let result = build_promoted_constructor(&props, "    ");
        assert!(
            result.contains("public string $name"),
            "should have public visibility: {result}"
        );
        assert!(
            result.contains("private int $age"),
            "should have private visibility: {result}"
        );
        assert!(result.contains(") {}"), "should have empty body: {result}");
        assert!(
            !result.contains("$this->"),
            "should not have assignments: {result}"
        );
    }

    #[test]
    fn builds_promoted_constructor_with_readonly() {
        let props = vec![pprop("id", Some("string"), None, "public", true)];

        let result = build_promoted_constructor(&props, "    ");
        assert!(
            result.contains("public readonly string $id"),
            "should have readonly modifier: {result}"
        );
    }

    #[test]
    fn builds_promoted_constructor_with_defaults() {
        let props = vec![
            pprop("name", Some("string"), None, "public", false),
            pprop(
                "status",
                Some("string"),
                Some("'active'"),
                "protected",
                false,
            ),
        ];

        let result = build_promoted_constructor(&props, "    ");
        // Required comes before optional.
        let name_pos = result.find("$name").unwrap();
        let status_pos = result.find("$status").unwrap();
        assert!(name_pos < status_pos, "required before optional: {result}");
        assert!(
            result.contains("protected string $status = 'active'"),
            "should carry over default: {result}"
        );
    }

    #[test]
    fn builds_promoted_constructor_trailing_comma() {
        let props = vec![pprop("name", Some("string"), None, "public", false)];

        let result = build_promoted_constructor(&props, "    ");
        // Should have a trailing comma after the last parameter.
        assert!(
            result.contains("$name,\n"),
            "should have trailing comma: {result}"
        );
    }

    #[test]
    fn builds_promoted_constructor_multiline() {
        let props = vec![
            pprop("name", Some("string"), None, "public", false),
            pprop("age", Some("int"), None, "private", false),
        ];

        let result = build_promoted_constructor(&props, "    ");
        // Each parameter should be on its own line.
        assert!(
            result.contains("        public string $name,\n        private int $age,\n"),
            "parameters should be on separate lines: {result}"
        );
    }

    #[test]
    fn promoted_constructor_tabs() {
        let props = vec![pprop("name", Some("string"), None, "public", false)];

        let result = build_promoted_constructor(&props, "\t");
        assert!(
            result.contains("\tpublic function __construct(\n"),
            "should use tab indent: {result}"
        );
        assert!(
            result.contains("\t\tpublic string $name,\n"),
            "params should use double tab: {result}"
        );
    }

    #[test]
    fn promoted_constructor_no_type_hint() {
        let props = vec![pprop("data", None, None, "private", false)];

        let result = build_promoted_constructor(&props, "    ");
        assert!(
            result.contains("private $data"),
            "untyped param should have no type hint: {result}"
        );
    }

    // ── has_constructor ─────────────────────────────────────────────────

    #[test]
    fn detects_existing_constructor() {
        let arena = Box::leak(Box::new(Bump::new()));
        let file_id = mago_database::file::FileId::new("input.php");
        let php = "<?php\nclass Foo {\n    public function __construct() {}\n}";
        let program = mago_syntax::parser::parse_file_content(arena, file_id, php);

        // Find the class and check for constructor.
        let ctx = find_cursor_context(&program.statements, 20);
        if let CursorContext::InClassLike { all_members, .. } = &ctx {
            assert!(has_constructor(all_members));
        } else {
            panic!("should find class");
        }
    }

    #[test]
    fn detects_no_constructor() {
        let arena = Box::leak(Box::new(Bump::new()));
        let file_id = mago_database::file::FileId::new("input.php");
        let php = "<?php\nclass Foo {\n    public string $name;\n}";
        let program = mago_syntax::parser::parse_file_content(arena, file_id, php);

        let ctx = find_cursor_context(&program.statements, 20);
        if let CursorContext::InClassLike { all_members, .. } = &ctx {
            assert!(!has_constructor(all_members));
        } else {
            panic!("should find class");
        }
    }

    #[test]
    fn detects_constructor_case_insensitive() {
        let arena = Box::leak(Box::new(Bump::new()));
        let file_id = mago_database::file::FileId::new("input.php");
        let php = "<?php\nclass Foo {\n    public function __CONSTRUCT() {}\n}";
        let program = mago_syntax::parser::parse_file_content(arena, file_id, php);

        let ctx = find_cursor_context(&program.statements, 20);
        if let CursorContext::InClassLike { all_members, .. } = &ctx {
            assert!(has_constructor(all_members));
        } else {
            panic!("should find class");
        }
    }

    // ── collect_qualifying_properties ───────────────────────────────────

    #[test]
    fn collects_non_static() {
        let arena = Box::leak(Box::new(Bump::new()));
        let file_id = mago_database::file::FileId::new("input.php");
        let php = "<?php\nclass Foo {\n    public string $name;\n    private int $age;\n}";
        let program = mago_syntax::parser::parse_file_content(arena, file_id, php);

        let ctx = find_cursor_context(&program.statements, 20);
        if let CursorContext::InClassLike { all_members, .. } = &ctx {
            let props = collect_qualifying_properties(all_members, php, program.trivia.as_slice());
            assert_eq!(props.len(), 2);
            assert_eq!(props[0].name, "name");
            assert_eq!(
                props[0]
                    .type_hint
                    .as_ref()
                    .map(|t| t.to_string())
                    .as_deref(),
                Some("string")
            );
            assert_eq!(props[0].visibility, "public");
            assert_eq!(props[1].name, "age");
            assert_eq!(
                props[1]
                    .type_hint
                    .as_ref()
                    .map(|t| t.to_string())
                    .as_deref(),
                Some("int")
            );
            assert_eq!(props[1].visibility, "private");
        } else {
            panic!("should find class");
        }
    }

    #[test]
    fn skips_static_properties() {
        let arena = Box::leak(Box::new(Bump::new()));
        let file_id = mago_database::file::FileId::new("input.php");
        let php = "<?php\nclass Foo {\n    public string $name;\n    public static int $count;\n}";
        let program = mago_syntax::parser::parse_file_content(arena, file_id, php);

        let ctx = find_cursor_context(&program.statements, 20);
        if let CursorContext::InClassLike { all_members, .. } = &ctx {
            let props = collect_qualifying_properties(all_members, php, program.trivia.as_slice());
            assert_eq!(props.len(), 1);
            assert_eq!(props[0].name, "name");
        } else {
            panic!("should find class");
        }
    }

    #[test]
    fn includes_readonly_properties() {
        let arena = Box::leak(Box::new(Bump::new()));
        let file_id = mago_database::file::FileId::new("input.php");
        let php = "<?php\nclass Foo {\n    public string $name;\n    public readonly int $id;\n}";
        let program = mago_syntax::parser::parse_file_content(arena, file_id, php);

        let ctx = find_cursor_context(&program.statements, 20);
        if let CursorContext::InClassLike { all_members, .. } = &ctx {
            let props = collect_qualifying_properties(all_members, php, program.trivia.as_slice());
            assert_eq!(props.len(), 2);
            assert_eq!(props[0].name, "name");
            assert!(!props[0].is_readonly);
            assert_eq!(props[1].name, "id");
            assert_eq!(
                props[1]
                    .type_hint
                    .as_ref()
                    .map(|t| t.to_string())
                    .as_deref(),
                Some("int")
            );
            assert!(props[1].is_readonly);
        } else {
            panic!("should find class");
        }
    }

    #[test]
    fn extracts_default_values() {
        let arena = Box::leak(Box::new(Bump::new()));
        let file_id = mago_database::file::FileId::new("input.php");
        let php = "<?php\nclass Foo {\n    public string $status = 'active';\n}";
        let program = mago_syntax::parser::parse_file_content(arena, file_id, php);

        let ctx = find_cursor_context(&program.statements, 20);
        if let CursorContext::InClassLike { all_members, .. } = &ctx {
            let props = collect_qualifying_properties(all_members, php, program.trivia.as_slice());
            assert_eq!(props.len(), 1);
            assert_eq!(props[0].name, "status");
            assert_eq!(props[0].default_value.as_deref(), Some("'active'"));
        } else {
            panic!("should find class");
        }
    }

    #[test]
    fn extracts_docblock_type_when_no_native_hint() {
        let arena = Box::leak(Box::new(Bump::new()));
        let file_id = mago_database::file::FileId::new("input.php");
        let php = "<?php\nclass Foo {\n    /** @var string */\n    public $name;\n}";
        let program = mago_syntax::parser::parse_file_content(arena, file_id, php);

        let ctx = find_cursor_context(&program.statements, 20);
        if let CursorContext::InClassLike { all_members, .. } = &ctx {
            let props = collect_qualifying_properties(all_members, php, program.trivia.as_slice());
            assert_eq!(props.len(), 1);
            assert_eq!(props[0].name, "name");
            assert_eq!(
                props[0]
                    .type_hint
                    .as_ref()
                    .map(|t| t.to_string())
                    .as_deref(),
                Some("string")
            );
        } else {
            panic!("should find class");
        }
    }

    #[test]
    fn skips_compound_docblock_type() {
        let arena = Box::leak(Box::new(Bump::new()));
        let file_id = mago_database::file::FileId::new("input.php");
        let php = "<?php\nclass Foo {\n    /** @var int|string */\n    public $id;\n}";
        let program = mago_syntax::parser::parse_file_content(arena, file_id, php);

        let ctx = find_cursor_context(&program.statements, 20);
        if let CursorContext::InClassLike { all_members, .. } = &ctx {
            let props = collect_qualifying_properties(all_members, php, program.trivia.as_slice());
            assert_eq!(props.len(), 1);
            assert_eq!(props[0].name, "id");
            assert!(
                props[0].type_hint.is_none(),
                "compound docblock type should be skipped"
            );
        } else {
            panic!("should find class");
        }
    }

    #[test]
    fn preserves_nullable_native_type() {
        let arena = Box::leak(Box::new(Bump::new()));
        let file_id = mago_database::file::FileId::new("input.php");
        let php = "<?php\nclass Foo {\n    public ?string $name;\n}";
        let program = mago_syntax::parser::parse_file_content(arena, file_id, php);

        let ctx = find_cursor_context(&program.statements, 20);
        if let CursorContext::InClassLike { all_members, .. } = &ctx {
            let props = collect_qualifying_properties(all_members, php, program.trivia.as_slice());
            assert_eq!(props.len(), 1);
            assert_eq!(
                props[0]
                    .type_hint
                    .as_ref()
                    .map(|t| t.to_string())
                    .as_deref(),
                Some("?string")
            );
        } else {
            panic!("should find class");
        }
    }

    #[test]
    fn preserves_union_native_type() {
        let arena = Box::leak(Box::new(Bump::new()));
        let file_id = mago_database::file::FileId::new("input.php");
        let php = "<?php\nclass Foo {\n    public int|string $id;\n}";
        let program = mago_syntax::parser::parse_file_content(arena, file_id, php);

        let ctx = find_cursor_context(&program.statements, 20);
        if let CursorContext::InClassLike { all_members, .. } = &ctx {
            let props = collect_qualifying_properties(all_members, php, program.trivia.as_slice());
            assert_eq!(props.len(), 1);
            assert_eq!(
                props[0]
                    .type_hint
                    .as_ref()
                    .map(|t| t.to_string())
                    .as_deref(),
                Some("int|string")
            );
        } else {
            panic!("should find class");
        }
    }

    #[test]
    fn captures_declaration_span() {
        let arena = Box::leak(Box::new(Bump::new()));
        let file_id = mago_database::file::FileId::new("input.php");
        let php = "<?php\nclass Foo {\n    public string $name;\n}\n";
        let program = mago_syntax::parser::parse_file_content(arena, file_id, php);

        let ctx = find_cursor_context(&program.statements, 20);
        if let CursorContext::InClassLike { all_members, .. } = &ctx {
            let props = collect_qualifying_properties(all_members, php, program.trivia.as_slice());
            assert_eq!(props.len(), 1);
            let (start, end) = props[0].declaration_span;
            let deleted = &php[start..end];
            assert!(
                deleted.contains("public string $name;"),
                "span should cover property declaration: {deleted:?}"
            );
        } else {
            panic!("should find class");
        }
    }
}