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
use super::*;

/// Backward-compatible helper for tests: returns the position **after**
/// the last existing `use` statement (or the appropriate fallback).
fn find_use_insert_position(content: &str) -> Position {
    let info = analyze_use_block(content);
    if info.existing.is_empty() {
        Position {
            line: info.fallback_line,
            character: 0,
        }
    } else {
        let last_line = info.existing.last().expect("non-empty checked above").0;
        Position {
            line: last_line + 1,
            character: 0,
        }
    }
}

// ── use_import_conflicts ────────────────────────────────────────

#[test]
fn conflict_when_short_name_taken_by_different_fqn() {
    let mut use_map = HashMap::new();
    use_map.insert("Exception".to_string(), "Cassandra\\Exception".to_string());

    assert!(use_import_conflicts("App\\Exception", &use_map));
}

#[test]
fn no_conflict_when_same_fqn() {
    let mut use_map = HashMap::new();
    use_map.insert("Exception".to_string(), "App\\Exception".to_string());

    assert!(!use_import_conflicts("App\\Exception", &use_map));
}

#[test]
fn no_conflict_when_different_short_name() {
    let mut use_map = HashMap::new();
    use_map.insert("Exception".to_string(), "Cassandra\\Exception".to_string());

    assert!(!use_import_conflicts("App\\Collection", &use_map));
}

#[test]
fn conflict_is_case_insensitive() {
    let mut use_map = HashMap::new();
    use_map.insert("exception".to_string(), "Cassandra\\Exception".to_string());

    assert!(use_import_conflicts("App\\Exception", &use_map));
}

#[test]
fn no_conflict_when_use_map_empty() {
    let use_map = HashMap::new();

    assert!(!use_import_conflicts("App\\Exception", &use_map));
}

#[test]
fn conflict_with_global_class_fqn() {
    // File has `use Cassandra\Exception;`, importing the global `Exception`
    // (no namespace) should conflict.
    let mut use_map = HashMap::new();
    use_map.insert("Exception".to_string(), "Cassandra\\Exception".to_string());

    assert!(use_import_conflicts("Exception", &use_map));
}

#[test]
fn no_conflict_same_fqn_case_insensitive() {
    let mut use_map = HashMap::new();
    use_map.insert("Exception".to_string(), "app\\exception".to_string());

    assert!(!use_import_conflicts("App\\Exception", &use_map));
}

// ── Leading-segment collision ───────────────────────────────────

#[test]
fn conflict_when_first_segment_matches_alias() {
    // `use Stringable as pq;` — importing `pq\Exception` would be
    // confusing because `pq\Exception` in code resolves through the
    // alias to `Stringable\Exception`.
    let mut use_map = HashMap::new();
    use_map.insert("pq".to_string(), "Stringable".to_string());

    assert!(use_import_conflicts("pq\\Exception", &use_map));
}

#[test]
fn conflict_when_first_segment_matches_alias_case_insensitive() {
    let mut use_map = HashMap::new();
    use_map.insert("PQ".to_string(), "Stringable".to_string());

    assert!(use_import_conflicts("pq\\Exception", &use_map));
}

#[test]
fn no_leading_segment_conflict_for_single_segment_fqn() {
    // `use Stringable;` should not conflict with importing global
    // class `Stringable` — single-segment FQNs skip the leading-
    // segment check to avoid a false positive.
    let mut use_map = HashMap::new();
    use_map.insert("Stringable".to_string(), "Stringable".to_string());

    assert!(!use_import_conflicts("Stringable", &use_map));
}

#[test]
fn leading_segment_conflict_with_deep_namespace() {
    // `use Something as App;` blocks `App\Models\User` because `App`
    // in code would resolve through the alias.
    let mut use_map = HashMap::new();
    use_map.insert("App".to_string(), "Something".to_string());

    assert!(use_import_conflicts("App\\Models\\User", &use_map));
}

#[test]
fn no_leading_segment_conflict_when_no_alias_matches() {
    let mut use_map = HashMap::new();
    use_map.insert("Exception".to_string(), "Cassandra\\Exception".to_string());

    // First segment is `App`, alias is `Exception` — no match.
    assert!(!use_import_conflicts("App\\Collection", &use_map));
}

// ── extract_use_sort_key ────────────────────────────────────────

#[test]
fn sort_key_simple_use() {
    assert_eq!(
        extract_use_sort_key("use Foo\\Bar;"),
        Some("foo\\bar".to_string())
    );
}

#[test]
fn sort_key_with_alias() {
    assert_eq!(
        extract_use_sort_key("use Foo\\Bar as Baz;"),
        Some("foo\\bar".to_string())
    );
}

#[test]
fn sort_key_grouped_use() {
    assert_eq!(
        extract_use_sort_key("use Foo\\{Bar, Baz};"),
        Some("foo\\".to_string())
    );
}

#[test]
fn sort_key_function_use() {
    assert_eq!(
        extract_use_sort_key("use function Foo\\bar;"),
        Some("function foo\\bar".to_string())
    );
}

#[test]
fn sort_key_const_use() {
    assert_eq!(
        extract_use_sort_key("use const Foo\\BAR;"),
        Some("const foo\\bar".to_string())
    );
}

#[test]
fn sort_key_leading_backslash_stripped() {
    assert_eq!(
        extract_use_sort_key("use \\Foo\\Bar;"),
        Some("foo\\bar".to_string())
    );
}

#[test]
fn sort_key_not_a_use_statement() {
    assert_eq!(extract_use_sort_key("class Foo {}"), None);
}

#[test]
fn sort_key_closure_use_ignored() {
    assert_eq!(extract_use_sort_key("use ($var)"), None);
}

// ── analyze_use_block ───────────────────────────────────────────

#[test]
fn collects_existing_uses_with_sort_keys() {
    let content = "<?php\nnamespace App;\nuse Foo\\Bar;\nuse Baz\\Qux;\n\nclass X {}\n";
    let info = analyze_use_block(content);
    assert_eq!(info.existing.len(), 2);
    assert_eq!(info.existing[0], (2, "foo\\bar".to_string()));
    assert_eq!(info.existing[1], (3, "baz\\qux".to_string()));
}

#[test]
fn fallback_after_namespace_when_no_use() {
    let content = "<?php\nnamespace App;\n\nclass X {}\n";
    let info = analyze_use_block(content);
    assert!(info.existing.is_empty());
    assert_eq!(info.fallback_line, 2);
}

#[test]
fn fallback_after_php_open_tag_when_no_namespace() {
    let content = "<?php\n\nclass X {}\n";
    let info = analyze_use_block(content);
    assert!(info.existing.is_empty());
    assert_eq!(info.fallback_line, 1);
}

#[test]
fn trait_use_inside_class_not_collected() {
    let content = "<?php\nnamespace App;\nuse Foo\\Bar;\n\nclass X {\n    use SomeTrait;\n}\n";
    let info = analyze_use_block(content);
    // Only the top-level `use Foo\Bar;` should be collected.
    assert_eq!(info.existing.len(), 1);
    assert_eq!(info.existing[0], (2, "foo\\bar".to_string()));
}

// ── UseBlockInfo::insert_position_for ───────────────────────────

#[test]
fn insert_alphabetically_before_first() {
    // Existing: App\Zoo (line 2). Inserting App\Alpha should go before it.
    let info = UseBlockInfo {
        existing: vec![(2, "app\\zoo".to_string())],
        fallback_line: 1,
    };
    assert_eq!(
        info.insert_position_for("App\\Alpha"),
        Position {
            line: 2,
            character: 0,
        }
    );
}

#[test]
fn insert_alphabetically_after_last() {
    // Existing: App\Alpha (line 2). Inserting App\Zoo should go after it.
    let info = UseBlockInfo {
        existing: vec![(2, "app\\alpha".to_string())],
        fallback_line: 1,
    };
    assert_eq!(
        info.insert_position_for("App\\Zoo"),
        Position {
            line: 3,
            character: 0,
        }
    );
}

#[test]
fn insert_alphabetically_in_the_middle() {
    // Existing: App\Alpha (line 2), App\Zoo (line 3).
    // Inserting App\Middle should go between them.
    let info = UseBlockInfo {
        existing: vec![(2, "app\\alpha".to_string()), (3, "app\\zoo".to_string())],
        fallback_line: 1,
    };
    assert_eq!(
        info.insert_position_for("App\\Middle"),
        Position {
            line: 3,
            character: 0,
        }
    );
}

#[test]
fn insert_uses_fallback_when_no_existing() {
    let info = UseBlockInfo {
        existing: vec![],
        fallback_line: 2,
    };
    assert_eq!(
        info.insert_position_for("App\\Foo"),
        Position {
            line: 2,
            character: 0,
        }
    );
}

#[test]
fn insert_case_insensitive_comparison() {
    // Existing: app\alpha (line 2), app\zoo (line 3).
    // Inserting App\Middle (mixed case) should still land between them.
    let info = UseBlockInfo {
        existing: vec![(2, "app\\alpha".to_string()), (3, "app\\zoo".to_string())],
        fallback_line: 1,
    };
    assert_eq!(
        info.insert_position_for("APP\\MIDDLE"),
        Position {
            line: 3,
            character: 0,
        }
    );
}

#[test]
fn insert_among_three_existing() {
    // Existing: A (line 2), C (line 3), E (line 4).
    // Inserting D should go before E (line 4).
    let info = UseBlockInfo {
        existing: vec![
            (2, "a\\a".to_string()),
            (3, "c\\c".to_string()),
            (4, "e\\e".to_string()),
        ],
        fallback_line: 1,
    };
    assert_eq!(
        info.insert_position_for("D\\D"),
        Position {
            line: 4,
            character: 0,
        }
    );
}

// ── find_use_insert_position (backward compat) ──────────────────

#[test]
fn compat_insert_after_last_use_statement() {
    let content = "<?php\nnamespace App;\nuse Foo\\Bar;\nuse Baz\\Qux;\n\nclass X {}\n";
    let pos = find_use_insert_position(content);
    assert_eq!(
        pos,
        Position {
            line: 4,
            character: 0
        }
    );
}

#[test]
fn compat_insert_after_namespace_when_no_use() {
    let content = "<?php\nnamespace App;\n\nclass X {}\n";
    let pos = find_use_insert_position(content);
    assert_eq!(
        pos,
        Position {
            line: 2,
            character: 0
        }
    );
}

#[test]
fn compat_insert_after_php_open_tag_when_no_namespace() {
    let content = "<?php\n\nclass X {}\n";
    let pos = find_use_insert_position(content);
    assert_eq!(
        pos,
        Position {
            line: 1,
            character: 0
        }
    );
}

#[test]
fn compat_trait_use_inside_class_not_treated_as_import() {
    let content = "<?php\nnamespace App;\nuse Foo\\Bar;\n\nclass X {\n    use SomeTrait;\n}\n";
    let pos = find_use_insert_position(content);
    // Should insert after `use Foo\Bar;` (line 2), not after `use SomeTrait;`
    assert_eq!(
        pos,
        Position {
            line: 3,
            character: 0
        }
    );
}

// ── build_use_edit (alphabetical) ───────────────────────────────

#[test]
fn build_edit_inserts_at_correct_alpha_position() {
    let info = UseBlockInfo {
        existing: vec![(2, "app\\alpha".to_string()), (3, "app\\zoo".to_string())],
        fallback_line: 1,
    };
    let edits = build_use_edit("App\\Middle", &info, &Some("App".to_string()))
        .expect("should produce edit");
    assert_eq!(edits.len(), 1);
    assert_eq!(edits[0].new_text, "use App\\Middle;\n");
    assert_eq!(
        edits[0].range.start,
        Position {
            line: 3,
            character: 0
        }
    );
}

#[test]
fn build_edit_skips_global_class_without_namespace() {
    let info = UseBlockInfo {
        existing: vec![],
        fallback_line: 1,
    };
    assert!(build_use_edit("PDO", &info, &None).is_none());
}

#[test]
fn build_edit_includes_global_class_with_namespace() {
    let info = UseBlockInfo {
        existing: vec![],
        fallback_line: 2,
    };
    let edits =
        build_use_edit("PDO", &info, &Some("App".to_string())).expect("should produce edit");
    assert_eq!(edits[0].new_text, "use PDO;\n");
    assert_eq!(
        edits[0].range.start,
        Position {
            line: 2,
            character: 0
        }
    );
}

// ── End-to-end: analyze_use_block + build_use_edit ──────────────

#[test]
fn end_to_end_insert_before_existing_alphabetically() {
    let content = concat!(
        "<?php\n",
        "namespace App;\n",
        "use Exception;\n",
        "use Stringable;\n",
        "\n",
        "class X {}\n",
    );
    let info = analyze_use_block(content);
    let edits = build_use_edit("Cassandra\\DefaultCluster", &info, &Some("App".to_string()))
        .expect("should produce edit");

    assert_eq!(edits[0].new_text, "use Cassandra\\DefaultCluster;\n");
    // `Cassandra\DefaultCluster` < `Exception`, so insert before line 2.
    assert_eq!(
        edits[0].range.start,
        Position {
            line: 2,
            character: 0,
        }
    );
}

#[test]
fn end_to_end_insert_after_all_existing() {
    let content = concat!(
        "<?php\n",
        "namespace App;\n",
        "use App\\Alpha;\n",
        "use App\\Beta;\n",
        "\n",
        "class X {}\n",
    );
    let info = analyze_use_block(content);
    let edits =
        build_use_edit("App\\Zeta", &info, &Some("App".to_string())).expect("should produce edit");

    assert_eq!(edits[0].new_text, "use App\\Zeta;\n");
    // `App\Zeta` > `App\Beta`, so insert after line 3 → line 4.
    assert_eq!(
        edits[0].range.start,
        Position {
            line: 4,
            character: 0,
        }
    );
}

#[test]
fn end_to_end_insert_between_existing() {
    let content = concat!(
        "<?php\n",
        "namespace App;\n",
        "use App\\Alpha;\n",
        "use App\\Zeta;\n",
        "\n",
        "class X {}\n",
    );
    let info = analyze_use_block(content);
    let edits = build_use_edit("App\\Middle", &info, &Some("App".to_string()))
        .expect("should produce edit");

    assert_eq!(edits[0].new_text, "use App\\Middle;\n");
    // `App\Middle` > `App\Alpha` but < `App\Zeta`, so insert at line 3.
    assert_eq!(
        edits[0].range.start,
        Position {
            line: 3,
            character: 0,
        }
    );
}

// ── build_use_function_edit ─────────────────────────────────────

#[test]
fn build_function_edit_skips_global_function() {
    let info = UseBlockInfo {
        existing: vec![],
        fallback_line: 1,
    };
    assert!(
        build_use_function_edit("array_map", &info).is_none(),
        "Global function (no backslash) should not produce an import"
    );
}

#[test]
fn build_function_edit_namespaced_no_existing_imports() {
    let info = UseBlockInfo {
        existing: vec![],
        fallback_line: 2,
    };
    let edits = build_use_function_edit("Illuminate\\Support\\enum_value", &info)
        .expect("namespaced function should produce edit");
    // No existing imports → no blank-line separator.
    assert_eq!(
        edits[0].new_text,
        "use function Illuminate\\Support\\enum_value;\n"
    );
    assert_eq!(
        edits[0].range.start,
        Position {
            line: 2,
            character: 0,
        }
    );
}

#[test]
fn build_function_edit_sorts_after_class_imports_with_separator() {
    let content = concat!(
        "<?php\n",
        "namespace App;\n",
        "use App\\Models\\User;\n",
        "use Symfony\\Component\\HttpKernel;\n",
        "\n",
    );
    let info = analyze_use_block(content);
    let edits = build_use_function_edit("Illuminate\\Support\\enum_value", &info)
        .expect("should produce edit");

    // The prefixed sort key `"function illuminate\..."` sorts after
    // all bare class import keys, so the function import goes after
    // the last class import (line 3) → insert at line 4.
    assert_eq!(
        edits[0].range.start,
        Position {
            line: 4,
            character: 0,
        },
        "Function import should be placed after all class imports"
    );
    // First function import with existing class imports → blank line separator.
    assert_eq!(
        edits[0].new_text, "\nuse function Illuminate\\Support\\enum_value;\n",
        "Should prepend blank line to separate from class imports"
    );
}

#[test]
fn build_function_edit_no_separator_when_function_group_exists() {
    let content = concat!(
        "<?php\n",
        "namespace App;\n",
        "use App\\Models\\User;\n",
        "\n",
        "use function App\\Helpers\\format_price;\n",
        "\n",
    );
    let info = analyze_use_block(content);
    let edits = build_use_function_edit("Illuminate\\Support\\enum_value", &info)
        .expect("should produce edit");

    // There is already a function import → no extra blank line.
    assert_eq!(
        edits[0].new_text, "use function Illuminate\\Support\\enum_value;\n",
        "Should NOT prepend blank line when function group already exists"
    );
    // Alphabetically after `app\helpers\format_price` → after line 4.
    assert_eq!(
        edits[0].range.start,
        Position {
            line: 5,
            character: 0,
        }
    );
}

#[test]
fn build_function_edit_alphabetical_among_existing_functions() {
    let content = concat!(
        "<?php\n",
        "use App\\Models\\User;\n",
        "\n",
        "use function App\\Helpers\\format_price;\n",
        "use function Symfony\\String\\u;\n",
        "\n",
    );
    let info = analyze_use_block(content);
    let edits = build_use_function_edit("Illuminate\\Support\\enum_value", &info)
        .expect("should produce edit");

    // `function illuminate\...` sorts between `function app\...` and
    // `function symfony\...` → insert before line 4 (the Symfony line).
    assert_eq!(
        edits[0].range.start,
        Position {
            line: 4,
            character: 0,
        }
    );
    assert_eq!(
        edits[0].new_text, "use function Illuminate\\Support\\enum_value;\n",
        "No separator needed — already in the function group"
    );
}

#[test]
fn build_function_edit_deeply_namespaced() {
    let info = UseBlockInfo {
        existing: vec![],
        fallback_line: 3,
    };
    let edits = build_use_function_edit("Vendor\\Package\\Sub\\Module\\helper_func", &info)
        .expect("deeply namespaced function should produce edit");
    assert_eq!(
        edits[0].new_text,
        "use function Vendor\\Package\\Sub\\Module\\helper_func;\n"
    );
}