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

#[test]
fn test_nullsafe_chain_with_call() {
    // $user->getAddress()?->getCity()->
    let input = "$user->getAddress()?->getCity()->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    // Should include the full chain, not lose ->getAddress()
    assert!(
        result.contains("getAddress"),
        "Expected chain to include getAddress(), got: {result}"
    );
    assert!(
        result.contains("getCity"),
        "Expected chain to include getCity, got: {result}"
    );
}

#[test]
fn test_nullsafe_simple_var() {
    // $user?->getCity()->
    let input = "$user?->getCity()->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert!(
        result.contains("$user") && result.contains("getCity"),
        "Expected $user...getCity, got: {result}"
    );
}

#[test]
fn test_nullsafe_property_chain() {
    // $a?->b?->c->
    let input = "$a?->b?->c->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert!(
        result.contains("$a") && result.contains("b") && result.contains("c"),
        "Expected full chain $a...b...c, got: {result}"
    );
}

#[test]
fn test_regular_chain() {
    let input = "$user->getProfile()->getName()->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert!(
        result.contains("getProfile") && result.contains("getName"),
        "Expected full chain, got: {result}"
    );
}

#[test]
fn test_simple_variable() {
    let input = "$user->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert_eq!(result, "$user");
}

#[test]
fn test_nullsafe_simple() {
    let input = "$user?->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert_eq!(result, "$user");
}

// ── Multi-line chain collapse tests ─────────────────────────────

#[test]
fn test_collapse_simple_chain() {
    let lines = vec!["$this->getRepository()", "    ->findAll()", "    ->"];
    let (collapsed, col) = collapse_continuation_lines(&lines, 2, 6);
    assert!(
        collapsed.starts_with("$this->getRepository()"),
        "collapsed should start with base expression, got: {collapsed}"
    );
    assert!(
        collapsed.contains("->findAll()->"),
        "collapsed should contain intermediate chain, got: {collapsed}"
    );
    // The cursor should be past the `->` in the collapsed string.
    let chars: Vec<char> = collapsed.chars().collect();
    assert!(
        col <= chars.len(),
        "col {col} should be within collapsed len {}",
        chars.len()
    );
}

#[test]
fn test_collapse_not_a_continuation() {
    let lines = vec!["$this->getRepository()", "    $foo->bar()"];
    let (collapsed, col) = collapse_continuation_lines(&lines, 1, 10);
    assert_eq!(collapsed, "    $foo->bar()");
    assert_eq!(col, 10);
}

#[test]
fn test_collapse_nullsafe_chain() {
    let lines = vec!["$user->getAddress()", "    ?->getCity()", "    ->"];
    let (collapsed, col) = collapse_continuation_lines(&lines, 2, 6);
    assert!(
        collapsed.contains("?->getCity()"),
        "collapsed should preserve nullsafe operator, got: {collapsed}"
    );
    let chars: Vec<char> = collapsed.chars().collect();
    assert!(col <= chars.len());
}

#[test]
fn test_collapse_with_static_call_base() {
    let lines = vec![
        "SomeClass::query()",
        "    ->where('active', true)",
        "    ->",
    ];
    let (collapsed, col) = collapse_continuation_lines(&lines, 2, 6);
    assert!(
        collapsed.starts_with("SomeClass::query()"),
        "collapsed should start with static call, got: {collapsed}"
    );
    assert!(
        collapsed.contains("->where('active', true)->"),
        "collapsed should contain chained call, got: {collapsed}"
    );
    let chars: Vec<char> = collapsed.chars().collect();
    assert!(col <= chars.len());
}

#[test]
fn test_collapse_cursor_mid_identifier() {
    // Cursor is in the middle of typing an identifier after `->`.
    let lines = vec!["$builder->configure()", "    ->whe"];
    let (collapsed, col) = collapse_continuation_lines(&lines, 1, 9);
    assert!(
        collapsed.contains("->configure()->whe"),
        "collapsed should contain the partial identifier, got: {collapsed}"
    );
    // col should point at the end of `whe`
    let chars: Vec<char> = collapsed.chars().collect();
    assert!(col <= chars.len());
}

#[test]
fn test_collapse_single_continuation() {
    let lines = vec!["$foo->bar()", "    ->"];
    let (collapsed, _col) = collapse_continuation_lines(&lines, 1, 6);
    assert_eq!(collapsed, "$foo->bar()->");
}

#[test]
fn test_collapse_multiline_closure_argument() {
    // Brand::whereNested(function (Builder $q): void {
    // })
    // ->
    let lines = vec![
        "Brand::whereNested(function (Builder $q): void {",
        "})",
        "    ->",
    ];
    let (collapsed, col) = collapse_continuation_lines(&lines, 2, 6);
    assert!(
        collapsed.starts_with("Brand::whereNested("),
        "collapsed should start with the call expression, got: {collapsed}"
    );
    assert!(
        collapsed.contains("})->"),
        "collapsed should join the closing brace/paren with the arrow, got: {collapsed}"
    );
    let chars: Vec<char> = collapsed.chars().collect();
    assert!(
        col <= chars.len(),
        "col {col} should be within collapsed len {}",
        chars.len()
    );
}

#[test]
fn test_collapse_multiline_closure_with_body() {
    // Brand::whereNested(function (Builder $q): void {
    //     $q->where('active', true);
    // })
    // ->
    let lines = vec![
        "Brand::whereNested(function (Builder $q): void {",
        "    $q->where('active', true);",
        "})",
        "    ->",
    ];
    let (collapsed, col) = collapse_continuation_lines(&lines, 3, 6);
    assert!(
        collapsed.starts_with("Brand::whereNested("),
        "collapsed should start with the call expression, got: {collapsed}"
    );
    let chars: Vec<char> = collapsed.chars().collect();
    assert!(
        col <= chars.len(),
        "col {col} should be within collapsed len {}",
        chars.len()
    );
}

#[test]
fn test_collapse_multiline_closure_then_chain() {
    // Brand::whereNested(function (Builder $q): void {
    // })
    // ->where('active', 1)
    // ->
    let lines = vec![
        "Brand::whereNested(function (Builder $q): void {",
        "})",
        "    ->where('active', 1)",
        "    ->",
    ];
    let (collapsed, col) = collapse_continuation_lines(&lines, 3, 6);
    assert!(
        collapsed.starts_with("Brand::whereNested("),
        "collapsed should start with the call expression, got: {collapsed}"
    );
    assert!(
        collapsed.contains("->where('active', 1)->"),
        "collapsed should contain the chained call, got: {collapsed}"
    );
    let chars: Vec<char> = collapsed.chars().collect();
    assert!(
        col <= chars.len(),
        "col {col} should be within collapsed len {}",
        chars.len()
    );
}

#[test]
fn test_collapse_multiline_closure_intermediate_chain() {
    // $builder->where('x', 1)
    // ->whereNested(function ($q) {
    // })
    // ->
    let lines = vec![
        "$builder->where('x', 1)",
        "    ->whereNested(function ($q) {",
        "    })",
        "    ->",
    ];
    let (collapsed, col) = collapse_continuation_lines(&lines, 3, 6);
    assert!(
        collapsed.starts_with("$builder->where('x', 1)"),
        "collapsed should start with the base expression, got: {collapsed}"
    );
    assert!(
        collapsed.contains("->whereNested("),
        "collapsed should contain the closure call, got: {collapsed}"
    );
    let chars: Vec<char> = collapsed.chars().collect();
    assert!(
        col <= chars.len(),
        "col {col} should be within collapsed len {}",
        chars.len()
    );
}

#[test]
fn test_collapse_blank_line_in_chain() {
    // A blank line between chain segments should not break the collapse.
    //
    //   Brand::with('english')
    //
    //       ->paginate()
    //       ->
    let lines = vec!["Brand::with('english')", "", "    ->paginate()", "    ->"];
    let (collapsed, col) = collapse_continuation_lines(&lines, 3, 6);
    assert!(
        collapsed.starts_with("Brand::with('english')"),
        "collapsed should start with the base expression, got: {collapsed}"
    );
    assert!(
        collapsed.contains("->paginate()->"),
        "collapsed should contain the intermediate chain, got: {collapsed}"
    );
    let chars: Vec<char> = collapsed.chars().collect();
    assert!(
        col <= chars.len(),
        "col {col} should be within collapsed len {}",
        chars.len()
    );
}

#[test]
fn test_collapse_multiple_blank_lines_in_chain() {
    // Multiple blank lines should all be skipped.
    let lines = vec!["$foo->bar()", "", "", "    ->baz()", "    ->"];
    let (collapsed, _col) = collapse_continuation_lines(&lines, 4, 6);
    assert_eq!(collapsed, "$foo->bar()->baz()->");
}

#[test]
fn test_collapse_whitespace_only_line_in_chain() {
    // A line with only spaces/tabs should be treated as blank.
    let lines = vec![
        "SomeClass::query()",
        "    ",
        "    ->where('active', true)",
        "    ->",
    ];
    let (collapsed, col) = collapse_continuation_lines(&lines, 3, 6);
    assert!(
        collapsed.starts_with("SomeClass::query()"),
        "collapsed should start with static call, got: {collapsed}"
    );
    assert!(
        collapsed.contains("->where('active', true)->"),
        "collapsed should contain intermediate chain, got: {collapsed}"
    );
    let chars: Vec<char> = collapsed.chars().collect();
    assert!(
        col <= chars.len(),
        "col {col} should be within collapsed len {}",
        chars.len()
    );
}

#[test]
fn test_inline_array_literal_with_index_access() {
    // [Customer::first()][0]->
    let input = "[Customer::first()][0]->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert_eq!(
        result, "[Customer::first()][]",
        "Subject should be the literal base plus index segment"
    );
}

#[test]
fn test_inline_array_literal_new_expression() {
    // [new Foo()][0]->
    let input = "[new Foo()][0]->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert_eq!(
        result, "[new Foo()][]",
        "Subject should be the literal base plus index segment"
    );
}

#[test]
fn test_collapse_blank_line_cursor_on_first_continuation() {
    // Blank line right before the cursor's continuation line.
    let lines = vec!["$obj->method()", "", "    ->"];
    let (collapsed, _col) = collapse_continuation_lines(&lines, 2, 6);
    assert_eq!(collapsed, "$obj->method()->");
}

#[test]
fn test_parenthesized_property_invocation() {
    // ($this->formatter)()->
    // The subject should be `($this->formatter)()` so that the resolver
    // can unwrap the parenthesized property, resolve its type, and check
    // for __invoke().
    let input = "        ($this->formatter)()->";
    let chars: Vec<char> = input.chars().collect();
    let col = chars.len();
    let result = detect_access_operator(&chars, col);
    assert!(
        result.is_some(),
        "Expected Some from detect_access_operator"
    );
    let (subject, kind) = result.unwrap();
    assert_eq!(kind, AccessKind::Arrow);
    assert!(
        subject.contains("$this->formatter"),
        "Expected subject to contain $this->formatter, got: {subject}"
    );
    assert!(
        subject.contains("()"),
        "Expected subject to contain call parens (), got: {subject}"
    );
}

#[test]
fn test_call_expression_base_array_access() {
    // $c->items()[0]->
    let input = "$c->items()[0]->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert_eq!(
        result, "$c->items()[]",
        "Subject should be the call expression base plus index segment"
    );
}

#[test]
fn test_static_call_expression_base_array_access() {
    // Collection::all()[0]->
    let input = "Collection::all()[0]->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert_eq!(
        result, "Collection::all()[]",
        "Subject should be the static call expression base plus index segment"
    );
}

#[test]
fn test_function_call_base_array_access() {
    // getItems()[0]->
    let input = "getItems()[0]->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert_eq!(
        result, "getItems()[]",
        "Subject should be the function call base plus index segment"
    );
}

#[test]
fn test_clone_simple_variable() {
    // (clone $date)->
    let input = "(clone $date)->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert_eq!(result, "$date", "clone should preserve the inner variable");
}

#[test]
fn test_clone_property_chain() {
    // (clone $this->date)->
    let input = "(clone $this->date)->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert_eq!(
        result, "$this->date",
        "clone should preserve the inner property chain"
    );
}

#[test]
fn test_clone_method_chain() {
    // (clone $date)->endOfMonth()->
    let input = "(clone $date)->endOfMonth()->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert!(
        result.contains("$date") && result.contains("endOfMonth"),
        "Expected chain through clone, got: {result}"
    );
}

#[test]
fn test_clone_with_extra_whitespace() {
    // (clone   $date)->
    let input = "(clone   $date)->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert_eq!(
        result, "$date",
        "clone with extra whitespace should still resolve"
    );
}

#[test]
fn test_inline_new_expression_method_chain() {
    // (new Foo)->bar()->
    let input = "(new Foo)->bar()->";
    let chars: Vec<char> = input.chars().collect();
    let col = chars.len();
    let result = detect_access_operator(&chars, col);
    assert!(
        result.is_some(),
        "Expected Some from detect_access_operator"
    );
    let (subject, kind) = result.unwrap();
    assert_eq!(kind, AccessKind::Arrow);
    assert_eq!(
        subject, "Foo->bar()",
        "Expected subject 'Foo->bar()', got: {subject}"
    );
}

// ── Property chain with array bracket access ────────────────────

#[test]
fn test_property_chain_array_access_variable_key() {
    // $this->cache[$key]->
    let input = "$this->cache[$key]->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert_eq!(
        result, "$this->cache[]",
        "Subject should include the full property chain with bracket segment"
    );
}

#[test]
fn test_property_chain_array_access_numeric_index() {
    // $this->translations[0]->
    let input = "$this->translations[0]->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert_eq!(
        result, "$this->translations[]",
        "Subject should include the full property chain with bracket segment"
    );
}

#[test]
fn test_property_chain_array_access_string_literal_key() {
    // $this->cache['myKey']->
    let input = "$this->cache['myKey']->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert_eq!(
        result, "$this->cache['myKey']",
        "Subject should preserve the string key in the bracket segment"
    );
}

#[test]
fn test_object_property_array_access() {
    // $service->items[0]->
    let input = "$service->items[0]->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert_eq!(
        result, "$service->items[]",
        "Subject should include the full object property chain with bracket segment"
    );
}

#[test]
fn test_nested_property_chain_array_access() {
    // $this->nested->entries[0]->
    let input = "$this->nested->entries[0]->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert_eq!(
        result, "$this->nested->entries[]",
        "Subject should include the full nested property chain with bracket segment"
    );
}

#[test]
fn test_nullsafe_property_chain_array_access() {
    // $this?->cache[$key]->
    let input = "$this?->cache[$key]->";
    let chars: Vec<char> = input.chars().collect();
    let arrow_pos = input.rfind("->").unwrap();
    let result = extract_arrow_subject(&chars, arrow_pos);
    assert_eq!(
        result, "$this->cache[]",
        "Subject should include the property chain with bracket segment (? is stripped)"
    );
}