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

/// Helper: open a file, trigger document highlight at a position, and return results.
fn highlight_at(
    backend: &Backend,
    uri: &str,
    php: &str,
    line: u32,
    character: u32,
) -> Vec<DocumentHighlight> {
    backend.update_ast(uri, php);
    backend
        .handle_document_highlight(uri, php, Position { line, character })
        .unwrap_or_default()
}

/// Shorthand to check that a highlight has the expected line, start col, end col, and kind.
fn assert_highlight(
    h: &DocumentHighlight,
    line: u32,
    start_char: u32,
    end_char: u32,
    kind: DocumentHighlightKind,
) {
    assert_eq!(
        h.range.start.line, line,
        "expected line {}, got {}",
        line, h.range.start.line
    );
    assert_eq!(
        h.range.start.character, start_char,
        "expected start char {}, got {}",
        start_char, h.range.start.character
    );
    assert_eq!(
        h.range.end.character, end_char,
        "expected end char {}, got {}",
        end_char, h.range.end.character
    );
    assert_eq!(
        h.kind,
        Some(kind),
        "expected kind {:?}, got {:?}",
        kind,
        h.kind
    );
}

// ─── Variable highlighting ──────────────────────────────────────────────────

#[test]
fn highlight_variable_in_same_scope() {
    let backend = create_test_backend();
    let php = r#"<?php
function demo() {
    $user = new User();
    echo $user->name;
    return $user;
}
"#;

    // Cursor on `$user` at line 2 (the assignment)
    let highlights = highlight_at(&backend, "file:///test.php", php, 2, 5);

    assert_eq!(highlights.len(), 3);
    // Line 2: $user = ... (write)
    assert_highlight(&highlights[0], 2, 4, 9, DocumentHighlightKind::WRITE);
    // Line 3: echo $user->name (read)
    assert_highlight(&highlights[1], 3, 9, 14, DocumentHighlightKind::READ);
    // Line 4: return $user (read)
    assert_highlight(&highlights[2], 4, 11, 16, DocumentHighlightKind::READ);
}

#[test]
fn highlight_variable_scoped_to_function() {
    let backend = create_test_backend();
    let php = r#"<?php
function foo() {
    $x = 1;
    return $x;
}
function bar() {
    $x = 2;
    return $x;
}
"#;

    // Cursor on `$x` in foo() — should only highlight within foo
    let highlights = highlight_at(&backend, "file:///test.php", php, 2, 5);
    assert_eq!(highlights.len(), 2);
    assert_eq!(highlights[0].range.start.line, 2);
    assert_eq!(highlights[1].range.start.line, 3);
}

#[test]
fn highlight_variable_parameter_is_write() {
    let backend = create_test_backend();
    let php = r#"<?php
function greet(string $name) {
    echo $name;
}
"#;

    let highlights = highlight_at(&backend, "file:///test.php", php, 2, 10);

    // Should have at least the parameter def (write) and usage (read)
    assert!(highlights.len() >= 2);

    let has_write = highlights
        .iter()
        .any(|h| h.kind == Some(DocumentHighlightKind::WRITE));
    let has_read = highlights
        .iter()
        .any(|h| h.kind == Some(DocumentHighlightKind::READ));
    assert!(
        has_write,
        "expected a WRITE highlight for the parameter definition"
    );
    assert!(has_read, "expected a READ highlight for the variable usage");
}

// ─── Class name highlighting ────────────────────────────────────────────────

#[test]
fn highlight_class_references() {
    let backend = create_test_backend();
    let php = r#"<?php
class Foo {
    public function bar(): Foo {
        return new Foo();
    }
}
"#;

    // Cursor on `Foo` in the return type (line 2)
    let highlights = highlight_at(&backend, "file:///test.php", php, 2, 28);

    // Should match: class declaration, return type, new expression
    assert!(
        highlights.len() >= 2,
        "expected at least 2 highlights for class Foo, got {}",
        highlights.len()
    );

    // All should be READ kind (class declarations are still READ for highlighting purposes)
    for h in &highlights {
        assert_eq!(h.kind, Some(DocumentHighlightKind::READ));
    }
}

#[test]
fn highlight_class_declaration_highlights_all_references() {
    let backend = create_test_backend();
    let php = r#"<?php
class MyService {
    public static function create(): MyService {
        return new MyService();
    }
}
"#;

    // Cursor on the class declaration name
    let highlights = highlight_at(&backend, "file:///test.php", php, 1, 7);
    assert!(
        highlights.len() >= 2,
        "expected class declaration to highlight references too, got {}",
        highlights.len()
    );
}

// ─── Member highlighting ────────────────────────────────────────────────────

#[test]
fn highlight_method_accesses() {
    let backend = create_test_backend();
    let php = r#"<?php
class Calculator {
    public function add(int $a): int { return $a; }
    public function demo() {
        $this->add(1);
        $this->add(2);
    }
}
"#;

    // Cursor on `add` in `$this->add(1)` (line 4)
    let highlights = highlight_at(&backend, "file:///test.php", php, 4, 16);

    // Declaration + two usages
    assert!(
        highlights.len() >= 2,
        "expected at least 2 highlights for method 'add', got {}",
        highlights.len()
    );
}

#[test]
fn highlight_member_declaration_matches_accesses() {
    let backend = create_test_backend();
    let php = r#"<?php
class Dog {
    public string $name;
    public function greet() {
        echo $this->name;
    }
}
"#;

    // Cursor on the property access `name` in $this->name
    let highlights = highlight_at(&backend, "file:///test.php", php, 4, 21);

    assert!(
        !highlights.is_empty(),
        "expected at least 1 highlight for property 'name', got {}",
        highlights.len()
    );
}

// ─── Function highlighting ──────────────────────────────────────────────────

#[test]
fn highlight_function_calls() {
    let backend = create_test_backend();
    let php = r#"<?php
function helper() {}
helper();
helper();
"#;

    // Cursor on first call to `helper()` at line 2
    let highlights = highlight_at(&backend, "file:///test.php", php, 2, 1);

    assert!(
        highlights.len() >= 2,
        "expected at least 2 highlights for function 'helper', got {}",
        highlights.len()
    );
    for h in &highlights {
        assert_eq!(h.kind, Some(DocumentHighlightKind::READ));
    }
}

// ─── Constant highlighting ──────────────────────────────────────────────────

#[test]
fn highlight_constant_references() {
    let backend = create_test_backend();
    let php = r#"<?php
class Config {
    const MAX = 100;
    public function check(int $v): bool {
        return $v < self::MAX;
    }
}
"#;

    // Cursor on `MAX` in `self::MAX` (line 4)
    let highlights = highlight_at(&backend, "file:///test.php", php, 4, 26);

    assert!(
        !highlights.is_empty(),
        "expected at least 1 highlight for constant 'MAX', got {}",
        highlights.len()
    );
}

// ─── $this highlighting ─────────────────────────────────────────────────────

#[test]
fn highlight_this_keyword() {
    let backend = create_test_backend();
    let php = r#"<?php
class Example {
    private int $x;
    public function setX(int $v): void {
        $this->x = $v;
    }
    public function getX(): int {
        return $this->x;
    }
}
"#;

    // Cursor on `$this` at line 4
    let highlights = highlight_at(&backend, "file:///test.php", php, 4, 9);

    assert!(
        highlights.len() >= 2,
        "expected at least 2 highlights for $this, got {}",
        highlights.len()
    );
    for h in &highlights {
        assert_eq!(h.kind, Some(DocumentHighlightKind::READ));
    }
}

// ─── self/static/parent highlighting ────────────────────────────────────────

#[test]
fn highlight_self_keyword() {
    let backend = create_test_backend();
    let php = r#"<?php
class Counter {
    private static int $count = 0;
    public static function increment(): void {
        self::$count++;
    }
    public static function get(): int {
        return self::$count;
    }
}
"#;

    // Cursor on `self` at line 4
    let highlights = highlight_at(&backend, "file:///test.php", php, 4, 9);

    assert!(
        highlights.len() >= 2,
        "expected at least 2 highlights for self keyword, got {}",
        highlights.len()
    );
}

// ─── Edge cases ─────────────────────────────────────────────────────────────

#[test]
fn highlight_returns_none_on_whitespace() {
    let backend = create_test_backend();
    let php = r#"<?php
function foo() {}
"#;

    // Cursor on whitespace
    let result = {
        backend.update_ast("file:///test.php", php);
        backend.handle_document_highlight(
            "file:///test.php",
            php,
            Position {
                line: 0,
                character: 0,
            },
        )
    };

    assert!(
        result.is_none(),
        "expected None when cursor is on non-navigable token"
    );
}

#[test]
fn highlight_foreach_variable() {
    let backend = create_test_backend();
    let php = r#"<?php
function process(array $items) {
    foreach ($items as $item) {
        echo $item;
    }
}
"#;

    // Cursor on `$item` in foreach binding (line 2)
    let highlights = highlight_at(&backend, "file:///test.php", php, 2, 24);

    assert!(
        highlights.len() >= 2,
        "expected at least 2 highlights for $item, got {}",
        highlights.len()
    );

    let has_write = highlights
        .iter()
        .any(|h| h.kind == Some(DocumentHighlightKind::WRITE));
    assert!(has_write, "foreach binding should be a WRITE highlight");
}

#[test]
fn highlight_variable_assignment_is_write() {
    let backend = create_test_backend();
    let php = r#"<?php
function test() {
    $val = 1;
    $val = 2;
    echo $val;
}
"#;

    let highlights = highlight_at(&backend, "file:///test.php", php, 2, 5);

    assert_eq!(highlights.len(), 3);
    // First two are assignments (WRITE), last is usage (READ)
    assert_highlight(&highlights[0], 2, 4, 8, DocumentHighlightKind::WRITE);
    assert_highlight(&highlights[1], 3, 4, 8, DocumentHighlightKind::WRITE);
    assert_highlight(&highlights[2], 4, 9, 13, DocumentHighlightKind::READ);
}

#[test]
fn highlight_static_method() {
    let backend = create_test_backend();
    let php = r#"<?php
class Factory {
    public static function make(): self { return new self(); }
    public function demo() {
        self::make();
        static::make();
    }
}
"#;

    // Cursor on `make` at line 4
    let highlights = highlight_at(&backend, "file:///test.php", php, 4, 15);

    // Declaration + two calls
    assert!(
        highlights.len() >= 2,
        "expected at least 2 highlights for static method 'make', got {}",
        highlights.len()
    );
}